-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounterProp.py
169 lines (127 loc) · 4.5 KB
/
CounterProp.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
import numpy as np
import matplotlib.pyplot as plt
import copy
import operator
from scipy.spatial import distance
import math
from array import array
import random
import seaborn as sns
l=[]
d=[]
new_inputs=[]
loss = []
### Training ###
#Initialize inputs and targeted outputs
inputs=[[0.227, 0.289],[0.182, 0.789],[0.613, 0.553], [0.682, 0.842], [0.72, 0.342], [0.182, 0.684]]
targets=[[0,0,0,0,1],[0,0,1,0,0],[0,0,1,0,0],[0,1,0,0,0],[0,0,0,1,0],[1,0,0,0,0]]
def l_2(inputs):
d = []
l = []
inputs = np.asarray(inputs)
#l2 normalization
for i in range(0, len(inputs)):
l.append(math.sqrt(inputs[i][0]**2 + inputs[i][1]**2))
N_index = np.argmax(l)
N = l[N_index] + 0.01
for i in range(0,len(l)):
d.append(N**2 - l[i]**2)
d = np.reshape(d,(len(d),1))
inputs = np.append(inputs,d,1)
inputs = inputs/N
return inputs
new_inputs = l_2(inputs)
#Initialize weights from i to j with three inputs and 25 hidden nodes
weight_inputs=np.random.uniform(low=0, high=1, size=(3,10) )
#Initialize weights from j to k with 25 hidden nodes and 5 outputs
weight_outputs=np.random.uniform(low=0, high=1, size=(10,5) )
loss =[]
for epoch in range(600):
predictions = []
current_loss = 0
for i in range(0, len(new_inputs)):
#winner takes all
current_input = new_inputs[i]
current_input = np.reshape(current_input,(1,3))
hidden_inputs=np.dot(current_input,weight_inputs)
output = copy.deepcopy(hidden_inputs)
w_ij_win_index=np.argmax(hidden_inputs)
hidden_inputs = np.zeros((1,10))
hidden_inputs[0,w_ij_win_index] = 1
#define predicted output
predicted_output=np.dot(hidden_inputs,weight_outputs)
predictions.append(predicted_output)
pred = np.argmax(predicted_output)
##update weights
current_input = np.reshape(current_input,(1,3))
update = 0.1 * (current_input - weight_inputs[:,w_ij_win_index])
update = np.reshape(update,(3,))
weight_inputs[:,w_ij_win_index] += update
update = 0.1* (targets[i] - predicted_output)
update = np.reshape(update,(5,))
weight_outputs[w_ij_win_index,:] += update
#calculate loss
predictions = np.asarray(predictions)
predictions = np.reshape(predictions,(6,5))
targets_array = np.asarray(targets)
targets_array = np.reshape(targets_array, (6,5))
for i in range(0,len(predictions)):
current_loss += sum((np.asarray(predictions[i]) - np.asarray(targets[i]))**2)
loss.append(current_loss)
#plot loss function
plt.plot(np.arange(len(loss)),loss)
plt.show()
### Print Predicted Output Using Trained Weights ###
def predict(x):
predictions = []
for i in range(0,len(x)):
current_input = x[i]
current_input = np.reshape(current_input,(1,3))
hidden_inputs=np.dot(current_input,weight_inputs)
output = copy.deepcopy(hidden_inputs)
w_ij_win_index=np.argmax(hidden_inputs)
hidden_inputs = np.zeros((1,10))
hidden_inputs[0,w_ij_win_index] = 1
predicted_output=np.dot(hidden_inputs,weight_outputs)
predictions.append(predicted_output)
return predictions
pred = predict(new_inputs)
print(np.round(pred,2))
### Heat Map ###
#Initialize inputs and targeted outputs
inputs=[[0.227, 0.289],[0.182, 0.789],[0.613, 0.553], [0.682, 0.842], [0.72, 0.342], [0.182, 0.684]]
targets=[[0,0,0,0,1],[0,0,1,0,0],[0,0,1,0,0],[0,1,0,0,0],[0,0,0,1,0],[1,0,0,0,0]]
raw_data = np.asarray(inputs)
colors = []
for i in range(0,len(targets)):
colors.append(np.argmax(targets[i]))
for i in range(0,len(raw_data)):
plt.text(raw_data[i,0]*10,raw_data[i,1]*10,colors[i],ha="center", va="center",fontsize=20,color='r')
x = np.linspace(0,1,11)
matrix = []
cords_temp = []
for i in range(0,len(x)):
row = []
row_cords =[]
for j in range(0,len(x)):
cords_temp.append([x[i],x[j]])
cords_temp = l_2(cords_temp)
cords = []
counter = 0
matrix = []
for i in range(0,len(x)):
row_cords = []
matrix_temp = []
for j in range(0,len(x)):
row_cords.append(cords_temp[counter])
temp = np.reshape(cords_temp[counter],(1,3))
pred = predict([temp])
value = np.argmax(pred[0])
matrix_temp.append(value)
counter +=1
matrix.append(matrix_temp)
cords.append(row_cords)
matrix = np.asarray(matrix)
cords= np.asarray(cords)
plt.imshow(matrix.T)
plt.show()