我正在使用精度度量运行tf.keras.callbacks.ModelCheckpoint,但损失用于保存最佳检查点。我在不同的地方(我的电脑和collab(和两个不同的代码中测试过这个,但遇到了相同的问题。以下是示例代码和结果:
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_learningtf_kerassemantic_segmentationlogs', '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+00Epoch 1:val_loss从-inf改进到2.32311,将模型保存到D:\deep_learning\tf_keras\symantic_segmentation\logs\mymodel_11/1【=========================】-6s 6s/步-损耗:2.6475-精度:0.0000e+00-val_loss:2.3231-val_accuracy:0.1142
Epoch 2/31/1【=========================】-ETA:0s-损耗:1.9612-精度:1.0000Epoch 2:val_loss从2.32311提高到2.34286,将模型保存到D:\deep_learning\tf_keras\symantic_segmentation\logs\mymodel_21/1【=========================】-5s 5s/步-损耗:1.9612-精度:1.0000-val_loss:2.3429-val_accuracy:0.1187
Epoch 3/31/1【=========================】-ETA:0s-损耗:2.8378-精度:0.0000e+00Epoch 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
)
]