cs231n作业1--softmax

本文探讨了softmax分类器与SVM的区别,它通过指数归一化线性映射来计算损失,采用交叉熵损失函数。详细介绍了softmax损失函数的公式,并提供了梯度计算的推导链接。此外,还分享了softmax分类器的代码实现,包括softmax.py和softmax.ipynb两个文件。

1、思想:
与SVM不同,SVM直接利用线性映射的结果进行计算损失值,而softmax需要对线性映射得到的值进行指数归一化,然后在进行损失值计算。在SVM损失函数中使用的是折叶函数,而在softmax中使用的交叉熵函数。
2、损失函数公式:
这里写图片描述 每个测试样本的损失值计算
这里写图片描述所有测试样本的损失值计算

3、梯度公式:
http://www.jianshu.com/p/8eb17fa41164#给出了很详细的推导过程

4、代码实现:
softmax.py

import numpy as np
from random import shuffle
#from past.builtins import xrange

def softmax_loss_naive(W, X, y, reg):
  """
  Softmax 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
  """
  # Initialize the loss and gradient to zero.
  loss = 0.0
  dW = np.zeros_like(W)

  #############################################################################
  # TODO: 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!                                                           #
  #################################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值