In [1]:
import numpy as np
from cs231n.classifiers.linear_svm import svm_loss_naive
W = np.array([[2,1,7],[1,1,0]])
print('W=',W)
X = np.array([[1,2],[7,11],[3,5]])
print('X.dot(W)=',X.dot(W))
y = np.array([1,1,0])
In [3]:
num_train = y.shape[0]
# N * C
scores_all = X.dot(W)
# N * 1, value of each row = the correct class score w.r.t corresponding input x_i
correct_class_scores_all = scores_all[range(num_train),y]
# raw loss matrix of shape (N, C)
loss_matrix = scores_all - correct_class_scores_all[:, None] + 1
# set loss value of correct class to 0
loss_matrix[range(num_train),y] = 0
loss_matrix[loss_matrix<.0] = 0 # zero out negative values
loss_matrix
Out[3]:
In [4]:
dW = np.zeros(W.shape) # initialize the gradient as zero
# compute the loss and the gradient
num_classes = W.shape[1]
num_train = X.shape[0]
for i in xrange(num_train):
scores = X[i].dot(W)
correct_class_score = scores[y[i]]
for j in xrange(num_classes):
if j == y[i]:
continue
margin = scores[j] - correct_class_score + 1 # note delta = 1
if margin > 0:
dW[:, j] += X[i]
dW[:, y[i]] -= X[i]
dW
Out[4]:
In [ ]:
In [9]:
dW = np.zeros(W.shape)
positive_loss_index = np.argwhere(loss_matrix>0)
print(positive_loss_index)
%debug
print(X[positive_loss_index[:,0],:].T)
%debug
dW[:,positive_loss_index[:,1]] += X[positive_loss_index[:,0],:].T
print(dW)
%debug
loss_row_count = np.sum(loss_matrix>0, axis=1)
dW[:,y] -= (loss_row_count[:,None] * X).T
#print(dW[:,y_dev].shape)
#print((loss_row_count[:,None] * X_dev).T.shape)
dW
Out[9]:
In [ ]:
In [ ]: