Pybrain 简明教程

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

我们将使用 Pybrain 工具中的 NetworkWriter 和 NetworkReader,即:pybrain.tools.customxml。

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')

网络保存在 network.xml 中。

The network is saved inside network.xml.

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

要读取所需的 xml,我们可以使用以下代码:

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

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

下面是创建的 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.linearlayer.LinearLayer" inmodule="True" name="in">
            <name val="'in'"/>
            <dim val="2"/>
         </LinearLayer>

         <LinearLayer class="pybrain.structure.modules.linearlayer.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

以下是本教程中使用的 API 列表。

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

For Networks

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

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

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

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

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

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

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

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

For Supervised Datasets

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

  2. 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) - 用于在数据集上训练模块直到它收敛。如果没有给出数据集,它将尝试训练在开始时使用的已训练数据集。

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.