forked from EricSchles/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxor.py
More file actions
34 lines (28 loc) · 954 Bytes
/
xor.py
File metadata and controls
34 lines (28 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#learn XOR with a nerual network with saving of the learned paramaters
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, 3000)
trainer.testOnData()
# f = open('_learned', 'w')
# pickle.dump(net, f)
# f.close()
print net.activate((1,1))
print net.activate((1,0))
print net.activate((0,1))
print net.activate((0,0))