PyBrain - API & Tools

Now we know how to build a network and train it. In this chapter, we will understand how to create and save the network, and use the network whenever required.

Save and Recover Network

We are going to make use of NetworkWriter and NetworkReader from Pybrain tool, i.e.,

Here is a working example of the same −

from import buildNetwork
from import NetworkWriter
from import NetworkReader

net = buildNetwork(2,1,1)
NetworkWriter.writeToFile(net, 'network.xml')
net = NetworkReader.readFrom('network.xml')

The network is saved inside network.xml.

NetworkWriter.writeToFile(net, 'network.xml')

To read the xml when required we can use code as follows −

net = NetworkReader.readFrom('network.xml')

Here is the network.xml file created −

<?xml version="1.0" ?>
   <Network class="pybrain.structure.networks.feedforward.FeedForwardNetwork" name="FeedForwardNetwork-8">
      <name val="'FeedForwardNetwork-8'"/>
         <LinearLayer class="pybrain.structure.modules.linearlayer.LinearLayer" inmodule="True" name="in">
            <name val="'in'"/>
            <dim val="2"/>
         <LinearLayer class="pybrain.structure.modules.linearlayer.LinearLayer" name="out" outmodule="True">
            <name val="'out'"/>
            <dim val="1"/>
         <BiasUnit class="pybrain.structure.modules.biasunit.BiasUnit" name="bias">
            <name val="'bias'"/>
         <SigmoidLayer class="pybrain.structure.modules.sigmoidlayer.SigmoidLayer" name="hidden0">
            <name val="'hidden0'"/>
            <dim val="1"/>
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-6">
            <inmod val="bias"/>
            <outmod val="out"/>
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-7">
            <inmod val="bias"/>
            <outmod val="hidden0"/>
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-4">
            <inmod val="in"/>
            <outmod val="hidden0"/>
            <Parameters>[-0.9429546042034236, -0.09858196752687162]</Parameters>
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-5">
            <inmod val="hidden0"/>
            <outmod val="out"/>


Below is a list of APIs that we have used throughout this tutorial.

For Networks

  • activate(input) − It takes parameter, i.e., the value to be tested. It will return back the result based on the input given.

  • activateOnDataset(dataset) − It will iterate over the dataset given and return the output.

  • addConnection(c) − Adds connection to the network.

  • addInputModule(m) − Adds the module given to the network and mark it as an input module.

  • addModule(m) − Adds the given module to the network.

  • addOutputModule(m) − Adds the module to the network and mark it as an output module.

  • reset() − Resets the modules and the network.

  • sortModules() − It prepares the network for activation by sorting internally. It has to be called before activation.

For Supervised Datasets

  • addSample(inp, target) − Adds a new sample of input and target.

  • splitWithProportion(proportion=0.5) − Divides dataset into two parts, the first part containing the proportion part data and the next set containing the remaining one.

For Trainers

trainUntilConvergence(dataset=None, maxEpochs=None, verbose=None, continueEpochs=10, validationProportion=0.25) − It is used to train the module on the dataset until it converges. If dataset is not given, it will try to train on the trained dataset used at the start.

Kickstart Your Career

Get certified by completing the course

Get Started