Search code examples
pythonneural-networkpredictionpybrainsupervised-learning

Prediction data in PyBrain


I make a prediction on the basis of the available data. But they are wrong. And i don`t know why. I have code creating and training neural networks.

ds = SupervisedDataSet(3, 1)

ds.addSample( (76.7, 13.8, 103.0), (770,))
ds.addSample( (70.9, 13.0, 92.0), (650,))
ds.addSample( (65.6, 15.9, 104.3), (713,))
ds.addSample( (59.3, 14.8, 88.0), (593,))
ds.addSample( (50.0, 13.0, 65.2), (443,))
ds.addSample( (44.9, 17.6, 79.0), (547,))
ds.addSample( (44.3, 18.4, 78.6), (553,))
ds.addSample( (44.4, 18.4, 81.8), (576,))

net = buildNetwork(ds.indim, 5, ds.outdim, bias=True)

trainer = BackpropTrainer(net, dataset=ds, verbose=True,learningrate=0.05)
trainer.setData(ds)
trainer.trainEpochs(100)

But when i write

net.activate((76.7, 13.8, 103.0))

I got wrong result array([ 570.34849909]). And when I change the input values, the result does not change. For example, net.activate((76.7, 13.8, 90.0)) - array([ 570.34849909]).

I don`t understand how to fix it. I tried different ways of learning, different number of neurons in the hidden layer, and a different number of epoch.


Solution

  • I found a solution to the problem. The data must be normalized before training. This greatly increases the effectiveness of training. Finally code:

    ds = SupervisedDataSet(3, 1)
    #not normalize data
    ds.addSample( (76.7, 13.8, 103.0), 770)
    ds.addSample( (70.9, 13.0, 92.0), 650)
    ds.addSample( (65.6, 15.9, 104.3), 713)
    ds.addSample( (59.3, 14.8, 88.0), 593)
    ds.addSample( (50.0, 13.0, 65.2), 443)
    ds.addSample( (44.9, 17.6, 79.0), 547)
    ds.addSample( (44.3, 18.4, 78.6), 553)
    ds.addSample( (44.4, 18.4, 81.8), 576)
    
    #code for normalize data in ds
    i = np.array([d[0] for d in ds])
    i /= np.max(np.abs(i),axis=0)
    o = np.array([d[1] for d in ds])
    o /= np.max(np.abs(o),axis=0)
    
    #creating new object for normalized data
    nds = SupervisedDataSet(3, 1)
    for ix in range(len(ds)):
        nds.addSample( i[ix], o[ix])
    
    #creating net
    net = buildNetwork(nds.indim, 3, nds.outdim, bias = True, hiddenclass=TanhLayer)
    
    #training net
    trainer = RPropMinusTrainer(net, verbose=True)
    trainer.trainOnDataset(nds,100)
    trainer.testOnData(verbose=True)