'ListWrapper' 对象在绘制 keras 自定义模型时没有属性 'name'
'ListWrapper' object has no attribute 'name' when plotting keras custom model
我想绘制自定义 keras 模型的底层拓扑。根据这个 link (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
Link你说的是用keras
,而你用的是tf.keras
(Tensorflow的高级API)。
而不是:
from keras.utils.vis_utils import plot_model
将此行更改为:
from tensorflow.keras.utils import plot_model
编辑:
虽然您会消除此错误,但由于您使用的是子 classed 模型,您将在 plot.To 绘图完整模型图中看到的只是一个模型块,您将不得不使用 顺序或功能模型。我还建议将 class 的名称更改为 Model
以外的名称。
我想绘制自定义 keras 模型的底层拓扑。根据这个 link (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
Link你说的是用keras
,而你用的是tf.keras
(Tensorflow的高级API)。
而不是:
from keras.utils.vis_utils import plot_model
将此行更改为:
from tensorflow.keras.utils import plot_model
编辑:
虽然您会消除此错误,但由于您使用的是子 classed 模型,您将在 plot.To 绘图完整模型图中看到的只是一个模型块,您将不得不使用 顺序或功能模型。我还建议将 class 的名称更改为 Model
以外的名称。