Python 为什么dot产品在斯坦福落后';s cs231n支持向量机?
我正在看斯坦福大学cs231n的Youtube视频,并试图做练习。在执行SVM one时,我遇到了以下代码:Python 为什么dot产品在斯坦福落后';s cs231n支持向量机?,python,math,machine-learning,deep-learning,svm,Python,Math,Machine Learning,Deep Learning,Svm,我正在看斯坦福大学cs231n的Youtube视频,并试图做练习。在执行SVM one时,我遇到了以下代码: 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 ex
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
这是在做产品xW,不是应该是Wx吗?我的意思是,
W.dot(X[I])
因为数组的形状分别是(D,C)
和(N,D)
对于W
和X
,你不能直接取点积,而不首先将它们都转置(对于矩阵乘法,它们必须是(C,D)·(D,N)
由于
X.T.dot(W.T)=W.dot(X)
,实现简单地颠倒了点积的顺序,而不是对每个数组进行变换。实际上,这取决于输入的排列方式。在这种情况下(有些随意)我们决定以更直观的方式安排样本和特征,而不是将dot产品作为x·W
很好的解释,尽管我认为应该是xᵀ.点(Wᵀ) == [W.dot(X)]ᵀ代码>。
scores = X[i].dot(W)