-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoftmax.py
executable file
·89 lines (72 loc) · 3.14 KB
/
softmax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from builtins import range
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)
n = X.shape[0]
classes = W.shape[1]
#############################################################################
# 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! #
#############################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
scores = np.dot(X, W)
scores -= np.max(scores, axis=1)[:, None]
for i in range(n):
loss += -scores[i][y[i]] + np.log(np.exp(scores[i]).sum())
dW += X[i][:, None] * np.exp(scores[i]) / np.exp(scores[i]).sum()
dW[:, y[i]] -= X[i]
loss /= n
dW /= n
loss += reg * np.sum(W ** 2)
dW += reg * 2 * W
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
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)
n = X.shape[0]
#############################################################################
# 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! #
#############################################################################
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
scores = np.dot(X, W)
scores -= np.max(scores, axis=1)[:, None]
loss = (-scores[range(n), y] + np.log(np.sum(np.exp(scores), axis=1))).sum()
coef = np.exp(scores) / np.sum(np.exp(scores), axis=1)[:, None]
coef[np.arange(n), y] -= 1
dW = np.dot(X.T, coef)
loss /= n
dW /= n
loss += reg * np.sum(W*W)
# print('reg', W.shape, (W ** 2)[-1])
dW += reg * 2 * W
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
return loss, dW