简体   繁体   中英

keras tensor reshaping (lstm input shape error)

I am using LSTM on keras and using a reshape layer prior in hopes that I don't have to specify shape for the LSTM layer.

the input is 84600 x 6

84600 seconds in 2 months. 6 metric/[labels] im measuring throughout the 2 months

so far I have

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'))

which throws an error:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 86400, 1, 6]

this is understandable. The batch size, plus 3 layers equals 4. However, when I reshape

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)))

It throws

ValueError: Error when checking input: expected reshape_input to have 3 dimensions, but got array with shape (86400, 6)

It seems to ignore the batch size as an array element. And treats it as 2 indexes. It jumps from 4 dimensions to 2 dimensions.

The problem is LSTM takes 3 dimensions as input, and I can't seem to get that. Ideally I want a 86400 x 1 x 6 array/tensor. So it becomes 84600 examples of 1x6 data.

Thank you very much!

The problem is that the way you are reshaping your input is incompatible with an LSTM layer. An LSTM layer expects an input with 3 dimensions: (batch_size, timesteps, features) . However, you are feeding it an input with shape (batch_size, 84600, 1, 6) .

In your case it seems like 84600 is the number of timesteps and 6 is the number of features per timestep. So, it makes more sense to leave out the Reshape layer and simply use input_shape (84600, 6) for your LSTM layer:

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM