我'm a bit new to Keras and deep learning. I'目前正在尝试复制这个paper但是当我编译第二个模型(使用LSTM)时,我收到以下错误:
"TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'"
模型的描述是这样的:
-
输入(长度
T
是设备特定的窗口大小) -
并行1D卷积,分别为过滤器
size
3,5和7,stride=1
,number of filters=32
,activation type=linear
,border mode=same
-
合并并行1D卷积输出的合并层
-
双向LSTM由前向LSTM和后向LSTM组成,
output_dim=128
-
双向LSTM由前向LSTM和后向LSTM组成,
output_dim=128
-
密集层,
output_dim=128
,activation type=ReLU
-
密集层,
output_dim= T
,activation type=linear
我的代码是这样的:
from keras import layers, Input
from keras.models import Model
def lstm_net(T):
input_layer = Input(shape=(T,1))
branch_a = layers.Conv1D(32, 3, activation='linear', padding='same', strides=1)(input_layer)
branch_b = layers.Conv1D(32, 5, activation='linear', padding='same', strides=1)(input_layer)
branch_c = layers.Conv1D(32, 7, activation='linear', padding='same', strides=1)(input_layer)
merge_layer = layers.Concatenate(axis=-1)([branch_a, branch_b, branch_c])
print(merge_layer.shape)
BLSTM1 = layers.Bidirectional(layers.LSTM(128, input_shape=(8,40,96)))(merge_layer)
print(BLSTM1.shape)
BLSTM2 = layers.Bidirectional(layers.LSTM(128))(BLSTM1)
dense_layer = layers.Dense(128, activation='relu')(BLSTM2)
output_dense = layers.Dense(1, activation='linear')(dense_layer)
model = Model(input_layer, output_dense)
model.name = "lstm_net"
return model
model = lstm_net(40)
之后我得到了上述错误 . 我的目标是给出一批长度为40的8个序列作为输入,并获得一批8个长度为40的序列作为输出 . 我在Keras Github LSTM layer cannot connect to Dense layer after Flatten #818上发现了这个问题,并且@fchollet建议我应该在第一层指定'input_shape',但我可能不正确 . 我把两个打印语句看看形状是如何变化的,输出是:
(?, 40, 96)
(?, 256)
行BLSTM2定义的错误发生在here中
1 回答
你的问题在于以下三个方面:
默认情况下,
LSTM
仅返回计算的最后一个元素 - 因此您的数据正在失去其顺序性质 . 这就是前一层引发错误的原因 . 将此行更改为:为了使第二个
LSTM
的输入也具有顺序性质 .除此之外 - 我宁愿不在中间模型层使用
input_shape
,因为它是自动推断的 .