CS231N assignment1——Softmax exercise

Softmax exercise

Complete and hand in this completed worksheet (including its outputs and any supporting code outside of the worksheet) with your assignment submission. For more details see the assignments page on the course website.

This exercise is analogous to the SVM exercise. You will:

  • implement a fully-vectorized loss function for the Softmax classifier
  • implement the fully-vectorized expression for its analytic gradient
  • check your implementation with numerical gradient
  • use a validation set to tune the learning rate and regularization strength
  • optimize the loss function with SGD
  • visualize the final learned weights

关键点分析

  • 损失函数的梯度推导及表示

损失函数的梯度推导及表示

对于softmax,它的想法其实和SVM差不多,同样是多分类器,提供了一个行是特征维度数,列为标签类别数的权重模板矩阵,然后通过样本集矩阵和权重模板矩阵相乘得到样本在各个类别上的分数,最后的类别选择就是按照分数高低来决定。记i是当前样本的下标,y_i是对应样本的目标标签, fyi f y i 是对应样本的分数具体的形式如下:

Li=log(efyijefj)=fyi+log(jefj)=Wyix+log(jeWjx) L i = − l o g ( e f y i ∑ j e f j ) = − f y i + l o g ( ∑ j e f j ) = − W y i ∗ x + l o g ( ∑ j e W j ∗ x )

单从式子看其实会发现这个应该是会比之前的SVM的推导好点,因为没有像max这样的非线性运算在里面需要你去做额外的操作,
LiWj=eWjxxjeWjxLiWj=x+eWjxxjewjxjyijyi { ∂ L i ∂ W j = e W j ∗ x ∗ x ∑ j e W j ∗ x j ≠ y i ∂ L i ∂ W j = − x + e W j ∗ x ∗ x ∑ j e w j ∗ x j ≠ y i

乍一看上面的式子很复杂,但如果你稍微细想一下,你会觉得还好,为什么呢? Wjx W j ∗ x 不就是我们之前要计算损失值时就已经求出来的分数矩阵里了么,如果你希望再简化下操作,那就把这个计算分数矩阵先指数化,然后再归一化的话就是整一个的计算值了,其实就是对应标签的概率,如果你觉得上面 j=yi j = y i 那步看起来不直观还可以稍微整理一下:
LiWj=(1+eWjxjewjx)x ∂ L i ∂ W j = ( − 1 + e W j ∗ x ∑ j e w j ∗ x ) ∗ x

这下看起来就很清晰了,所以整一个的流程最后可以总结如下:

  1. 先用样本集矩阵乘上权重矩阵,计算出一个初始的分数
  2. 将分数矩阵进行对数化并归一化,一来有助于计算损失值,二来在后续的梯度计算过程中较为方便
  3. 梯度求导,把分数矩阵中指定样本的对应标签的分数列加1
  4. 将样本集矩阵和经过处理的新分数矩阵进行相乘得到最后的梯度结果

这里写图片描述

最后补充一句,这里的损失函数是没有包括正则化的结构风险,这个是需要加进去的,写法和之前的SVM那一章的内容一致。

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.
  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!                                                           #
  #############################################################################
  raw_scores = X.dot(W)
  scores = np.exp(raw_scores - np.max(raw_scores, axis=1).reshape((-1,1)))  #这里减去最大值是考虑运算的稳定性,具体参考CS231N的笔记
  scores /= np.sum(scores, axis=1).reshape((-1,1))  #归一化
  target_scores = scores[np.arange(X.shape[0]),y]  #取得目标标签列分数
  loss = np.sum(-np.log(target_scores))/y.shape[0] + 1/2*reg*np.sum(W*W)  #计算损失函数

  coe_matrix = np.array(scores)  #复制归一化后的分数矩阵
  coe_matrix[np.arange(X.shape[0]), y] -=1  #对目标标签列权重修改
  dW = np.transpose(X).dot(coe_matrix)/y.shape[0]+reg*W
  #############################################################################
  #                          END OF YOUR CODE                                 #
  #############################################################################

  return loss, dW
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值