-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmlp_helper.py
275 lines (246 loc) · 11.5 KB
/
mlp_helper.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
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
def return_layer_weights_notation(inp, out, l):
capa = []
biases = []
for i in range(inp):
capa.append([])
for j in range(out):
capa[i].append(f"$w_{{{i+1},{j+1}}}^{l}$")
for j in range(out):
biases.append(f"$w_{{{0},{j+1}}}^{l}$")
return capa, biases
def return_weights_notation(shape):
weights=[]
biases = []
for i in range(len(shape)-1):
inp = shape[i]
out = shape[i+1]
capa, bias = return_layer_weights_notation(inp, out, i+1)
weights.append(np.array(capa))
biases.append(bias)
return weights, biases
def draw_neural_net(ax, left, right, bottom, top, layer_sizes, coefs_, intercepts_):
'''
Draw a neural network cartoon using matplotilb.
:usage:
>>> fig = plt.figure(figsize=(12, 12))
>>> draw_neural_net(fig.gca(), .1, .9, .1, .9, [4, 7, 2])
:parameters:
- ax : matplotlib.axes.AxesSubplot
The axes on which to plot the cartoon (get e.g. by plt.gca())
- left : float
The center of the leftmost node(s) will be placed here
- right : float
The center of the rightmost node(s) will be placed here
- bottom : float
The center of the bottommost node(s) will be placed here
- top : float
The center of the topmost node(s) will be placed here
- layer_sizes : list of int
List of layer sizes, including input and output dimensionality
'''
ax.axis('off')
n_layers = len(layer_sizes)
v_spacing = (top - bottom)/float(max(layer_sizes))
h_spacing = (right - left)/float(len(layer_sizes) - 1)
# Input-Arrows
layer_top_0 = v_spacing*(layer_sizes[0] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[0]):
plt.arrow(left-0.18, layer_top_0 - m*v_spacing, 0.12, 0, lw =0.1, head_width=0.01, head_length=0.02)
# Nodes
for n, layer_size in enumerate(layer_sizes):
layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2.
for m in range(layer_size):
circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/8.,
color='w', ec='k', zorder=4)
if n == 0:
plt.text(left-0.125, layer_top - m*v_spacing, r'$X_{'+str(m+1)+'}$', fontsize=15)
elif (n_layers == 3) & (n == 1):
plt.text(n*h_spacing + left+0.00, layer_top - m*v_spacing+ (v_spacing/8.+0.01*v_spacing), r'$a_{'+str(m+1)+'}$', fontsize=15)
elif n == n_layers -1:
# plt.text(n*h_spacing + left+0.10, layer_top - m*v_spacing, r' $\hat{p}_{'+str(m+1)+'}$=sigmoid($h_{'+str(m+1)+'}$)', fontsize=15)
plt.text(n*h_spacing + left+0.10, layer_top - m*v_spacing, r' $h_{'+str(m+1)+'}$', fontsize=15)
ax.add_artist(circle)
# Bias-Nodes
for n, layer_size in enumerate(layer_sizes):
if n < n_layers -1:
x_bias = (n+0.5)*h_spacing + left
y_bias = top + 0.005
circle = plt.Circle((x_bias, y_bias), v_spacing/8., color='w', ec='k', zorder=4)
plt.text(x_bias-(v_spacing/8.+0.10*v_spacing+0.01), y_bias, r'$1$', fontsize=15)
ax.add_artist(circle)
# Edges
# Edges between nodes
for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
for m in range(layer_size_a):
for o in range(layer_size_b):
line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left],
[layer_top_a - m*v_spacing, layer_top_b - o*v_spacing], c='k', lw=0.1)
ax.add_artist(line)
xm = (n*h_spacing + left)
xo = ((n + 1)*h_spacing + left)
ym = (layer_top_a - m*v_spacing)
yo = (layer_top_b - o*v_spacing)
rot_mo_rad = np.arctan((yo-ym)/(xo-xm))
rot_mo_deg = rot_mo_rad*180./np.pi
xm1 = xm + (v_spacing/8.+0.05)*np.cos(rot_mo_rad)
if n == 0:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.05)*np.sin(rot_mo_rad)
else:
if yo > ym:
ym1 = ym + (v_spacing/8.+0.12)*np.sin(rot_mo_rad)
else:
ym1 = ym + (v_spacing/8.+0.04)*np.sin(rot_mo_rad)
# print(n, m, o, str(coefs_[n][m, o]))
plt.text( xm1, ym1,\
str(coefs_[n][m, o]),\
rotation = rot_mo_deg, \
fontsize = 12)
# Edges between bias and nodes
for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
if n < n_layers-1:
layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
x_bias = (n+0.5)*h_spacing + left
y_bias = top + 0.005
for o in range(layer_size_b):
line = plt.Line2D([x_bias, (n + 1)*h_spacing + left],
[y_bias, layer_top_b - o*v_spacing], c='k', lw=0.1)
ax.add_artist(line)
xo = ((n + 1)*h_spacing + left)
yo = (layer_top_b - o*v_spacing)
rot_bo_rad = np.arctan((yo-y_bias)/(xo-x_bias))
rot_bo_deg = rot_bo_rad*180./np.pi
xo2 = xo - (v_spacing/8.+0.01)*np.cos(rot_bo_rad)
yo2 = yo - (v_spacing/8.+0.01)*np.sin(rot_bo_rad)
xo1 = xo2 -0.05 *np.cos(rot_bo_rad)
yo1 = yo2 -0.05 *np.sin(rot_bo_rad)
plt.text( xo1, yo1,\
str(intercepts_[n][o]),\
rotation = rot_bo_deg, \
fontsize = 12)
# Output-Arrows
layer_top_0 = v_spacing*(layer_sizes[-1] - 1)/2. + (top + bottom)/2.
for m in range(layer_sizes[-1]):
plt.arrow(right+0.015, layer_top_0 - m*v_spacing, 0.16*h_spacing, 0, lw =1, head_width=0.01, head_length=0.02)
def get_dataset(random_seed=42, N=200):
N = N//4
np.random.seed(random_seed)
X1 = np.random.multivariate_normal(np.array([0, 0]), [[1,0],[0,1]], 2*N)
X2 = np.random.multivariate_normal(np.array([0, 6]), [[6,0],[0,1]], N)
X3 = np.random.multivariate_normal(np.array([6, 0]), [[1,0],[0,6]], N)
X = np.vstack([X1, X2, X3])
y = np.vstack([np.ones((2*N, 1)), np.zeros((2*N, 1))]).reshape(-1)
return X, y
def get_dataset_2(random_seed=42, N=200):
N = N//4
if random_seed is not None:
np.random.seed(random_seed)
X1 = np.random.multivariate_normal(np.array([0, 0]), [[1,0],[0,1]], 2*N)
X2 = np.random.multivariate_normal(np.array([0, 6]), [[6,0],[0,1]], N)
X3 = np.random.multivariate_normal(np.array([6, 0]), [[1,0],[0,6]], N)
X4 = np.random.multivariate_normal(np.array([-6, 0]), [[1,0],[0,6]], N)
X5 = np.random.multivariate_normal(np.array([0, -6]), [[6,0],[0,1]], N)
X = np.vstack([X1, X2, X3, X4, X5])
y = np.vstack([np.ones((2*N, 1)), np.zeros((4*N, 1))]).reshape(-1)
return X, y
def generate_gaussians_distributions(sep=1, N=500, random_state=42, normalize=True):
np.random.seed(random_state)
# Zeros
X1 = np.random.multivariate_normal(sep*np.array([0.5, 0.5]), [[0.1,-0.085],[-0.085,0.1]], N//2)
# Ones
X2 = np.random.multivariate_normal([-0.25, -0.25], [[0.1,0],[0,0.1]], N//2)
X = np.append(X1, X2, axis=0)
y = np.append(np.zeros(N//2), np.ones(N//2))
indexes = np.arange(len(y))
np.random.shuffle(indexes)
if normalize:
X = (X - X.mean(axis=0))/X.std(axis=0)
else:
X[:, 0] = X[:, 0]
X[:, 1] = X[:, 1]
return X[indexes], y[indexes]
def plot_boundaries_keras(X_train, y_train, score, probability_func, degree=None, bias=False, h = .02, ax = None, margin=0.5):
X = X_train
x_min, x_max = X[:, 0].min() - margin, X[:, 0].max() + margin
y_min, y_max = X[:, 1].min() - margin, X[:, 1].max() + margin
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
if ax is None:
ax = plt.subplot(1, 1, 1)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
if degree is not None:
polynomial_set = get_polynimial_set(np.c_[xx.ravel(), yy.ravel()], degree = degree, bias=bias)
Zaux = probability_func(polynomial_set)
else:
Zaux = probability_func(np.c_[xx.ravel(), yy.ravel()])
# Z = Z_aux[:, 1]
if Zaux.shape[1] == 2:
Z = Zaux[:, 1]
else:
Z = Zaux[:, 0]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
cf = ax.contourf(xx, yy, Z, 50, cmap=cm, alpha=.8)
plt.colorbar(cf, ax=ax)
#plt.colorbar(Z,ax=ax)
# Plot also the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors='k', s=100)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
if score is not None:
ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
size=40, horizontalalignment='right')
def plot_boundaries(X_train, y_train, score=None, probability_func=None, degree = None, n_colors = 100, mesh_res = 1000, ax = None):
X = X_train #np.vstack((X_test, X_train))
if len(y_train.shape) == 2 and y_train.shape[1] == 1:
y_train = y_train.reshape(-1)
margin_x = (X[:, 0].max() - X[:, 0].min())*0.05
margin_y = (X[:, 1].max() - X[:, 1].min())*0.05
x_min, x_max = X[:, 0].min() - margin_x, X[:, 0].max() + margin_x
y_min, y_max = X[:, 1].min() - margin_y, X[:, 1].max() + margin_y
hx = (x_max-x_min)/mesh_res
hy = (y_max-y_min)/mesh_res
x_domain = np.arange(x_min, x_max, hx)
y_domain = np.arange(y_min, y_max, hy)
xx, yy = np.meshgrid(x_domain, y_domain)
if ax is None:
ax = plt.subplot(1, 1, 1)
cm = plt.cm.RdBu
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
if probability_func is not None:
if degree is not None:
polynomial_set = get_polynimial_set(np.c_[xx.ravel(), yy.ravel()], degree = degree)
Z = probability_func(polynomial_set)[:, 1]
else:
Z_aux = probability_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z_aux[:, 1]
# Put the result into a color plot
Z = Z.reshape(xx.shape)
cf = ax.contourf(xx, yy, Z, n_colors, vmin=0., vmax=1., cmap=cm, alpha=.8)
plt.colorbar(cf, ax=ax)
#plt.colorbar(Z,ax=ax)
boundary_line = np.where(np.abs(Z-0.5)<0.001)
ax.scatter(x_domain[boundary_line[1]], y_domain[boundary_line[0]], color='k', alpha=0.5, s=1)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.text(xx.max() - .3, yy.min() + .3, score,
size=20, horizontalalignment='right')
# Plot also the training points
ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright,
edgecolors='k', s=40, marker='o')