-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxies.py
89 lines (67 loc) · 2.18 KB
/
proxies.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numba import vectorize, float64, int64
#%% Smoothing
temper = 0.1
cold= 1/temper
@vectorize([float64(float64), float64(int64)], nopython= True)
def pos(x):
return x if x>0 else 0
@vectorize([float64(float64), float64(int64)], nopython= True)
def derPos(x):
return 1 if x>0 else 0
@vectorize([float64(int64), float64(float64)], nopython= True)
def smPos(x):
if x<=-temper/2:
res = 0
elif x<=temper/2:
res = 0.5/temper*(x+0.5*temper)**2
else: res = x
return res
# return temper*np.logaddexp(0,cold*x)
@vectorize([float64(int64), float64(float64)], nopython= True)
def derSmPos(x):
if x<=-temper/2:
res = 0
elif x<=temper/2:
res = x/temper + 0.5
else: res = 1
return res
@vectorize([float64(int64), float64(float64)], nopython= True)
def smAbs(x):
return smPos(x) + smPos(-x)
# return smPos2(x) + smPos2(-x)
@vectorize([float64(int64), float64(float64)], nopython= True)
def derSmAbs(x):
return derSmPos(x) - derSmPos(-x)
#%% Plotting the proxies
from matplotlib import pyplot as plt
import numpy as np
plt.rcParams.update({"font.family":"serif",
"pgf.texsystem": "pdflatex",
"pgf.preamble": [
r"\usepackage[utf8]{inputenc}",
r"\usepackage[T1]{fontenc}",
r"\usepackage{serif}",
r"\usepackage{amsmath, amsfonts, amssymb, amstext, amsthm, bbm, mathtools}",
]
})
plt.rc('text', usetex=True)
# vec = np.linspace(-2e-1,2e-1,101)
# fig = plt.figure()
# plt.plot(vec, pos(vec), '--')
# plt.plot(vec, smPos(vec))
# plt.legend(["positive part", "smooth proxy"], fontsize= "x-large", loc= "lower right")
# #plt.ticklabel_format(useMathText= True, useOffset= True)
# plt.grid('on')
# plt.savefig("pos_proxy.pgf", format = "pgf", bbox_inches='tight')
# #%
# fig = plt.figure()
# plt.plot(vec, np.abs(vec), '--')
# plt.plot(vec, smAbs(vec))
# plt.legend([r"absolute value", r"smooth proxy"], fontsize= "x-large", loc= "lower right")
# plt.grid('on')
# plt.savefig("abs_proxy.pgf", format = "pgf", bbox_inches='tight')
# M = np.random.random((100,2))
# plt.scatter(*M.T)
# plt.savefig("test.pgf", format = "pgf", bbox_inches='tight')