python - 绘制 keras 自定义模型时,“ListWrapper”对象没有属性 'name'

标签 python python-2.7 tensorflow keras tensorflow2.0

我想绘制自定义 keras 模型的底层拓扑。根据此链接( https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/ ),我以为我可以使用 keras.utils.vis_utils.plot_model ,但这产生了错误。

这是重现错误的最小自定义模型和代码:

import tensorflow as tf

from keras.models import Model
from keras import backend as K
from keras.utils.vis_utils import plot_model

import unittest

'''
Construct a double-layer perceptron without an activation
'''

rows = 10
cols = 2

class Model(tf.keras.Model):
    def __init__(self, hidden_topology):
        super(Model, self).__init__(name='')
        self.hidden_topology = hidden_topology

    def call(self, inputs):
        hidden_output = inputs
        for hidden_layer in self.hidden_topology:
            hidden_output = hidden_layer(hidden_output)

        return hidden_output

    def compute_output_shape(self, input_shape):
        return (input_shape[0][0], 1)

model = Model(
    [
        tf.keras.layers.Dense(
            1,
            input_shape=((rows, cols), ),
            use_bias=True,
            kernel_initializer=tf.constant_initializer(1.0),
            bias_initializer=tf.constant_initializer(0.0)),
        tf.keras.layers.Dense(
            1,
            input_shape=((rows, cols), ),
            use_bias=True,
            kernel_initializer=tf.constant_initializer(1.0),
            bias_initializer=tf.constant_initializer(0.0))
    ])


test_data = np.reshape(range(rows*cols), (rows,cols)).astype(np.float32)
top = model.call(test_data)

#plot_model(top, to_file='model_plot.png')#, show_shapes=True, show_layer_names=True)
plot_model(model, to_file='model_plot.png')#, show_shapes=True, show_layer_names=True)

这会产生以下错误:

AttributeErrorTraceback (most recent call last)
<ipython-input-3-b73c347c7b0a> in <module>()
     49 # top = model.call(test_data)
     50 
---> 51 plot_model(model, to_file='model_plot.png')#, show_shapes=True, show_layer_names=True)
     52 
     53 # def call(self, inputs):

/package/python-2.7.15/lib/python2.7/site-packages/keras/utils/vis_utils.pyc in plot_model(model, to_file, show_shapes, show_layer_names, rankdir, expand_nested, dpi)
    238     """
    239     dot = model_to_dot(model, show_shapes, show_layer_names, rankdir,
--> 240                        expand_nested, dpi)
    241     _, extension = os.path.splitext(to_file)
    242     if not extension:

/package/python-2.7.15/lib/python2.7/site-packages/keras/utils/vis_utils.pyc in model_to_dot(model, show_shapes, show_layer_names, rankdir, expand_nested, dpi, subgraph)
    104 
    105         # Append a wrapped layer's label to node's label, if it exists.
--> 106         layer_name = layer.name
    107         class_name = layer.__class__.__name__
    108 

AttributeError: 'ListWrapper' object has no attribute 'name'

我也尝试了注释掉的行,但没有成功。

如何可视化此拓扑?我正在使用 tensorflow 2.0.0

最佳答案

您提到的链接在使用tf.keras时使用keras(Tensorflow的高级API)。
而不是:

from keras.utils.vis_utils import plot_model

将此行更改为:

from tensorflow.keras.utils import plot_model

编辑:
虽然您将摆脱此错误,但由于您使用的是子类模型,因此您将看到的只是图中的模型 block 。要绘制完整的模型图,您必须使用顺序功能模型。我还建议将类名称更改为 Model 以外的名称。

关于python - 绘制 keras 自定义模型时,“ListWrapper”对象没有属性 'name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443567/

相关文章:

hadoop - tensorflow 和 hadoop 的兼容性

python - 具有一些不可训练权重的自定义 Keras 层

python - 在 Python 中解决 CodingBat 拼砖难题

python - 保存麻烦的网页并导入回Python

python - 如何根据另一个变量的值调用列表中的元素?

python - Python可以 pickle lambda函数吗?

python - 属性错误 : 'module' object has no attribute 'tests'

python - 凯拉斯, tensorflow : How to set breakpoint (debug) in custom layer when evaluating?

python - MemoryError 与 numpy arange

python - 如何选择具有相同索引的行以在python pandas中形成一个新的DataFrame?