【问题标题】:CS231n: How to calculate gradient for Softmax loss function?CS231n:如何计算 Softmax 损失函数的梯度?
【发布时间】:2017-05-30 13:16:05
【问题描述】:

我正在观看 Stanford CS231: Convolutional Neural Networks for Visual Recognition 的一些视频,但不太了解如何使用 numpy 计算 softmax 损失函数的分析梯度。

来自this stackexchange的回答,softmax梯度计算为:

上面的 Python 实现是:

num_classes = W.shape[0]
num_train = X.shape[1]
for i in range(num_train):
  for j in range(num_classes):
    p = np.exp(f_i[j])/sum_i
    dW[j, :] += (p-(j == y[i])) * X[:, i]

谁能解释一下上面的 sn-p 是如何工作的?下面还包括softmax的详细实现。

def softmax_loss_naive(W, X, y, reg):
  """
  Softmax loss function, naive implementation (with loops)
  Inputs:
  - W: C x D array of weights
  - X: D x N array of data. Data are D-dimensional columns
  - y: 1-dimensional array of length N with labels 0...K-1, for K classes
  - reg: (float) regularization strength
  Returns:
  a tuple of:
  - loss as single float
  - gradient with respect to weights W, an array of same size as W
  """
  # Initialize the loss and gradient to zero.
  loss = 0.0
  dW = np.zeros_like(W)

  #############################################################################
  # Compute the softmax loss and its gradient using explicit loops.           #
  # Store the loss in loss and the gradient in dW. If you are not careful     #
  # here, it is easy to run into numeric instability. Don't forget the        #
  # regularization!                                                           #
  #############################################################################

  # Get shapes
  num_classes = W.shape[0]
  num_train = X.shape[1]

  for i in range(num_train):
    # Compute vector of scores
    f_i = W.dot(X[:, i]) # in R^{num_classes}

    # Normalization trick to avoid numerical instability, per http://cs231n.github.io/linear-classify/#softmax
    log_c = np.max(f_i)
    f_i -= log_c

    # Compute loss (and add to it, divided later)
    # L_i = - f(x_i)_{y_i} + log \sum_j e^{f(x_i)_j}
    sum_i = 0.0
    for f_i_j in f_i:
      sum_i += np.exp(f_i_j)
    loss += -f_i[y[i]] + np.log(sum_i)

    # Compute gradient
    # dw_j = 1/num_train * \sum_i[x_i * (p(y_i = j)-Ind{y_i = j} )]
    # Here we are computing the contribution to the inner sum for a given i.
    for j in range(num_classes):
      p = np.exp(f_i[j])/sum_i
      dW[j, :] += (p-(j == y[i])) * X[:, i]

  # Compute average
  loss /= num_train
  dW /= num_train

  # Regularization
  loss += 0.5 * reg * np.sum(W * W)
  dW += reg*W

  return loss, dW

【问题讨论】:

    标签: python numpy softmax


    【解决方案1】:

    不确定这是否有帮助,但是:

    确实是指标函数,如here 所述。这在代码中形成了表达式(j == y[i])

    另外,损失相对于权重的梯度是:

    在哪里

    这是代码中X[:,i]的来源。

    【讨论】:

    • 感谢您指出这一点。我一开始没看到。在关于 stackexchange 的问题中,它们隐含地表示 yj 用于指标函数
    • 并且,梯度中第一项(dL/df)的值为:y_pred-y。
    【解决方案2】:

    我知道这已经晚了,但这是我的答案:

    我假设您熟悉 cs231n Softmax 损失函数。 我们知道:

    就像我们对 SVM 损失函数所做的那样,梯度如下:

    希望有所帮助。

    【讨论】:

      【解决方案3】:

      supplement to this answer 有一个小例子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-01
        • 2020-03-16
        • 2023-02-26
        • 1970-01-01
        • 2018-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多