mohsin ammar
mohsin ammar

Reputation: 113

Stuck with input/output shapes in keras, lstm

I'm trying train a time series of energy demand using lstm. I had used timestamp and got the results but for this experiment I'm trying to split date time in days, month, year, hours. So after splitting data my csv file looks like this

timestamp     | day | month | year | hour | demand
01-07-15 1:00 |  1  |   7   | 2015 |   1  | 431607

I'm using keras for LSTM(I'm very new to it). I have following code written so far.

from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

dataframe = read_csv('patched_data_sorted.csv', engine='python')
dataset = dataframe.values
trainX = dataset[:, 1:5]
sampleSize = len(trainX)
trainX = trainX.reshape(1, len(trainX), 4)
trainY = dataset[:, 5]
trainY = trainY.reshape(1, len(trainY))
print(trainY)

# print(trainX)

model = Sequential()
model.add(LSTM(4, input_shape=(sampleSize, 4)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)
trainPredict = model.predict(trainX)
print(trainPredict)

But I'm getting this error

ValueError: Error when checking target: expected dense_1 to have shape (None, 1) but got array with shape (1, 20544)

I'm not sure why is this happening but I think I'm not reshaping correctly.

Upvotes: 0

Views: 528

Answers (1)

sietschie
sietschie

Reputation: 7543

In your data you have a label for every timestep of the sequence. Your current network is set up to have only one label for the whole sequence.

To get an output for every timestep you need to add return_sequences=True to your LSTM and wrap the Dense layer in a TimeDistributed layer so that it gets applied in every timestep.

model = Sequential()
model.add(LSTM(4, input_shape=(sampleSize, 4), return_sequences=True))
model.add(TimeDistributed(Dense(1)))

Upvotes: 1

Related Questions