Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to train a neural network to supervised data set using pybrain black-box optimization?

I have played around a bit with pybrain and understand how to generate neural networks with custom architectures and train them to supervised data sets using backpropagation algorithm.

However I am confused by the optimization algorithms and the concepts of tasks, learning agents and environments.

For example: How would I implement a neural network such as (1) to classify the XOR dataset using pybrain genetic algorithm (2)?

(1) pybrain.tools.shortcuts.buildNetwork(2, 3, 1)

(2) pybrain.optimization.GA()

like image 825
david_adler Avatar asked Apr 01 '13 20:04

david_adler


People also ask

Which optimization technique is the most commonly used for neural network training?

Gradient Descent is the most basic but most used optimization algorithm. It's used heavily in linear regression and classification algorithms. Backpropagation in neural networks also uses a gradient descent algorithm.

What are optimization techniques in neural network?

Optimizers are algorithms or methods used to change the attributes of the neural network such as weights and learning rate to reduce the losses. Optimizers are used to solve optimization problems by minimizing the function.

How neural networks are useful for doing supervised learning?

Neural networks reflect the behavior of the human brain, allowing computer programs to recognize patterns and solve common problems in the fields of AI, machine learning, and deep learning.

What is PyBrain in Python?

PyBrain is a modular Machine Learning Library for Python. Its goal is to offer flexible, easy-to-use yet still powerful algorithms for Machine Learning Tasks and a variety of predefined environments to test and compare your algorithms.


1 Answers

I finally worked it out!! Its always easy once you know how!

Essentially the first arg to the GA is the fitness function (called evaluator in docs) which must take the second argument (an individual, called evaluable in docs) as its only arg.

In this example will train to XOR

from pybrain.datasets.classification import ClassificationDataSet
# below line can be replaced with the algorithm of choice e.g.
# from pybrain.optimization.hillclimber import HillClimber
from pybrain.optimization.populationbased.ga import GA
from pybrain.tools.shortcuts import buildNetwork

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

nn = buildNetwork(2, 3, 1)
# d.evaluateModuleMSE takes nn as its first and only argument
ga = GA(d.evaluateModuleMSE, nn, minimize=True)
for i in range(100):
    nn = ga.learn(0)[0]

Test results after the above script:

In [68]: nn.activate([0,0])
Out[68]: array([-0.07944574])

In [69]: nn.activate([1,0])
Out[69]: array([ 0.97635635])

In [70]: nn.activate([0,1])
Out[70]: array([ 1.0216745])

In [71]: nn.activate([1,1])
Out[71]: array([ 0.03604205])
like image 61
david_adler Avatar answered Oct 05 '22 13:10

david_adler