Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this project, we will be developing a basic neural network from the ground up to classify various types of fashion items. The primary objective
In this project, we will be developing a basic neural network from the ground up to classify various types of fashion items. The primary objective of this project is to gain a comprehensive understanding of neural network architecture, including its theory and implementation details.
#initializing
# Notice that you don't need any other packages for this midterm
import numpy as np
import pandas as pd
import random
from matplotlib import pyplot as plt
random.seed # NEVER change this line
# Reading the dataset
data pdreadcsvfashiondata.csv
# The data preprocessing is done for you. Please do NOT edit the cell
# However, you should understand what these codes are doing
data nparraydata
m n data.shape
nprandom.shuffledata # shuffle before splitting into dev and training sets
datadev data:T
Ydev datadev
Xdev datadev:n
Xdev Xdev
datatrain data:mT
Ytrain datatrain
Xtrain datatrain:n
Xtrain Xtrain
mtrain Xtrain.shape
PART building NN
#initializing parameters
# Initialize the parameters in the neural network
# Based on the figure above, we need the weight and bias matrices.
# W b are the matrices for the first layer
# W b are the matrices for the second layer
# You should think about the sizes of the matrices
# then initialize elements in the matrix to be random numbers between to
def initparams:
W # Your code here
b # Your code here
W # Your code here
b # Your code here
return W b W b
# As a starting point, you only need a ReLu function, its derivative, and the softmax function
def ReLUZ:
# Your code here
def ReLUderivZ:
# Your code here
def softmaxZ:
# Your code here
return A
# In the forward propagation function, X is the inputs the image in vector form and we pass all the weights and biases
def forwardpropW b W b X:
Z # Your code here
A # Your code here
Z # Your code here
A # Your code here
return Z A Z A
#backward propagation
# This one hot function is to convert a numeric number into a onehot vector
def onehotY:
# Your code here
return onehotY
# Now performing the backward propagation
# Each function is only one line, but lots of Calculus behind
def backwardpropZ A Z A W W X Y:
onehotY onehotY
dZ # Your code here
dW # Your code here
db # Your code here
dZ # Your code here
dW # Your code here
db # Your code here
return dW db dW db
# Finally, we are ready to update the parameters
def updateparamsW b W b dW db dW db alpha:
W # Your code here
b # Your code here
W # Your code here
b # Your code here
return W b W b
#gradient descent
# Implement the helper function. We need to convert the softmax output into a numeric label
# This is done through getpredictions function
def getpredictionsA:
# Your code here
# We also want to have a simple function to compute the accuracy. Notice that "predictions" and Y are the same shape
def getaccuracypredictions Y:
return # Your code here
# Finally, we are ready to implement gradient descent
def gradientdescentX Y alpha, iterations:
W b W b # Your code here using the function you have implemented
for i in rangeiterations:
Z A Z A # Your code here using the function you have implemented
dW db dW db # Your code here using the function you have implemented
W b W b # Your code here using the function you have implemented
if i :
printIteration: i
predictions getpredictionsA
printgetaccuracypredictions Y
return W b W b
#validation set
def makepredictionsX W b W b:
A forwardpropW b W b X
predictions getpredictionsA
return predictions
devpredictions makepredictionsXdev, W b W b
getaccuracydevpredictions, Ydev
#exploring some samples
def testpredictionindex W b W b:
currentimage Xtrain: index, None
prediction makepredictionsXtrain: index, None W b W b
label Ytrainindex
printPrediction: prediction
printLabel: label
currentimage currentimage.reshape
pltgray
pltimshowcurrentimage, interpolation'nearest'
pltshow
testprediction W b W b
testprediction W b W b
Part : Error Analysis and Performance Improvements
You now will try to improve the model performance through, for example, different activation functions, learning rate cahnges, expanding the network complexity, regularization, and dropouts. Note solve Part in detail with reasons why model did or did not improve
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started