使用keras.concatenate层时发生AttributeError

时间:2019-10-20 15:47:15

标签: python tensorflow keras neural-network

我正在尝试基于用于图像但用于一维矢量的Inception架构构建神经网络。

我已经从这个链接https://keras.io/getting-started/functional-api-guide/的keras入门指南中基于这个模型创建了模型:

tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

input_vector = Input(shape=(71276,1),)

tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)

tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_3')(input_vector)
tower_2 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_2)

tower_3 = tf.keras.layers.MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_4')(tower_3)

output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])

model = tf.keras.models.Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
              optimizer=tf.keras.optimizers.Adam(lr=0.001),
              metrics=['mae'])

model.summary()

这是我的代码:

from keras.layers import Conv1D, MaxPooling1D, Input
from keras.models import Model

tf.keras.backend.clear_session()
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

input_vector = Input(shape=(71276,1),)

tower_1 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_1 = Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_1')(tower_1)

tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
tower_2 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_2)

tower_3 = MaxPooling1D(pool_size=3, strides=1, padding='same')(input_vector)
tower_3 = Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(tower_3)

output = tf.keras.layers.concatenate([tower_1, tower_2, tower_3])

model = Model(inputs=input_vector, outputs=output)
model.compile(loss='mse',
              optimizer=tf.keras.optimizers.Adam(lr=0.001),
              metrics=['mae'])

model.summary()

执行时,出现以下错误,并且不真正理解为什么:

AttributeError                            Traceback (most recent call last)
<ipython-input-9-2931ae837421> in <module>()
      6 input_vector = Input(shape=(71276,1),)
      7 
----> 8 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=1, padding='same', activation='relu', name='conv_1')(input_vector)
      9 tower_1 = tf.keras.layers.Conv1D(filters=64, kernel_size=3, padding='same', activation='relu', name='conv_2')(tower_1)
     10 

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer.py in <lambda>(t)
   2056             `call` method of the layer at the call that created the node.
   2057     """
-> 2058     inbound_layers = nest.map_structure(lambda t: t._keras_history.layer,
   2059                                         input_tensors)
   2060     node_indices = nest.map_structure(lambda t: t._keras_history.node_index,

AttributeError: 'tuple' object has no attribute 'layer'

我对卷积层没有太多经验,所以很可能我犯了一个非常明显的错误。在线搜索,我找不到其他遇到相同问题的人。

我正在python 3运行时中在Google Colaboratory上运行它。

任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:2)

几件事:

  • 您的所有图层都具有相同的名称?我敢打赌,这可能会导致很多奇怪的错误
  • tower_3与其他两个塔的形状不同。无法连接。 (您使用的是MaxPooling1D,请查看摘要以确认。)
  • 您正在混合使用kerastf.keras,这肯定是一个很大的问题。仅选择一个。