仅保存模型和Keras中的权重(在单个文件中) [英] Saving just model & weights in Keras (in single file)

查看:100
本文介绍了仅保存模型和Keras中的权重(在单个文件中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Keras生成深层卷积神经网络的Python代码.我正在尝试保存模型,但是结果是巨大的(100 MB).我想将其缩减一些以使内容更易于管理.

I have Python code that generates a deep convolutional neural network using Keras. I'm trying to save the model, but the result is gigantic (100s of MBs). I'd like to pare that down a bit to make something more manageable.

问题是model.save()存储(引用Keras常见问题解答):

The problem is that model.save() stores (quoting the Keras FAQ):

  • 模型的体系结构,允许重新创建模型
  • 模型的权重
  • 培训配置(损失,优化程序)
  • 优化器的状态,可以从您上次中断的地方继续进行精确训练.

如果我不再进行任何培训,我认为我只需要前两个.

If I'm not doing any more training, I think I just need the first two.

我可以使用model.to_json()生成体系结构的JSON字符串并将其保存,然后使用model.save_weights()生成包含权重的单独文件.这大约是完整model.save()结果的大小的三分之一.但是我想知道是否可以通过某种方式将它们存储在一个独立的文件中? (简短地输出两个文件,将它们压缩在一起,然后删除原始文件.)或者,也许有一种方法可以在训练完成后删除训练配置和优化器状态,​​以使model.save()不会给我太大的东西?

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. That's about a third the size of the full model.save() result. 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?

谢谢.

推荐答案

The save function of a Model has a parameter exactly for this, called include_optimizer, setting it to false will save the model without including the optimizer state, which should lead to a much smaller HDF5 file:

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

这篇关于仅保存模型和Keras中的权重(在单个文件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆