MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / evolution_strategy

Function evolution_strategy

rl3/es_mnist.py:91–126  ·  view source on GitHub ↗
(
    f,
    population_size,
    sigma,
    lr,
    initial_params,
    num_iters)

Source from the content-addressed store, hash-verified

89
90
91def evolution_strategy(
92 f,
93 population_size,
94 sigma,
95 lr,
96 initial_params,
97 num_iters):
98
99 # assume initial params is a 1-D array
100 num_params = len(initial_params)
101 reward_per_iteration = np.zeros(num_iters)
102
103 params = initial_params
104 for t in range(num_iters):
105 t0 = datetime.now()
106 N = np.random.randn(population_size, num_params)
107
108 # ### slow way
109 # R = np.zeros(population_size) # stores the reward
110
111 # # loop through each "offspring"
112 # for j in range(population_size):
113 # params_try = params + sigma*N[j]
114 # R[j] = f(params_try)
115
116 ### fast way
117 R = pool.map(f, [params + sigma*N[j] for j in range(population_size)])
118 R = np.array(R)
119
120 m = R.mean()
121 A = (R - m) / R.std()
122 reward_per_iteration[t] = m
123 params = params + lr/(population_size*sigma) * np.dot(N.T, A)
124 print("Iter:", t, "Avg Reward:", m, "Duration:", (datetime.now() - t0))
125
126 return params, reward_per_iteration
127
128
129def reward_function(params):

Callers 1

es_mnist.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected