-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralNetsWithCross.pyx
153 lines (125 loc) · 5.08 KB
/
neuralNetsWithCross.pyx
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import re
import numpy as np
import random as rand
#Sigmoidal Activation Function
#if a prime = True is passed in, the function returns the derivatve
def threshold(val, prime = False):
if(prime):
#print sigmoid(val) * (1.0 - sigmoid(val))
return sigmoid(val) * (1.0 - sigmoid(val))
else:
return sigmoid(val)
def sigmoid(num):
num = num * -1
return 1/(1+np.exp(num))
def main():
rand.seed()
np.random.seed()
h = 1
a = 100 #number of attributes
n = 7000 #numSamples
w1BEST = np.random.random((h, a)) #should be 101 but weird data thing
w2BEST = np.random.random((1, h))#NOTE: the dimensions of w1 and w2 were switched compared to the document
b1BEST = np.zeros(shape=(h, 1))
b2BEST = rand.random()
lowestError = 100
lastError = -1
bestNodeNum = -1
runNumber = -1
path = "mushroom-training.txt"
R = np.empty([1, n], dtype = np.int_) #Global Load Vector
K = np.empty([a, n], dtype = np.int_) #Global stiffness matrix of testing data
LEARNING_RATE = 0.1
count = 0
np.seterr(all='ignore')
for line in open(path, 'r'):
line = re.sub(',', '', line)
R[0, count] = int(line[0])
line = line[1:]
length = len(line)
for i in range(length-1):
K[i, count] = int(line[0])
line = line[1:]
count+=1
print('PARSING DONE')
for j in range(5, 10):
np.random.shuffle(K.T)
trainingRatio = 6.0/7.0
trainingSet = K[0:a,0:int(n*trainingRatio)]
trainingSetR = R[0, 0:int(n*trainingRatio)]
validationSet = K[0:a, int(n*trainingRatio):n]
validationSetR = R[0, int(n*trainingRatio):n]
h = j+1#the number of hidden nodes
w1 = np.random.random((h, 100)) #should be 101 but weird data thing
w2 = np.random.random((1, h))#NOTE: the dimensions of w1 and w2 were switched compared to the document
b1 = np.zeros(shape=(h, 1))
b2 = rand.random()
lastError = 100
prevError = 100
LEARNING_RATE = 0.01
x = 0
print('%i Node Run\n' % h)
while(x < 100000):
y0 = trainingSet
x1 = np.dot(w1, y0)
x1+=b1
y1 = threshold(x1)
x2 = np.dot(w2,y1)
x2+=b2
y2 = threshold(x2)
delta2 = np.inner(threshold(x2, prime = True), (y2-trainingSetR))
bchange = np.empty([h,], dtype = np.float_)
delta1 = np.sum(np.multiply(threshold(x1, prime = True), (w2.T * delta2)), axis = 1, out = bchange, keepdims = False)
delta1 = delta1.reshape((h, 1))
deltaW2 = np.dot(np.multiply(threshold(x2, prime = True), (y2 - trainingSetR)), y1.T)
deltaW1 = np.dot(np.multiply(threshold(x1, prime = True), (w2.T * delta2)), y0.T)
w2-=(LEARNING_RATE*deltaW2)
w1-=(LEARNING_RATE*deltaW1)
b1-=(LEARNING_RATE*delta1)
b2-=(LEARNING_RATE*delta2)
y0V = validationSet
x1V = np.dot(w1, y0V)
x1V +=b1
y1V = threshold(x1V)
x2V = np.dot(w2,y1V)
x2V+=b2
y2V = threshold(x2V)
MSE = (0.5*(np.square(validationSetR-y2V)).mean(axis = None)) #calculate error yo
if(True):#MSE < prevError):
prevError = MSE
if(MSE < lowestError):
w1BEST = w1
w2BEST = w2
b1BEST = b1
b2BEST = b2
bestNodeNum = h
lowestError = MSE
runNumber = x
if(x % 100 == 0):
if(abs(MSE - lastError) < 0.0000001 and x % 500 == 0):
break
lastError = MSE
print("Error: " + str(MSE) + " Run: " + str(x) + " Number of Hidden Nodes: " + str(h) + "\nLowest Error: " + str(lowestError) )
LEARNING_RATE = LEARNING_RATE - (0.1/100000)
x+=1
#else:
# lastError = MSE
# print("Error: " + str(MSE) + " Run: " + str(x) + " Number of Hidden Nodes: " + str(h) + "\nLowest Error: " + str(lowestError) )
# break
#what do I repeat exactly?
np.save('weights1.npy', w1BEST)
np.save('weights2.npy', w2BEST)
np.savetxt('weights1.txt', w1BEST)
np.savetxt('weights2.txt', w2BEST)
np.save('base1.npy', b1BEST)
np.save('base2.npy', b2BEST)
np.savetxt('base1.txt', b1BEST)
np.savetxt('base2.txt', b2BEST)
print(bestNodeNum)
print(lowestError)
print(runNumber)
text_file = open("NodeNumber.txt", "w")
text_file.write("Number of Nodes: " + str(bestNodeNum) + "\nError: " + str(lowestError) +"\nNumber of Runs: " + str(runNumber) )
text_file.close()
#look here: http://iamtrask.github.io/2015/07/12/basic-python-network/,
main()