簡體   English   中英

制作有狀態 LSTM 時出現 InvalidArgumentError

[英]InvalidArgumentError when making a stateful LSTM

我正在研究一個有狀態的 LSTM 來預測股票價格。

這些是我輸入數據的形狀:(更新)

x_train = (10269, 300, 89)
y_train = (10269, 1)
x_test = (4401, 300, 89)
y_test = (4401, 1)

這是我的模型初始化:

batch_size = 63
timesteps = x_train.shape[1]
data_dim = x_train.shape[2]

model = Sequential()

model.add(LSTM(32, return_sequences=True, batch_input_shape=(batch_size, timesteps, data_dim), stateful=True))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))

model.add(Dense(1))

model.compile(optimizer = 'adam', loss = 'mean_squared_error')

但是當我適應這個時,我得到了錯誤:

InvalidArgumentError:    Specified a list with shape [64,89] from a tensor with shape [29,89]
 [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
 [[sequential/lstm/PartitionedCall]] [Op:__inference_train_function_6536]

據我所知,我已經正確定義了 batch_input_shape 並且沒有看到我做錯了什么。

編輯:

有人建議我嘗試讓我的樣本量除以我的批量大小。 我試過了,得到了同樣的錯誤。

(我更新了我的火車和測試尺寸,如上所示)

我的新批次大小是 63,我的數據大小是 10269。10269/63 = 163。這是錯誤:

InvalidArgumentError:    Specified a list with shape [63,89] from a tensor with shape [54,89]
 [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
 [[sequential_1/lstm_3/PartitionedCall]] [Op:__inference_test_function_20179]

使用有狀態 LSTM 時,您的輸入必須能被批量大小整除。

在您的情況下3697 // 64不是整數。

由於 3697 是質數,因此您需要刪除一個樣本使您的輸入大小為 3696。當您有 3696 個樣本時,根據(模型定義保持不變)更改代碼:

batch_size = 33 # some number that divides your samples evenly.
timesteps = x_train.shape[1]
data_dim = x_train.shape[2]

model.fit(x_train, y_train, batch_size = batch_size, ...)

這個問題與stateful參數有關。 使用時,樣本數應能被樣本數整除。

在您的情況下,您有 3697 個不能被 64 整除的樣本。

因此,您可以做的是刪除 49 個樣本並僅取 3648 個樣本,因為 3648 可被 64 整除。

驗證數據的樣本數量也是如此。 您必須將其更改為可被批量大小整除的數字。

其次,使用: model.fit(x_train, y_train, batch_size=batch_size,validation_data=(x_val,y_val))

如果你不想從數據集中刪除任何樣本,您可以用數據發生器工作,如圖這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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