-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.py
211 lines (187 loc) · 7.42 KB
/
debug.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
# This file is to debug the curve in DPGAN paper, experiment 2, convergence of wdis
from __future__ import print_function
import tensorflow as tf
from numpy import random, sign, array
import matplotlib.pyplot as plt
from regression_class import generate_rnd_data
rng = random
def noise(tensor):
'''add noise to tensor'''
s = tensor.get_shape().as_list() # get shape of the tensor
rt = tf.random_normal(s, mean=0.0, stddev=0.5) # .04 for ls, 0.5 for lr
t = tf.add(tensor, rt)
return t
def s2hot(arr):
'''scalar to one-hot'''
h = [] # store one-hot vector
for i in range(len(arr)):
if arr[i][0] == 1.0:
h.append([1, 0])
else:
h.append([0, 1])
return array(h)
# least square, synthetic data, https://www.datahubbs.com/tensorflow-intro-linear-regression/
# logistic regression tensorflow, synthetic data, https://medium.com/all-of-us-are-belong-to-machines/gentlest-intro-to-tensorflow-4-logistic-regression-2afd0cabc54
# Parameters
n = 1000 # number of iterations
alpha = 0.1 # learning rate, 0.5 for ls, 0.1 for lr
c = 0.8 # clip value, 0.5 for ls, 0.8 for lr
class_num = 2 # number of class in lr
# Training Data
(feature_all, target_all, model) = generate_rnd_data(feature_size=30, sample_size=100, bias=True)
target_all = s2hot(sign(target_all)) # coutinues target to binary label, scalar to one-hot
# noise-free
# W = tf.Variable(tf.random_uniform([feature_all.shape[1], 1], -1.0, 1.0))
# b = tf.Variable(tf.zeros([feature_all.shape[0], 1]))
# x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
# y = tf.placeholder(tf.float32, [None, 1])
# y_hat = tf.matmul(x, W) + b
# loss = tf.reduce_mean(tf.square(y - y_hat))/(2*feature_all.shape[0])
W = tf.Variable(tf.random_uniform([feature_all.shape[1], class_num], -1.0, 1.0))
b = tf.Variable(tf.zeros([class_num]))
x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
y = tf.placeholder(tf.float32, [None, class_num])
y_hat = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_hat), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha)
train = optimizer.minimize(loss)
feed = {x: feature_all, y: target_all}
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
cost_val_1 = [] # store cost value
for step in range(n):
_, loss_val = sess.run([train, loss],
feed_dict=feed)
cost_val_1.append(loss_val)
if n % 20 == 0:
print(loss_val)
sess.close()
# noise-only
# W = tf.Variable(tf.random_uniform([feature_all.shape[1], 1], -1.0, 1.0))
# b = tf.Variable(tf.zeros([feature_all.shape[0], 1]))
# x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
# y = tf.placeholder(tf.float32, [None, 1])
# y_hat = tf.matmul(x, W) + b
# loss = tf.reduce_mean(tf.square(y - y_hat))/(2*feature_all.shape[0])
W = tf.Variable(tf.random_uniform([feature_all.shape[1], class_num], -1.0, 1.0))
b = tf.Variable(tf.zeros([class_num]))
x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
y = tf.placeholder(tf.float32, [None, class_num])
y_hat = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_hat), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha)
grads_and_vars = optimizer.compute_gradients(loss, var_list=tf.global_variables())
dp_grads_and_vars = []
for gv in grads_and_vars: # for each pair
g = gv[0] # get the gradient
if g is not None: # skip None case
g = noise(g) # add noise on the tensor
dp_grads_and_vars.append((g, gv[1]))
optimizer_new = optimizer.apply_gradients(dp_grads_and_vars) # should assign to a new optimizer
feed = {x: feature_all, y: target_all}
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
cost_val_2 = []
for step in range(n):
_, loss_val = sess.run([optimizer_new, loss],
feed_dict=feed)
cost_val_2.append(loss_val)
if n % 20 == 0:
print(loss_val)
sess.close()
# clip-only
# W = tf.Variable(tf.random_uniform([feature_all.shape[1], 1], -1.0, 1.0))
# b = tf.Variable(tf.zeros([feature_all.shape[0], 1]))
# x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
# y = tf.placeholder(tf.float32, [None, 1])
# y_hat = tf.matmul(x, W) + b
# loss = tf.reduce_mean(tf.square(y - y_hat))/(2*feature_all.shape[0])
W = tf.Variable(tf.random_uniform([feature_all.shape[1], class_num], -1.0, 1.0))
b = tf.Variable(tf.zeros([class_num]))
x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
y = tf.placeholder(tf.float32, [None, class_num])
y_hat = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_hat), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha)
train = optimizer.minimize(loss)
graph_clip = [v.assign(tf.clip_by_value(v, -c, c)) for v in tf.global_variables()]
feed = {x: feature_all, y: target_all}
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
cost_val_3 = [] # store cost value
for step in range(n):
_, loss_val = sess.run([train, loss],
feed_dict=feed)
sess.run(graph_clip)
cost_val_3.append(loss_val)
if n % 20 == 0:
print(loss_val)
sess.close()
# noise-clip
W = tf.Variable(tf.random_uniform([feature_all.shape[1], 1], -1.0, 1.0))
b = tf.Variable(tf.zeros([feature_all.shape[0], 1]))
x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
y = tf.placeholder(tf.float32, [None, 1])
y_hat = tf.matmul(x, W) + b
loss = tf.reduce_mean(tf.square(y - y_hat))/(2*feature_all.shape[0])
W = tf.Variable(tf.random_uniform([feature_all.shape[1], class_num], -1.0, 1.0))
b = tf.Variable(tf.zeros([class_num]))
x = tf.placeholder(tf.float32, [None, feature_all.shape[1]])
y = tf.placeholder(tf.float32, [None, class_num])
y_hat = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
loss = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_hat), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha)
grads_and_vars = optimizer.compute_gradients(loss, var_list=tf.global_variables())
dp_grads_and_vars = []
for gv in grads_and_vars: # for each pair
g = gv[0] # get the gradient
if g is not None: # skip None case
g = noise(g) # add noise on the tensor
dp_grads_and_vars.append((g, gv[1]))
optimizer_new = optimizer.apply_gradients(dp_grads_and_vars) # should assign to a new optimizer
graph_clip = [v.assign(tf.clip_by_value(v, -c, c)) for v in tf.global_variables()]
feed = {x: feature_all, y: target_all}
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
cost_val_4 = []
for step in range(n):
_, loss_val = sess.run([optimizer_new, loss],
feed_dict=feed)
sess.run(graph_clip)
cost_val_4.append(loss_val)
if n % 20 == 0:
print(loss_val)
sess.close()
plt.figure(1)
plt.subplot(221)
plt.plot(range(len(cost_val_1)), cost_val_1, 'b')
plt.xlabel("Iteration")
plt.ylabel("Objective")
plt.title("Noise-free")
plt.grid(True)
plt.subplot(222)
plt.plot(range(len(cost_val_2)), cost_val_2)
plt.yscale('linear')
plt.xlabel("Iteration")
plt.ylabel("Objective")
plt.title("Noise-only")
plt.grid(True)
plt.subplot(223)
plt.plot(range(len(cost_val_3)), cost_val_3)
plt.yscale('linear')
plt.xlabel("Iteration")
plt.ylabel("Objective")
plt.title("Clip-only")
plt.grid(True)
plt.subplot(224)
plt.plot(range(len(cost_val_4)), cost_val_4)
plt.yscale('linear')
plt.xlabel("Iteration")
plt.ylabel("Objective")
plt.title("Noise_clip")
plt.grid(True)
plt.show()