How to optimize networking using optimization algorithms in PyBrain
Last Updated :
17 Oct, 2022
In this article, we are going to see how to optimize networking using optimization algorithms in PyBrain using Python.
In the field of machine learning, Optimization algorithms are specifically used to reduce certain functions known as loss function/error function. By loss function, the optimization algorithm may result in reducing the difference between the actual and predicted output. Eventually, building the model more accurate for the task. This article focuses on optimizing networking using optimization algorithms in PyBrain. PyBrain provides the support of GA optimization algorithm in order to optimize a network.
GA optimization algorithm
Step 1: Construct a classification dataset.
Let us firstly create a classification dataset. In this example, we have taken OR dataset.
Python3
from pybrain.datasets.classification import ClassificationDataSet
orDataset = ClassificationDataSet( 2 )
orDataset.addSample([ 0. , 0. ], [ 0. ])
orDataset.addSample([ 0. , 1. ], [ 1. ])
orDataset.addSample([ 1. , 0. ], [ 1. ])
orDataset.addSample([ 1. , 1. ], [ 1. ])
orDataset.setField( 'class' , [[ 0. ],[ 1. ],[ 1. ],[ 1. ]])
|
Step 2: Creating a network.
To create a network, PyBrain provides us with pybrain.tools.shortcuts. We can import buildNetwork shortcuts from it.
Python3
from pybrain.tools.shortcuts import buildNetwork
myNetwork = buildNetwork( 2 , 4 , 1 )
|
Step 3: Applying GA optimization algorithm.
The GA has the following syntax:
GA(dataset, network, minimize = True / False)
Here,
- dataset: A dataset
- network: The created network
- minimize = “True”: For reducing error function
Python3
from pybrain.optimization.populationbased.ga import GA
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize = True )
|
Step 4: Applying learn operation.
After that, we need to iterate using a loop and optimize the created gaOptimization using learn(0) operation.
Python3
for i in range ( 100 ):
myNetwork = gaOptimization.learn( 0 )[ 0 ]
print (myNetwork.activate([ 0 , 0 ]))
print (myNetwork.activate([ 1 , 0 ]))
print (myNetwork.activate([ 0 , 1 ]))
print (myNetwork.activate([ 1 , 1 ]))
|
Below is the complete implementation:
Python3
from pybrain.datasets.classification import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.optimization.populationbased.ga import GA
orDataset = ClassificationDataSet( 2 )
orDataset.addSample([ 0. , 0. ], [ 0. ])
orDataset.addSample([ 0. , 1. ], [ 1. ])
orDataset.addSample([ 1. , 0. ], [ 1. ])
orDataset.addSample([ 1. , 1. ], [ 1. ])
orDataset.setField( 'class' , [[ 0. ], [ 1. ], [ 1. ], [ 1. ]])
myNetwork = buildNetwork( 2 , 4 , 1 )
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize = True )
for i in range ( 100 ):
myNetwork = gaOptimization.learn( 0 )[ 0 ]
print (myNetwork.activate([ 0 , 0 ]))
print (myNetwork.activate([ 1 , 0 ]))
print (myNetwork.activate([ 0 , 1 ]))
print (myNetwork.activate([ 1 , 1 ]))
|
Output:
Share your thoughts in the comments
Please Login to comment...