我不断从以下代码中收到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)
当我检查the website时,它会指定一个元组“(例如(100,)用于100维输入)。”
话虽这么说,我的数据集由一列长度为10320组成。我认为这意味着我应该将(10320,)
作为input_shape,但我仍然得到错误。有没有人有解决方案?
答案 0 :(得分:4)
我的理解是你的输入和输出都是一维向量。诀窍是根据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)
以下是要点:
希望这有帮助。
答案 1 :(得分:2)
更多信息:当使用带有可变长度序列的RNN(如LSTM)时,您必须采用数据格式。
当您对序列进行分组以将其传递给fit方法时,keras将尝试构建样本矩阵,这意味着所有输入序列必须具有相同的大小,否则您将不会有一个矩阵正确的维度。
有几种可能的解决方案:
第三种解决方案对应于可变大小的最常见策略。如果你填充序列(第二或第三个解决方案),你可能想要添加一个掩蔽层作为输入。
如果您不确定,请尝试打印数据的形状(使用numpy数组的shape属性。)
您可能需要查看:https://keras.io/preprocessing/sequence/(pad_sequences)和https://keras.io/layers/core/#masking
答案 2 :(得分:2)
以下是 Keras 2.0.0 的工作版本,修改后的基数代码
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)
答案 3 :(得分:0)
尝试使用LSTM图层而不指定输入形状。让Keras为你工作。我认为你也评论了掩蔽,因为你得到了类似的问题。我之前遇到过它,结果是input_shape =(time_steps,input_dim)。我认为这是因为Keras新的自动形状推断。