Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C2 - Advanced Learning Algorithms/week2/optional-labs/lab_utils_relu.py
3584 views
1
import numpy as np
2
import matplotlib.pyplot as plt
3
from matplotlib.gridspec import GridSpec
4
plt.style.use('./deeplearning.mplstyle')
5
from matplotlib.widgets import Slider
6
from lab_utils_common import dlc
7
8
def widgvis(fig):
9
fig.canvas.toolbar_visible = False
10
fig.canvas.header_visible = False
11
fig.canvas.footer_visible = False
12
13
14
def plt_base(ax):
15
X = np.linspace(0, 3, 3*100)
16
y = np.r_[ -2*X[0:100]+2, 1*X[100:200]-3+2, 3*X[200:300]-7+2 ]
17
w00 = -2
18
b00 = 2
19
w01 = 0 # 1
20
b01 = 0 # -1
21
w02 = 0 # 2
22
b02 = 0 # -4
23
ax[0].plot(X, y, color = dlc["dlblue"], label="target")
24
arts = []
25
arts.extend( plt_yhat(ax[0], X, w00, b00, w01, b01, w02, b02) )
26
_ = plt_unit(ax[1], X, w00, b00) #Fixed
27
arts.extend( plt_unit(ax[2], X, w01, b01) )
28
arts.extend( plt_unit(ax[3], X, w02, b02) )
29
return(X, arts)
30
31
def plt_yhat(ax, X, w00, b00, w01, b01, w02, b02):
32
yhat = np.maximum(0, np.dot(w00, X) + b00) + \
33
np.maximum(0, np.dot(w01, X) + b01) + \
34
np.maximum(0, np.dot(w02, X) + b02)
35
lp = ax.plot(X, yhat, lw=2, color = dlc["dlorange"], label="a2")
36
return(lp)
37
38
def plt_unit(ax, X, w, b):
39
z = np.dot(w,X) + b
40
yhat = np.maximum(0,z)
41
lpa = ax.plot(X, z, dlc["dlblue"], label="z")
42
lpb = ax.plot(X, yhat, dlc["dlmagenta"], lw=1, label="a")
43
return([lpa[0], lpb[0]])
44
45
# if output is need for debug, put this in a cell and call ahead of time. Output will be below that cell.
46
#from ipywidgets import Output #this line stays here
47
#output = Output() #this line stays here
48
#display(output) #this line goes in notebook
49
50
def plt_relu_ex():
51
artists = []
52
53
fig = plt.figure()
54
fig.suptitle("Explore Non-Linear Activation")
55
56
gs = GridSpec(3, 2, width_ratios=[2, 1], height_ratios=[1, 1, 1])
57
ax1 = fig.add_subplot(gs[0:2,0])
58
ax2 = fig.add_subplot(gs[0,1])
59
ax3 = fig.add_subplot(gs[1,1])
60
ax4 = fig.add_subplot(gs[2,1])
61
ax = [ax1,ax2,ax3,ax4]
62
63
widgvis(fig)
64
#plt.subplots_adjust(bottom=0.35)
65
66
axb2 = fig.add_axes([0.15, 0.10, 0.30, 0.03]) # [left, bottom, width, height]
67
axw2 = fig.add_axes([0.15, 0.15, 0.30, 0.03])
68
axb1 = fig.add_axes([0.15, 0.20, 0.30, 0.03])
69
axw1 = fig.add_axes([0.15, 0.25, 0.30, 0.03])
70
71
sw1 = Slider(axw1, 'w1', -4.0, 4.0, valinit=0, valstep=0.1)
72
sb1 = Slider(axb1, 'b1', -4.0, 4.0, valinit=0, valstep=0.1)
73
sw2 = Slider(axw2, 'w2', -4.0, 4.0, valinit=0, valstep=0.1)
74
sb2 = Slider(axb2, 'b2', -4.0, 4.0, valinit=0, valstep=0.1)
75
76
X,lp = plt_base(ax)
77
artists.extend( lp )
78
79
#@output.capture()
80
def update(val):
81
#print("-----------")
82
#print(f"len artists {len(artists)}", artists)
83
for i in range(len(artists)):
84
artist = artists[i]
85
#print("artist:", artist)
86
artist.remove()
87
artists.clear()
88
#print(artists)
89
w00 = -2
90
b00 = 2
91
w01 = sw1.val # 1
92
b01 = sb1.val # -1
93
w02 = sw2.val # 2
94
b02 = sb2.val # -4
95
artists.extend(plt_yhat(ax[0], X, w00, b00, w01, b01, w02, b02))
96
artists.extend(plt_unit(ax[2], X, w01, b01) )
97
artists.extend(plt_unit(ax[3], X, w02, b02) )
98
#fig.canvas.draw_idle()
99
100
sw1.on_changed(update)
101
sb1.on_changed(update)
102
sw2.on_changed(update)
103
sb2.on_changed(update)
104
105
ax[0].set_title(" Match Target ")
106
ax[0].legend()
107
ax[0].set_xlabel("x")
108
ax[1].set_title("Unit 0 (fixed) ")
109
ax[1].legend()
110
ax[2].set_title("Unit 1")
111
ax[2].legend()
112
ax[3].set_title("Unit 2")
113
ax[3].legend()
114
plt.tight_layout()
115
116
plt.show()
117
return([sw1,sw2,sb1,sb2,artists]) # returned to keep a live reference to sliders
118
119
120