如何使用 PyBrain 中的优化算法优化网络
在本文中,我们将了解如何使用Python中的优化算法在 PyBrain 中优化网络。
在机器学习领域,优化算法专门用于减少某些称为损失函数/误差函数的函数。通过损失函数,优化算法可以减少实际输出和预测输出之间的差异。最终,为任务构建更准确的模型。本文重点介绍使用 PyBrain 中的优化算法优化网络。 PyBrain 提供 GA 优化算法的支持以优化网络。
遗传算法优化算法
第 1 步:构建分类数据集。
让我们首先创建一个分类数据集。在这个例子中,我们采用了 OR 数据集。
Python3
# Python program to create a classification dataset
# importing library
from pybrain.datasets.classification import ClassificationDataSet
# Creating OR dataset
orDataset = ClassificationDataSet(2)
# Inserting sample to orDataset
orDataset.addSample([0., 0.], [0.])
orDataset.addSample([0., 1.], [1.])
orDataset.addSample([1., 0.], [1.])
orDataset.addSample([1., 1.], [1.])
# Set the target field
orDataset.setField('class', [[0.],[1.],[1.],[1.]])
Python3
# Python program to create a network
from pybrain.tools.shortcuts import buildNetwork
# Building a network
# The network consists of two input layers,
# four hidden layers and one output layer
myNetwork = buildNetwork(2, 4, 1)
Python3
from pybrain.optimization.populationbased.ga import GA
# GA optimization algorithm
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize=True)
Python3
# 100 iterations for learning
for i in range(100):
myNetwork = gaOptimization.learn(0)[0]
# Giving input to activate the network
print(myNetwork.activate([0, 0]))
print(myNetwork.activate([1, 0]))
print(myNetwork.activate([0, 1]))
print(myNetwork.activate([1, 1]))
Python3
# Python program to demonstrate how to
# optimize a network using Optimization
# algorithms in PyBrain
# Importing library
from pybrain.datasets.classification import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.optimization.populationbased.ga import GA
# Creating OR dataset
orDataset = ClassificationDataSet(2)
# Inserting sample to orDataset
orDataset.addSample([0., 0.], [0.])
orDataset.addSample([0., 1.], [1.])
orDataset.addSample([1., 0.], [1.])
orDataset.addSample([1., 1.], [1.])
# Set the target field
orDataset.setField('class', [[0.], [1.], [1.], [1.]])
# Building a network
# The network consists of two input layers,
# four hidden layers and one output layer
myNetwork = buildNetwork(2, 4, 1)
# GA optimization algorithm
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize=True)
# 100 iterations for learning
for i in range(100):
myNetwork = gaOptimization.learn(0)[0]
# By passing input optimize the network
print(myNetwork.activate([0, 0]))
print(myNetwork.activate([1, 0]))
print(myNetwork.activate([0, 1]))
print(myNetwork.activate([1, 1]))
第 2 步:创建网络。
为了创建网络,PyBrain 为我们提供了 pybrain.tools.shortcuts。我们可以从中导入 buildNetwork 快捷方式。
Python3
# Python program to create a network
from pybrain.tools.shortcuts import buildNetwork
# Building a network
# The network consists of two input layers,
# four hidden layers and one output layer
myNetwork = buildNetwork(2, 4, 1)
第三步:应用遗传算法优化算法。
GA 具有以下语法:
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
# GA optimization algorithm
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize=True)
第四步:应用学习操作。
之后,我们需要使用循环进行迭代,并使用 learn(0) 操作优化创建的 gaOptimization。
Python3
# 100 iterations for learning
for i in range(100):
myNetwork = gaOptimization.learn(0)[0]
# Giving input to activate the network
print(myNetwork.activate([0, 0]))
print(myNetwork.activate([1, 0]))
print(myNetwork.activate([0, 1]))
print(myNetwork.activate([1, 1]))
下面是完整的实现:
Python3
# Python program to demonstrate how to
# optimize a network using Optimization
# algorithms in PyBrain
# Importing library
from pybrain.datasets.classification import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.optimization.populationbased.ga import GA
# Creating OR dataset
orDataset = ClassificationDataSet(2)
# Inserting sample to orDataset
orDataset.addSample([0., 0.], [0.])
orDataset.addSample([0., 1.], [1.])
orDataset.addSample([1., 0.], [1.])
orDataset.addSample([1., 1.], [1.])
# Set the target field
orDataset.setField('class', [[0.], [1.], [1.], [1.]])
# Building a network
# The network consists of two input layers,
# four hidden layers and one output layer
myNetwork = buildNetwork(2, 4, 1)
# GA optimization algorithm
gaOptimization = GA(orDataset.evaluateModuleMSE,
myNetwork, minimize=True)
# 100 iterations for learning
for i in range(100):
myNetwork = gaOptimization.learn(0)[0]
# By passing input optimize the network
print(myNetwork.activate([0, 0]))
print(myNetwork.activate([1, 0]))
print(myNetwork.activate([0, 1]))
print(myNetwork.activate([1, 1]))
输出: