# Training Network Using Optimization Algorithms

We have seen how to train a network using trainers in pybrain. In this chapter, will use optimization algorithms available with Pybrain to train a network.

In the example, we will use the GA optimization algorithm which needs to be imported as shown below −

from pybrain.optimization.populationbased.ga import GA


## Example

Below is a working example of a training network using a GA optimization algorithm −

from pybrain.datasets.classification import ClassificationDataSet
from pybrain.optimization.populationbased.ga import GA
from pybrain.tools.shortcuts import buildNetwork

# create XOR dataset
ds = ClassificationDataSet(2)
ds.setField('class', [ [0.],[1.],[1.],[0.]])

net = buildNetwork(2, 3, 1)
ga = GA(ds.evaluateModuleMSE, net, minimize=True)

for i in range(100):
net = ga.learn(0)[0]

print(net.activate([0,0]))
print(net.activate([1,0]))
print(net.activate([0,1]))
print(net.activate([1,1]))


## Output

The activate method on the network for the inputs almost matches with the output as shown below −

C:\pybrain\pybrain\src>python example15.py
[0.03055398]
[0.92094839]
[1.12246157]
[0.02071285]