-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathann.py
executable file
·103 lines (90 loc) · 4.44 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import numpy
import os.path
import sys
import math
def sigmoid(input):
#sigmoid function
return 1/(1+numpy.exp(-input))
def sigmoidDeriv(input):
#derivative of the sigmoid function
return sigmoid(input)*(1-sigmoid(input))
def main(argv=None):
#check arguments & set defaults
if len(argv) > 6 or len(argv) < 2 or len(argv)%2==1:
print "The correct input format is as follows: python ann.py <filename> [h <number of hidden nodes> | p <holdout percentage>]"
return
inputData = []
outputData = []
inputWeights = []
outputWeights = []
numHidden = 5
percentTest = .2
for i in xrange(len(argv)):
if argv[i] == "h":
numHidden = int(argv[i+1])
elif argv[i] == "p":
percentTest = float(argv[i+1])
#try and open file
if os.path.isfile(argv[1]):
#read the file into the input and output arrays two inputs and one output
inputData = numpy.loadtxt(argv[1],usecols=(0,1))
outputData = numpy.loadtxt(argv[1],usecols=(2,))
outputData = numpy.reshape(outputData, (-1, 1))
else:
print "This file either doesn't exist or the name was misspelled"
sys.exit(0)
#initialize the input and hidden layer with random weights
#weight[i][j] is the weight between node i and j, and by default ranges between -1 and 1
numpy.random.seed(1)
inputWeights = 2*(numpy.random.random((inputData.shape[1], numHidden)))-1
hiddenWeights = 2*(numpy.random.random((numHidden, 1)))-1
#loops over the training data
for j in xrange(100):
for i in xrange(int(math.floor(inputData.shape[0] * (1-percentTest)))):
#copy input data to input array
inputLayer = inputData[i]
inputLayer = numpy.reshape(inputLayer, (-1, 1)).T
#forward propegate the input to the hidden layers
#dot product produces sum of node i output times weight Wi,j for all nodes i,j
hiddenLayerIN = numpy.dot(inputLayer, inputWeights)
hiddenLayer = sigmoid(hiddenLayerIN)
#forward propegate hidden layer outputs to the output layer
#dot product produces sum of node i output times weight Wi,j for all nodes i,j
outputLayerIN = numpy.dot(hiddenLayer, hiddenWeights)
outputLayer = sigmoid(outputLayerIN)
#calculate output layer error using the given data.
outputError = outputData[i]-outputLayer
#calculate the output layer delta.
outputDelta = outputError*sigmoidDeriv(outputLayerIN)
#calculate the hidden layer error using the output delta value and weights.
#dot product produces sum of delta[j] times weights Wi,j for all i,j (in this case i are hidden nodes and j are output nodes)
hiddenError = numpy.dot(outputDelta, hiddenWeights.T)
#calculate the hidden layer delta by using the errors
hiddenDelta = hiddenError*sigmoidDeriv(hiddenLayerIN)
#modify input weights and hidden layer weights depending on the delta values and the output of the neurons.
#dot product produces a(i) * delta(j) for every node i connected to node j with weight weight[i][j]
inputWeights = inputWeights+numpy.dot(inputLayer.T, hiddenDelta)
hiddenWeights = hiddenWeights+numpy.dot(hiddenLayer.T, outputDelta)
count = 0.0
errors = 0.0
for i in xrange(int(math.floor(inputData.shape[0] * (1-percentTest))), inputData.shape[0]):
#copy input data to input array
inputLayer = inputData[i]
inputLayer = numpy.reshape(inputLayer, (-1, 1)).T
#forward propegate the input to the hidden layers
#dot product produces sum of node i output times weight Wi,j for all nodes i,j
hiddenLayerIN = numpy.dot(inputLayer, inputWeights)
hiddenLayer = sigmoid(hiddenLayerIN)
#forward propegate hidden layer outputs to the output layer
#dot product produces sum of node i output times weight Wi,j for all nodes i,j
outputLayerIN = numpy.dot(hiddenLayer, hiddenWeights)
outputLayer = sigmoid(outputLayerIN)
#calculate output layer hit or miss.
count += 1
if (not int(round(outputLayer[0])) == int(outputData[i][0])):
errors += 1
errorRate = errors/count
print "Tests done: " + str(count)
print "Test errors: " + str(errors)
print "The error rate on the test data was: " + str(errorRate)
main(sys.argv)