Machine Learning Assignment 2

Abhijith Ravikumar Puthussery (qaw164)


In [4]:
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Part 1

$[W^{1,2}] = \begin{bmatrix} -0.18 -0.16 -0.03 -0.15\\ 0.1666 -0.18 0.01 -0.06\\ 0.14 -0.14 -0.065 -0.06\\ \end{bmatrix}$ $\quad [W^{2,3}] = \begin{bmatrix} -1.01\\ -1.99\\ -0.25\\ -1.64 \end{bmatrix}\\$

If $(X_1,X_2) = (2.00,0.26)$ and label $Y=1$, calculate $H(x),e$

In [5]:
# Layer 1:
print("Layer 1:")
x1 = np.array([1,2.00,0.26]) #Inputs
print("X1=", x1)

# Layer 2:
print("Layer 2:")
w12 = np.array([[-0.18,-0.16,-0.03,-0.15],
       [0.166,-0.18,0.01,-0.06],
       [0.14,-0.14,-0.065,-0.06]])
print("W12 =", w12)


Layer 1:
X1= [ 1.    2.    0.26]
Layer 2:
W12 = [[-0.18  -0.16  -0.03  -0.15 ]
 [ 0.166 -0.18   0.01  -0.06 ]
 [ 0.14  -0.14  -0.065 -0.06 ]]

In [6]:
z2 = x1.dot(w12) # z2 = [x1].[w12]
print("Y2 =", z2)
a2 = 1/(1+np.exp(-z2)) # a2 = f(z2) where f = sigmoid fn
print("A2 =", a2)

# Layer 3:
print("Layer 3:")
w23 = np.array([-1.01,-1.99,-0.25,-1.64]).T
print("W23 =", w23)

z3 = a2.dot(w23) # z3 = [a2].[w23]
print("Z3 =", z3)
a3 = 1/(1+np.exp(-z3)) # a3 = f(z3) 
yhat = a3
y = 1
print("h(x) =", yhat)
print("e =", (y-yhat))


Y2 = [ 0.1884 -0.5564 -0.0269 -0.2856]
A2 = [ 0.54696118  0.36438084  0.49327541  0.4290814 ]
Layer 3:
W23 = [-1.01 -1.99 -0.25 -1.64]
Z3 = -2.10456100402
h(x) = 0.10865430549
e = 0.89134569451

Part 2

$[W^{ 1,2 }] = \begin{bmatrix} 12.70 -0.20 -0.74 -0.19\\ 1.49 -8.85 7.08 -8.29\\ -19.85 -2.61 -3.59 -2.70\\ \end{bmatrix}$ $\quad [W^{ 2,3 }] = \begin{bmatrix} 7.44\\ 2.78\\ -4.31\\ -2.70 \end{bmatrix}\\$

If $(X_1,X_2) = (2.00,0.26)$ and label $Y=1$, calculate $H(x),e$

In [7]:
# Layer 1:
print("Layer 1:")
x2 = np.array([1,2.00,0.26]) #Inputs
print("X1=", x2)

# Layer 2:
print("Layer 2:")
w2_12 = np.array([[12.70,-0.20,-0.74,-0.19],
       [-1.49,-8.85,7.08,-8.29],
       [-19.85,-2.61,-3.59,-2.70]])
print("W12 =", w2_12)

z2_2 = x2.dot(w2_12) # z2 = [x1].[w12]
print("Y2 =", z2_2)
a2_2 = 1/(1+np.exp(-z2_2)) # a2 = f(z2) where f = sigmoid fn
print("A2 =", a2_2)

# Layer 3:
print("Layer 3")
w2_23 = np.array([7.44,2.78,-4.31,2.70]).T
print("W23 =", w2_23)

z2_3 = a2_2.dot(w2_23) # z3 = [a2].[w23]
print("Z3 =", z2_3)
a2_3 = 1/(1+np.exp(-z2_3)) # a3 = f(z3) 
yhat2 = a2_3
print("h(x) =", yhat2)
print("e =", (y-yhat2))


Layer 1:
X1= [ 1.    2.    0.26]
Layer 2:
W12 = [[ 12.7   -0.2   -0.74  -0.19]
 [ -1.49  -8.85   7.08  -8.29]
 [-19.85  -2.61  -3.59  -2.7 ]]
Y2 = [  4.559  -18.5786  12.4866 -17.472 ]
A2 = [  9.89636011e-01   8.53918721e-09   9.99996223e-01   2.58230063e-08]
Layer 3
W23 = [ 7.44  2.78 -4.31  2.7 ]
Z3 = 3.05290829314
h(x) = 0.954907919892
e = 0.0450920801081