首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么斯坦福大学的cs231n支持向量机中的点产品是反向的?

为什么斯坦福大学的cs231n支持向量机中的点产品是反向的?
EN

Stack Overflow用户
提问于 2018-11-10 03:29:48
回答 1查看 113关注 0票数 2

我正在观看斯坦福大学cs231n的Youtube视频,并试图把作业作为练习来做。在执行SVM时,我遇到了以下代码:

代码语言:javascript
复制
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

这是我遇到麻烦的台词:

代码语言:javascript
复制
scores = X[i].dot(W) 

这是在做xW产品,是不是应该是Wx?我的意思是W.dot(X[i])

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-10 18:57:19

由于数组形状分别为(D, C)(N, D) ( WX ),所以如果不首先将它们转换,就不能直接取点积(矩阵乘法必须是(C, D)·(D, N) )。

由于X.T.dot(W.T) == W.dot(X),实现只是反转点积的顺序,而不是接受每个数组的转换。实际上,这只是取决于如何安排输入的决定。在这种情况下,(有些武断的)决定以更直观的方式安排样本和特性,而不是将点积作为x·W

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53235775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档