简体   繁体   English

在Keras中保存模型和重量(单个文件中)

[英]Saving just model & weights in Keras (in single file)

I have Python code that generates a deep convolutional neural network using Keras. 我有Python代码,使用Keras生成深度卷积神经网络。 I'm trying to save the model, but the result is gigantic (100s of MBs). 我正在尝试保存模型,但结果是巨大的(100s的MB)。 I'd like to pare that down a bit to make something more manageable. 我想稍微削减一点,以使一些更易于管理的东西。

The problem is that model.save() stores (quoting the Keras FAQ): 问题是model.save()存储(引用model.save() FAQ):

  • the architecture of the model, allowing to re-create the model 模型的体系结构,允许重新创建模型
  • the weights of the model 模型的权重
  • the training configuration (loss, optimizer) 训练配置(损失,优化器)
  • the state of the optimizer, allowing to resume training exactly where you left off. 优化器的状态,允许您从中断的位置恢复训练。

If I'm not doing any more training, I think I just need the first two. 如果我不再接受任何培训,我想我只需要前两个。

I can use model.to_json() to make a JSON string of the architecture and save that off, and model.save_weights() to make a separate file containing the weights. 我可以使用model.to_json()来创建体系结构的JSON字符串并将其保存,并使用model.save_weights()创建包含权重的单独文件。 That's about a third the size of the full model.save() result. 这大约是完整model.save()结果的三分之一。 But I'm wondering if there's some way to store these in a single self-contained file? 但我想知道是否有一些方法可以将这些存储在一个自包含的文件中? (Short of outputting two files, zipping them together, and deleting the originals.) Alternatively, maybe there's a way to delete the training configuration and optimizer state when training is complete, so that model.save() doesn't give me something nearly so big? (没有输出两个文件,将它们压缩在一起,并删除原件。)或者,也许有一种方法可以在训练完成时删除训练配置和优化器状态,​​因此model.save()不会给我一些东西很大?

Thanks. 谢谢。

模型的保存功能具有完全相同的参数,称为include_optimizer,将其设置为false将保存模型而不包括优化器状态,​​这将导致更小的HDF5文件:

model.save("something.hdf5", include_optimizer=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM