Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Sequences, Recurrent Neural Networks, PyBrain

I am trying to predict sales demand using recurrent neural networks. Here

https://stackoverflow.com/a/2525149/423805

it was mentioned sequences are supported in PyBrain with example code. Even though are data is not exactly categories, I modeled them as such for this example. Data is here

6 6 6 6 2 6 2 6 2 2 6 2 6 6 2 6 2 4 4 4 5 6 6 1 2 2 6 6 6 2 6 2 6 6 2 6 2 2 6 2 1 2 2 6 6 6 2 1 2 6 2 6 6 2 2 6 2 2 2 6 2 6 2 2 2 2 2 6 2 2 6 6 6 6 1 2 2 6 2 2 2 2 6 2 2 2 2 3 3 2 3 2 6 6 6 6 2 6 2 6 6 2 6 2 6 6 2 6 6 2 2 3 4 3 3 1 3 1 2 1 6 1 6 6 1 6 6 2 6 2 6 2 2 2 6 6 1 6 2 6 1 2 1 6 2 6 2 2 2 2 6 6 1 6 6 2 2 6 2 2 2 3 4 4 4 6 4 6 1 6 6 1 6 6 6 6 1 6 2 2 2 6 6 6 6 2 6 6 2 2 6 2 6 2 2 2 6 2 2 2 6 6 6 6 3 2 2 6 2 2 2 2 2 2 6 2 6 2 2 2 6 2 2 6 6 2 6 6 6 2 2 2 3 3 3 4 1 6 6 1 6 6 1 6 1 6 6 6 6 1 6 6 6 2 1 2 2 2 2 2 2 3 6 6 6 6 6 2 6
1 6 6 1 6 1 1 1 1 1 1 6 6 6 1 2 1 6 6 1 1 1 6 6 2 1 6 6 1 1 1 6 1 2 1 6 2 2 2 2 2 6 1 6 6 1 2 1 6 6 6 1 1 1 6 6 1 1 1 1 6 1 1 2 1 6 1 6 1 1 6 2 6 2 6 6 6 3 6 6 1 6 6 2 2 2 3 2 2 6 6 6 1 1 6 2 6 6 2 6 2 6 6 1 3 6 6 1 1 1 2 2 3 2 2 6 2 2 2 1 6 1 6 1 1 6 2 1 1 1 2 2 1 6 1 1 1 1 2 6 1 1 1 1 6 1 6 1 2 1 6 1 6 6 1 6 1 2 2 2 2 3 3 2 2 2 6 6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 2 2 2 3 3 3 5 5 4 5 3 3 3 6 2 6 6 2 2 6 2 2 2 2 6 2 3 2 2 3 6 3 2 2 3 4 4 4 4 5 5 4 4 6 6 2 6 2 6 2 2 2 2 2 2 2 5 5 4 4 5 5 2 6 2 6 6 2 6 2 6 2 2 3 3 4 4 5 4 4 4 3 4 3 6 2 6 2 2 2 2 2 2 2 2 2 2 2 3 4 4 4 4 5 4 4 4 3 2 2 2 6 2 2 2 6 2 6 2 6 2 2 2 2 2 3 2
6 2 2 2 2 3 3 4 4 4 5 4 3 3 6 2 6 6 2 3 4 4 3 4 4 3 3 2 2 6 3 4 4 4 4 3 4 2 3 2 2 6 3 3 6 6 3 4 5 4 5 3 3 2 6 6 2 6 2 6 6 2 2 6 4 4 4 4 4 4 5 4 4 6 2 6 6 2 2 6 2 6 6 2 6 6 2 6 6 2 6 2 6 3 4 4 4 4 4 4 4 6 2 6 6 2 6 2 6 6 6 6 2 6 2 2 6 4 4 4 4 4 4 6 3 3 6 2 2 2 6 2 6 2 2 2 2 2 2 2 2 2 2 2 2 3 6 4 5 5 5 5 2 4 6 6 2 6 6 2 2 6 2 2 2 2 6 2 3 2 2 3 6 3 2 2 3 4 4 4 4 5 5 4 3 3 6 2 6 2 2 2 6 3 2 2 2 2 5 5 4 4 4 4 3 6 2 6 6 2 6 2 6 2 2 3 3 4 4 5 4 4 4 4 4 3 6 2 6 2 2 2 6 2 2 2 2 2 2 2 3 4 4 4 4 5 4 4 4 3 2 2 2 6 6 6 2 6 2 6 2 6 2 2 2 2 2 2 2

Each row is a seperate product, and columns are demand for those products in time. I used this code

from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets import SequentialDataSet
from pybrain.structure import SigmoidLayer
from pybrain.structure import LSTMLayer

import itertools
import numpy as np

data = np.loadtxt("sales").T
print data

datain = data[:-1,:] 
dataout = data[1:,:] 

INPUTS = 5
OUTPUTS = 5
HIDDEN = 40

net = buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True) 

ds = SequentialDataSet(INPUTS, OUTPUTS)

for x,y in itertools.izip(datain,dataout):
    ds.newSequence()
    ds.appendLinked(tuple(x), tuple(y))

net.randomize()

trainer = BackpropTrainer(net, ds)

for _ in range(1000):
    print trainer.train()

The error hovers around 245.xx, there is improvement with numbers after the decimal poiint, but the integer part of the errordoes not go any lower. Does it look like the method is working? I just wanted to check with a PyBrain / NN expert to see I am not doing anything wrong.

Correction: Apparently while copying from a PDF file, the data got corrupted. The correct data is shared above. I repeat, the data was bad. With correct data, NN code (also shared) will start from error rate 5.9807501187, and gradually go down. I am very sorry for the confusion I might have caused.

like image 936
BBSysDyn Avatar asked Oct 02 '12 11:10

BBSysDyn


1 Answers

Try plotting train error on each iteration. If method works then it should go down on each step. Also have you tried adding bias?

buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True, bias=True)

Where do you get the error from? Is it the one reported by the trainer? Then it is an error on training set and you suffer from high bias. Things that could help:

  1. Running more iterations.
  2. Adding more internal layers or nodes.
  3. Setting regularization to lower value, but I cannot find a way to specify regularization in pybrain.
like image 67
Ivan Koblik Avatar answered Sep 30 '22 21:09

Ivan Koblik