簡體   English   中英

如何在Keras中為LSTM模型正確指定輸入形狀

[英]How to properly specify an input shape for a LSTM model in Keras

假設我們有一個Numpy數組,其中包含整數。

arr = [1, 2, 3]

我們有一個Keras模型。

model.add(LSTM(128, input_shape=(arr)))

如何正確指定輸入形狀?

LSTM層的輸入數據必須是三維的,並且形狀為(num_samples, timesteps, num_features)

您為LSTM圖層指定的input_shape具有形狀(timesteps, num_features) input_shape並不關心樣本數量,僅關心每個樣本的形狀。

如果我們假設timesteps=1 ,那么您想要做這樣的事情。

arr = np.array([1, 2, 3])
arr.shape  # (3,)
arr = arr.reshape(arr.shape[0], 1, 1)
arr.shape  # (3, 1, 1)

model.add(LSTM(128, input_shape=(arr.shape[1], arr.shape[2])))

timesteps=1使用LSTM並沒有多大意義,但希望您能理解。

暫無
暫無

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

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