Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save and recover PyBrain training?

Tags:

pybrain

Is there a way to save and recover a trained Neural Network in PyBrain, so that I don't have to retrain it each time I run the script?

like image 588
solartic Avatar asked May 15 '11 02:05

solartic


2 Answers

PyBrain's Neural Networks can be saved and loaded using either python's built in pickle/cPickle module, or by using PyBrain's XML NetworkWriter.

# Using pickle  from pybrain.tools.shortcuts import buildNetwork import pickle  net = buildNetwork(2,4,1)  fileObject = open('filename', 'w')  pickle.dump(net, fileObject)  fileObject.close()  fileObject = open('filename','r') net = pickle.load(fileObject) 

Note cPickle is implemented in C, and therefore should be much faster than pickle. Usage should mostly be the same as pickle, so just import and use cPickle instead.

# Using NetworkWriter  from pybrain.tools.shortcuts import buildNetwork from pybrain.tools.customxml.networkwriter import NetworkWriter from pybrain.tools.customxml.networkreader import NetworkReader  net = buildNetwork(2,4,1)  NetworkWriter.writeToFile(net, 'filename.xml') net = NetworkReader.readFrom('filename.xml')  
like image 198
solartic Avatar answered Dec 04 '22 01:12

solartic


The NetworkWriter and NetworkReader work great. I noticed that upon saving and loading via pickle, that the network is no longer changeable via training-functions. Thus, I would recommend using the NetworkWriter-method.

like image 41
Jorg Avatar answered Dec 04 '22 01:12

Jorg