【问题标题】:'ListWrapper' object has no attribute 'name' when plotting keras custom model绘制 keras 自定义模型时,“ListWrapper”对象没有属性“名称”
【发布时间】:2020-04-14 01:10:00
【问题描述】:

我想绘制自定义 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'

我也尝试了注释掉的行,但无济于事。

如何可视化此拓扑?我正在使用张量流 2.0.0

【问题讨论】:

    标签: python python-2.7 tensorflow keras tensorflow2.0


    【解决方案1】:

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

    from keras.utils.vis_utils import plot_model
    

    将此行更改为:

    from tensorflow.keras.utils import plot_model
    

    编辑:
    尽管您将摆脱此错误,但由于您使用的是子分类模型,因此您将在图中看到一个模型块。要绘制完整的模型图,您必须使用 Sequential功能模型。我还建议将您的班级名称更改为 Model 以外的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2019-08-07
      • 2020-09-12
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多