出现报错 Keras 3 only supports V3 .keras and .weights.h5 files

发布于:2024-04-29 ⋅ 阅读:(45) ⋅ 点赞:(0)

这是在运行RNN代码时出现的报错,

filepath=./checkpoint/rnn_onehot_1pre1.ckpt. Keras 3 only supports V3 .keras and .weights.h5 files, or legacy V1/V2 .h5 files.

经过多方修改后,是因为新安装的keras版本较新,与之前文件的保存格式不同。

这通常意味着你正在使用Keras 3.0尝试加载一个早期版本Keras(如2.x版本)保存的模型权重文件。

Keras 3.0目前只支持V3格式的.keras.weights.h5文件,或者旧的V1/V2格式的.h5文件。由于文件格式在不同Keras版本之间有变化,所以Keras 3.0不能识别早期版本保存的文件。

一些解决方法:

  1. 使用保存该模型文件的Keras版本来加载权重文件。比如如果用Keras 2.x保存的,就使用Keras 2.x版本加载。

  2. 将旧文件格式转换为Keras 3.0支持的新格式。可以参考这个Issue下评论中的方法:add .gitignore to filter the files shouldn't be included. by copybara-service[bot] · Pull Request #14882 · keras-team/keras · GitHub

  3. 只加载权重,而不加载整个检查点配置。用model.load_weights(filepath)代替model.load(filepath)

  4. 使用Keras 3.0重新训练模型并保存新格式的检查点文件。

checkpoint_save_path = "./checkpoint/rnn_onehot_1pre1.ckpt"

if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

 

checkpoint_save_path = "./checkpoint/rnn_onehot_1pre1.weights.h5"

if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

 以上分别是我修改之前和修改之后的代码,在保存修改之后,一定要记得从开始重新进行加载运行,不要只运行这一部分代码。

最后,终于运行成功了,好开心。 


网站公告

今日签到

点亮在社区的每一天
去签到