简体   繁体   English

Python:在Keras上发布培训和预测回归问题

[英]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: 我正在使用Keras处理一个简单的时间序列回归问题,我想用最后20个收盘价来预测下一个收盘价,根据我发现的一些例子,我有以下代码:

I write my sequential model in a separated function, as needed by "build_fn" parameter: 我根据“build_fn”参数的需要在一个单独的函数中编写我的顺序模型:

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: 我创建了KerasRegressor对象,传递模型创建者函数和所需的拟合参数:

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

I train the model trough the KerasRegressor Object with 592 samples: 我用592个样本通过KerasRegressor对象训练模型:

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: 问题开始显现,虽然nb_epoch = 100我的模型只训练了10个时代:

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 预测值应该接近0.02-0.04范围但是当我打印它时我得到0.000980315962806344

Q1: How can I set the training epochs to the desired value? Q1:如何将训练时期设置为所需的值?

Q2: How can I generate predictions with my NN? Q2:如何用我的NN生成预测?

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. 第一件事是你最有可能使用Keras 2.0,在那个版本中,参数nb_epochs被重命名为epochs。

The second thing is that you have to normalize your inputs and outputs to the [0, 1] range. 第二件事是你必须将输入和输出标准化为[0,1]范围。 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. 此外,为了匹配标准化输出和网络范围,最好在输出层使用sigmoid激活。

Your network is not converging. 您的网络没有融合。 Try changing the parameters. 尝试更改参数。 The loss should reduce consistently. 损失应该持续减少。 Also initialize the parameters properly. 还要正确初始化参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM