-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path120050018_2.py
executable file
·177 lines (145 loc) · 5.1 KB
/
120050018_2.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pintu lal M
# assignment 2
import math
import random
import string
import csv
import sys
import numpy as np
random.seed(0)
def rand(a, b):
return (b-a)*random.random() + a
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def dsigmoid(y):
return y *(1.0 - y)
class NN:
def __init__(self, input_node_num, hidden_node_num, output_node_num):
self.LR = 0.5;
self.MF = 0.1;
self.input_node_num = input_node_num
self.hidden_node_num = hidden_node_num
self.output_node_num = output_node_num
self.inv = np.zeros(self.input_node_num) #input node vector
self.hnv = np.zeros(self.hidden_node_num) #hidden layer node vector
self.onv = np.zeros(self.output_node_num) # output node vector
self.wi = np.zeros((self.input_node_num, self.hidden_node_num)) #input weight matrix
self.wo = np.zeros((self.hidden_node_num, self.output_node_num)) # output weight matrix
self.lrci = np.zeros((self.input_node_num, self.hidden_node_num)) # last recent weight change inpput matrix
self.lrco = np.zeros((self.hidden_node_num, self.output_node_num)) #last recent weight change output matrix
for i in range(self.input_node_num):
for j in range(self.hidden_node_num):
self.wi[i][j] = rand(-1.0, 1.0)
for j in range(self.hidden_node_num):
for k in range(self.output_node_num):
self.wo[j][k] = rand(-1.0, 1.0)
#self.lrco[j][k] = rand(-1.0, 1.0)
def update(self, inputs):
if len(inputs) != self.input_node_num-1:
raise ValueError('error update')
for i in range(self.input_node_num-1):
self.inv[i] = sigmoid(inputs[i])
for j in range(self.hidden_node_num):
sum = 0.0
for i in range(self.input_node_num):
sum = sum + self.inv[i] * self.wi[i][j]
self.hnv[j] = sigmoid(sum)
for k in range(self.output_node_num):
sum = 0.0
for j in range(self.hidden_node_num):
sum = sum + self.hnv[j] * self.wo[j][k]
self.onv[k] = sigmoid(sum)
return self.onv[:]
def update_out_weight(self,output_deltas):
for j in range(self.hidden_node_num):
for k in range(self.output_node_num):
change = output_deltas[k]*self.hnv[j]
self.wo[j][k] = self.wo[j][k] + self.LR*change + self.MF*self.lrco[j][k]
self.lrco[j][k] = change
def update_input_weight(self,hidden_deltas ):
for i in range(self.input_node_num):
for j in range(self.hidden_node_num):
change = hidden_deltas[j]*self.inv[i]
self.wi[i][j] = self.wi[i][j] + self.LR*change + self.MF*self.lrci[i][j]
self.lrci[i][j] = change
def backPropagate(self, targets):
if len(targets) != self.output_node_num:
raise ValueError('error backPropagate')
output_deltas = [0.0] * self.output_node_num
for k in range(self.output_node_num):
error = targets[k]-self.onv[k]
output_deltas[k] = dsigmoid(self.onv[k]) * error
hidden_deltas = [0.0] * self.hidden_node_num
for j in range(self.hidden_node_num):
error = 0.0
for k in range(self.output_node_num):
error = error + output_deltas[k]*self.wo[j][k]
hidden_deltas[j] = dsigmoid(self.hnv[j]) * error
self.update_out_weight(output_deltas)
self.update_input_weight(hidden_deltas)
def test(self, patterns):
out_file=open("output.csv",'wb')
writer=csv.writer(out_file, dialect='excel')
writer.writerow(['Id','Label',])
count = 0
for p in patterns:
if self.update(p[0])[0]>.5:
writer.writerow([count,1])
else :
writer.writerow([count,0])
count=count+1
def train(self, patterns, iterations=100):
for i in range(iterations):
for p in patterns:
inputs = p[0]
targets = p[1]
self.update(inputs)
self.backPropagate(targets)
def readfle(file):
input_file = open(file, 'rt')
inp = []
try:
reader = csv.reader(input_file)
for row in reader:
inp.append(row)
finally:
input_file.close()
return inp;
def run():
n = NN(58, 2, 1)
inp = readfle('Train.csv')
fininp = []
finout = []
for row in inp:
l = []
i=0
for i in range(57):
l.append(float(row[i]))
i=i+1
fininp.append(l)
m = [int(row[57])]
finout.append(m)
pattern=[]
for i in range(len(finout)):
l= []
l.append(fininp[i])
l.append(finout[i])
pattern.append(l)
#print(pattern)
n.train(pattern)
inp = readfle('TestX.csv')
fininp = []
for row in inp:
l = []
i=0
for i in range(57):
l.append(float(row[i]))
i=i+1
fininp.append(l)
pattern=[]
for i in range(len(fininp)):
l= []
l.append(fininp[i])
pattern.append(l)
n.test(pattern)
run()