-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss-surface.py
More file actions
164 lines (142 loc) · 5.37 KB
/
loss-surface.py
File metadata and controls
164 lines (142 loc) · 5.37 KB
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
from matplotlib import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import os
EPS = np.float(1e-6)
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
def sigmoid(x):
return np.float(1) / (1 + np.exp(-x))
def floss(x, y, GT):
TP = GT[0]*x + GT[1]*y
FP = (1-GT[0])*x + (1-GT[1])*y
FN = GT[0]*(1-x) + GT[1]*(1-y)
p = TP / (TP + FP + EPS)
r = TP / (TP + FN + EPS)
return 1 - 2*p*r/(p+r)
def log_floss(x, y, GT):
TP = GT[0]*x + GT[1]*y
FP = (1-GT[0])*x + (1-GT[1])*y
FN = GT[0]*(1-x) + GT[1]*(1-y)
p = TP / (TP + FP + EPS)
r = TP / (TP + FN + EPS)
return -np.log(2*p*r/(p+r))
def celoss(x, y, GT):
def loss(y1, gt1):
return -(gt1*np.log(y1) + (1 - gt1) * np.log(1 - y1))
return loss(x, GT[0]) + loss(y, GT[1])
# y = 1
# x = np.arange(-0.1, 1.1, 0.01)
# tp = y*x
# fp = y*(1-x)
# fn = (1-y) * x
# p = tp / (tp+fp)
# r = tp / (tp + fn)
# f = 2*p*r/(p+r)
# plt.plot(1-f, x)
# plt.hold(True)
# plt.plot(np.arange(0.01, 1, 0.01), -np.log(np.arange(0.01, 1, 0.01)))
# plt.grid(True)
#=====================================================================
# loss function surface
#=====================================================================
def set_spines(ax, lw=2):
ax.spines['top'].set_linewidth(lw)
ax.spines['right'].set_linewidth(lw)
ax.spines['bottom'].set_linewidth(lw)
ax.spines['left'].set_linewidth(lw)
colormap = cm.jet
fontsiz = 25
fig = plt.figure(figsize=(60, 36))
# GT 1 0
GT = np.array([1, 0])
x0 = np.arange(-5, 5, 0.5)
x1 = np.arange(-5, 5, 0.5)
x0 = sigmoid(x0)
x1 = sigmoid(x1)
X0, X1 = np.meshgrid(x0, x1)
fl = np.array([floss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
fl = fl.reshape(X0.shape)
# GT = [1, 0] FLoss
ax = fig.add_subplot(231, projection='3d')
ax.set_zlim(0, 1.01)
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$FLoss$", fontsize=fontsiz, labelpad=10)
surf_fl = ax.plot_surface(X0, X1, fl, cmap=colormap)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
# # GT = [1, 0] Log-Floss
ax = fig.add_subplot(232, projection='3d')
logfl = np.array([log_floss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
logfl = logfl.reshape(X0.shape)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
ax.set_zlim(0, 5)
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$Log-Floss$", fontsize=fontsiz, labelpad=10)
surf_logfl = ax.plot_surface(X0, X1, logfl, cmap=colormap)
# GT = [1, 0] CELoss
ax = fig.add_subplot(233, projection='3d')
cel = np.array([celoss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
cel = cel.reshape(X0.shape)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
ax.set_zlim(0, 10)
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$CELoss$", fontsize=fontsiz, labelpad=10)
surf_cel = ax.plot_surface(X0, X1, cel, cmap=colormap)
# GT 1 1
GT = np.array([1, 1])
x0 = np.arange(-5, 5, 0.5)
x1 = np.arange(-5, 5, 0.5)
x0 = sigmoid(x0)
x1 = sigmoid(x1)
X0, X1 = np.meshgrid(x0, x1)
fl = np.array([floss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
fl = fl.reshape(X0.shape)
cel = np.array([celoss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
cel = cel.reshape(X0.shape)
# GT 1 1 FLoss
ax = fig.add_subplot(234, projection='3d')
ax.set_zlim(0, 1.01)
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$FLoss$", fontsize=fontsiz, labelpad=10)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
surf_fl = ax.plot_surface(X0, X1, fl, cmap=colormap)
# # GT = [1, 1] Log-Floss
ax = fig.add_subplot(235, projection='3d')
logfl = np.array([log_floss(x0, x1, GT) for x0,x1 in zip(np.ravel(X0), np.ravel(X1))])
logfl = logfl.reshape(X0.shape)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
ax.set_zlim(0, 5)
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$Log-Floss$", fontsize=fontsiz, labelpad=10)
surf_logfl = ax.plot_surface(X0, X1, logfl, cmap=colormap)
# GT 1 1 CELoss
ax = fig.add_subplot(236, projection='3d')
ax.set_xlabel("$\\hat{y}[1]$", fontsize=fontsiz+4, labelpad=28)
ax.set_ylabel("$\\hat{y}[0]$", fontsize=fontsiz+4, labelpad=28)
ax.set_zlabel("$CELoss$", fontsize=fontsiz, labelpad=10)
ax.set_xticks(np.arange(0, 1.1, 0.2))
ax.set_yticks(np.arange(0, 1.1, 0.2))
ax.set_zlim(0, 10)
ax.tick_params(axis='x', labelsize=fontsiz)
ax.tick_params(axis='y', labelsize=fontsiz)
ax.tick_params(axis='z', labelsize=fontsiz)
surf_cel = ax.plot_surface(X0, X1, cel, cmap=colormap)
plt.tight_layout(pad=20)
fig.savefig(os.path.join(THIS_DIR, 'loss-surface.pdf'))