Created
March 13, 2015 21:03
-
-
Save agamm/d9bf9a1818e7142db311 to your computer and use it in GitHub Desktop.
XOR neural network
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
import pybrain | |
from pybrain.datasets import * | |
from pybrain.tools.shortcuts import buildNetwork | |
from pybrain.supervised.trainers import BackpropTrainer | |
import pickle | |
if __name__ == "__main__": | |
ds = SupervisedDataSet(2, 1) | |
ds.addSample( (0,0) , (0,)) | |
ds.addSample( (0,1) , (1,)) | |
ds.addSample( (1,0) , (1,)) | |
ds.addSample( (1,1) , (0,)) | |
net = buildNetwork(2, 4, 1, bias=True) | |
try: | |
f = open('_learned', 'r') | |
net = pickle.load(f) | |
f.close() | |
except: | |
trainer = BackpropTrainer(net, learningrate = 0.01, momentum = 0.99) | |
trainer.trainOnDataset(ds, 100000) | |
trainer.testOnData() | |
f = open('_learned', 'w') | |
pickle.dump(net, f) | |
f.close() | |
print net.activate((1,1)) , 0 | |
print net.activate((0,0)) , 0 | |
print net.activate((0,1)) , 1 | |
print net.activate((1,0)) , 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment