有没有办法保存权重并将它们加载到另一个文件中

原文标题Is there a way to save the weights and load them on another file

所以,就像问题的标题一样简单,有没有办法在这样的训练后保存权重

model.save_weights("path")

然后仅将它们加载到另一个项目中

model = load_weights("path")
model.predict(x)

可能吗 ?

原文链接:https://stackoverflow.com//questions/71472317/is-there-a-way-to-save-the-weights-and-load-them-on-another-file

回复

我来回复
  • dimas krisrianto的头像
    dimas krisrianto 评论

    是的。如果你调用正确的路径是可能的

    例如,你有这条路

    - project1/cool.py
    - project2/another_cool.py
    

    你用cool.py训练,模型保存在project1的文件夹中。然后你想在another_cool.py中加载模型

    只需用路径调用load_model函数../project1/weigh.h5

    2年前 0条评论
  • Théo Rubenach的头像
    Théo Rubenach 评论

    如果您只想保存/加载权重,您可以使用

    model.save_weights("path/to/my_model_weights.hdf5")

    然后重新加载(可能在另一个python项目/另一个解释器中,你只需要相应地更新路径)

    other_model.load_weights("path/to/my_model_weights.hdf5")

    但是,两个模型应该具有相同的架构(同一类的实例),并且 Python/tensorflow/keras 版本应该相同。有关更多信息,请参阅文档。

    您可以通过model.save("path/to/my_model.hdf5")保存在磁盘上和keras.models.load_model("path/to/my_model.hdf5")从磁盘加载来保存权重和架构(再次,文档应该提供详细信息)。

    一旦加载到内存中,您可以重新训练您的模型,或在其上使用预测,项目之间的预测应该相同

    2年前 0条评论