簡體   English   中英

為什么我會收到Keras LSTM RNN input_shape錯誤?

[英]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將嘗試構建樣本矩陣,這意味着所有輸入序列必須具有相同的大小,否則您將不具有正確維度的矩陣。

有幾種可能的解決方案

  1. 逐個使用樣本訓練您的網絡(例如使用fit_generator)
  2. 填充所有序列,使它們具有相同的大小
  3. 按大小對序列進行分組(最終填充它們)並按組訓練您的網絡組(再次使用基於生成器的擬合)

第三種解決方案對應於可變大小的最常見策略。 如果你填充序列(第二或第三個解決方案),你可能想要添加一個掩蔽層作為輸入。

如果您不確定,請嘗試打印數據的形狀(使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM