我建立了一个训练集,其形状为X(形状为batch_size,50),Y为形状(batch_size,10(序列长度),10(输出矢量))。
LSTM单元的Keras文档说,需要3D输入,但是Sequence 2 Sequence模型可以正常工作并做到这一点。有另一种常见方法可以解决此任务吗?
model = Sequential()
model.add(LSTM(50, input_shape=(50,)))
model.add(TimeDistributed(Dense(10, activation='softmax')))
我得到错误:“ ValueError:输入0与lstm_1层不兼容:预期的ndim = 3,找到的ndim = 2”
答案 0 :(得分:1)
这仅返回序列的最后一步,您应该设置参数: return_sequences = True
对于以下型号:
model = Sequential()
model.add(LSTM(50, input_shape=(1, 50), return_sequences=True))
model.add(TimeDistributed(Dense(10, activation='softmax')))
我有:
Layer (type) Output Shape Param #
=================================================================
lstm_3 (LSTM) (None, 1, 50) 20200
_________________________________________________________________
time_distributed_2 (TimeDist (None, 1, 10) 510
=================================================================
Total params: 20,710
Trainable params: 20,710
Non-trainable params: 0
_________________________________________________________________
答案 1 :(得分:0)
将input_shape更改为(1,50)并添加
model.add(RepeatVector(10, input_shape=(50, )))
作为第一层,解决了该问题。附加的RepeatVector使输入始终在网络上可见。
关于Keras LSTM网络的一个很好的概述是: How to use return_sequences option and TimeDistributed layer in Keras?