在Keras序列模型中添加时间分布(密集)层时出现断言错误

在Keras序列模型中添加时间分布(密集)层时出现断言错误,keras,rnn,Keras,Rnn,我正在构建一个RNN来预测多对一问题 #Input_X: [ [1,2,3,4,5,6,7,8,9,10], [2,3,4,5,6,7,8,9,10,11] ] #Input_Y: [ 11, 12 ] #Each number represent a category X=np.reshape(Input_X,(len(Input_X), 10, 1)) y=np.utils.to_catgeorical(Input_Y) #one hot encode, 我的模型设置: #####

我正在构建一个RNN来预测多对一问题

#Input_X:
[
[1,2,3,4,5,6,7,8,9,10],
[2,3,4,5,6,7,8,9,10,11]
]

#Input_Y:
[
11,
12
]
#Each number represent a category

X=np.reshape(Input_X,(len(Input_X), 10, 1))

y=np.utils.to_catgeorical(Input_Y)  #one hot encode,
我的模型设置:

#####This works 
model=Sequential()
model.add(LSTM(256, input_shape(X.shape[1], X.shape[2])))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 
我想改为尝试时间分布(密集)层,例如:。因此,我将上面的内容改为下面的内容:

model=Sequential()
model.add(LSTM(256, input_shape(X.shape[1], X.shape[2])))
model.add(TimeDistributed(Dense(y.shape[1], activation='softmax')))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 
我得到了一个断言错误。哪个报表的矩阵大小不是预期的。
我错过了哪些步骤?

我认为您需要将
return\u sequences=True
添加到LSTM单元格中

```
model=Sequential()
model.add(LSTM(256, return_sequences=True, input_shape(X.shape[1], X.shape[2])))
model.add(TimeDistributed(Dense(y.shape[1], activation='softmax')))
model.compile(loss='categorical_crossentrophy', optimizer='adam', metrics=['accuracy'])
model.fit(X,y, ........) 

```
return\u sequences=True
适合我


在OP的问题中,y.shape是(2,1),它有两个样本,只有一个特征,因此它不适合多对多模型。

时间分布密度确实适用于多对多,如果我将输入y修改为每个输出的长度为2。当使用
return\u sequences=True
I get
ValueError:Error当检查target:expected time\u distributed\u 11有3个维度时,我仍然得到了AssertionErrorNope,但得到了形状为(2,50)的数组