python - 属性错误 : 'Model' object has no attribute 'name'

标签 python tensorflow keras deep-learning conv-neural-network

我是 Keras 的新手,我在尝试使用 Python 3.6 构建一个 text-classification CNN 模型时遇到了这个错误:

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

这是我写的代码:

print("\nCreating Model...")
x1 = Input(shape=(seq_len1, 100), name='x1')
x2 = Input(shape=(seq_len2, 100), name='x2')
x1 = Reshape((seq_len1, embedding_dim, 1))(x1)
x2 = Reshape((seq_len2, embedding_dim, 1))(x2)

conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], 1), padding='valid', kernel_initializer='normal', activation='relu')
conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], 1), padding='valid', kernel_initializer='normal', activation='relu')
conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], 1), padding='valid', kernel_initializer='normal', activation='relu')

maxpool = MaxPool2D(pool_size=(2, 1), strides=(1,1), padding='valid')

output1 = conv_0(x1)
output1 = maxpool(output1)
output1 = conv_1(output1)
output1 = maxpool(output1)
output1 = conv_2(output1)
output1 = maxpool(output1)
.
.
# Same for output2
.
concatenated_tensor = Concatenate(axis=1)([output1, output2])
flatten = Flatten()(concatenated_tensor)
#dropout = Dropout(drop)(flatten)
output = Dense(units=1024, activation='relu')(flatten)
output = Dense(units=1024, activation='relu')(output)
output = Dense(units=1, activation='softmax')(output)

# this creates a model that includes
model = Model(inputs=[x1, x2], outputs=[output])

最后一行遇到错误。请帮我解决这个问题

编辑:

Traceback (most recent call last):
  File "model.py", line 91, in <module>
    model = Model(inputs=[x1, x2], outputs=[out])
  File "/../../anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 91, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/../../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 183, in _init_graph_network
    'The tensor that caused the issue was: ' +
AttributeError: 'Model' object has no attribute 'name'

最佳答案

x1 和 x2 指向输入的 Reshape 层,而不是输入层本身。

关于python - 属性错误 : 'Model' object has no attribute 'name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51253644/

相关文章:

python - 在不停止另一个功能的情况下运行一个功能

tensorflow - Tensorflow 中的 while_loop 错误

python - Tensorflow tf.matmul 示例不正确?

python - 带有掩蔽和直觉的 LSTM 回归问题 (keras)

tensorflow - 连接 ConvLSTM2D 模型和表格模型的更好方法

python - 循环一个函数直到它达到阈值然后完成脚本

python - 带标题的休息图像网格?

python - Matplotlib 绘图 : AttributeError: 'list' object has no attribute 'xaxis'

python - tensorflow :未创建XLA设备,未设置tf_xla_enable_xla_devices

python - 在 Keras 中将 LSTM 与具有不同张量维度的 CNN 连接起来