@@ -26,41 +26,28 @@ def svm_solver(X, y, C, num_iter=50000, num_per_batch=32):
2626 w = np .zeros (X .shape [1 ])
2727 b = 0.
2828 for t in range (1 , num_iter ):
29- start = time .time ()
30- #######################################################################
31- # TODO: #
32- # Perform one step of stochastic gradient descent: #
33- # - Select a single training example at random #
34- # - Update theta based on alpha and using gradient_function #
35- # #
36- #######################################################################
37-
38- # 1st Step: Sample a random mini-batch of size num_per_batch (done)
29+ start = time .time ()
3930
31+ # 1st Step: Sample a random mini-batch of size num_per_batch (done)
4032 samp = np .random .choice (np .arange (X .shape [0 ]), size = num_per_batch )
4133 X_ = X [samp ]
4234 Y_ = y [samp ]
4335
44- # 2nd Step: Compute the learning-rate n_t=1/(lambda*t) where lambda=1/C (done)
45-
36+ # 2nd Step: Compute the learning-rate n_t=1/(lambda*t) where lambda=1/C (done)
4637 lambda_ = 1 / C
4738 n_t = 1 / (lambda_ * (t + 1 ))
4839
4940 # 3rd Step: Compute the gradients and update the parameters as
50- # w:=w-n_t*grad_w and b:=b-n_t*grad_b
51-
41+ # w:=w-n_t*grad_w and b:=b-n_t*grad_b
5242 grad_w , grad_b = svm_gradient (w , b , X_ , Y_ , C )
5343 w = w - n_t * grad_w
5444 b = b - n_t * grad_b
5545
5646 # http://ttic.uchicago.edu/~nati/Publications/PegasosMPB.pdf
57-
58- #######################################################################
59- # END OF YOUR CODE #
60- #######################################################################
47+
6148 if t % 5000 == 0 :
6249 exec_time = time .time () - start
6350 loss = svm_loss (w , b , X , y , C )
6451 print ('Iter {}/{}: cost = {} ({}s)' .format (t , num_iter , loss , exec_time ))
6552
66- return w , b
53+ return w , b
0 commit comments