softmax.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:ML_algorithm 作者: luoshao23 项目源码 文件源码
def softmax_loss_vectorized(W, X, y, reg):
  """
  Softmax loss function, vectorized version.

  Inputs and outputs are the same as softmax_loss_naive.
  """
  # Initialize the loss and gradient to zero.
  num_train = X.shape[0]
  loss = 0.0
  dW = np.zeros_like(W)

  #############################################################################
  # TODO: Compute the softmax loss and its gradient using no 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!                                                           #
  #############################################################################
  scores = X.dot(W)
  scores -= np.max(scores, axis=1, keepdims=True)
  # print scores.shape
  pscores = np.exp(scores)
  pscores_norm = pscores/np.sum(pscores, axis=1, keepdims=True)
  loss = np.sum(-scores[xrange(num_train),y] + np.log(np.sum(pscores, axis=1)))

  pscores_norm[xrange(num_train),y] -= 1
  dW = X.T.dot(pscores_norm)

  loss /= num_train
  loss += 0.5*reg*np.sum(W*W)

  dW /= num_train
  dW += reg * W

  #############################################################################
  #                          END OF YOUR CODE                                 #
  #############################################################################

  return loss, dW
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号