您的位置:首页 > 其它

cs231n作业一之实现softmax

2017-07-23 22:34 288 查看
这是我看到的人家写的关于soft max和svm的区别,写的挺好的就先借用了


SVM

支持向量机和后面的 Softmax 分类器都是线性模型,这里只是损失函数不同罢了。阅读文档 Linear classification: Support Vector Machine, Softmax 大概就知道什么线性模型了。

对于 SVM 而言,分类器输入样本 xi 后,对第 j 个
类别的评分是:

sj=f(xi,W)j
如果是 Multiclass SVM loss,具体对第 i 个样本的损失为:

Li=∑j≠yimax(0,sj−syi+Δ)
当然,损失应该还包括正则化项,比较简单就略过了。

反向传播就比较麻烦一点,因为要用随机梯度下降的迭代方法,所以要计算梯度。读完文档 Optimization: Stochastic Gradient Descent,可以找到 SVM 的梯度是:

⎧⎩⎨⎪⎪⎪⎪⎪⎪∇wyiLi=∇wjLi=−⎛⎝∑j≠yi1(wTjxi−wTyixi+Δ>0)⎞⎠xi1(wTjxi−wTyixi+Δ>0)xij=yij≠yi

具体需要完成 svm.ipynb 中的代码,具体的理论推导得出的梯度计算是否正确,会有 gradient check 的数值计算方式来检查。


Softmax

Softmax 是和 SVM 一起对比介绍的,只是把 hinge Loss 换成 cross-entropy Loss,即交叉熵损失而已,如下:

Li=−log⎛⎝efyi∑jefj⎞⎠or
equivalentlyLi=−fyi+log∑jefj

而要算梯度就麻烦一点,课程文档里也没有给公式,我推了一下写在下边,如果有错误请读者批评指正,

⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪∇wyiLi=∇wjLi=−xi+efyi∑jefjxiefj∑jefjxij=yij≠yi

在 Putting it together: Minimal Neural Network Case Study 里,构造了一个二维的数据集,用 Softmax 和 2-layer Neural Network 来做实验。完成 softmax.ipynb 的时候,计算
loss 和梯度都可以参考这篇笔记。

代码是官方的代码,没有在自己的编译器熵运行,如果是找可以运行的代码的同学请忽视了。

import numpy as np
from random import shuffle

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
"""
# 将损失和梯度初始化为零。
loss = 0.0
dW = np.zeros_like(W)

#############################################################################
# TODO:使用显式循环计算softmax损失及其梯度。
# 将损失和梯度存储在dW中 如果你不细心 #
# 在这里,很容易遇到数字不稳定性。不要忘记正则化! f=w*x+b #
#############################################################################
num_classes = W.shape[1]#矩阵
num_train = X.shape[0]

for i in xrange(num_train):
scores = X[i].dot(W)
shift_scores = scores - max(scores)
loss_i = - shift_scores[y[i]] + np.log(sum(np.exp(shift_scores)))
loss += loss_i
for j in xrange(num_classes):
softmax_output = np.exp(shift_scores[j])/sum(np.exp(shift_scores))
if j == y[i]:
dW[:,j] += (-1 + softmax_output) *X[i]
else:
dW[:,j] += softmax_output *X[i]

loss /= num_train
loss += 0.5* reg * np.sum(W * W)
dW = dW/num_train + reg* W
#pass
#############################################################################
# END OF YOUR CODE #
#############################################################################

return loss, dW

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! #
#############################################################################
num_classes = W.shape[1]
num_train = X.shape[0]
scores = X.dot(W)
shift_scores = scores - np.max(scores, axis = 1).reshape(-1,1)
softmax_output = np.exp(shift_scores)/np.sum(np.exp(shift_scores), axis = 1).reshape(-1,1)
loss = -np.sum(np.log(softmax_output[range(num_train), list(y)]))
loss /= num_train #平均损失
loss += 0.5* reg * np.sum(W * W)#正则化处理

dS = softmax_output.copy()
dS[range(num_train), list(y)] += -1
dW = (X.T).dot(dS)
dW = dW/num_train + reg* W
#pass
#############################################################################
# END OF YOUR CODE #
#############################################################################

return loss, dW
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: