0

I am trying to make an autoencoder with Keras. I am having error as follows

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (480, 7)

These are the following data info

df.shape => (480, 7)

timesteps = 15
dim = 7
lH = LossHistory()

model = Sequential()
model.add(LSTM(50, input_shape=(timesteps,dim), return_sequences=True))
model.add(Dense(dim))
model.compile(loss='mae',optimizer = 'adam')

and here is the problem while using fit

model.fit(data,data, epochs=20, batch_size=100, validation_data=(data,data),verbose=0, shuffle=False, callbacks=[lH])
TheTechGuy
  • 1,568
  • 4
  • 18
  • 45
  • Your data need to be of shape (15,7), thus when feeding the batches will be (100,15,7). do you intend to have 480 samples with 7 features each? or you have 32 samples there, each with 15x7 features? – Dinari Dec 11 '18 at 20:58
  • The data has 7 features and 480 samples – TheTechGuy Dec 11 '18 at 20:59
  • @Dinari I am not even sure if the approach is right for LSTM autoencoder – TheTechGuy Dec 11 '18 at 21:02
  • 1
    Havent experienced with LSTM autoencoder, but each sample need to be of shape `(timesteps, dim)`, and your data is only `(dim)` as far as i understand, that cant work. either try a different model, or some other data. (or shape it differently). – Dinari Dec 11 '18 at 21:09

1 Answers1

0

From this link, you can setup an auto-encoder as

inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(latent_dim)(inputs)

decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(input_dim, return_sequences=True)(decoded)

sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)

But you should decide on time steps per sample. For example, if you decide to have 10 steps per each sample, then you can "chop" your whole data with 480 observations into 48 samples each with 10 time steps. Now the input shape would be (48, 10, 7).

SaTa
  • 2,422
  • 2
  • 14
  • 26
  • What is `latent_dim` here ? – TheTechGuy Dec 12 '18 at 03:59
  • Take a look at the blog. It will be the dimension of the space you want to encode your signal into. For example, let's say timesteps = 20, input_dim = 10, and latent_dim = 5. This means that I want my signal of 20 time steps and 10 features at each time step to be encoded into a vector of size 5. – SaTa Dec 12 '18 at 04:06