33

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?

Schahriar SaffarShargh
  • 1,971
  • 2
  • 16
  • 24
solartic
  • 4,249
  • 3
  • 25
  • 26

3 Answers3

45

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') 
Dorieke
  • 3
  • 3
solartic
  • 4,249
  • 3
  • 25
  • 26
  • 1
    From Review: The xml package was renamed in Sep 2010: https://github.com/pybrain/pybrain/commit/fc8e7a8807963e93d8331f7ffbe2745eaff3ae92 – SiHa Nov 03 '16 at 12:53
11

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.

user1558604
  • 947
  • 6
  • 20
Jorg
  • 111
  • 1
  • 2
2

NetworkWriter is the way to go. Using Pickle you can't retrain network as Jorg tells.

You need something like this:

from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.customxml import NetworkWriter
from pybrain.tools.customxml import NetworkReader

net = buildNetwork(4,6,1)

NetworkWriter.writeToFile(net, 'filename.xml')
net = NetworkReader.readFrom('filename.xml')