问题描述
限时送ChatGPT账号..当我继续训练我的模型并在 tensorboard 上可视化进度时,我遇到了一些问题.
I encountered some problems when I continued training my model and visualized the progress on tensorboard.
我的问题是如何在不手动指定任何时期的情况下从同一步骤恢复训练?如果可能,只需加载保存的模型,它就可以以某种方式从保存的优化器中读取 global_step
并从那里继续训练.
My question is how do I resume training from the same step without specifying any epoch manually? If possible, simply by loading the saved model, it somehow could read the global_step
from the optimizer saved and continue training from there.
我在下面提供了一些代码来重现类似的错误.
I have provided some codes below to reproduce similar errors.
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.models import load_model
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
modelpile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, callbacks=[Tensorboard()])
model.save('./final_model.h5', include_optimizer=True)
del model
model = load_model('./final_model.h5')
model.fit(x_train, y_train, epochs=10, callbacks=[Tensorboard()])
您可以使用以下命令运行tensorboard
:
You can run the tensorboard
by using the command:
tensorboard --logdir ./logs
推荐答案
你可以在函数model.fit()
中设置参数initial_epoch
为你希望你的训练从哪个时代开始.考虑到模型训练直到达到索引 epochs
的纪元(而不是由 epochs
给出的迭代次数).在您的示例中,如果您想再训练 10 个时期,则应该是:
You can set the parameter initial_epoch
in the function model.fit()
to the number of the epoch you want your training to start from. Take into account that the model trains until the epoch of index epochs
is reached (and not a number of iterations given by epochs
).
In your example, if you want to train for 10 epochs more, it should be:
model.fit(x_train, y_train, initial_epoch=9, epochs=19, callbacks=[Tensorboard()])
它将允许您以正确的方式在 Tensorboard 上可视化您的绘图.有关这些参数的更多信息,请参阅文档.
It will allow you to visualise your plots on Tensorboard in a correct manner. More extensive information about these parameters can be found in the docs.
这篇关于恢复训练 tf.keras Tensorboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论