keras.layers.Concatenate 给出 'NoneType' 对象不可下标
Posted
技术标签:
【中文标题】keras.layers.Concatenate 给出 \'NoneType\' 对象不可下标【英文标题】:keras.layers.Concatenate gives 'NoneType' object is not subscriptablekeras.layers.Concatenate 给出 'NoneType' 对象不可下标 【发布时间】:2022-01-15 21:31:16 【问题描述】:当我尝试连接卷积层和 LSTM 层时。它注意到我“'NoneType' 对象不可下标”。我该如何解决?我不明白为什么我不能连接它们。
我的代码是这样的:
x = inputI
x = keras.layers.Reshape((126,40,1))(x)
x = keras.layers.Conv2D(32, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.Conv2D(32, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.MaxPooling2D(pool_size=(2, 2))(x)
x = keras.layers.Flatten()(x)
y = inputE
y = keras.layers.LSTM(16, return_sequences=True)
y = keras.layers.Flatten()
y = keras.layers.Dense(2, activation='sigmoid')
z = keras.layers.Concatenate()([x,y])
z = keras.layers.Dense(100, activation='sigmoid')(z)
z = keras.layers.Dense(10, activation='sigmoid')(z)
它给出:
TypeError Traceback (most recent call last)
<ipython-input-45-2cb5d4fc2fb1> in <module>()
18 y = keras.layers.Dense(2, activation='sigmoid')
19
---> 20 z = keras.layers.Concatenate()([x,y])
21 z = keras.layers.Dense(100, activation='sigmoid')(z)
22 z = keras.layers.Dense(10, activation='sigmoid')(z)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/merge.py in build(self, input_shape)
495 def build(self, input_shape):
496 # Used purely for shape validation.
--> 497 if not isinstance(input_shape[0], tuple) or len(input_shape) < 1:
498 raise ValueError(
499 'A `Concatenate` layer should be called on a list of '
TypeError: 'NoneType' object is not subscriptable
【问题讨论】:
你需要在所有y
层前面添加(y)
,以连接它们,
【参考方案1】:
这是因为你的y
层没有正确连接,试试这个:
y = inputE
y = keras.layers.LSTM(16, return_sequences=True)(y)
y = keras.layers.Flatten()(y)
y = keras.layers.Dense(2, activation='sigmoid')(y)
【讨论】:
以上是关于keras.layers.Concatenate 给出 'NoneType' 对象不可下标的主要内容,如果未能解决你的问题,请参考以下文章