gpt4 book ai didi

python - tf.keras.models.save_model 和优化器警告

转载 作者:太空狗 更新时间:2023-10-30 00:28:57 27 4
gpt4 key购买 nike

我使用 tf.keras 创建了一个Sequential 模型,如下所示:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8, input_dim=4))
model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))

opt = tf.train.AdamOptimizer(learning_rate=0.001)
model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"])

model.summary()

之后,我使用 train_on_batch 创建了一个训练过程:

EPOCHS=50
for epoch in range(EPOCHS):
for metrics, labels in dataset:
# Calculate training loss and accuracy
tr_loss, tr_accuracy = model.train_on_batch(metrics, labels)

当我尝试保存模型时,我收到一条警告。我不明白为什么,因为我将优化器作为 model.compile 的一部分包含在内:

tf.keras.models.save_model(
model,
"./model/iris_model.h5",
overwrite=True,
include_optimizer=True
)

WARNING:tensorflow:TensorFlow optimizers do not make it possible to access optimizer attributes or optimizer state after instantiation. As a result, we cannot save the optimizer as part of the model save file.You will have to compile your model again after loading it. Prefer using a Keras optimizer instead (see keras.io/optimizers).

我使用的TF版本是1.9.0-rc2。

最佳答案

正如警告所说,保存模型时无法保存 Tensorflow 优化器。相反,使用 optimizers provided by Keras :

opt = tf.keras.optimizers.Adam(lr=0.001)

关于python - tf.keras.models.save_model 和优化器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51252555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com