tf.keras.callbacks.ModelCheckpoint忽略monitor参数,一直使用loss
tf.keras.callbacks.ModelCheckpoint ignores the montior parameter and always use loss
我 运行 tf.keras.callbacks.ModelCheckpoint 具有准确度指标,但损失用于保存最佳检查点。我在不同的地方(我的电脑和协作)和两个不同的代码测试过这个并面临同样的问题。这是一个示例代码和结果:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import os
import shutil
def get_uncompiled_model():
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
return model
def get_compiled_model():
model = get_uncompiled_model()
model.compile(
optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)
return model
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess the data (these are NumPy arrays)
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255
y_train = y_train.astype("float32")
y_test = y_test.astype("float32")
# Reserve 10,000 samples for validation
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
ckpt_folder = os.path.join(os.getcwd(), 'ckpt')
if os.path.exists(ckpt_folder):
shutil.rmtree(ckpt_folder)
ckpt_path = os.path.join(r'D:\deep_learning\tf_keras\semantic_segmentation\logs', 'mymodel_{epoch}')
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
# Path where to save the model
# The two parameters below mean that we will overwrite
# the current checkpoint if and only if
# the `val_loss` score has improved.
# The saved model name will include the current epoch.
filepath=ckpt_path,
montior="val_accuracy",
# save the model weights with best validation accuracy
mode='max',
save_best_only=True, # only save the best weights
save_weights_only=False,
# only save model weights (not whole model)
verbose=1
)
]
model = get_compiled_model()
model.fit(
x_train, y_train, epochs=3, batch_size=1, callbacks=callbacks, validation_split=0.2, steps_per_epoch=1
)
1/1 [==============================] - ETA:0s - 损失:2.6475 - 准确性: 0.0000e+00
Epoch 1:val_loss 从 -inf 改进到 2.32311,将模型保存到 D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_1
1/1 [================================] - 6s 6s/步 - 损失:2.6475 - 精度:0.0000e+ 00 - val_loss: 2.3231 - val_accuracy: 0.1142
纪元 2/3
1/1 [==============================] - ETA:0s - 损失:1.9612 - 精度:1.0000
Epoch 2:val_loss 从 2.32311 改进到 2.34286,将模型保存到 D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_2
1/1 [================================] - 5s 5s/步 - 损失:1.9612 - 精度:1.0000 - val_loss: 2.3429 - val_accuracy: 0.1187
时代 3/3
1/1 [==============================] - ETA:0s - 损失:2.8378 - 精度:0.0000e+00
Epoch 3:val_loss 没有从 2.34286 提高
1/1 [================================] - 5s 5s/步 - 损失:2.8378 - 精度:0.0000e+ 00 - val_loss: 2.2943 - val_accuracy: 0.1346
在你的代码中,你写 montior
而不是 monitor
,函数没有这个词作为参数然后使用默认值,如果你像下面这样写,你会得到什么你想要:
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
filepath=ckpt_path,
monitor="val_accuracy",
mode='max',
save_best_only=True,
save_weights_only=False,
verbose=1
)
]
我 运行 tf.keras.callbacks.ModelCheckpoint 具有准确度指标,但损失用于保存最佳检查点。我在不同的地方(我的电脑和协作)和两个不同的代码测试过这个并面临同样的问题。这是一个示例代码和结果:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import os
import shutil
def get_uncompiled_model():
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
return model
def get_compiled_model():
model = get_uncompiled_model()
model.compile(
optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"],
)
return model
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Preprocess the data (these are NumPy arrays)
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255
y_train = y_train.astype("float32")
y_test = y_test.astype("float32")
# Reserve 10,000 samples for validation
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
ckpt_folder = os.path.join(os.getcwd(), 'ckpt')
if os.path.exists(ckpt_folder):
shutil.rmtree(ckpt_folder)
ckpt_path = os.path.join(r'D:\deep_learning\tf_keras\semantic_segmentation\logs', 'mymodel_{epoch}')
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
# Path where to save the model
# The two parameters below mean that we will overwrite
# the current checkpoint if and only if
# the `val_loss` score has improved.
# The saved model name will include the current epoch.
filepath=ckpt_path,
montior="val_accuracy",
# save the model weights with best validation accuracy
mode='max',
save_best_only=True, # only save the best weights
save_weights_only=False,
# only save model weights (not whole model)
verbose=1
)
]
model = get_compiled_model()
model.fit(
x_train, y_train, epochs=3, batch_size=1, callbacks=callbacks, validation_split=0.2, steps_per_epoch=1
)
1/1 [==============================] - ETA:0s - 损失:2.6475 - 准确性: 0.0000e+00 Epoch 1:val_loss 从 -inf 改进到 2.32311,将模型保存到 D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_1 1/1 [================================] - 6s 6s/步 - 损失:2.6475 - 精度:0.0000e+ 00 - val_loss: 2.3231 - val_accuracy: 0.1142
纪元 2/3 1/1 [==============================] - ETA:0s - 损失:1.9612 - 精度:1.0000 Epoch 2:val_loss 从 2.32311 改进到 2.34286,将模型保存到 D:\deep_learning\tf_keras\semantic_segmentation\logs\mymodel_2 1/1 [================================] - 5s 5s/步 - 损失:1.9612 - 精度:1.0000 - val_loss: 2.3429 - val_accuracy: 0.1187
时代 3/3 1/1 [==============================] - ETA:0s - 损失:2.8378 - 精度:0.0000e+00 Epoch 3:val_loss 没有从 2.34286 提高 1/1 [================================] - 5s 5s/步 - 损失:2.8378 - 精度:0.0000e+ 00 - val_loss: 2.2943 - val_accuracy: 0.1346
在你的代码中,你写 montior
而不是 monitor
,函数没有这个词作为参数然后使用默认值,如果你像下面这样写,你会得到什么你想要:
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
filepath=ckpt_path,
monitor="val_accuracy",
mode='max',
save_best_only=True,
save_weights_only=False,
verbose=1
)
]