Question: def svm _ loss ( X , y , Weight,reg _ para,batch _ size,num _ classes ) : # Here you are required to calculate
def svmlossXyWeight,regpara,batchsize,numclasses:
# Here you are required to calculate the hinge loss with L regularization, and the gradient dW for updating the weight
margindelta # define the delta to determine the margin softness
loss #initialize the loss with value
dW npzerosWeightshapeastypefloat # initialize the gradient as zero
# implement your code below ~ lines
scores npdotXWeight # predict the class score to generate a number of sample, number of class tensor
correctclassscores scoresnparangenumtrain y # find the predict score of the correct class return a number of sample, tensor
correctclassscores npreshapecorrectclassscores,batchsize, # reshape the predct score into number of sample, tensor for matrix propagation
margin scores correctclassscores margindelta # calculate the margin
marginsnparangenumtrain y
marginsmargins
marginsmargins
loss #calculate loss with regularization part lines
rowsum # sum the margin per data point by N
marginsnparangenumtrain y
dW
# your code ends here
return loss, dW # return the loss and gradient
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
