0

I have created a simple pybrain neural network, what I want to do is save the training and learning data so that the neural network can continues learn.

Here is my code. I can't figure out how to solve this problem.

from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer    
from pybrain.datasets            import ClassificationDataSet
from pybrain.utilities           import percentError
from pybrain.tools.shortcuts     import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules   import SoftmaxLayer    
from pylab import ion, ioff, figure, draw, contourf, clf, show, hold, plot
from scipy import diag, arange, meshgrid, where
from numpy.random import multivariate_normal
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.xml.networkwriter import NetworkWriter
from pybrain.tools.xml.networkreader import NetworkReader
import csv

n = NetworkReader.readFrom('weatherlearned.csv') 

ds = SupervisedDataSet(6,1)
tf = open('weather.csv','r')


for line in tf.readlines():
    try:
        data = [float(x) for x in line.strip().split(',') if x != '']
        indata =  tuple(data[:6])
        outdata = tuple(data[6:])
        ds.addSample(indata,outdata)
    except ValueError,e:
            print "error",e,"on line"


n = buildNetwork(ds.indim,8,8,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.005,momentum=0.05,verbose=True)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)
tf.close()
NetworkWriter.writeToFile(n, 'weatherlearned.csv')

I also Tried Pickel

from pybrain.datasets import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer

from pybrain.datasets            import ClassificationDataSet
from pybrain.utilities           import percentError
from pybrain.tools.shortcuts     import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules   import SoftmaxLayer

from pylab import ion, ioff, figure, draw, contourf, clf, show, hold, plot
from scipy import diag, arange, meshgrid, where
from numpy.random import multivariate_normal
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.xml.networkwriter import NetworkWriter
from pybrain.tools.xml.networkreader import NetworkReader
import csv
from pybrain.tools.shortcuts import buildNetwork
import pickle


ds = SupervisedDataSet(6,1)
fileObject = open('weatherlearned.csv','r')
tf = open('weather.csv','r')

fileObject = open('weatherlearned.csv', 'w') 



for line in tf.readlines():
    try:
        data = [float(x) for x in line.strip().split(',') if x != '']
        indata =  tuple(data[:6])
        outdata = tuple(data[6:])
        ds.addSample(indata,outdata)
    except ValueError,e:
            print "error",e,"on line"


n = buildNetwork(ds.indim,8,8,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.05,momentum=0.05,verbose=True)
pickle.dump(n, fileObject)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)
tf.close()
fileObject = open('weatherlearned.csv','r')
n = pickle.load(fileObject)
halfer
  • 19,824
  • 17
  • 99
  • 186
ben olsen
  • 663
  • 1
  • 14
  • 27
  • Have you looked at the answer here: http://stackoverflow.com/questions/4334941/how-to-serialize-deserialized-pybrain-networks ? – rossdavidh Sep 26 '14 at 14:45
  • I tried that too did not work – ben olsen Sep 27 '14 at 21:05
  • Do you mean you want to save the trained network so that you don't have to run the training script each time you want to use the trained network? In that case, this might help: http://stackoverflow.com/questions/6006187/how-to-save-and-recover-pybrain-traning – dc95 Jul 14 '15 at 03:55
  • Hello Chauhan - I tried that. I would run it for 1000 times and bring the error down to 0.001 . However, when I run the same file with the trained network again. It starts back at error 0.9998. Why does the network not go lower to level such as 0.000001 . – ben olsen Jul 15 '15 at 22:16

1 Answers1

0

Looking at the suggested links in the comments, they say the same thing for loading a net from file as my answer here, but if you have left in the buildNetwork function after loading the network from file then that may be the problem i.e. maybe you need something like this in your code:

if os.path.exists('weatherlearned.csv'):       
    n = NetworkReader.readFrom('weatherlearned.csv')
else:
    n = buildNetwork(ds.indim,8,8,ds.outdim,recurrent=True)
t = BackpropTrainer(n,learningrate=0.005,momentum=0.05,verbose=True)
t.trainOnDataset(ds,1000)
t.testOnData(verbose=True)
tf.close()
NetworkWriter.writeToFile(n, 'weatherlearned.csv')

In the pybrain docs it says for the buildNetwork...

The net is already initialized with random values

So if that function is called after the previously trained net has been loaded then it just re-initializes the net to random weights. Loading in a network from file excludes the need to use the buildNetwork function.

John Harrison
  • 41
  • 1
  • 4