cs231n作业一之实现softmax
2017-07-23 22:34
288 查看
这是我看到的人家写的关于soft max和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 是和 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
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
相关文章推荐
- cs231n作业一之 在cifar-10上实现KNN
- cs231n作业一之实现SVM
- CS231n作业笔记1.5:Softmax的误差以及梯度计算
- CS231n作业笔记2.5:dropout的实现与应用
- CS231n作业笔记2.2:多层神经网络的实现
- CS231n 2016 通关 第三章-Softmax 作业
- CS231n作业笔记2.4:Batchnorm的实现与使用
- 关于cs231n中作业1的SVM和Softmax线性分类器实现的感悟
- CS231n作业笔记2.1:两层全连接神经网络的分层实现
- CS231n-Assignment1(作业1)-softmax
- cs231n作业1--softmax
- cs231n课程作业assignment1(Softmax)
- CS231n作业笔记2.6:卷积层以及池化层的实现
- JSP+Servlet培训班作业管理系统[1]-登录功能的简单实现
- 作业四-4、输入x、n后输出下列算式的值,fact(n)实现n!,mypow(x,n)实现x^n。
- 梯度下降法实现softmax回归MATLAB程序
- Spring联姻Quartz实现作业调度
- 纯作业代码——Java实现接口、继承
- 『cs231n』作业2选讲_通过代码理解卷积层&池化层
- 機器學習基石(Machine Learning Foundations) 机器学习基石 作业四 Q13-20 MATLAB实现