# Why is predicted rainfall by LSTM coming negative for some data points?

I have used supervised learning with LSTM network using tanh activation function and 0.1 dropout for time series prediction.my loss='mean_squared_error', optimizer='adam'. The predicted time series is shown below where x axis shows future months and y axis shows rainfall in mm.Orange line is predicted and blue line is actual. Some points are being predicted below 0 (negative) even though training dataset has all points having values >=0. My training dataset is of 64 years (all positive data) and I am predicting for 12 years (144 data points shown)

Model code using ReLU activation:

model = Sequential()
# LSTM model
model.add(LSTM(128, batch_input_shape=(batch_size, look_back, 1),activation='relu', stateful=True, return_sequences=False))

• Because your model is not aware of the non-negative constraint. You could try using a non-negative transformation like $\exp$ or $\max(x,0)$. Welcome to the site. – Emre Aug 13 '18 at 19:57
• Emre, that is an answer not a comment. – kbrose Aug 13 '18 at 22:50
• So if I use relu activation function, will this problem get solved? – Roy Aug 14 '18 at 7:11

Currently your model is not restricted from guessing any number. In this case, you may want to use something like a ReLU activation function to restrict your output domain.

The ReLU activation needs to be added to the last layer of your model. If you add it to an intermediate layer but not the last layer then your model can still output negative numbers, e.g. in the case of the last layer having a negative weight.

Note this is not really a new idea. There are strong similarities between logistic regression and what we’re doing here. In both cases we want to limit the possible guesses, in your case to non-negative numbers and in logistic regression‘s case to [0, 1].

• Thanks. ReLU should work then, I will try and let you know. – Roy Aug 14 '18 at 18:44
• I tried using ReLU but in that case while training, after some epochs, the loss is all of a sudden going to NA. I am not sure why this is happening? – Roy Aug 28 '18 at 6:32
• Are you doing division anywhere in your model? – kbrose Aug 28 '18 at 14:02
• If you share your architecture it will be easier to help. – kbrose Aug 28 '18 at 14:03
• model = Sequential() #LSTM model model.add(LSTM(128, batch_input_shape=(batch_size, look_back, 1),activation='relu', stateful=True, return_sequences=False)) model.add(Dropout(0.1)) #for better regularization model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') – Roy Aug 29 '18 at 3:40