Created
October 16, 2014 22:59
-
-
Save szolotykh/69c1ffb2ad936d520d3e to your computer and use it in GitHub Desktop.
Neural network test: Binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pybrain.tools.shortcuts import buildNetwork | |
from pybrain.structure import FeedForwardNetwork | |
from pybrain.datasets import SupervisedDataSet | |
from pybrain.supervised.trainers import BackpropTrainer | |
from pybrain.structure import LinearLayer, SigmoidLayer, TanhLayer | |
from pybrain.structure import FullConnection | |
import random | |
def int2bin(a): | |
arr = [int(x) for x in bin(a)[2:]] | |
return [0]*(4-len(arr)) + arr | |
# Network | |
net = FeedForwardNetwork() | |
# Layers | |
inLayer = LinearLayer(1) | |
hiddenLayer = SigmoidLayer(16) | |
outLayer = LinearLayer(4) | |
net.addInputModule(inLayer) | |
net.addModule(hiddenLayer) | |
net.addOutputModule(outLayer) | |
# Connection | |
in_to_hidden = FullConnection(inLayer, hiddenLayer) | |
hidden_to_out = FullConnection(hiddenLayer, outLayer) | |
net.addConnection(in_to_hidden) | |
net.addConnection(hidden_to_out) | |
# init network | |
net.sortModules() | |
# Data set | |
ds = SupervisedDataSet(1, 4) | |
#numbers = [] | |
#for num in range(0,10): | |
# numbers += [int(random.randint(0, 15))] | |
numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15] | |
for num in numbers: | |
ds.addSample((num,), tuple(int2bin(num))) | |
#print numbers | |
print ds | |
# Traning | |
trainer = BackpropTrainer(net, ds) | |
if True: | |
i = 0 | |
while i < 1000: | |
err = trainer.train() | |
print "Traning error:", err | |
i=i+1 | |
else: | |
err = trainer.trainUntilConvergence() | |
# Activation | |
for i in range(16): | |
print i, net.activate((i,)) | |
#trainer.trainUntilConvergence() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment