Python LSTM generates all the same output value

Viewed 38

I tried to set up a LSTM model with input matrix 7 columns, ca. 1650 rows Output matrix is 1 column, 1650 rows.

My model code is shown in the following. Problem, the prediction does in every 1650 columns have the same value 26,19...

Can anyone help? Thank you!

trainX = []
trainY = []
testX = []
testY = []

ts = 20
timestep = ts

def create_dataset(datasetX,datasetY, timestep=10):
    dataX, dataY = [],[]
    for i in range(len(datasetX)-timestep-1):
        a = datasetX[i:(i+timestep),0]
        dataX.append(a)
        dataY.append(datasetY[i+timestep,0])
    return np.array(dataX), np.array(dataY)

timestep=ts
trainX, trainY = create_dataset(train_X, train_Y, timestep)
timestep=ts
testX, testY = create_dataset(test_X,test_Y,timestep)
model = tf.keras.Sequential()
model.add(LSTM(units=70,return_sequences=True, input_shape=(trainX.shape[1],trainX.shape[2]) ))
model.add(Dropout(0.2))
model.add(LSTM(units=70, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=70))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.compile(optimizer="adam",loss="mean_squared_error")
model.fit(trainX, trainY, epochs=5, batch_size=32,verbose=1
model.summary()
predict = model.predict(testX)
print(predict)
2 Answers

You are using return_sequences=True in your LSTM layers which means that all outputs from each timestep are fed to the next timestep. This is most likely what you want to do in a sequential model, but you need to remove it from the last LSTM layer, where you want the outputs from the last timestep.

model.add(LSTM(units=70,return_sequences=True, input_shape=(trainX.shape[1],trainX.shape[2]) ))

model.add(Dropout(0.2))

model.add(LSTM(units=70, return_sequences=True))

model.add(Dropout(0.2))

model.add(LSTM(units=70))

model.add(Dropout(0.2))

there is something really crazy, even the prediction of the trainX, so trainY is resulting in the same number in every single field of the output array. This seems to mean that something is really wrong, since otherwise the prediction of trainX should be more or less close to trainY?!

I'm lost...

Related