dx_error:1.58523685005e‐10
dwerror:3.68009862446e‐10
dberror:1.04868325611e‐10
defsvm_loss(x,y):
''':paramx:(N,C),x[i,j]表示第i个输入是第j个的分数:paramy:(N,),
x[i]的标签:return:loss,dx'''
N=x.shape[0]
correct_class_scores=x[np.arange(N),y]
margins=np.maximum(0,x‐correct_class_scores[:,np.newaxis]+1.0)
margins[np.arange(N),y]=0
loss=np.sum(margins)/N
num_pos=np.sum(margins>0,axis=1)
dx=np.zeros_like(x)
dx[margins>0]=1
dx[np.arange(N),y]‐=num_pos
dx/=N
returnloss,dx
defsoftmax_loss(x,y):
probs=np.exp(x‐np.max(x,axis=1,keepdims=True))
probs/=np.sum(probs,axis=1,keepdims=True)
N=x.shape[0]
loss=‐np.sum(np.log(probs[np.arange(N),y]))/N
dx=probs.copy()
dx[np.arange(N),y]‐=1
dx/=N
returnloss,dx
num_classes,num_inputs=10,50
x=0.001*np.random.randn(num_inputs,num_classes)
y=np.random.randint(num_classes,size=num_inputs)