绘制keras自定义模型时,"ListWrapper"对象没有属性“名称" [英] 'ListWrapper' object has no attribute 'name' when plotting keras custom model
问题描述
我想绘制一个定制keras模型的基础拓扑.根据此链接( https://machinelearningmastery.com/visualize-我认为我可以只使用keras.utils.vis_utils.plot_model
,但这会产生错误.
I would like to plot the underlying topology of a custom keras model. According to this link (https://machinelearningmastery.com/visualize-deep-learning-neural-network-model-keras/) I thought I'd be able to just use keras.utils.vis_utils.plot_model
, but this yielded an error.
这是最小的自定义模型和代码来重现该错误:
Here's a minimal custom model and code to reproduce the error:
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'
我也尝试了注释行,但无济于事.
I also tried the commented out line, to no avail.
如何可视化此拓扑?我正在使用tensorflow 2.0.0
How can I visualise this topology? I'm using tensorflow 2.0.0
推荐答案
在使用tf.keras
时,您提到的链接正在使用keras
(Tensorflow的高级
Link you have mentioned is using keras
while you are using tf.keras
(Tensorflow's high level API).
Instead of:
from keras.utils.vis_utils import plot_model
将此行更改为:
from tensorflow.keras.utils import plot_model
修改:
尽管您将摆脱此错误,但是由于您使用的是子类化模型,因此您只能在绘图中看到一个模型块.要绘制完整的模型图,您将必须使用 Sequential 或功能模型.我还建议将班级名称更改为Model
以外的其他名称.
Although you will get rid of this error, but since you are using sub-classed model all you will see is a model block in your plot.To plot complete model graph you'll have to use either Sequential or Functional model. I would also suggest to change name of your class to something other than Model
.
这篇关于绘制keras自定义模型时,"ListWrapper"对象没有属性“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!