如何使用pybrain黑盒优化将神经网络训练为监督数据集? [英] How to train a neural network to supervised data set using pybrain black-box optimization?

查看:115
本文介绍了如何使用pybrain黑盒优化将神经网络训练为监督数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pybrain上玩了一些,并了解了如何使用自定义架构生成神经网络,以及如何使用反向传播算法将其训练为受监管的数据集.

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.

例如: 如何使用pybrain遗传算法(2)实现诸如(1)的神经网络来对XOR数据集进行分类?

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

推荐答案

我终于解决了!一旦知道如何就总是很容易!

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

本质上,GA的第一个参数是适应度函数(在文档中称为evaluator),该函数必须将第二个参数(在文档中称为evaluable的个人)作为唯一参数.

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.

在此示例中,将训练为XOR

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

这篇关于如何使用pybrain黑盒优化将神经网络训练为监督数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆