作业地址:https://github.com/donghaiyu233/cs231n,欢迎fork~。
先看看作业要求:
其实跟SVM相比较,依旧时线性模型只是变化了loss function,我们不再使用hinge loss而是使用交叉熵cross_entropy Loss:
,
然后是导数:
Softmax有个好处是得到的结果为该类别的概率,而在SVM下,我们只知道得分最高的为预测的结果,分数的大小没有明确的物理意义。
实在比较懒,搬运下原理写的比较好的:SVM和Softmax的原理区别对比
课程作业
softmax.py
与SVM基本相似,注意替换Loss与dW即可。
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! #
#############################################################################
num_train = X.shape[0]
num_classes = W.shape[1]
for i in range(num_train):
scores = np.dot(X[i], W)
#scores:(1,C)
correct_scores = scores[y[i]]
exp_sum = np.sum(np.exp(scores))
loss += np.log(exp_sum) - correct_scores
dW[:,y[i]] += -X[i]
for j in range(num_classes):
dW[:,j] += (np.exp(scores[j]) / exp_sum) * X[i]
loss /= num_train
dW /= num_train
#增加正则项
loss += 0.5 * reg * np.sum(W * W)
dW += reg * W
#############################################################################
# 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! #
#############################################################################
scores = np.dot(X, W)
num_train = X.shape[0]
rows = range(num_train)
correct_class_score = scores[rows,y]
correct_class_score = np.reshape(correct_class_score,[num_train,1])
exp_sum = np.sum(np.exp(scores), axis = 1).reshape(num_train,1)
loss += np.sum(np.log(exp_sum) - correct_class_score)
p = np.exp(scores) / exp_sum
p[rows, y] += -1
dW = X.T.dot(p)
loss /= num_train
dW /= num_train
#正则项
loss += 0.5 * reg * np.sum(W * W)
dW += reg * W
#############################################################################
# END OF YOUR CODE #
#############################################################################
return loss, dW
ipython中需要填写的部分与SVM相同,只需要修改实例化的一行,其余都一样。