Andrés Rangel
Andrés Rangel

Reputation: 71

Python: Issues training and predicting regression on Keras

I'm working on a simple time series regression problem using Keras, I want to predict the next closing price using the last 20 closing prices, I have the following code according to some examples I found:

I write my sequential model in a separated function, as needed by "build_fn" parameter:

def modelcreator():
   model = Sequential()
   model.add(Dense(500, input_shape = (20, ),activation='relu'))
   model.add(Dropout(0.25))
   model.add(Dense(250,activation='relu'))
   model.add(Dense(1,activation='linear'))

   model.compile(optimizer=optimizers.Adam(),
                 loss=losses.mean_squared_error)

   return model 

I create the KerasRegressor Object passing the model creator function and the desired fit parameters:

estimator = KerasRegressor(build_fn=modelcreator,nb_epoch=100, batch_size=32)

I train the model trough the KerasRegressor Object with 592 samples:

self.estimator.fit(X_train, Y_train)

And the issues start to show up, although nb_epoch=100 my model only trains for 10 epochs:

Epoch 1/10
592/592 [==============================] - 0s - loss: 6.9555e-05     
Epoch 2/10
592/592 [==============================] - 0s - loss: 1.2777e-05     
Epoch 3/10
592/592 [==============================] - 0s - loss: 1.0596e-05     
Epoch 4/10
592/592 [==============================] - 0s - loss: 8.8115e-06     
Epoch 5/10
592/592 [==============================] - 0s - loss: 7.4438e-06     
Epoch 6/10
592/592 [==============================] - 0s - loss: 8.4615e-06     
Epoch 7/10
592/592 [==============================] - 0s - loss: 6.4859e-06     
Epoch 8/10
592/592 [==============================] - 0s - loss: 6.9010e-06     
Epoch 9/10
592/592 [==============================] - 0s - loss: 5.8951e-06     
Epoch 10/10
592/592 [==============================] - 0s - loss: 7.2253e-06  

When I try to get a prediction using a data sample:

prediction = self.estimator.predict(test)

The prediction value should be close to the 0.02-0.04 range but when I print it I get 0.000980315962806344

Q1: How can I set the training epochs to the desired value?

Q2: How can I generate predictions with my NN?

Upvotes: 1

Views: 1518

Answers (2)

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

The first thing is that you are most likely using Keras 2.0, and in that version the parameter nb_epochs was renamed to epochs.

The second thing is that you have to normalize your inputs and outputs to the [0, 1] range. It won't work without normalization. Also to match the normalized output and the network range, it would be best to use a sigmoid activation at the output layer.

Upvotes: 2

Abhishek Vijayan
Abhishek Vijayan

Reputation: 753

Your network is not converging. Try changing the parameters. The loss should reduce consistently. Also initialize the parameters properly.

Upvotes: 1

Related Questions