Skip to content

Commit fb5aeb1

Browse files
authored
Added main ipynb + modules
1 parent 9b26643 commit fb5aeb1

4 files changed

Lines changed: 882 additions & 0 deletions

File tree

cost_function.ipynb

Lines changed: 248 additions & 0 deletions
Large diffs are not rendered by default.

deeplearning.mplstyle

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# see https://matplotlib.org/stable/tutorials/introductory/customizing.html
2+
lines.linewidth: 4
3+
lines.solid_capstyle: butt
4+
5+
legend.fancybox: true
6+
7+
# Verdana" for non-math text,
8+
# Cambria Math
9+
10+
#Blue (Crayon-Aqua) 0096FF
11+
#Dark Red C00000
12+
#Orange (Apple Orange) FF9300
13+
#Black 000000
14+
#Magenta FF40FF
15+
#Purple 7030A0
16+
17+
axes.prop_cycle: cycler('color', ['0096FF', 'FF9300', 'FF40FF', '7030A0', 'C00000'])
18+
#axes.facecolor: f0f0f0 # grey
19+
axes.facecolor: ffffff # white
20+
axes.labelsize: large
21+
axes.axisbelow: true
22+
axes.grid: False
23+
axes.edgecolor: f0f0f0
24+
axes.linewidth: 3.0
25+
axes.titlesize: x-large
26+
27+
patch.edgecolor: f0f0f0
28+
patch.linewidth: 0.5
29+
30+
svg.fonttype: path
31+
32+
grid.linestyle: -
33+
grid.linewidth: 1.0
34+
grid.color: cbcbcb
35+
36+
xtick.major.size: 0
37+
xtick.minor.size: 0
38+
ytick.major.size: 0
39+
ytick.minor.size: 0
40+
41+
savefig.edgecolor: f0f0f0
42+
savefig.facecolor: f0f0f0
43+
44+
#figure.subplot.left: 0.08
45+
#figure.subplot.right: 0.95
46+
#figure.subplot.bottom: 0.07
47+
48+
#figure.facecolor: f0f0f0 # grey
49+
figure.facecolor: ffffff # white
50+
51+
## ***************************************************************************
52+
## * FONT *
53+
## ***************************************************************************
54+
## The font properties used by `text.Text`.
55+
## See https://matplotlib.org/api/font_manager_api.html for more information
56+
## on font properties. The 6 font properties used for font matching are
57+
## given below with their default values.
58+
##
59+
## The font.family property can take either a concrete font name (not supported
60+
## when rendering text with usetex), or one of the following five generic
61+
## values:
62+
## - 'serif' (e.g., Times),
63+
## - 'sans-serif' (e.g., Helvetica),
64+
## - 'cursive' (e.g., Zapf-Chancery),
65+
## - 'fantasy' (e.g., Western), and
66+
## - 'monospace' (e.g., Courier).
67+
## Each of these values has a corresponding default list of font names
68+
## (font.serif, etc.); the first available font in the list is used. Note that
69+
## for font.serif, font.sans-serif, and font.monospace, the first element of
70+
## the list (a DejaVu font) will always be used because DejaVu is shipped with
71+
## Matplotlib and is thus guaranteed to be available; the other entries are
72+
## left as examples of other possible values.
73+
##
74+
## The font.style property has three values: normal (or roman), italic
75+
## or oblique. The oblique style will be used for italic, if it is not
76+
## present.
77+
##
78+
## The font.variant property has two values: normal or small-caps. For
79+
## TrueType fonts, which are scalable fonts, small-caps is equivalent
80+
## to using a font size of 'smaller', or about 83%% of the current font
81+
## size.
82+
##
83+
## The font.weight property has effectively 13 values: normal, bold,
84+
## bolder, lighter, 100, 200, 300, ..., 900. Normal is the same as
85+
## 400, and bold is 700. bolder and lighter are relative values with
86+
## respect to the current weight.
87+
##
88+
## The font.stretch property has 11 values: ultra-condensed,
89+
## extra-condensed, condensed, semi-condensed, normal, semi-expanded,
90+
## expanded, extra-expanded, ultra-expanded, wider, and narrower. This
91+
## property is not currently implemented.
92+
##
93+
## The font.size property is the default font size for text, given in points.
94+
## 10 pt is the standard value.
95+
##
96+
## Note that font.size controls default text sizes. To configure
97+
## special text sizes tick labels, axes, labels, title, etc., see the rc
98+
## settings for axes and ticks. Special text sizes can be defined
99+
## relative to font.size, using the following values: xx-small, x-small,
100+
## small, medium, large, x-large, xx-large, larger, or smaller
101+
102+
103+
font.family: sans-serif
104+
font.style: normal
105+
font.variant: normal
106+
font.weight: normal
107+
font.stretch: normal
108+
font.size: 8.0
109+
110+
font.serif: DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
111+
font.sans-serif: Verdana, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
112+
font.cursive: Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive
113+
font.fantasy: Chicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy
114+
font.monospace: DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace
115+
116+
117+
## ***************************************************************************
118+
## * TEXT *
119+
## ***************************************************************************
120+
## The text properties used by `text.Text`.
121+
## See https://matplotlib.org/api/artist_api.html#module-matplotlib.text
122+
## for more information on text properties
123+
#text.color: black
124+

lab_utils_common.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
lab_utils_common.py
3+
functions common to all optional labs, Course 1, Week 2
4+
"""
5+
6+
import numpy as np
7+
import matplotlib.pyplot as plt
8+
9+
plt.style.use('./deeplearning.mplstyle')
10+
dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';
11+
dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple]
12+
dlc = dict(dlblue = '#0096ff', dlorange = '#FF9300', dldarkred='#C00000', dlmagenta='#FF40FF', dlpurple='#7030A0')
13+
14+
15+
##########################################################
16+
# Regression Routines
17+
##########################################################
18+
19+
#Function to calculate the cost
20+
def compute_cost_matrix(X, y, w, b, verbose=False):
21+
"""
22+
Computes the gradient for linear regression
23+
Args:
24+
X (ndarray (m,n)): Data, m examples with n features
25+
y (ndarray (m,)) : target values
26+
w (ndarray (n,)) : model parameters
27+
b (scalar) : model parameter
28+
verbose : (Boolean) If true, print out intermediate value f_wb
29+
Returns
30+
cost: (scalar)
31+
"""
32+
m = X.shape[0]
33+
34+
# calculate f_wb for all examples.
35+
f_wb = X @ w + b
36+
# calculate cost
37+
total_cost = (1/(2*m)) * np.sum((f_wb-y)**2)
38+
39+
if verbose: print("f_wb:")
40+
if verbose: print(f_wb)
41+
42+
return total_cost
43+
44+
def compute_gradient_matrix(X, y, w, b):
45+
"""
46+
Computes the gradient for linear regression
47+
48+
Args:
49+
X (ndarray (m,n)): Data, m examples with n features
50+
y (ndarray (m,)) : target values
51+
w (ndarray (n,)) : model parameters
52+
b (scalar) : model parameter
53+
Returns
54+
dj_dw (ndarray (n,1)): The gradient of the cost w.r.t. the parameters w.
55+
dj_db (scalar): The gradient of the cost w.r.t. the parameter b.
56+
57+
"""
58+
m,n = X.shape
59+
f_wb = X @ w + b
60+
e = f_wb - y
61+
dj_dw = (1/m) * (X.T @ e)
62+
dj_db = (1/m) * np.sum(e)
63+
64+
return dj_db,dj_dw
65+
66+
67+
# Loop version of multi-variable compute_cost
68+
def compute_cost(X, y, w, b):
69+
"""
70+
compute cost
71+
Args:
72+
X (ndarray (m,n)): Data, m examples with n features
73+
y (ndarray (m,)) : target values
74+
w (ndarray (n,)) : model parameters
75+
b (scalar) : model parameter
76+
Returns
77+
cost (scalar) : cost
78+
"""
79+
m = X.shape[0]
80+
cost = 0.0
81+
for i in range(m):
82+
f_wb_i = np.dot(X[i],w) + b #(n,)(n,)=scalar
83+
cost = cost + (f_wb_i - y[i])**2
84+
cost = cost/(2*m)
85+
return cost
86+
87+
def compute_gradient(X, y, w, b):
88+
"""
89+
Computes the gradient for linear regression
90+
Args:
91+
X (ndarray (m,n)): Data, m examples with n features
92+
y (ndarray (m,)) : target values
93+
w (ndarray (n,)) : model parameters
94+
b (scalar) : model parameter
95+
Returns
96+
dj_dw (ndarray Shape (n,)): The gradient of the cost w.r.t. the parameters w.
97+
dj_db (scalar): The gradient of the cost w.r.t. the parameter b.
98+
"""
99+
m,n = X.shape #(number of examples, number of features)
100+
dj_dw = np.zeros((n,))
101+
dj_db = 0.
102+
103+
for i in range(m):
104+
err = (np.dot(X[i], w) + b) - y[i]
105+
for j in range(n):
106+
dj_dw[j] = dj_dw[j] + err * X[i,j]
107+
dj_db = dj_db + err
108+
dj_dw = dj_dw/m
109+
dj_db = dj_db/m
110+
111+
return dj_db,dj_dw
112+

0 commit comments

Comments
 (0)