Guido
Guido

Reputation: 483

Reshaping a batch for LSTM in Keras

By using an LSTM AutoEncoder I am facing some problems in the shaping of the dataset accordingly for the LSTM needs. Since I am training on batches, I generate a loop of windows of my time series - the code looks like this:

X_batch = np.array(file.loc[window * WINDOWS_SIZE:(window + 1) * WINDOWS_SIZE - 1], dtype="f")
print(X_batch.shape)
X_batch = np.reshape(1, WINDOWS_SIZE, cluster_feature_size)
print(X_batch.shape)
history = model.fit(X_batch, X_batch, epochs=1, verbose=False)

My batches are shaped of 48 data points(WINDOWS_SIZE) and 45 metrics (cluster_feature_size variable).

I have read that I need to reshape my data in the following format (samples, timesteps, features) but I am failing somewhere and lacking of some information.

My assumption is that 1 sample is 1 batch and in a batch I have 48 data points and therefore I set 48 timesteps.

A draft of the model architecture I built so far is the following:

model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(WINDOWS_SIZE, cluster_feature_size)))
model.add(RepeatVector(WINDOWS_SIZE))
model.add(Dense(1))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))

I followed an online tutorial and I am still working on it.

The error I get is this one, while I am reshaping:

(48, 45)
---> 17 X_batch = np.reshape(1, WINDOWS_SIZE, cluster_feature_size)
ValueError: cannot reshape array of size 1 into shape (48,)

Upvotes: 1

Views: 705

Answers (1)

Frayal
Frayal

Reputation: 2161

No need of a reshape here:

import numpy as np
X = np.random.rand(48, 45)
X = np.array([X])
print(X.shape)

gives me:

>>> (1, 48, 45)

As i don't know more the context i can't help you more, but that should solve the reshape issue.

Upvotes: 1

Related Questions