PyBrain使用简介

来源:互联网 发布:ubuntu tftp-hpa配置 编辑:程序博客网 时间:2024/06/07 01:17

PyBrain使用简介

PyBrain介绍

PyBrain(Python-Based Reinforcement Learning, Artificial Intelligence and Neural Network)是Python的一个机器学习模块,它的目标是为机器学习任务提供灵活、易应、强大的机器学习算法。
PyBrain正如其名,包括神经网络、强化学习(及二者结合)、无监督学习、进化算法。因为目前的许多问题需要处理连续态和行为空间,必须使用函数逼近(如神经网络)以应对高维数据。PyBrain以神经网络为核心,所有的训练方法都以神经网络为一个实例。

PyBrain项目主页:
http://www.pybrain.org/
https://github.com/pybrain/pybrain/

PyBrain v0.3 doucumentation:
http://pybrain.org/docs/

PyBrain下载安装

PyBrain下载

PyBrain下载安装参考PyBrain Download。
PyBrain使用git和GitHub管理代码,所以可以通过git获得。具体方式可以参考:

We use git and GitHub to manage our code. If you want to clone the PyBrain repository to get the latest version of PyBrain, you can do so by using this command (you need to have git installed on your system):

git clone git://github.com/pybrain/pybrain.git

同样也可以从Downloads · pybrain/pybrain · GitHub下载到GitHub上的zip压缩包。

PyBrain安装

PyBrain安装过程参考Installation · pybrain/pybrain Wiki · GitHub。

PyBrain需要Scipy模块,numpy模块支持(可能还有nose模块),要首先安装这些相关模块。
安装方法与其他python模块安装方法相同。可以使用pip安装方式,可以参考《Pip 安装_我很好》和《安装pybrain 机器学习包 - steady_pace的专栏》。

系统中安装好相关模块后,使用命令行进入 pybrain的根目录,输入

python setup.py install

即可安装PyBrain。安装后,可以在python环境下

import  pybrain

测试是否安装成功。如果没有错误,则安装成功。

PyBrain使用

PyBrain的使用可以参考PyBrain v0.3 doucumentation。
一个简单的建立神经网络模型的例子是:

from pybrain.tools.shortcuts import buildNetworknet = buildNetwork(2, 3, 1)

其中 buildNetwork意义为

This call returns a network that has two inputs, three hidden and a single output neuron.

这样就建立了一个具有随机初始参数的神经网络,可以对其进行测试:

net.activate([2, 1])

这样就会返回一个预测值,但初始参数是随机的,所以这个预测值是不确定并且无意义的。

如果需要自己建立神经网络,需要自己建立PyBrain接受的数据集Building a DataSet。
并在数据集上进行训练Training your Network on your Dataset

PyBrain神经网络的导入导出

训练好的神经网络模型需要保存下来以后使用。方法参考How to save and recover PyBrain traning? - Stack Overflow.

PyBrain’s Neural Networks can be saved and loaded using either python’s built in pickle/cPickle module, or by using PyBrain’s XML NetworkWriter.
Note cPickle is implemented in C, and therefore should be much faster than pickle. Usage should mostly be the same as pickle, so just import and use cPickle instead.

If you’re using 0.3.1 or newer (which is 2 years old as I write this) the NetworkReader and NetworkWriter objects are in pybrain.tools.customxml (so you need: from pybrain.tools.customxml.networkwriter import NetworkWriter from pybrain.tools.customxml.networkreader import NetworkReader).

对于新版PyBrain,使用例子:

from pybrain.tools.shortcuts import buildNetworkfrom pybrain.tools.customxml import NetworkWriterfrom pybrain.tools.customxml import NetworkReadernet = buildNetwork(2, 4, 1)NetworkWriter.writeToFile(net, 'testNet.xml')net = NetworkReader.readFrom('testNet.xml')

References:

  • PyBrain
  • pybrain/pybrain · GitHub
  • PyBrain v0.3 documentation
  • 安装pybrain 机器学习包 - steady_pace的专栏
  • Pip 安装_我很好
  • How to save and recover PyBrain traning? - Stack Overflow.
2 0
原创粉丝点击