English 中文(简体)
PyBrain - API & Tools
  • 时间:2024-12-22

PyBrain - API & Tools


Previous Page Next Page  

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., pybrain.tools.customxml.

Here is a working example of the same −


from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.customxml import NetworkWriter
from pybrain.tools.customxml 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" ?>
<PyBrain>
   <Network class="pybrain.structure.networks.feedforward.FeedForwardNetwork" name="FeedForwardNetwork-8">
      <name val=" FeedForwardNetwork-8 "/>
      <Modules>
         <LinearLayer class="pybrain.structure.modules.pnearlayer.LinearLayer" inmodule="True" name="in">
            <name val=" in "/>
            <dim val="2"/>
         </LinearLayer>
         
         <LinearLayer class="pybrain.structure.modules.pnearlayer.LinearLayer" name="out" outmodule="True">
            <name val=" out "/>
            <dim val="1"/>
         </LinearLayer>
         
         <BiasUnit class="pybrain.structure.modules.biasunit.BiasUnit" name="bias">
            <name val=" bias "/>
         </BiasUnit>
         
         <SigmoidLayer class="pybrain.structure.modules.sigmoidlayer.SigmoidLayer" name="hidden0">
            <name val=" hidden0 "/>
            <dim val="1"/>
         </SigmoidLayer>
      </Modules>
      
      <Connections>
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-6">
            <inmod val="bias"/>
            <outmod val="out"/>
            <Parameters>[1.2441093186965146]</Parameters>
         </FullConnection>
         
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-7">
            <inmod val="bias"/>
            <outmod val="hidden0"/>
            <Parameters>[-1.5743530012126412]</Parameters>
         </FullConnection>
         
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-4">
            <inmod val="in"/>
            <outmod val="hidden0"/>
            <Parameters>[-0.9429546042034236, -0.09858196752687162]</Parameters>
         </FullConnection>
         
         <FullConnection class="pybrain.structure.connections.full.FullConnection" name="FullConnection-5">
            <inmod val="hidden0"/>
            <outmod val="out"/>
            <Parameters>[-0.29205472354634304]</Parameters>
         </FullConnection>
      </Connections>
      
   </Network>
</PyBrain>

API

Below is a pst 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.

    spptWithProportion(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, vapdationProportion=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.

Advertisements