Guest User

Untitled

a guest
Mar 1st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. def saga(A, y, gradf, prox, maxiter, gamma):
  2.     """Minimal and straightforward implementation of SAGA."""
  3.     n, d = A.shape  # number of observations x dimensionality
  4.     x = np.zeros(d)
  5.     grads = np.array([gradf(A, x, y, i) for i in range(n)])
  6.     avg = grads.mean(axis=0)
  7.     X = np.empty((maxiter, d))
  8.    
  9.     for epoch in range(maxiter):
  10.         #indices = np.random.permutation(n)
  11.         indices = np.random.randint(0, n, n)
  12.        
  13.         for i in indices:
  14.             grad = gradf(A, x, y, i)
  15.             x = x - gamma * (grad - grads[i,:] + avg)
  16.             x = prox(x, gamma)
  17.             avg += (grad - grads[i,:]) / n
  18.             grads[i,:] = grad
  19.            
  20.         X[epoch,:] = x
  21.        
  22.     return X
  23.  
  24.  
  25. gradf = lambda A, x, y, i: A[i,:].T.dot(A[i,:].dot(x) - y[i])
  26. prox_no = lambda x, gamma: x
Add Comment
Please, Sign In to add comment