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.
num_train = X.shape[0]
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 = X.dot(W)
scores -= np.max(scores, axis=1, keepdims=True)
# print scores.shape
pscores = np.exp(scores)
pscores_norm = pscores/np.sum(pscores, axis=1, keepdims=True)
loss = np.sum(-scores[xrange(num_train),y] + np.log(np.sum(pscores, axis=1)))
pscores_norm[xrange(num_train),y] -= 1
dW = X.T.dot(pscores_norm)
loss /= num_train
loss += 0.5*reg*np.sum(W*W)
dW /= num_train
dW += reg * W
#############################################################################
# END OF YOUR CODE #
#############################################################################
return loss, dW
评论列表
文章目录