from cs231n.classifiers.linear_svm import svm_loss_naive
import time
# generate a random SVM weight matrix of small numbers
W = np.random.randn(3073,10)*0.0001#W生成了一个随机的W权重矩阵是 3073 X 10的矩阵#X_dev是一个500 X 3073的输入向量#y_dev是500X1的答案
loss, grad = svm_loss_naive(W, X_dev, y_dev,0.000005)print('loss: %f'%(loss,))
defsvm_loss_naive(W, X, y, reg):"""
Structured SVM loss function, naive implementation (with loops).
Inputs have dimension D, there are C classes, and we operate on minibatches
of N examples.
Inputs:
- W: A numpy array of shape (D, C) containing weights.
- X: A numpy array of shape (N, D) containing a minibatch of data.
- y: A numpy array of shape (N,) containing training labels; y[i] = c means
that X[i] has label c, where 0 <= c < C.
- reg: (float) regularization strength
Returns a tuple of:
- loss as single float
- gradient with respect to weights W; an array of same shape as W
"""#dW是3073X10的0矩阵
dW = np.zeros(W.shape)# initialize the gradient as zero# compute the loss and the gradient
num_classes = W.shape[1]#10
num_train = X.shape[0]#500
loss =0.0