This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeight_Flipping_in_NN.py
245 lines (194 loc) · 5.9 KB
/
Weight_Flipping_in_NN.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import tensorflow as tf
import numpy as np
n_nodes_hl1=4 #500
n_nodes_hl2=4 #500
n_nodes_hl3=4 #500
label=[]
input_data=[]
fin_input=[]
file_data=open("Skin_NonSkin.txt")
data_read=file_data.readline()
while data_read!="":
data_read=data_read.split("\t")
data_read[3]=data_read[3][:-1]
ty=int(data_read[-1])-1
if ty==0:
label.append([1,0])
if ty==1:
label.append([0,1])
tmp=data_read[:-1]
for i in range(len(tmp)):
tmp[i]=int(tmp[i])
input_data.append(tmp)
data_read=file_data.readline()
print("len data = ",len(input_data))
train_x=input_data[:1000]+input_data[-1000:]+input_data[1000:2000]+input_data[-2000:-1000]
train_y=label[:1000]+label[-1000:]+label[1000:2000]+label[-2000:-1000]
n_classes=2
batch_size=1
x=tf.placeholder(tf.float32,[None,3])
y=tf.placeholder(tf.float32,[None,2])
############
#This function swaps the weights w.r.t the column of weight matrix ( i.e. w.r.t inputs ).
def swap_column(arr):
size=arr.shape
row=size[0]
colmn=size[1]
res=arr[:]
for i in range(colmn):
tmp=[]
for j in range(row):
tmp.append(arr[j][i])
tmp1=sorted(tmp)
for it in range(len(tmp)):
cnt=0
for jt in range(len(tmp)):
if it != jt:
if tmp[it] >=tmp[jt]:
cnt=cnt+1
res[it][i]=tmp1[len(tmp1)-1-cnt]
return res
###########
#This function swaps the weights w.r.t the row of weight matrix( i.e. w.r.t. neurons ).
def swap_row(arr):
size=arr.shape
row=size[0]
colmn=size[1]
res=arr[:]
for i in range(row):
tmp=[]
for j in range(colmn):
tmp.append(arr[i][j])
tmp1=sorted(tmp)
for it in range(len(tmp)):
cnt=0
for jt in range(len(tmp)):
if it != jt:
if tmp[it] >=tmp[jt]:
cnt=cnt+1
res[i][it]=tmp1[len(tmp1)-1-cnt]
return res
###########
#This function swaps the weights w.r.t the individual layers.
def swap(arr):
size=arr.shape
row=size[0]
colmn=size[1]
rest=arr[:]
res_tmp=arr[:]
res_t=[]
for t in arr:
for u in t:
res_t.append(u)
rtm=sorted(res_t)
rtm_cpy=[]
for tyu in range(len(rtm)):
rtm_cpy.append(0)
print(rtm)
for it in range(len(res_t)):
cnt=0
for jt in range(len(res_t)):
if it!=jt:
if res_t[it]>res_t[jt]:
cnt=cnt+1
rtm_cpy[it]=rtm[len(res_t)-1-cnt]
count_num=0
rw=0
for wd in rtm_cpy:
#print(wd)
if count_num<colmn:
res_tmp[rw][count_num]=wd
count_num=count_num+1
else:
count_num=0
rw=rw+1
res_tmp[rw][count_num]=wd
count_num=count_num+1
return res_tmp
#############
def neural_network_node(data):
hidden_1_layer={'weights':tf.Variable(tf.random_normal([3,n_nodes_hl1])),'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
output_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),'biases':tf.Variable(tf.random_normal([n_classes]))}
l1=tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
l1=tf.nn.relu(l1)
output=tf.add(tf.matmul(l1,output_layer['weights']),output_layer['biases'])
return [output,hidden_1_layer['weights'],hidden_1_layer['biases'],output_layer['weights'],output_layer['biases']]
def train_neural_network(x):
prediction_t=neural_network_node(x)
prediction=prediction_t[0]
predict=tf.nn.softmax(prediction)
wt1=prediction_t[1]
b1=prediction_t[2]
wt2=prediction_t[3]
b2=prediction_t[4]
tdy=tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y)
cost=tf.reduce_mean(tdy)
optimizer=tf.train.AdamOptimizer().minimize(cost)
hm_epochs=10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss=0
i=0
while i<len(train_x):
start=i
end=i+batch_size
epoch_x=np.array(train_x[start:end])
epoch_y=np.array(train_y[start:end])
_,pred,c,wtr1,br1,wtr2,br2=sess.run([optimizer,predict,cost,wt1,b1,wt2,b2],feed_dict={x:epoch_x,y:epoch_y})
print("prediction =",pred)
epoch_loss=epoch_loss+c
i=i+batch_size
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct,'float'))
return [wtr1,br1,wtr2,br2]
tpl=train_neural_network(x)
tpl[0]=np.array(tpl[0])
tpl[1]=np.array(tpl[1])
tpl[2]=np.array(tpl[2])
tpl[3]=np.array(tpl[3])
print("weights layer 1 :")
print(tpl[0])
print("biases layer 1 :")
print(tpl[1])
print("weights layer 2 :")
print(tpl[2])
print("biases layer 2 :")
print(tpl[3])
n_nodes_hl1=4 #500
n_nodes_hl2=4 #500
n_nodes_hl3=4 #500
def neural_network_node2(data):
hidden_1_layer={'weights':tpl[0],'biases':tpl[1]}
#hidden_2_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl1 , n_nodes_hl2])),'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
#hidden_3_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl2 , n_nodes_hl3])),'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer={'weights':tpl[2],'biases':tpl[3]}
l1=tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
l1=tf.nn.relu(l1)
output=tf.add(tf.matmul(l1,output_layer['weights']),output_layer['biases'])
return output
def predict_neural_network(x):
prediction=tf.nn.softmax(neural_network_node2(x))
#print("prediction === ",prediction)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
pred=sess.run(prediction,{x:train_x})
return pred
pred1=predict_neural_network(x)
############swapping
tpl[0]=swap(tpl[0]) #swap_row(tpl[0]) #swap_column(tpl[0])
tpl[2]=swap(tpl[2]) #swap_row(tpl[2]) #swap_column(tpl[2])
############swapping
print("new weights layer 1 :")
print(tpl[0])
print("new biases layer 1 :")
print(tpl[1])
print("new weights layer 2 :")
print(tpl[2])
print("new biases layer 2 :")
print(tpl[3])
############
pred2=predict_neural_network(x)
print("pred 1 :\n",pred1)
print("pred 2 :\n",pred2)
print("fin===\n",pred1-pred2)