-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_coffee_utils.py
148 lines (133 loc) · 5.89 KB
/
lab_coffee_utils.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
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.activations import sigmoid
from matplotlib import cm
import matplotlib.colors as colors
from lab_utils_common import dlc
def load_coffee_data():
""" Creates a coffee roasting data set.
roasting duration: 12-15 minutes is best
temperature range: 175-260C is best
"""
rng = np.random.default_rng(2)
X = rng.random(400).reshape(-1,2)
X[:,1] = X[:,1] * 4 + 11.5 # 12-15 min is best
X[:,0] = X[:,0] * (285-150) + 150 # 350-500 F (175-260 C) is best
Y = np.zeros(len(X))
i=0
for t,d in X:
y = -3/(260-175)*t + 21
if (t > 175 and t < 260 and d > 12 and d < 15 and d<=y ):
Y[i] = 1
else:
Y[i] = 0
i += 1
return (X, Y.reshape(-1,1))
def plt_roast(X,Y):
Y = Y.reshape(-1,)
colormap = np.array(['r', 'b'])
fig, ax = plt.subplots(1,1,)
ax.scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" )
ax.scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none',
edgecolors="blue",linewidth=1, label="Bad Roast")
tr = np.linspace(175,260,50)
ax.plot(tr, (-3/85) * tr + 21, color=dlc["dlpurple"],linewidth=1)
ax.axhline(y=12,color=dlc["dlpurple"],linewidth=1)
ax.axvline(x=175,color=dlc["dlpurple"],linewidth=1)
ax.set_title(f"Coffee Roasting", size=16)
ax.set_xlabel("Temperature \n(Celsius)",size=12)
ax.set_ylabel("Duration \n(minutes)",size=12)
ax.legend(loc='upper right')
plt.show()
def plt_prob(ax,fwb):
""" plots a decision boundary but include shading to indicate the probability """
#setup useful ranges and common linspaces
x0_space = np.linspace(150, 285 , 40)
x1_space = np.linspace(11.5, 15.5 , 40)
# get probability for x0,x1 ranges
tmp_x0,tmp_x1 = np.meshgrid(x0_space,x1_space)
z = np.zeros_like(tmp_x0)
for i in range(tmp_x0.shape[0]):
for j in range(tmp_x1.shape[1]):
x = np.array([[tmp_x0[i,j],tmp_x1[i,j]]])
z[i,j] = fwb(x)
cmap = plt.get_cmap('Blues')
new_cmap = truncate_colormap(cmap, 0.0, 0.5)
pcm = ax.pcolormesh(tmp_x0, tmp_x1, z,
norm=cm.colors.Normalize(vmin=0, vmax=1),
cmap=new_cmap, shading='nearest', alpha = 0.9)
ax.figure.colorbar(pcm, ax=ax)
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
""" truncates color map """
new_cmap = colors.LinearSegmentedColormap.from_list(
'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
cmap(np.linspace(minval, maxval, n)))
return new_cmap
def plt_layer(X,Y,W1,b1,norm_l):
Y = Y.reshape(-1,)
fig,ax = plt.subplots(1,W1.shape[1], figsize=(16,4))
for i in range(W1.shape[1]):
layerf= lambda x : sigmoid(np.dot(norm_l(x),W1[:,i]) + b1[i])
plt_prob(ax[i], layerf)
ax[i].scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" )
ax[i].scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none',
edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast")
tr = np.linspace(175,260,50)
ax[i].plot(tr, (-3/85) * tr + 21, color=dlc["dlpurple"],linewidth=2)
ax[i].axhline(y= 12, color=dlc["dlpurple"], linewidth=2)
ax[i].axvline(x=175, color=dlc["dlpurple"], linewidth=2)
ax[i].set_title(f"Layer 1, unit {i}")
ax[i].set_xlabel("Temperature \n(Celsius)",size=12)
ax[0].set_ylabel("Duration \n(minutes)",size=12)
plt.show()
def plt_network(X,Y,netf):
fig, ax = plt.subplots(1,2,figsize=(16,4))
Y = Y.reshape(-1,)
plt_prob(ax[0], netf)
ax[0].scatter(X[Y==1,0],X[Y==1,1], s=70, marker='x', c='red', label="Good Roast" )
ax[0].scatter(X[Y==0,0],X[Y==0,1], s=100, marker='o', facecolors='none',
edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast")
ax[0].plot(X[:,0], (-3/85) * X[:,0] + 21, color=dlc["dlpurple"],linewidth=1)
ax[0].axhline(y= 12, color=dlc["dlpurple"], linewidth=1)
ax[0].axvline(x=175, color=dlc["dlpurple"], linewidth=1)
ax[0].set_xlabel("Temperature \n(Celsius)",size=12)
ax[0].set_ylabel("Duration \n(minutes)",size=12)
ax[0].legend(loc='upper right')
ax[0].set_title(f"network probability")
ax[1].plot(X[:,0], (-3/85) * X[:,0] + 21, color=dlc["dlpurple"],linewidth=1)
ax[1].axhline(y= 12, color=dlc["dlpurple"], linewidth=1)
ax[1].axvline(x=175, color=dlc["dlpurple"], linewidth=1)
fwb = netf(X)
yhat = (fwb > 0.5).astype(int)
ax[1].scatter(X[yhat[:,0]==1,0],X[yhat[:,0]==1,1], s=70, marker='x', c='orange', label="Predicted Good Roast" )
ax[1].scatter(X[yhat[:,0]==0,0],X[yhat[:,0]==0,1], s=100, marker='o', facecolors='none',
edgecolors=dlc["dldarkblue"],linewidth=1, label="Bad Roast")
ax[1].set_title(f"network decision")
ax[1].set_xlabel("Temperature \n(Celsius)",size=12)
ax[1].set_ylabel("Duration \n(minutes)",size=12)
ax[1].legend(loc='upper right')
def plt_output_unit(W,b):
""" plots a single unit function with 3 inputs """
steps = 10
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x_ = np.linspace(0., 1., steps)
y_ = np.linspace(0., 1., steps)
z_ = np.linspace(0., 1., steps)
x, y, z = np.meshgrid(x_, y_, z_, indexing='ij')
d = np.zeros((steps,steps,steps))
cmap = plt.get_cmap('Blues')
for i in range(steps):
for j in range(steps):
for k in range(steps):
v = np.array([x[i,j,k],y[i,j,k],z[i,j,k]])
d[i,j,k] = tf.keras.activations.sigmoid(np.dot(v,W[:,0])+b).numpy()
pcm = ax.scatter(x, y, z, c=d, cmap=cmap, alpha = 1 )
ax.set_xlabel("unit 0");
ax.set_ylabel("unit 1");
ax.set_zlabel("unit 2");
ax.view_init(30, -120)
ax.figure.colorbar(pcm, ax=ax)
ax.set_title(f"Layer 2, output unit")
plt.show()