举一个简单的例子:
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.optimizer_v2.adam import Adam
negatives = np.random.beta(0.6, 1.2, 100)
positives = np.random.beta(1.7, 0.9, 100)
x = np.concatenate((negatives, positives))
y = np.concatenate((np.full(negatives.size, 0), np.full(positives.size, 1)))
model = Sequential()
model.add(Dense(1, input_shape=(1,), activation="sigmoid"))
model.compile(optimizer=Adam(learning_rate=0.1),
loss="binary_crossentropy",
metrics=[tf.keras.metrics.AUC(curve="ROC", name="AUROC")])
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=100, verbose=True, batch_size=10000))
auroc_val = tf.keras.metrics.AUC(curve="ROC", name="AUROC")(y_test, model.predict(x_test).flatten()).numpy()
print(f"{auroc_val=}")
我希望auroc_val
等于val_AUROC
(从最后一个训练期开始)。然而,事实并非如此。
一次运行的输出:
...
Epoch 98/100
5/5 [==============================] - 0s 5ms/step - loss: 0.5247 - AUROC: 0.8144 - val_loss: 0.5277 - val_AUROC: 0.8143
Epoch 99/100
5/5 [==============================] - 0s 5ms/step - loss: 0.5242 - AUROC: 0.8143 - val_loss: 0.5290 - val_AUROC: 0.8144
Epoch 100/100
5/5 [==============================] - 0s 5ms/step - loss: 0.5250 - AUROC: 0.8145 - val_loss: 0.5264 - val_AUROC: 0.8145
auroc_val=0.83203566
另一次运行的输出:
...
Epoch 100/100
5/5 [==============================] - 0s 5ms/step - loss: 0.4771 - AUROC: 0.8055 - val_loss: 0.6642 - val_AUROC: 0.8054
auroc_val=0.7224694
为什么会这样呢?
1条答案
按热度按时间flseospp1#
我不能复制你的错误,对我来说,价值观是一样的。但是我必须删除
from tensorflow.python..
导入并创建例如。致密层为tf.keras.layers.Dense
。有关为什么不使用tensorflow.python
的详细说明,请参阅此说明。我的TF版本是2.13.0(这可以解释
tf.python.keras
导入错误),python 3.10.11。请尝试不使用上面提到的导入并检查您的TF版本。(This答案最初只是问题下的评论。)