通过求和另一层的一部分来定义皮层



任何人都可以给我一个建议:运行以下代码时,发生错误(我将TF用作后端(

inputs = Input(shape=(100, 1, ))
lstm = LSTM(3, return_sequences=True)(inputs)
outputs = 2*lstm[:, :, 0] + 5*lstm[:, :, 1] + 10*lstm[:, :, 2]
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x, y)

错误是

TypeError:模型的输出张量必须是keras张量。找到:张量(" add_1:0",shape =(?,?(,dtype = float32(

如果我正确理解这个问题,

outputs = Lambda(lambda x: 2*x[:, :, 0] + 5*x[:, :, 1] + 10*x[:, :, 2])(lstm)

应该做您要寻找的事情。

In [94]: model = Model(inputs=inputs, outputs=outputs)
In [95]: model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_5 (InputLayer)         (None, 100, 1)            0
_________________________________________________________________
lstm_12 (LSTM)               (None, 100, 3)            60
_________________________________________________________________
lambda_4 (Lambda)            (None, 100)               0
=================================================================
Total params: 60
Trainable params: 60
Non-trainable params: 0

例如,简单地添加两个输入

In [143]: inputs = Input(shape=(2,))
In [144]: outputs = Lambda(lambda x: x[:, 0] + x[:, 1])(inputs)
In [145]: model = Model(inputs, outputs)
In [146]: model.predict(np.array([[1, 5], [2, 6]]))
Out[146]: array([ 6.,  8.], dtype=float32)

最新更新