1. 程式人生 > >python 簡單神經網路

python 簡單神經網路

#!/usr/bin/python
#coding: utf-8
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import *

net = buildNetwork(2, 3, 1 , bias=True, hiddenclass=TanhLayer)

ds = SupervisedDataSet(2, 1)
ds.addSample((0, 0), (0,))
ds.addSample((0, 1), (1,))
ds.addSample((1, 0), (1,))
ds.addSample((1, 1), (0,))


for inpt, target in ds:
    print inpt,target


trainer=BackpropTrainer(net, ds)
d=1
while d>1e-8:
    d = trainer.train()


print("結果:")
print net.activate([0,0])
print net.activate([0,1])
print net.activate([1,0])
print net.activate([1,1])
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
[ 0.  0.] [ 0.]
[ 0.  1.] [ 1.]
[ 1.  0.] [ 1.]
[ 1.  1.] [ 0.]
[ 0.00010477]
[ 0.99989226]
[ 0.99986574]
[ 0.00018769]
>>>