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])


('W=', array([[2, 1, 7],
       [1, 1, 0]]))
('X.dot(W)=', array([[ 4,  3,  7],
       [25, 18, 49],
       [11,  8, 21]]))

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]:
array([[ 2,  0,  5],
       [ 8,  0, 32],
       [ 0,  0, 11]])

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]:
array([[  5., -16.,  11.],
       [  8., -26.,  18.]])

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


[[0 0]
 [0 2]
 [1 0]
 [1 2]
 [2 2]]
> /home/Administrator/assignment1/.env/local/lib/python2.7/site-packages/IPython/core/compilerop.py(100)ast_parse()
     99         and are passed to the built-in compile function."""
--> 100         return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
    101 

ipdb> c
[[  7.   0.   3.]
 [ 11.   0.   5.]]
> /home/Administrator/assignment1/.env/local/lib/python2.7/site-packages/IPython/core/compilerop.py(100)ast_parse()
     99         and are passed to the built-in compile function."""
--> 100         return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
    101 


KeyboardInterrupt
Out[9]:
array([[  4., -14.,   3.],
       [  6., -22.,   5.]])

In [ ]:


In [ ]: