-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftmax.py
More file actions
160 lines (142 loc) · 7.33 KB
/
softmax.py
File metadata and controls
160 lines (142 loc) · 7.33 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import numpy as np
class Softmax (object):
"""" Softmax classifier """
def __init__ (self, inputDim, outputDim):
self.W = None
#########################################################################
# TODO: 5 points #
# - Generate a random softmax weight matrix to use to compute loss. #
# with standard normal distribution and Standard deviation = 0.01. #
#########################################################################
sigma =0.01
self.W = sigma * np.random.randn(inputDim,outputDim)
#########################################################################
# END OF YOUR CODE #
#########################################################################
def calLoss (self, x, y, reg):
"""
Softmax loss function
D: Input dimension.
C: Number of Classes.
N: Number of example.
Inputs:
- x: A numpy array of shape (batchSize, D).
- y: A numpy array of shape (N,) where value < C.
- reg: (float) regularization strength.
Returns a tuple of:
- loss as single float.
- gradient with respect to weights self.W (dW) with the same shape of self.W.
"""
loss = 0.0
dW = np.zeros_like(self.W)
#############################################################################
# TODO: 20 points #
# - Compute the softmax loss and store to loss variable. #
# - Compute gradient and store to dW variable. #
# - Use L2 regularization #
# Bonus: #
# - +2 points if done without loop #
#############################################################################
#Calculating loss for softmax
#calculate the score matrix
N = x.shape[0]
s =x.dot(self.W)
# calculating s-max(s)
s_ = s-np.max(s, axis=1, keepdims= True)
exp_s_ = np.exp(s_)
# calculating base
sum_f = np.sum(exp_s_, axis=1, keepdims=True)
# calculating probability of incorrect label by dividing by base
p = exp_s_/sum_f
p_yi= p[np.arange(N),y]
# Calculating loss by applying log over the probability
loss_i = - np.log(p_yi)
#keep as column vector
#TODO: add regularization
loss = np.sum(loss_i)/N
loss += reg * np.sum(self.W*self.W)
ds = p.copy()
ds[np.arange(x.shape[0]),y] += -1
dW = (x.T).dot(ds)/N
dW = dW + (2* reg* self.W)
#############################################################################
# END OF YOUR CODE #
#############################################################################
return loss, dW
def train (self, x, y, lr=1e-3, reg=1e-5, iter=100, batchSize=200, verbose=False):
"""
Train this Softmax classifier using stochastic gradient descent.
D: Input dimension.
C: Number of Classes.
N: Number of example.
Inputs:
- x: training data of shape (N, D)
- y: output data of shape (N, ) where value < C
- lr: (float) learning rate for optimization.
- reg: (float) regularization strength.
- iter: (integer) total number of iterations.
- batchSize: (integer) number of example in each batch running.
- verbose: (boolean) Print log of loss and training accuracy.
Outputs:
A list containing the value of the loss function at each training iteration.
"""
# Run stochastic gradient descent to optimize W.
lossHistory = []
for i in range(iter):
xBatch = None
yBatch = None
#########################################################################
# TODO: 10 points #
# - Sample batchSize from training data and save to xBatch and yBatch #
# - After sampling xBatch should have shape (D, batchSize) #
# yBatch (batchSize, ) #
# - Use that sample for gradient decent optimization. #
# - Update the weights using the gradient and the learning rate. #
# #
# Hint: #
# - Use np.random.choice #
#########################################################################
num_train = np.random.choice(x.shape[0], batchSize)
xBatch = x[num_train]
yBatch = y[num_train]
loss, dW = self.calLoss(xBatch,yBatch,reg)
self.W= self.W - lr * dW
lossHistory.append(loss)
#########################################################################
# END OF YOUR CODE #
#########################################################################
# Print loss for every 100 iterations
if verbose and i % 100 == 0 and len(lossHistory) is not 0:
print ('Loop {0} loss {1}'.format(i, lossHistory[i]))
return lossHistory
def predict (self, x,):
"""
Predict the y output.
Inputs:
- x: training data of shape (N, D)
Returns:
- yPred: output data of shape (N, ) where value < C
"""
yPred = np.zeros(x.shape[0])
###########################################################################
# TODO: 5 points #
# - Store the predict output in yPred #
###########################################################################
s =x.dot(self.W)
yPred = np.argmax(s, axis=1)
###########################################################################
# END OF YOUR CODE #
###########################################################################
return yPred
def calAccuracy (self, x, y):
acc = 0
###########################################################################
# TODO: 5 points #
# - Calculate accuracy of the predict value and store to acc variable #
###########################################################################
yPred = self.predict(x)
acc = np.mean(y == yPred)*100
###########################################################################
# END OF YOUR CODE #
###########################################################################
return acc