[英]keras tensor reshaping (lstm input shape error)
我在keras上使用LSTM並事先使用了重塑層,希望我不必為LSTM層指定形狀。
輸入為84600 x 6
2個月內84600秒。 在整個2個月中進行6種度量/ [標簽]即時測量
到目前為止,我有
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Reshape((86400,1,6), input_shape=(84600, 6)))
model.add(tf.keras.layers.LSTM(128, activation='relu', input_shape=
(x_train.shape), return_sequences=True))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
這會引發錯誤:
ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 86400, 1, 6]
這是可以理解的。 批處理大小加上3層等於4。但是,當我重塑形狀時
model.add(tf.keras.layers.Reshape((86400,1,6), input_shape=(84600, 6)))
vvvvvvv
model.add(tf.keras.layers.Reshape((86400,6), input_shape=(84600, 6)))
它拋出
ValueError: Error when checking input: expected reshape_input to have 3 dimensions, but got array with shape (86400, 6)
似乎忽略了批處理大小作為數組元素。 並將其視為2個索引。 它從4維跳到2維。
問題是LSTM將3維作為輸入,但我似乎無法理解。 理想情況下,我想要86400 x 1 x 6陣列/張量。 因此,它成為1x6數據的84600個示例。
非常感謝你!
問題在於,重塑輸入的方式與LSTM層不兼容。 LSTM層期望輸入3個維度: (batch_size, timesteps, features)
要素(batch_size, timesteps, features)
。 但是,您正在向其輸入形狀為(batch_size, 84600, 1, 6)
。
在您的情況下,似乎時間步數為84600,每個時間步的要素數為6。 因此,省略Reshape層並為LSTM層簡單使用input_shape (84600, 6)
84600,6 (84600, 6)
更有意義:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.LSTM(128, activation='relu', input_shape=(84600, 6), return_sequences=True))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.