使用keras在json中转储cnn的权重 [英] dump weights of cnn in json using keras

查看:233
本文介绍了使用keras在json中转储cnn的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在其他框架中使用转储的权重和模型架构进行测试.

I want to use the dumped weights and model architecture in other framework for testing.

我知道:

  • model.get_config()可以给出模型的配置
  • model.to_json以JSON字符串形式返回模型的表示形式,但是该表示形式不包括权重,仅包括体系结构
  • model.save_weights(filepath)将模型的权重另存为HDF5文件
  • model.get_config() can give the configuration of the model
  • model.to_json returns a representation of the model as a JSON string, but that the representation does not include the weights, only the architecture
  • model.save_weights(filepath) saves the weights of the model as a HDF5 file

我想将体系结构和权重保存在json文件中.

I want to save the architecture as well as weights in a json file.

推荐答案

Keras没有将权重导出到JSON的任何内置方法.

Keras does not have any built-in way to export the weights to JSON.

解决方案1:

现在,您可以通过遍历权重并将其保存到JSON文件中来轻松完成此操作.

For now you can easily do it by iterating over the weights and saving it to the JSON file.

weights_list = model.get_weights()

将以Numpy数组的形式返回模型中所有权重张量的列表.

will return a list of all weight tensors in the model, as Numpy arrays.

然后,接下来要做的就是遍历此列表并写入文件:

Then, all you have to do next is to iterate over this list and write to the file:

for i, weights in enumerate(weights_list):
    writeJSON(weights)

解决方案2:

import json
weights_list = model.get_weights()
print json.dumps(weights_list.tolist())

这篇关于使用keras在json中转储cnn的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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