forked from huanzhang12/RecurJac-and-CROWN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivation_functions.py
293 lines (253 loc) · 8.28 KB
/
activation_functions.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
## activation_functions.py
##
## Definitions of ReLU, leaky-ReLU and sigmoid family
## activation functions and their upper and lower bounds
##
## Copyright (C) 2018, Huan Zhang <[email protected]> and contributors
##
## This program is licenced under the BSD 2-Clause License,
## contained in the LICENCE file in this directory.
## See CREDITS for a list of contributors.
##
import numpy as np
from numba import jit
@jit(nopython=True)
def relu_ub_pn(u, l):
a = u / (u - l)
return a, -l * a
# upper bound, unsure
@jit(nopython=True)
def leaky_relu_ub_pn(u, l, k):
a = (u - k * l) / (u - l)
b = l * u * (k - 1.0) / (u - l)
return a, b
@jit(nopython=True)
def relu_lb_pn(u, l):
# adaptive bound
intercept = np.zeros_like(u)
slope = np.zeros_like(u)
mask = np.abs(u) > np.abs(l)
slope[mask] = 1.0
return slope, intercept
# lower bound, unsure (adaptive)
@jit(nopython=True)
def leaky_relu_lb_pn(u, l, k):
# adaptive bound
intercept = np.zeros_like(u)
slope = np.full(len(u), k, dtype=u.dtype)
mask = np.abs(u) > np.abs(l)
slope[mask] = 1.0
return slope, intercept
@jit(nopython=True)
def relu_ub_p(u, l):
return np.ones_like(u), np.zeros_like(u)
# upper bound, positive part
@jit(nopython=True)
def leaky_relu_ub_p(u, l, k):
return np.ones_like(u), np.zeros_like(u)
@jit(nopython=True)
def relu_lb_p(u, l):
return np.ones_like(u), np.zeros_like(u)
# lower bound, positive part
@jit(nopython=True)
def leaky_relu_lb_p(u, l, k):
return np.ones_like(u), np.zeros_like(u)
@jit(nopython=True)
def relu_ub_n(u, l):
return np.zeros_like(u), np.zeros_like(u)
# upper bound, negative part
@jit(nopython=True)
def leaky_relu_ub_n(u, l, k):
return np.full(len(u), k, dtype=u.dtype), np.zeros_like(u)
@jit(nopython=True)
def relu_lb_n(u, l):
return np.zeros_like(u), np.zeros_like(u)
# lower bound, negative part
@jit(nopython=True)
def leaky_relu_lb_n(u, l, k):
return np.full(len(u), k, dtype=u.dtype), np.zeros_like(u)
@jit(nopython=True)
def act_tanh(y):
return np.tanh(y)
@jit(nopython=True)
def act_tanh_d(y):
t = np.cosh(y)
t = t * t
return 1.0 / t
@jit(nopython=True)
def act_arctan(y):
return np.arctan(y)
@jit(nopython=True)
def act_arctan_d(y):
return 1.0 / (1 + y * y)
@jit(nopython=True)
def act_sigmoid(y):
return 1.0 / (1.0 + np.exp(-y))
@jit(nopython=True)
def act_sigmoid_d(y):
return act_sigmoid(y) * (1 - act_sigmoid(y))
# for I+ case, upper bound
@jit(nopython=True)
def general_ub_n(u, l, func, dfunc):
alpha = np.empty_like(u)
mask = np.abs(u-l) > 1e-5
alpha[mask] = (func(u[mask])-func(l[mask]))/(u[mask]-l[mask])
mask = np.logical_not(mask)
alpha[mask] = dfunc(u[mask])
alpha_UB = alpha;
beta_UB = func(l) - l * alpha
return alpha_UB, beta_UB
@jit(nopython=True)
def general_lb_n(u, l, func, dfunc):
d = 0.5*(u+l);
alpha_LB = dfunc(d)
beta_LB = func(d) - d * alpha_LB;
return alpha_LB, beta_LB
@jit(nopython=True)
def general_ub_p(u, l, func, dfunc):
d = 0.5*(u+l);
alpha_UB = dfunc(d)
beta_UB = func(d) - d * alpha_UB
return alpha_UB, beta_UB
@jit(nopython=True)
def general_lb_p(u, l, func, dfunc):
alpha = np.empty_like(u)
mask = np.abs(u-l) > 1e-5
alpha[mask] = (func(u[mask])-func(l[mask]))/(u[mask]-l[mask])
mask = np.logical_not(mask)
alpha[mask] = dfunc(l[mask])
beta_LB = func(l) - l * alpha
alpha_LB = alpha
return alpha_LB, beta_LB
@jit(nopython=True)
def general_ub_pn(u, l, func, dfunc):
d_UB = np.empty_like(u)
for i in range(len(d_UB)):
d_UB[i] = find_d_UB(u[i],l[i],func,dfunc)
alpha_UB = (func(d_UB)-func(l))/(d_UB-l)
beta_UB = func(l) - (l - 0.01) * alpha_UB
return alpha_UB, beta_UB
@jit(nopython=True)
def general_lb_pn(u, l, func, dfunc):
d_LB = np.empty_like(u)
for i in range(len(d_LB)):
d_LB[i] = find_d_LB(u[i],l[i],func,dfunc)
alpha_LB = (func(d_LB)-func(u))/(d_LB-u)
beta_LB = func(u) - (u + 0.01) * alpha_LB
return alpha_LB, beta_LB
@jit(nopython=True)
def general_ub_pn_scalar(u, l, func, dfunc):
d_UB = find_d_UB(u,l,func,dfunc)
alpha_UB = (func(d_UB)-func(l))/(d_UB-l)
beta_UB = func(l) - (l - 0.01) * alpha_UB
return alpha_UB, beta_UB
@jit(nopython=True)
def general_lb_pn_scalar(u, l, func, dfunc):
d_LB = find_d_LB(u,l,func,dfunc)
alpha_LB = (func(d_LB)-func(u))/(d_LB-u)
beta_LB = func(u) - (u + 0.01) * alpha_LB
return alpha_LB, beta_LB
@jit(nopython=True)
def find_d_UB(u, l, func, dfunc):
diff = lambda d,l: (func(d)-func(l))/(d-l) - dfunc(d)
max_iter = 10;
d = u/2;
ub = u; lb = 0;
for i in range(max_iter):
t = diff(d, l)
if t > 0 and np.abs(t) < 0.01:
break
if t > 0:
ub = d;
d = (d+lb)/2;
else:
lb = d;
d = (d+ub)/2;
return d
@jit(nopython=True)
def find_d_LB(u,l,func,dfunc):
diff = lambda d,u: (func(u)-func(d))/(u-d) - dfunc(d)
max_iter = 10;
d = l/2;
ub = 0; lb = l;
for i in range(max_iter):
t = diff(d,u)
if t > 0 and abs(t) < 0.01:
break
if t > 0:
lb = d;
d = (d+ub)/2;
else:
ub = d;
d = (d+lb)/2;
return d
def plot_line(u, l, slope, intercept, linestype='--', label='linear'):
linear_func = lambda x: slope * x + intercept
plt.plot([l, u], [linear_func(l), linear_func(u)], linestyle=linestype, label=label, marker="o")
if __name__ == "__main__":
import matplotlib
import matplotlib.pyplot as plt
import sys
matplotlib.rc('font',family='sans-serif')
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['font.size'] = 18
matplotlib.rcParams['font.weight'] = 'bold'
matplotlib.rcParams['axes.xmargin'] = 0
matplotlib.rcParams['axes.ymargin'] = 0
matplotlib.rcParams['lines.linewidth'] = 2
matplotlib.rcParams['xtick.labelsize'] = 23
matplotlib.rcParams['ytick.labelsize'] = 23
# 'axes.labelsize': 17, 'legend.fontsize': 18,'xtick.labelsize': 16,'ytick.labelsize': 16
plt.figure(figsize=(6,4))
if len(sys.argv) == 3:
l = float(sys.argv[1])
u = float(sys.argv[2])
else:
l = -1.0
u = 1.75
assert l < u
func_name = "tanh"
if func_name == "tanh":
func = act_tanh
dfunc = act_tanh_d
elif func_name == "sigmoid":
func = act_sigmoid
dfunc = act_sigmoid_d
if func_name == "arctan":
func = act_arctan
dfunc = act_arctan_d
x = np.linspace(-3, 3, 1000)
# plot function
plt.plot(x, func(x), linestyle='-', label="$\sigma(x)=\\textrm{"+func_name+"}(x)$")
# get best bounds for func
if u < 0:
upper_k, upper_b = general_ub_n(u, l, func, dfunc)
lower_k, lower_b = general_lb_n(u, l, func, dfunc)
elif l > 0:
upper_k, upper_b = general_ub_p(u, l, func, dfunc)
lower_k, lower_b = general_lb_p(u, l, func, dfunc)
else:
upper_k, upper_b = general_ub_pn_scalar(u, l, func, dfunc)
lower_k, lower_b = general_lb_pn_scalar(u, l, func, dfunc)
upper_k_test, upper_b_test = general_ub_pn(np.array([u,u]), np.array([l,l]), func, dfunc)
lower_k_test, lower_b_test = general_lb_pn(np.array([u,u]), np.array([l,l]), func, dfunc)
assert upper_k_test[0] == upper_k_test[1] == upper_k
assert lower_k_test[0] == lower_k_test[1] == lower_k
scale = func(100) - func(-100)
print(upper_k, upper_b)
print(lower_k, lower_b)
plot_line(u, l, upper_k, upper_b, '-.', "Upper Bound")
plot_line(u, l, lower_k, lower_b, '-.', "Lower Bound")
plt.plot([u, u], [min(func(-100)-0.1*scale, lower_k*l + lower_b), upper_k*u + upper_b+0.05], linestyle=':', color='gray')
plt.text(u+0.05, func(-100)-0.075*scale, "$u$", fontsize=23)
plt.plot([l, l], [min(func(-100)-0.1*scale, lower_k*l + lower_b), upper_k*l + upper_b+0.05], linestyle=':', color='gray')
plt.text(l+0.05, func(-100)-0.075*scale, "$l$", fontsize=23)
# plt.xlim(-lim*0.3, lim)
bottom, top = plt.gca().get_ylim()
plt.ylim(bottom, top * 1.2)
plt.locator_params(axis='x', nbins=10)
plt.locator_params(axis='y', nbins=6)
plt.legend()
plt.tight_layout(h_pad=0.0, w_pad=0.0, pad=0.3)
plt.savefig('plot_{}_l_{}_u_{}.pdf'.format(func_name, l, u))
plt.show()