【问题标题】:In tensorflow, is it possible to see another models build structure?在 tensorflow 中,是否可以看到其他模型构建结构?
【发布时间】:2020-08-26 19:43:14
【问题描述】:

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

我想得到这个的代码表示,例如:

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

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

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

如何做到这一点?

【问题讨论】:

    标签: tensorflow keras neural-network


    【解决方案1】:

    如果您保存了具有完整架构及其训练状态的模型。即你用过这样的东西。

    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 数组。

    您可以使用 Netron 来查找模型的架构以及权重。使用这些结构信息,您可以重建模型。

    Link

    你会得到这样的输出:

    【讨论】:

    • 如果它有效,请您接受并支持答案。
    • 是的,谢谢,我今天刚试过。除了使用带有激活的 json 外,Neutron 也可以毫不费力地工作。我能够重新创建模型
    【解决方案2】:

    您可以使用model.get_config() 获取重新实例化相同模型所需的所有信息的dict(至少在理论上,因为这取决于模型中的每一层都具有正确的get_config 方法本身,历史上并非总是如此)。

    如果您导入的模型表现良好,那么您甚至可以通过这样做来创建克隆模型

    new_model = tf.keras.Model.from_config(model.get_config())
    

    这并不总是有效,所以一般来说,您可以从model.get_config() 开始检查模型的详细信息。

    【讨论】:

      【解决方案3】:

      答案是。 tf 或 Keras 无法从模型文件或 YAML 或 JSON 中获取代码表示。但是,是的,您可以编写一段代码来完成这项工作。这应该很容易。如果你像下面这样保存你的模型:

      model.to_yaml()
      

      它会保存您的模型配置以及激活函数。您需要做的就是一层一层地遍历层并添加层的代码表示。但是损失函数对你来说可能是个问题。

      顺便说一句,如果你想重新训练保存的模型,你不需要代码结构。你所能做的就是加载和训练。只是不要冻结任何层。它将重新训练和更新所有路径权重。

      【讨论】:

        猜你喜欢
        • 2016-12-29
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        • 2019-09-03
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多