-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANN.py
70 lines (48 loc) · 2.02 KB
/
ANN.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.utilities import percentError
import pybrain
import dataset
from pybrain.supervised.trainers import BackpropTrainer
import time
from pybrain.tools.customxml import NetworkWriter
data = dataset.genTestDataset()
theNet = FeedForwardNetwork()
bitsPerByte = 8;
bytesToAnalize = 128;
numberOfOutputs = 17
inLayer = []
hiddenLayer1 = []
hiddenLayer2 = SigmoidLayer(bytesToAnalize, name='hidden2')
outLayer = LinearLayer(numberOfOutputs, name = 'output')
in_to_hidden = []
hidden1_to_hidden2 = []
theNet.addModule(hiddenLayer2)
for i in range(0,bytesToAnalize):
inLayer.append(LinearLayer(bitsPerByte, name=("bit " + str(i*8) + " to " +str(i*8+bitsPerByte))))
hiddenLayer1.append(SigmoidLayer(1, name = ("byte " + str(i))))
in_to_hidden.append(pybrain.structure.FullConnection(inLayer[i],hiddenLayer1[i], name =("connection "+ str(i+1) + " of 128")))
theNet.addInputModule(inLayer[i])
theNet.addModule(hiddenLayer1[i])
theNet.addConnection(in_to_hidden[i])
hidden1_to_hidden2.append(pybrain.structure.FullConnection(hiddenLayer1[i],hiddenLayer2, name= ("byte " + str(i) + " to hidden2")))
theNet.addConnection(hidden1_to_hidden2[i])
theNet.addOutputModule(outLayer)
theNet.sortModules()
hidden_to_out = pybrain.structure.FullConnection(hiddenLayer2,outLayer, name = 'hidden to output')
theNet.addConnection(hidden_to_out)
theNet.sortModules()
inp = data['input']
oup = data['target']
print(inp[2,:])
print(oup[2,:])
testData, trainData = data.splitWithProportion(0.2)
trainer = BackpropTrainer(theNet, trainData, verbose=True)
for i in range(2500):
print ("epoch " + str(i))
trainer.trainEpochs(1)
if i%20 == 0:
NetworkWriter.writeToFile(theNet,"/Users/arcadigonzalez/PycharmProjects/anncomp_pro/ANN_Backups/"+str(i))
NetworkWriter.writeToFile(theNet,"/Users/arcadigonzalez/Google Drive/NN_BU/NN1/"+str(i))
print("resting processor, writing file")
time.sleep(122)