Question: import numpy as np from sklearn import datasets import matplotlib.pyplot as plt from matplotlib.colors import colorConverter, ListedColormap % matplotlib inline class Network: def _ _

import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from matplotlib.colors import colorConverter, ListedColormap
%matplotlib inline
class Network:
def __init__(self, sizes):
"""
Initialize the neural network
:param sizes: a list of the number of neurons in each layer
"""
# save the number of layers in the network
self.L = len(sizes)
# store the list of layer sizes
self.sizes = sizes
# initialize the bias vectors for each hidden and output layer
self.b =[np.random.randn(n,1) for n in self.sizes[1:]]
# initialize the matrices of weights for each hidden and output layer
self.W =[np.random.randn(n, m) for m, n in zip(self.sizes[:-1], self.sizes[1:])]
# initialize the derivatives of biases for backprop
self.db =[np.zeros((n,1)) for n in self.sizes[1:]]
# initialize the derivatives of weights for backprop
self.dW =[np.zeros((n, m)) for m, n in zip(self.sizes[:-1], self.sizes[1:])]
# initialize the activities on each hidden and output layer
self.z =[np.zeros((n,1)) for n in self.sizes]
# initialize the activations on each hidden and output layer
self.a =[np.zeros((n,1)) for n in self.sizes]
# Initialize delta (assuming 3 layers)
self.delta =[np.zeros((n,1)) for n in self.sizes[1:]]
def g(self, z):
"""
sigmoid activation function
:param z: vector of activities to apply activation to
"""
return 1.0/(1.0+ np.exp(-z))
def g_prime(self, z):
"""
derivative of sigmoid activation function
:param z: vector of activities to apply derivative of activation to
"""
return self.g(z)*(1.0- self.g(z))
def grad_loss(self, a, y):
"""
evaluate gradient of cost function for squared-loss C(a,y)=(a-y)^2/2
:param a: activations on output layer
:param y: vector-encoded label
"""
return (a - y)
def forward_prop(self, x):
"""
take an feature vector and propagate through network
:param x: input feature vector
"""
if len(x.shape)==1:
x = x.reshape(-1,1)
# TODO: step 1. Initialize activation on initial layer to x
# self.act=g(self.z)
# self.Z=np.dot(self.act,self.W[0])
# self.yhat=g(self.Z)
# your code here check the logic self.a[l-1]
self.a[0]= x
# Step 2-4: Loop over layers and compute activities and activations
for layer in range(self.L-1):
self.z[layer]= np.dot(self.W[layer], self.a[layer -1])+ self.b[layer]
self.a[layer]= self.g(self.z[layer])
# Return the final prediction
return self.a[-1]
## TODO: step 2-4. Loop over layers and compute activities and activations
## Use Sigmoid activation function defined above
# your code here
def back_prop(self, x, y):
"""
Back propagation to get derivatives of cost function wrt weights and biases for a given training example
:param x: training features
:param y: vector-encoded label
"""
# Ensure input and output have correct shape
if len(x.shape)==1:
x = x.reshape(-1,1)
if len(y.shape)==1:
y = y.reshape(-1,1)
# Forward propagation to fill in activities and activations
self.forward_prop(x)
# Calculate delta for the output layer
self.delta[-1]= self.grad_loss(self.a[-1], y)* self.g_prime(self.z[-1])
# Backward pass through the network
for l in reversed(range(1, self.L)):
self.delta[l -1]= np.dot(self.W[l].T, self.delta[l])* self.g_prime(self.z[l -1])
# Compute gradients for weights and biases
for l in range(self.L -1):
if l ==0:
self.dW[l]= np.dot(self.delta[l], x.T)
else:
self.dW[l]= np.dot(self.delta[l], self.a[l -1].T)
self.db[l]= self.delta[l]
the above code gives the error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
in
1 # test back_prop
2 nn = Network([2,3,2])
---->3 nn.back_prop(X_train[0,:], y_train[0,:])
4 print(nn.W[0])
in back_prop(self, x, y)
115 # Backward pass through the network
116 for l in reversed(range(1, self.L)):
-->117 self.delta[l -1]= np.dot(self.W[l].T, self.delta[l])* self.g_prime(self.z[l -1])
118
119 # Compute gradients for weights and biases
IndexError: list index out o

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!