Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Machine learning question, use jupyter lab, no AI plz: Import this: import sys from packaging import version import sklearn import matplotlib.pyplot as plt import numpy
Machine learning question, use jupyter lab, no AI plz:
Import this:
import sys
from packaging import version
import sklearn
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import adddummyfeature
from sklearn.datasets import makeblobs
from sklearn import metrics #Import scikitlearn metrics module for accuracy calculation
from sklearn.modelselection import traintestsplit
from sklearn.metrics import confusionmatrix, ConfusionMatrixDisplay
printSklearn package",sysversioninfo
printSklearn package",sklearn.version
printTensorFlow version:", tfversion
assert sysversioninfo
assert version.parsesklearnversion version.parse
pltrcfont size
pltrcaxes labelsize titlesize
pltrclegend fontsize
pltrcxtick labelsize
pltrcytick labelsize
Questions:
Q Constructing three layer neural network for binary classification.
Modify the two layer network for binary classification from the tutorial. The new code should have two hidden layers.
def sigmoidx:
return npexpx
#Calculating the loss function.
def crossentropypreds labels:
return npsumlabels nplogpreds labels nplog preds
#return npmeanlabels nplogpreds labels nplog preds
#Write your code here
class ThreeLayersBinaryClassifier:
def initself inputsize, hiddensize, hiddensize, outputsize, lre:
Initialize the model. Weights are initialized to random values.
w: First layer weights; has shape inputsize, hiddensize
w: Second layer weights; has shape hiddensize, outputsize
Inputs:
inputsize: The dimension D of the input data.
hiddensize: The number of neurons in the first hidden layer.
hiddensize: The number of neurons in the second hidden layer.
outputsize: The number of classes C
# reset seed before start
nprandom.seed
#initialize self.w self.w selfw
#Give X a matrix with N by D dimensions
#Calculate ypred, N by
def predictself X:
#calculate z from X and W and calculate h from sigmoidz
#calculate z from h and W and calculate h from sigmoidz
#calculate z from h and W and calculate ypred from sigmoidz
ypred None
returnypred
def forwardbackwardself X y:
loss
gradw None
gradw None
gradw None
#
returnloss gradw gradw gradw
def trainself Xtrain, ytrain, learningratee epochs:
losshistory
for iter in rangeepochs:
printiter
#get loss and gradgrad grad from the forwardbackward method
#update self.wself.wself.w
#losshistory.appendloss
return losshistory
#Testing the three layers network
N Din H H Dout
nprandom.seed # to make this code example reproducible
classcenters nparray
X y makeblobsnsamples N centers classcenters, nfeatures Dinrandomstate
y yreshapeN
printXshape Xshape, yshapeyshape
pltscatterX: X: cy
Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize shuffleFalse
epochs
learningrate
printDin H H Dout, learningrate
clf ThreeLayersBinaryClassifierDin H H Dout, learningrate
losspath clftrainXtrain,ytrain, learningrate, epochs
printThree Layers Binary Classification"
printFinal Loss",losspath
#Print Final Prediction
ypred clfpredictXtest #It has the updated W W and W
ypredypred
ypredypred
printTesting Accuracy:", metrics.accuracyscoreytest: ypred:
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