简体   繁体   English

用于股票预测的PyBrain神经网络将不会学习

[英]PyBrain neural network for stock prediction won't learn

I am trying to code a neural network that can forecast some data. 我正在尝试编写一个可以预测某些数据的神经网络。 Therefore I use PyBrain for python. 因此我使用pyBrain for python。 I figured out that a SupervisedDataset would be a good fit for this task. 我发现SupervisedDataset非常适合这项任务。 I took some stock data and put 5 values from it as input and the sixths as the target. 我拿了一些股票数据并将其中的5个值作为输入,将第六个作为目标。 Then I build a feed forward network with the buildNetwork function and trained it with the BackpropTrainer . 然后,我建立一个与前馈网络buildNetwork功能,并与训练有素的它BackpropTrainer

Anyway, the error won't get less. 无论如何,错误不会减少。 It is stuck at ~0.6 and it seems to oscillate around there. 它被卡在~0.6左右,它似乎在那里振荡。 I tried to tweak the momentum and the learning rate but it didn't help. 我试图调整动量和学习率,但它没有帮助。 What am I doing wrong? 我究竟做错了什么?

from pybrain.datasets import SupervisedDataSet
DS = SupervisedDataSet(5, 1)

DS.addSample((44.055, 44.54, 44.04, 43.975, 43.49), (42.04,))
DS.addSample((44.54, 44.04, 43.975, 43.49, 42.04), (42.6,))
DS.addSample((44.04, 43.975, 43.49, 42.04, 42.6), (42.46,))
DS.addSample((43.975, 43.49, 42.04, 42.6, 42.46), (41.405,))
DS.addSample((43.49, 42.04, 42.6, 42.46, 41.405), (42.385,))
DS.addSample((42.04, 42.6, 42.46, 41.405, 42.385), (42.655,))
DS.addSample((42.6, 42.46, 41.405, 42.385, 42.655), (41.53,))
DS.addSample((42.46, 41.405, 42.385, 42.655, 41.53), (40.09,))
DS.addSample((41.405, 42.385, 42.655, 41.53, 40.09), (39.8,))
DS.addSample((42.385, 42.655, 41.53, 40.09, 39.8), (40.2,))
DS.addSample((42.655, 41.53, 40.09, 39.8, 40.2), (39.915,))
DS.addSample((41.53, 40.09, 39.8, 40.2, 39.915), (40.21,))
DS.addSample((40.09, 39.8, 40.2, 39.915, 40.21), (40.34,))
DS.addSample((39.8, 40.2, 39.915, 40.21, 40.34), (41.195,))
DS.addSample((40.2, 39.915, 40.21, 40.34, 41.195), (41.595,))
DS.addSample((39.915, 40.21, 40.34, 41.195, 41.595), (41.975,))
DS.addSample((40.21, 40.34, 41.195, 41.595, 41.975), (42.045,))
DS.addSample((40.34, 41.195, 41.595, 41.975, 42.045), (40.13,))
DS.addSample((41.195, 41.595, 41.975, 42.045, 40.13), (38.99,))
DS.addSample((41.595, 41.975, 42.045, 40.13, 38.99), (39.81,))
DS.addSample((41.975, 42.045, 40.13, 38.99, 39.81), (40.23,))
DS.addSample((42.045, 40.13, 38.99, 39.81, 40.23), (40.47,))
DS.addSample((40.13, 38.99, 39.81, 40.23, 40.47), (40.45,))
DS.addSample((38.99, 39.81, 40.23, 40.47, 40.45), (40.01,))
DS.addSample((39.81, 40.23, 40.47, 40.45, 40.01), (40.23,))
DS.addSample((40.23, 40.47, 40.45, 40.01, 40.23), (40.2,))
DS.addSample((40.47, 40.45, 40.01, 40.23, 40.2), (41.605,))
DS.addSample((40.45, 40.01, 40.23, 40.2, 41.605), (42.1,))
DS.addSample((40.01, 40.23, 40.2, 41.605, 42.1), (42.135,))
DS.addSample((40.23, 40.2, 41.605, 42.1, 42.135), (41.95,))
DS.addSample((40.2, 41.605, 42.1, 42.135, 41.95), (41.145,))
DS.addSample((41.605, 42.1, 42.135, 41.95, 41.145), (40.635,))
DS.addSample((42.1, 42.135, 41.95, 41.145, 40.635), (41.25,))
DS.addSample((42.135, 41.95, 41.145, 40.635, 41.25), (41.19,))
DS.addSample((41.95, 41.145, 40.635, 41.25, 41.19), (42.065,))
DS.addSample((41.145, 40.635, 41.25, 41.19, 42.065), (42.025,))
DS.addSample((40.635, 41.25, 41.19, 42.065, 42.025), (42.09,))
DS.addSample((41.25, 41.19, 42.065, 42.025, 42.09), (41.79,))
DS.addSample((41.19, 42.065, 42.025, 42.09, 41.79), (43.11,))


from pybrain.tools.shortcuts import buildNetwork
FNN = buildNetwork(DS.indim, 15, DS.outdim, bias=True)

from pybrain.supervised.trainers import BackpropTrainer
TRAINER = BackpropTrainer(FNN, dataset=DS, learningrate = 0.005, \
    momentum=0.1, verbose=True)

for i in range(1000):
    TRAINER.train()

Edit: Some of the comments doubted that those data would fit for a neural network in general. 编辑:一些评论怀疑这些数据一般适合神经网络。 Therefore I did the same net in MATLAB and it worked just fine. 因此,我在MATLAB中做了同样的网,它工作得很好。 After 11 training epochs the error was less then 0.002. 在11个训练时期之后,误差小于0.002。

MATLAB中相同的网络和数据

Furthermore I tried to use the SupervisedDataset from PyBrain but this wouldn't work as well. 此外,我尝试使用PyBrain的SupervisedDataset ,但这不会有效。 I am out of ideas now. 我现在没有想法了。

I found a solution. 我找到了解决方案。 Turned out the stock data had to be normalized first. 原来,股票数据必须先规范化。 So i wrote this function: 所以我写了这个函数:

def normalization(data, new_max, new_min):
    old_max = 0
    old_min = 0

    # Finde altes Max- und Minimum
    for i in range(len(data)):
        if old_max < data[i]:
            old_max = data[i]
        elif old_min > data[i]:
            old_min = data[i]

    old_range = (old_max - old_min)

    for i in range(len(data)):
        if old_range == 0:
            data[i] = new_min
        else:
            new_range = (new_max - new_min)
            data[i] = (((data[i] - old_min) * new_range) / old_range) + new_min

I scaled the data between 0 and 1 and voilà - the network would finally learn. 我将数据在0和1之间进行了缩放,并且vo - 网络最终会学习。

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

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