-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_utils_uni.py
More file actions
526 lines (440 loc) · 18 KB
/
lab_utils_uni.py
File metadata and controls
526 lines (440 loc) · 18 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
"""
lab_utils_uni.py
routines used in Course 1, Week2, labs1-3 dealing with single variables (univariate)
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib.gridspec import GridSpec
from matplotlib.colors import LinearSegmentedColormap
from ipywidgets import interact
from lab_utils_common import compute_cost
from lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors
plt.style.use('./deeplearning.mplstyle')
n_bin = 5
dlcm = LinearSegmentedColormap.from_list(
'dl_map', dlcolors, N=n_bin)
##########################################################
# Plotting Routines
##########################################################
def plt_house_x(X, y, f_wb=None, ax=None):
''' plot house with aXis '''
if not ax:
fig, ax = plt.subplots(1, 1)
ax.scatter(X, y, marker='x', c='r', label="Actual Value")
ax.set_title("Housing Prices")
ax.set_ylabel('Price (in 1000s of dollars)')
ax.set_xlabel(f'Size (1000 sqft)')
if f_wb is not None:
ax.plot(X, f_wb, c=dlblue, label="Our Prediction")
ax.legend()
def mk_cost_lines(x, y, w, b, ax):
''' makes vertical cost lines'''
cstr = "cost = (1/m)*("
ctot = 0
label = 'cost for point'
addedbreak = False
for p in zip(x, y):
f_wb_p = w*p[0]+b
c_p = ((f_wb_p - p[1])**2)/2
c_p_txt = c_p
ax.vlines(p[0], p[1], f_wb_p, lw=3,
color=dlpurple, ls='dotted', label=label)
label = '' # just one
cxy = [p[0], p[1] + (f_wb_p-p[1])/2]
ax.annotate(f'{c_p_txt:0.0f}', xy=cxy, xycoords='data', color=dlpurple,
xytext=(5, 0), textcoords='offset points')
cstr += f"{c_p_txt:0.0f} +"
if len(cstr) > 38 and addedbreak is False:
cstr += "\n"
addedbreak = True
ctot += c_p
ctot = ctot/(len(x))
cstr = cstr[:-1] + f") = {ctot:0.0f}"
ax.text(0.15, 0.02, cstr, transform=ax.transAxes, color=dlpurple)
##########
# Cost lab
##########
def plt_intuition(x_train, y_train):
w_range = np.array([200-200, 200+200])
tmp_b = 100
w_array = np.arange(*w_range, 5)
cost = np.zeros_like(w_array)
for i in range(len(w_array)):
tmp_w = w_array[i]
cost[i] = compute_cost(x_train, y_train, tmp_w, tmp_b)
def func(w=150):
f_wb = np.dot(x_train, w) + tmp_b
fig, ax = plt.subplots(1, 2, constrained_layout=True, figsize=(8, 4))
fig.canvas.toolbar_position = 'bottom'
mk_cost_lines(x_train, y_train, w, tmp_b, ax[0])
plt_house_x(x_train, y_train, f_wb=f_wb, ax=ax[0])
ax[1].plot(w_array, cost)
cur_cost = compute_cost(x_train, y_train, w, tmp_b)
ax[1].scatter(w, cur_cost, s=100, color=dldarkred,
zorder=10, label=f"cost at w={w}")
ax[1].hlines(cur_cost, ax[1].get_xlim()[0], w,
lw=4, color=dlpurple, ls='dotted')
ax[1].vlines(w, ax[1].get_ylim()[0], cur_cost,
lw=4, color=dlpurple, ls='dotted')
ax[1].set_title("Cost vs. w, (b fixed at 100)")
ax[1].set_ylabel('Cost')
ax[1].set_xlabel('w')
ax[1].legend(loc='upper center')
fig.suptitle(
f"Minimize Cost: Current Cost = {cur_cost:0.0f}", fontsize=12)
plt.show()
return func
# this is the 2D cost curve with interactive slider
def plt_stationary(x_train, y_train):
# setup figure
fig = plt.figure(figsize=(9, 8))
# fig = plt.figure(constrained_layout=True, figsize=(12,10))
fig.set_facecolor('#ffffff') # white
fig.canvas.toolbar_position = 'top'
# gs = GridSpec(2, 2, figure=fig, wspace = 0.01)
gs = GridSpec(2, 2, figure=fig)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1, :], projection='3d')
ax = np.array([ax0, ax1, ax2])
# setup useful ranges and common linspaces
w_range = np.array([200-300., 200+300])
b_range = np.array([50-300., 50+300])
b_space = np.linspace(*b_range, 100)
w_space = np.linspace(*w_range, 100)
# get cost for w,b ranges for contour and 3D
tmp_b, tmp_w = np.meshgrid(b_space, w_space)
z = np.zeros_like(tmp_b)
for i in range(tmp_w.shape[0]):
for j in range(tmp_w.shape[1]):
z[i, j] = compute_cost(x_train, y_train, tmp_w[i][j], tmp_b[i][j])
if z[i, j] == 0:
z[i, j] = 1e-6
w0 = 200
b = -100 # initial point
### plot model w cost ###
f_wb = np.dot(x_train, w0) + b
mk_cost_lines(x_train, y_train, w0, b, ax[0])
plt_house_x(x_train, y_train, f_wb=f_wb, ax=ax[0])
### plot contour ###
CS = ax[1].contour(tmp_w, tmp_b, np.log(z), levels=12,
linewidths=2, alpha=0.7, colors=dlcolors)
ax[1].set_title('Cost(w,b)')
ax[1].set_xlabel('w', fontsize=10)
ax[1].set_ylabel('b', fontsize=10)
ax[1].set_xlim(w_range)
ax[1].set_ylim(b_range)
cscat = ax[1].scatter(w0, b, s=100, color=dlblue,
zorder=10, label="cost with \ncurrent w,b")
chline = ax[1].hlines(b, ax[1].get_xlim()[0], w0,
lw=4, color=dlpurple, ls='dotted')
cvline = ax[1].vlines(w0, ax[1].get_ylim()[0], b,
lw=4, color=dlpurple, ls='dotted')
ax[1].text(0.5, 0.95, "Click to choose w,b", bbox=dict(facecolor='white', ec='black'), fontsize=10,
transform=ax[1].transAxes, verticalalignment='center', horizontalalignment='center')
# Surface plot of the cost function J(w,b)
ax[2].plot_surface(tmp_w, tmp_b, z, cmap=dlcm,
alpha=0.3, antialiased=True)
ax[2].plot_wireframe(tmp_w, tmp_b, z, color='k', alpha=0.1)
plt.xlabel("$w$")
plt.ylabel("$b$")
ax[2].zaxis.set_rotate_label(False)
ax[2].xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax[2].yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax[2].zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax[2].set_zlabel("J(w, b)\n\n", rotation=90)
plt.title("Cost(w,b) \n [You can rotate this figure]", size=12)
ax[2].view_init(30, -120)
return fig, ax, [cscat, chline, cvline]
# https://matplotlib.org/stable/users/event_handling.html
class plt_update_onclick:
def __init__(self, fig, ax, x_train, y_train, dyn_items):
self.fig = fig
self.ax = ax
self.x_train = x_train
self.y_train = y_train
self.dyn_items = dyn_items
self.cid = fig.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
if event.inaxes == self.ax[1]:
ws = event.xdata
bs = event.ydata
cst = compute_cost(self.x_train, self.y_train, ws, bs)
# clear and redraw line plot
self.ax[0].clear()
f_wb = np.dot(self.x_train, ws) + bs
mk_cost_lines(self.x_train, self.y_train, ws, bs, self.ax[0])
plt_house_x(self.x_train, self.y_train, f_wb=f_wb, ax=self.ax[0])
# remove lines and re-add on countour plot and 3d plot
for artist in self.dyn_items:
artist.remove()
a = self.ax[1].scatter(
ws, bs, s=100, color=dlblue, zorder=10, label="cost with \ncurrent w,b")
b = self.ax[1].hlines(bs, self.ax[1].get_xlim()[
0], ws, lw=4, color=dlpurple, ls='dotted')
c = self.ax[1].vlines(ws, self.ax[1].get_ylim()[
0], bs, lw=4, color=dlpurple, ls='dotted')
d = self.ax[1].annotate(f"Cost: {cst:.0f}", xy=(ws, bs), xytext=(4, 4), textcoords='offset points',
bbox=dict(facecolor='white'), size=10)
# Add point in 3D surface plot
e = self.ax[2].scatter3D(ws, bs, cst, marker='X', s=100)
self.dyn_items = [a, b, c, d, e]
self.fig.canvas.draw()
def soup_bowl():
""" Create figure and plot with a 3D projection"""
fig = plt.figure(figsize=(8, 8))
# Plot configuration
ax = fig.add_subplot(111, projection='3d')
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_rotate_label(False)
ax.view_init(45, -120)
# Useful linearspaces to give values to the parameters w and b
w = np.linspace(-20, 20, 100)
b = np.linspace(-20, 20, 100)
# Get the z value for a bowl-shaped cost function
z = np.zeros((len(w), len(b)))
j = 0
for x in w:
i = 0
for y in b:
z[i, j] = x**2 + y**2
i += 1
j += 1
# Meshgrid used for plotting 3D functions
W, B = np.meshgrid(w, b)
# Create the 3D surface plot of the bowl-shaped cost function
ax.plot_surface(W, B, z, cmap="Spectral_r", alpha=0.7, antialiased=False)
ax.plot_wireframe(W, B, z, color='k', alpha=0.1)
ax.set_xlabel("$w$")
ax.set_ylabel("$b$")
ax.set_zlabel("$J(w,b)$", rotation=90)
ax.set_title("$J(w,b)$\n [You can rotate this figure]", size=15)
plt.show()
def inbounds(a, b, xlim, ylim):
xlow, xhigh = xlim
ylow, yhigh = ylim
ax, ay = a
bx, by = b
if (ax > xlow and ax < xhigh) and (bx > xlow and bx < xhigh) \
and (ay > ylow and ay < yhigh) and (by > ylow and by < yhigh):
return True
return False
def plt_contour_wgrad(x, y, hist, ax, w_range=[-100, 500, 5], b_range=[-500, 500, 5],
contours=[0.1, 50, 1000, 5000, 10000, 25000, 50000],
resolution=5, w_final=200, b_final=100, step=10):
b0, w0 = np.meshgrid(np.arange(*b_range), np.arange(*w_range))
z = np.zeros_like(b0)
for i in range(w0.shape[0]):
for j in range(w0.shape[1]):
z[i][j] = compute_cost(x, y, w0[i][j], b0[i][j])
CS = ax.contour(w0, b0, z, contours, linewidths=2,
colors=[dlblue, dlorange, dldarkred, dlmagenta, dlpurple])
ax.clabel(CS, inline=1, fmt='%1.0f', fontsize=10)
ax.set_xlabel("w")
ax.set_ylabel("b")
ax.set_title(
'Contour plot of cost J(w,b), vs b,w with path of gradient descent')
w = w_final
b = b_final
ax.hlines(b, ax.get_xlim()[0], w, lw=2, color=dlpurple, ls='dotted')
ax.vlines(w, ax.get_ylim()[0], b, lw=2, color=dlpurple, ls='dotted')
base = hist[0]
for point in hist[0::step]:
edist = np.sqrt((base[0] - point[0])**2 + (base[1] - point[1])**2)
if (edist > resolution or point == hist[-1]):
if inbounds(point, base, ax.get_xlim(), ax.get_ylim()):
plt.annotate('', xy=point, xytext=base, xycoords='data',
arrowprops={'arrowstyle': '->',
'color': 'r', 'lw': 3},
va='center', ha='center')
base = point
return
def plt_divergence(p_hist, J_hist, x_train, y_train):
x = np.zeros(len(p_hist))
y = np.zeros(len(p_hist))
v = np.zeros(len(p_hist))
for i in range(len(p_hist)):
x[i] = p_hist[i][0]
y[i] = p_hist[i][1]
v[i] = J_hist[i]
fig = plt.figure(figsize=(12, 5))
plt.subplots_adjust(wspace=0)
gs = fig.add_gridspec(1, 5)
fig.suptitle(f"Cost escalates when learning rate is too large")
# ===============
# First subplot
# ===============
ax = fig.add_subplot(gs[:2], )
# Print w vs cost to see minimum
fix_b = 100
w_array = np.arange(-70000, 70000, 1000)
cost = np.zeros_like(w_array)
for i in range(len(w_array)):
tmp_w = w_array[i]
cost[i] = compute_cost(x_train, y_train, tmp_w, fix_b)
ax.plot(w_array, cost)
ax.plot(x, v, c=dlmagenta)
ax.set_title("Cost vs w, b set to 100")
ax.set_ylabel('Cost')
ax.set_xlabel('w')
ax.xaxis.set_major_locator(MaxNLocator(2))
# ===============
# Second Subplot
# ===============
tmp_b, tmp_w = np.meshgrid(
np.arange(-35000, 35000, 500), np.arange(-70000, 70000, 500))
z = np.zeros_like(tmp_b)
for i in range(tmp_w.shape[0]):
for j in range(tmp_w.shape[1]):
z[i][j] = compute_cost(x_train, y_train, tmp_w[i][j], tmp_b[i][j])
ax = fig.add_subplot(gs[2:], projection='3d')
ax.plot_surface(tmp_w, tmp_b, z, alpha=0.3, color=dlblue)
ax.xaxis.set_major_locator(MaxNLocator(2))
ax.yaxis.set_major_locator(MaxNLocator(2))
ax.set_xlabel('w', fontsize=16)
ax.set_ylabel('b', fontsize=16)
ax.set_zlabel('\ncost', fontsize=16)
plt.title('Cost vs (b, w)')
# Customize the view angle
ax.view_init(elev=20., azim=-65)
ax.plot(x, y, v, c=dlmagenta)
return
# draw derivative line
# y = m*(x - x1) + y1
def add_line(dj_dx, x1, y1, d, ax):
x = np.linspace(x1-d, x1+d, 50)
y = dj_dx*(x - x1) + y1
ax.scatter(x1, y1, color=dlblue, s=50)
ax.plot(x, y, '--', c=dldarkred, zorder=10, linewidth=1)
xoff = 30 if x1 == 200 else 10
ax.annotate(r"$\frac{\partial J}{\partial w}$ =%d" % dj_dx, fontsize=14,
xy=(x1, y1), xycoords='data',
xytext=(xoff, 10), textcoords='offset points',
arrowprops=dict(arrowstyle="->"),
horizontalalignment='left', verticalalignment='top')
def plt_gradients(x_train, y_train, f_compute_cost, f_compute_gradient):
# ===============
# First subplot
# ===============
fig, ax = plt.subplots(1, 2, figsize=(12, 4))
# Print w vs cost to see minimum
fix_b = 100
w_array = np.linspace(-100, 500, 50)
w_array = np.linspace(0, 400, 50)
cost = np.zeros_like(w_array)
for i in range(len(w_array)):
tmp_w = w_array[i]
cost[i] = f_compute_cost(x_train, y_train, tmp_w, fix_b)
ax[0].plot(w_array, cost, linewidth=1)
ax[0].set_title("Cost vs w, with gradient; b set to 100")
ax[0].set_ylabel('Cost')
ax[0].set_xlabel('w')
# plot lines for fixed b=100
for tmp_w in [100, 200, 300]:
fix_b = 100
dj_dw, dj_db = f_compute_gradient(x_train, y_train, tmp_w, fix_b)
j = f_compute_cost(x_train, y_train, tmp_w, fix_b)
add_line(dj_dw, tmp_w, j, 30, ax[0])
# ===============
# Second Subplot
# ===============
tmp_b, tmp_w = np.meshgrid(
np.linspace(-200, 200, 10), np.linspace(-100, 600, 10))
U = np.zeros_like(tmp_w)
V = np.zeros_like(tmp_b)
for i in range(tmp_w.shape[0]):
for j in range(tmp_w.shape[1]):
U[i][j], V[i][j] = f_compute_gradient(
x_train, y_train, tmp_w[i][j], tmp_b[i][j])
X = tmp_w
Y = tmp_b
n = -2
color_array = np.sqrt(((V-n)/2)**2 + ((U-n)/2)**2)
ax[1].set_title('Gradient shown in quiver plot')
Q = ax[1].quiver(X, Y, U, V, color_array, units='width', )
ax[1].quiverkey(
Q, 0.9, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E', coordinates='figure')
ax[1].set_xlabel("w")
ax[1].set_ylabel("b")
def plt_stationary_fixed_point(x_train, y_train, best_w, best_b):
# setup figure
fig = plt.figure(figsize=(9, 8))
fig.set_facecolor('#ffffff')
gs = GridSpec(2, 2, figure=fig)
ax0 = fig.add_subplot(gs[0, 0])
ax1 = fig.add_subplot(gs[0, 1])
ax2 = fig.add_subplot(gs[1, :], projection='3d')
ax = np.array([ax0, ax1, ax2])
# setup ranges
w_range = np.array([best_w - 0.5, best_w + 0.5])
b_range = np.array([best_b - 2.0, best_b + 2.0])
b_space = np.linspace(*b_range, 100)
w_space = np.linspace(*w_range, 100)
tmp_b, tmp_w = np.meshgrid(b_space, w_space)
z = np.zeros_like(tmp_b)
for i in range(tmp_w.shape[0]):
for j in range(tmp_w.shape[1]):
z[i, j] = compute_cost(x_train, y_train, tmp_w[i][j], tmp_b[i][j])
if z[i, j] == 0:
z[i, j] = 1e-6
# 1. LEFT: House prices
f_wb = np.dot(x_train, best_w) + best_b
mk_cost_lines(x_train, y_train, best_w, best_b, ax[0])
plt_house_x(x_train, y_train, f_wb=f_wb, ax=ax[0])
# 2. CENTER: Contour plot
ax[1].contour(tmp_w, tmp_b, np.log(z), levels=12,
linewidths=2, alpha=0.7, colors=dlcolors)
ax[1].scatter(best_w, best_b, s=100, color=dlblue,
zorder=10, label="Best w,b")
ax[1].hlines(best_b, ax[1].get_xlim()[0], best_w,
lw=3, color=dlpurple, ls='dotted')
ax[1].vlines(best_w, ax[1].get_ylim()[0], best_b,
lw=3, color=dlpurple, ls='dotted')
ax[1].set_title("Cost(w,b) contour")
ax[1].set_xlabel("w")
ax[1].set_ylabel("b")
ax[1].legend()
# 3. RIGHT: 3D surface
ax[2].plot_surface(tmp_w, tmp_b, z, cmap=dlcm, alpha=0.4, antialiased=True)
ax[2].plot_wireframe(tmp_w, tmp_b, z, color='k', alpha=0.1)
ax[2].scatter3D(best_w, best_b, compute_cost(x_train, y_train, best_w, best_b),
color='red', marker='X', s=100, label='Best w,b')
ax[2].set_xlabel("w")
ax[2].set_ylabel("b")
ax[2].set_zlabel("J(w,b)")
ax[2].set_title("Cost surface [rotate to view]")
ax[2].view_init(30, -120)
plt.show()
def soup_bowl_actual_cost(x_train, y_train, best_w, best_b):
"""
Plot a real 3D cost surface based on compute_cost() over a range of w, b values.
"""
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Set range centered around best_w and best_b
w = np.linspace(best_w - 0.5, best_w + 0.5, 100)
b = np.linspace(best_b - 2.0, best_b + 2.0, 100)
W, B = np.meshgrid(w, b)
# Compute cost surface using actual cost function
Z = np.zeros_like(W)
for i in range(W.shape[0]):
for j in range(W.shape[1]):
Z[i, j] = compute_cost(x_train, y_train, W[i, j], B[i, j])
if Z[i, j] == 0:
Z[i, j] = 1e-6 # prevent log(0) if needed later
# Plot surface
ax.plot_surface(W, B, Z, cmap="viridis", alpha=0.7)
ax.plot_wireframe(W, B, Z, color='k', alpha=0.1)
# Highlight optimal point
ax.scatter3D(best_w, best_b, compute_cost(x_train, y_train, best_w, best_b),
color='red', s=240, marker='X', label="Minimum")
ax.set_xlabel("w")
ax.set_ylabel("b")
ax.set_zlabel("J(w, b)")
ax.set_title("Cost Surface J(w, b)")
ax.view_init(elev=30, azim=-120)
ax.legend()
plt.show()