Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. # simple example: minimize a quadratic around some solution point
  2. import numpy as np
  3. solution = np.array([0.5, 0.1, -0.3])
  4. def f(w): return -np.sum((w - solution)**2)
  5.  
  6. npop = 50 # population size
  7. sigma = 0.1 # noise standard deviation
  8. alpha = 0.001 # learning rate
  9. w = np.random.randn(3) # initial guess
  10. for i in range(300):
  11. N = np.random.randn(npop, 3)
  12. R = np.zeros(npop)
  13. for j in range(npop):
  14. w_try = w + sigma*N[j]
  15. R[j] = f(w_try)
  16. A = (R - np.mean(R)) / np.std(R)
  17. w = w + alpha/(npop*sigma) * np.dot(N.T, A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement