使用reccurent层运行ANN时出错

时间:2016-10-10 07:48:11

标签: python neural-network theano keras

我创建了下面的ANN,其中有2个完全连接的层和一个循环。但是在运行时我会收到错误:Exception: Input 0 is incompatible with layer lstm_11: expected ndim=3, found ndim=2为什么会这样?

from keras.models import Sequential
from keras.layers import Dense
from sklearn.cross_validation import train_test_split
import numpy
from sklearn.preprocessing import StandardScaler
from keras.layers import LSTM 




seed = 7
numpy.random.seed(seed)

dataset = numpy.loadtxt("sorted_output.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:15]
scaler = StandardScaler(copy=True, with_mean=True, with_std=True ) #data normalization
X = scaler.fit_transform(X) #data normalization
Y = dataset[:,15]
# split into 67% for train and 33% for test
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=seed)
# create model
model = Sequential()
model.add(Dense(12, input_dim=15, init='uniform', activation='relu'))
model.add(LSTM(10, return_sequences=True))
model.add(Dense(15, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test,y_test), nb_epoch=150, batch_size=10)

2 个答案:

答案 0 :(得分:0)

基于以上所有图层都是LSTM中的“密集”图层,您将序列返回为true ..您应该设置return_sequences = False,因为所有Dense图层都应该有效。

答案 1 :(得分:0)

此错误的原因是LSTM期望输入具有3维形状(对于批次,序列长度和输入维度)。但是Dense图层在输出2维的形状之前(对于批量和输出维度)。

您可以通过执行以下代码行来查看Dense图层的输出形状

>>> model = Sequential()
>>> model.add(Dense(12, input_dim=15, init='uniform', activation='relu'))
>>> model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
dense_4 (Dense)                  (None, 12)            192         dense_input_2[0][0]              
====================================================================================================
Total params: 192
Trainable params: 192
Non-trainable params: 0
____________________________________________________________________________________________________

但是,您不能用模型解释您的意图,因此我无法为您提供有关此问题的进一步指导。你的输入数据是什么?你期望输入是序列吗?

如果您的输入是序列,那么我建议您删除第一个Dense图层。但如果您的输入不是序列,那么我建议您删除LSTM图层。