Home > Back-end >  Can I create an LSTM where the input data and the target data are asymmetric?
Can I create an LSTM where the input data and the target data are asymmetric?

Time:02-15

Input data : x.shape = (500, 20, 1)

Target data: y.shape = (500, 5, 1)

I create the following model:

model = Sequential()
model.add(LSTM(units = 64, input_shape=(20, 1), return_sequences = True))
model.add(Dense(units=1))
model.compile(loss='mae', optimizer='adam')
model.fit(x, y, epochs=1000, batch_size=1)

but I get this error:

ValueError: Dimensions must be equal, but are 20 and 5 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](sequential/dense/BiasAdd, IteratorGetNext:1)' with input shapes: [1,20,1], [1,5,1].

Is there a way around this?

CodePudding user response:

If you are using return_sequences=True, y also has to have the same number of timesteps. This is what is causing the mismatch since x has 20 and y 5. You have a few options to experiment with:

Option 1:

You could try using tf.keras.layers.RepeatVector:

import tensorflow as tf

x = tf.random.normal(((500, 20, 1)))
y = tf.random.normal(((500, 5, 1)))
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units = 64, input_shape=(20, 1), return_sequences = False))
model.add(tf.keras.layers.RepeatVector(5))
model.add(tf.keras.layers.Dense(units=1))
model.compile(loss='mae', optimizer='adam')
model.fit(x, y, epochs=1000, batch_size=1)

Option 2:

You could use return_sequences=True and a Conv1D layer to downsample the timesteps:

x = tf.random.normal(((500, 20, 1)))
y = tf.random.normal(((500, 5, 1)))
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units = 64, input_shape=(20, 1), return_sequences = True))
model.add(tf.keras.layers.Conv1D(32, 16))
model.add(tf.keras.layers.Dense(units=1))
model.compile(loss='mae', optimizer='adam')
model.fit(x, y, epochs=1000, batch_size=1)

Option 3:

You could use tf.keras.layers.Flatten() and then reshape your tensor to have the correct output shape:

x = tf.random.normal(((500, 20, 1)))
y = tf.random.normal(((500, 5, 1)))
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units = 64, input_shape=(20, 1), return_sequences = True))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(5))
model.add(tf.keras.layers.Reshape(y.shape[1:]))
  • Related