[英]Why do I get a Keras LSTM RNN input_shape error?
我不断从以下代码中获取input_shape错误。
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
def _load_data(data):
"""
data should be pd.DataFrame()
"""
n_prev = 10
docX, docY = [], []
for i in range(len(data)-n_prev):
docX.append(data.iloc[i:i+n_prev].as_matrix())
docY.append(data.iloc[i+n_prev].as_matrix())
if not docX:
pass
else:
alsX = np.array(docX)
alsY = np.array(docY)
return alsX, alsY
X, y = _load_data(dframe)
poi = int(len(X) * .8)
X_train = X[:poi]
X_test = X[poi:]
y_train = y[:poi]
y_test = y[poi:]
input_dim = 3
以上所有都顺利进行。 这是它出错的地方。
in_out_neurons = 2
hidden_neurons = 300
model = Sequential()
#model.add(Masking(mask_value=0, input_shape=(input_dim,)))
model.add(LSTM(in_out_neurons, hidden_neurons, return_sequences=False, input_shape=(len(full_data),)))
model.add(Dense(hidden_neurons, in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.fit(X_train, y_train, nb_epoch=10, validation_split=0.05)
它返回此错误。
Exception: Invalid input shape - Layer expects input ndim=3, was provided with input shape (None, 10320)
当我检查网站时,它说要指定一个元组“(例如(100,)用于100维输入)。”
话虽这么说,我的数据集由一列长度为10320组成。我认为这意味着我应该把(10320,)
作为input_shape,但我仍然得到错误。 有没有人有办法解决吗?
我的理解是你的输入和输出都是一维向量。 诀窍是根据Keras要求重塑它们:
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
import numpy as np
X= np.random.rand(1000)
y = 2*X
poi = int(len(X) * .8)
X_train = X[:poi]
y_train = y[:poi]
X_test = X[poi:]
y_test = y[poi:]
# you have to change your input shape (nb_samples, timesteps, input_dim)
X_train = X_train.reshape(len(X_train), 1, 1)
# and also the output shape (note that the output *shape* is 2 dimensional)
y_train = y_train.reshape(len(y_train), 1)
#in_out_neurons = 2
in_out_neurons = 1
hidden_neurons = 300
model = Sequential()
#model.add(Masking(mask_value=0, input_shape=(input_dim,)))
model.add(LSTM(hidden_neurons, return_sequences=False, batch_input_shape=X_train.shape))
# only specify the output dimension
model.add(Dense(in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.fit(X_train, y_train, nb_epoch=10, validation_split=0.05)
# calculate test set MSE
preds = model.predict(X_test).reshape(len(y_test))
MSE = np.mean((preds-y_test)**2)
以下是要点:
希望这可以帮助。
更多信息:当使用具有可变长度序列的RNN(如LSTM)时,您必须采用数据格式。
当您对序列进行分组以将其传递给fit方法时,keras将尝试构建样本矩阵,这意味着所有输入序列必须具有相同的大小,否则您将不具有正确维度的矩阵。
有几种可能的解决方案
第三种解决方案对应于可变大小的最常见策略。 如果你填充序列(第二或第三个解决方案),你可能想要添加一个掩蔽层作为输入。
如果您不确定,请尝试打印数据的形状(使用numpy数组的shape属性。)
您可能需要查看: https ://keras.io/preprocessing/sequence/(pad_sequences)和https://keras.io/layers/core/#masking
以下是使用Keras 2.0.0 ,Modified radix代码的工作版本
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
import numpy as np
X= np.random.rand(1000)
y = 2 * X
poi = int(len(X) * .8)
X_train = X[:poi]
y_train = y[:poi]
X_test = X[poi:]
y_test = y[poi:]
# you have to change your input shape (nb_samples, timesteps, input_dim)
X_train = X_train.reshape(len(X_train), 1, 1)
# and also the output shape (note that the output *shape* is 2 dimensional)
y_train = y_train.reshape(len(y_train), 1)
# Change test data's dimension also.
X_test = X_test.reshape(len(X_test),1,1)
y_test = y_test.reshape(len(y_test),1)
#in_out_neurons = 2
in_out_neurons = 1
hidden_neurons = 300
model = Sequential()
# model.add(Masking(mask_value=0, input_shape=(input_dim,)))
# Remove batch_input_shape and add input_shape = (1,1) - Imp change for Keras 2.0.0
model.add(LSTM(hidden_neurons, return_sequences=False, input_shape=(X_train.shape[1],X_train.shape[2])))
# only specify the output dimension
model.add(Dense(in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
model.summary()
model.fit(X_train, y_train, epochs=10, validation_split=0.05)
# calculate test set MSE
preds = model.predict(X_test).reshape(len(y_test))
print(preds)
MSE = np.mean((preds-y_test)**2)
print('MSE ', MSE)
尝试使用LSTM图层而不指定输入形状。 让Keras为你工作。 我认为你也评论了掩蔽,因为你得到了类似的问题。 我之前遇到过它,结果是input_shape =(time_steps,input_dim)。 我认为这是因为Keras的新自动形状推断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.