训练我的模型后,可以保存KERAS模型
我正在遵循keras.io的代码示例,内容涉及无关注的视觉变压器,在这里。我想在完成培训后将其保存在tensorflow/keras
中,但会产生错误。下面给出了我的模型代码的最后一部分,
# Get the total number of steps for training.
total_steps = int((len(x_train) / config.batch_size) * config.epochs)
# Calculate the number of steps for warmup.
warmup_epoch_percentage = 0.15
warmup_steps = int(total_steps * warmup_epoch_percentage)
# Initialize the warmupcosine schedule.
scheduled_lrs = WarmUpCosine(
lr_start=1e-5, lr_max=1e-3, warmup_steps=warmup_steps, total_steps=total_steps,
)
# Get the optimizer.
optimizer = tfa.optimizers.AdamW(
learning_rate=scheduled_lrs, weight_decay=config.weight_decay
)
# Compile and pretrain the model.
model.compile(
optimizer=optimizer,
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[
keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
],
)
# Train the model
history = model.fit(
train_ds,
epochs=config.epochs,
validation_data=val_ds,
callbacks=[
keras.callbacks.EarlyStopping(monitor="val_accuracy", patience=5, mode="auto",)
],
)
# Evaluate the model with the test dataset.
print("TESTING")
loss, acc_top1, acc_top5 = model.evaluate(test_ds)
print(f"Loss: {loss:0.2f}")
print(f"Top 1 test accuracy: {acc_top1*100:0.2f}%")
print(f"Top 5 test accuracy: {acc_top5*100:0.2f}%")
我尝试了下面的两种方法来保存模型
model.save('/content/drive/MyDrive/VIT-SHIFT')
,
history.save('/content/drive/MyDrive/VIT-SHIFT')
但它说模型和历史记录尚未定义。完整代码可在 nofollow Noreferrer“
I'm following a code example from keras.io about A Vision Transformer without Attention, here. I want to save it in the tensorflow/keras
after completing training but it generates an error. The last part of my model code is given below
# Get the total number of steps for training.
total_steps = int((len(x_train) / config.batch_size) * config.epochs)
# Calculate the number of steps for warmup.
warmup_epoch_percentage = 0.15
warmup_steps = int(total_steps * warmup_epoch_percentage)
# Initialize the warmupcosine schedule.
scheduled_lrs = WarmUpCosine(
lr_start=1e-5, lr_max=1e-3, warmup_steps=warmup_steps, total_steps=total_steps,
)
# Get the optimizer.
optimizer = tfa.optimizers.AdamW(
learning_rate=scheduled_lrs, weight_decay=config.weight_decay
)
# Compile and pretrain the model.
model.compile(
optimizer=optimizer,
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[
keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
],
)
# Train the model
history = model.fit(
train_ds,
epochs=config.epochs,
validation_data=val_ds,
callbacks=[
keras.callbacks.EarlyStopping(monitor="val_accuracy", patience=5, mode="auto",)
],
)
# Evaluate the model with the test dataset.
print("TESTING")
loss, acc_top1, acc_top5 = model.evaluate(test_ds)
print(f"Loss: {loss:0.2f}")
print(f"Top 1 test accuracy: {acc_top1*100:0.2f}%")
print(f"Top 5 test accuracy: {acc_top5*100:0.2f}%")
I tried below two methods to save my model
model.save('/content/drive/MyDrive/VIT-SHIFT')
and
history.save('/content/drive/MyDrive/VIT-SHIFT')
but it says model and history are not defined. Full code is available in this colab notebook
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如您有关同一代码的另一个问题所述,在这里,您可能需要实现
呼叫>呼叫
首先。现在,保存和重新加载模型应该很简单,但是我遇到了有关layers.randomcrop
增强层的问题。尽管我看了看源代码,但没有任何可疑错误。因此,为了结束这一点,我实施了自定义的随机作物层,从理论上讲,这些层也是如此。但是,如果您坚持使用内置
layers.randomcrop
,那么我建议您在Github上开票有关此问题。要使整个代码端到端培训和节省+重新加载,请按以下方式更改代码。首先,在
shiftVitModel
类中实现呼叫
方法。接下来,实现自定义的随机裁剪层,并按照以下方式使用它,而不是
layers.randomcrop
。在这些更改之后,我们现在可以按以下方式执行任何错误。
节省和重新加载工作。
这是完整的工作 code noreferrer“> code-in-code-in-colab 。请保存文件,我可能有一天会从驱动器中删除文件。
最后,fyi,
history.save('...')
,您不能在keras中这样做。为了保存tensorflow/keras
模型,请参阅 /a>文档。历史记录
对象将仅返回训练时间期间跟踪的指标和损失。例如,您可以从上面的字典中保存训练日志,也可以更好地使用 > 在培训期间。
As mentioned in your other question about the same code, HERE, you may need to implement the
call
method first. Now, saving and reloading the model should be straightforward but I've encountered an issue regarding alayers.RandomCrop
augmentation layer that is used in the code.Though I looked over the source code but didn't feel any suspicious bug. So, to end this, I've implemented custom random crop layers which theoretically do the same. However, if you insist to use built-in
layers.RandomCrop
, then I would recommend opening a ticket on GitHub regarding this issue.To make the whole code end-to-end training and saving+reloading, change the code as follows. First, implement the
call
method in theShiftViTModel
class.Next, implement a custom random crop layer and use it as follows instead of
layers.RandomCrop
.After these changes, we can now do as follows without any error.
Saving and reloading work as well.
HERE is the complete working Code-in-Colab. Please save the file, I might erase the file from the drive someday.
Lastly, FYI,
history.save('...')
, you can't do that in keras. In order to save thetensorflow/keras
model, please refer to this document. Thehistory
object will return only the tracked metrics and loss during training time. For exampleYou can save the training logs from the above dictionary or better use the
CSVLogger
during training.