【发布时间】:2019-04-13 14:47:40
【问题描述】:
我正在观看斯坦福大学 cs231n 的 Youtube 视频,并尝试将作业作为练习来完成。在执行 SVM 时,我遇到了以下代码:
def svm_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 = 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]
loss = 0.0
for i in range(num_train):
scores = X[i].dot(W) # This line
correct_class_score = scores[y[i]]
for j in range(num_classes):
if j == y[i]:
continue
margin = scores[j] - correct_class_score + 1 # note delta = 1
if margin > 0:
loss += margin
这是我遇到问题的线路:
scores = X[i].dot(W)
这是在做乘积xW,不应该是Wx吗?我的意思是W.dot(X[i])
【问题讨论】:
标签: python math machine-learning deep-learning svm