在tensorflow中,是否可以看到其他模型的构建结构? [英] In tensorflow, is it possible to see another models build structure?

查看:119
本文介绍了在tensorflow中,是否可以看到其他模型的构建结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我加载其他人的模型,这就是我看到的:

For example, if I load somebody else's model, this is what I see:

我想获取此代码的表示形式,例如:

I want to get the code representation of this, for example:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
... etc

并不是说以上说法是正确的,但是我想知道是否有办法以物理方式在代码中重建模型,包括所有激活函数?

Not saying that the above is correct, but I want to know if there a way for me to physically reconstruct the model in code, including all activation functions?

我想我可以阅读摘要,但是我不知道是否能够从摘要中确定激活.

I guess I can read the summary, but I don't know if I will be able to determine activation from it.

这怎么办?

推荐答案

如果您保存的模型具有完整的体系结构及其训练状态.也就是说,您已经使用过类似的东西.

If you have a model saved with complete architecture along with its training states. ie u have used something like this.

model.save('myfirstmodel.h5')

您可以使用

pprint(model.to_json())
pprint(model.to_yaml())

json的输出:

('{"class_name": "Sequential", "config": {"name": "sequential", "layers": '
 '[{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 13], '
 '"dtype": "float32", "sparse": false, "ragged": false, "name": "d1_input"}}, '
 '{"class_name": "Dense", "config": {"name": "d1", "trainable": true, '
 '"batch_input_shape": [null, 13], "dtype": "float32", "units": 4, '
 '"activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": '
 '"Ones", "config": {}}, "bias_initializer": {"class_name": "Zeros", "config": '
 '{}}, "kernel_regularizer": null, "bias_regularizer": null, '
 '"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
 'null}}, {"class_name": "Dense", "config": {"name": "d2", "trainable": true, '
 '"dtype": "float32", "units": 6, "activation": "relu", "use_bias": true, '
 '"kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": '
 'null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, '
 '"kernel_regularizer": null, "bias_regularizer": null, '
 '"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
 'null}}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": '
 'true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}}, '
 '{"class_name": "Dense", "config": {"name": "out", "trainable": true, '
 '"dtype": "float32", "units": 2, "activation": "sigmoid", "use_bias": true, '
 '"kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": '
 'null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, '
 '"kernel_regularizer": null, "bias_regularizer": null, '
 '"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
 'null}}]}, "keras_version": "2.4.0", "backend": "tensorflow"}')

但是,如果您的冻结模型无法正常使用,则可以使用netron查看模型的结构. 它显示了逐层体系结构以及使用的激活函数,参数及其权重.您可以将这些权重下载为NumPy数组.

However, if you have a frozen model in which your normal means dont work you can take a look at the structure of the model using netron. It shows layer-wise architecture as well as which activation functions are used, parameter as well as its weights. You can download these weights as NumPy arrays.

您可以使用Netron来找到模型的结构以及权重.使用此结构信息,您可以重建模型.

You can use Netron in order to find the architecture of the model along with weights. Using this structural information you can rebuild your model.

请参见链接.

您将获得如下输出:

You get output like this:

这篇关于在tensorflow中,是否可以看到其他模型的构建结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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