我正在尝试keras数据的示例,数据形状如下所示:
x_train形状:(25000,80)
我只需将keras示例的原始代码更改为如下代码:
model = Sequential()
layer1 = Embedding(max_features, 128)
layer2 = LSTM(128, dropout = 0.2, recurrent_dropout = 0.2, return_sequences = True)
layer3 = Dense(1, activation = 'sigmoid')
model.add(layer1)
model.add(layer2)
model.add(layer3)
最初的模型将return_sequences
设置为False
,我将其更改为True
,并遇到了以下错误:
期望dense_1具有三维,但得到形状为(25000,1)的数组
但是我打印了模型的结构,发现LSTM层的输出正好是一个三维张量:
lstm_1:(无,无,128)
发布于 2017-11-09 22:58:04
您需要重塑您的培训数组,请使用以下代码:
x_train = np.reshape(x_train,(x_train.shape[0],1,x_train.shape[1]))
还有您的测试数组:
x_test = np.reshape(x_test,(x_test.shape[0],1,x_test.shape[1]))
np是一种不稳定的包装。
LSTM模型中的时间步骤:https://machinelearningmastery.com/use-timesteps-lstm-networks-time-series-forecasting/
时间步骤:这相当于运行递归神经网络的时间步骤的数量。如果你想要你的网络有60个字符的内存,这个数字应该是60。
发布于 2017-11-09 23:43:58
我认为您需要一个TimeDistributed层,在使用return_sequences=True的LSTM之后
layer2= LSTM(128, dropout=0.2,
recurrent_dropout=0.2,return_sequences=True)
layer3= TimeDistributed(Dense(1, activation='sigmoid')))
https://stackoverflow.com/questions/47217151
复制