-
Notifications
You must be signed in to change notification settings - Fork 1
/
CREPS.py
214 lines (161 loc) · 6.29 KB
/
CREPS.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Numpy implementation of the CREPS optimizer and upper-level policy.
This implementation will generally be faster for relatively small problems
comapred to the Theano and Torch implementations.
"""
import numpy as np
from scipy.optimize import fmin_l_bfgs_b
from numpy.random import multivariate_normal as mvnrnd
def computeSampleWeighting(R, F, eps):
"""Compute sample weights for the upper-level policy update.
Computes the sample weights used to update the upper-level policy, according
to the set of features and rewards found by interacting with the environment.
Parameters
----------
R: numpy.ndarray, shape (n_samples, 1)
Rewards
F: numpy.ndarray, shape (n_samples, n_context_features)
Context features
eps: float
Epsilon
Returns
-------
p: numpy.ndarray, shape (n_samples,)
Weights for policy update
"""
assert(R.shape[1] == 1 and
R.shape[0] == F.shape[0]
), "Incorrect parameter size"
# ----------------------------------------------------------------------
# Minimize dual function using L-BFGS-B
# ----------------------------------------------------------------------
def dual_fnc(x): # Dual function with analyitical gradients
eta = x[0]
theta = x[1:].reshape(-1, 1)
F_mean = F.mean(0).reshape(1, -1)
R_over_eta = (R - F.dot(theta)) / eta
R_over_eta_max = R_over_eta.max()
Z = np.exp(R_over_eta - R_over_eta_max).T
Z_sum = Z.sum()
log_sum_exp = R_over_eta_max + np.log(Z_sum / F.shape[0])
f = eta * (eps + log_sum_exp) + F_mean.dot(theta)
d_eta = eps + log_sum_exp - (Z.dot(R_over_eta) / Z_sum)
d_theta = F_mean - (Z.dot(F) / Z_sum)
return f, np.append(d_eta, d_theta)
# Initial point
x0 = [1] + [1] * F.shape[1]
# Bounds
min_eta = 1e-10
bds = np.vstack(([[min_eta, None]], np.tile(None, (F.shape[1], 2))))
# Minimize using L-BFGS-B algorithm
x = fmin_l_bfgs_b(dual_fnc, x0, bounds=bds)[0]
# ----------------------------------------------------------------------
# Determine weights of individual samples for policy update
# ----------------------------------------------------------------------
eta = x[0]
theta = x[1:].reshape(-1, 1)
R_baseline_eta = (R - F.dot(theta)) / eta
p = np.exp(R_baseline_eta - R_baseline_eta.max())
p /= p.sum()
return p.reshape(-1,)
class UpperPolicy:
"""Upper-level policy.
Upper-level policy \pi(w | s) implemented as a linear-Gaussian model
parametrized by {a, A, sigma}:
\pi(w | s) = N(w | a + As, sigma)
Parameters
----------
n_context: int
Number of context features
verbose: bool, optional (default: False)
If True prints the policy parameters after a policy update
"""
def __init__(self, n_context, verbose = False):
self.n_context = n_context
self.verbose = verbose
def set_parameters(self, a, A, sigma):
"""Set the paramaters of the upper-level policy.
Parameters
----------
a: numpy.ndarray, shape (1, n_lower_policy_weights)
Parameter 'a'
A: numpy.ndarray, shape (n_context_features, n_lower_policy_weights)
Parameter 'A'
sigma: numpy.ndarray, shape (n_lower_policy_weights,
n_lower_policy_weights)
Covariance matrix
"""
n_lower_policy_weights = a.shape[1]
assert(a.shape[0] == 1 and
A.shape[1] == n_lower_policy_weights and
A.shape[0] == self.n_context and
sigma.shape[0] == n_lower_policy_weights and
sigma.shape[1] == n_lower_policy_weights
), "Incorrect parameter sizes"
self.a = a
self.sigma = sigma
self.A = A
def sample(self, S):
"""Sample the upper-level policy given the context features.
Sample distribution \pi(w | s) = N(w | a + As, sigma)
Parameters
----------
S: numpy.ndarray, shape (n_samples, n_context_features)
Context features
Returns
-------
W: numpy.ndarray, shape (n_samples, n_lower_policy_weights)
Sampled lower-policy parameters.
"""
W = np.zeros((S.shape[0], self.a.shape[1]))
mus = self.mean(S)
for sample in range(S.shape[0]):
W[sample, :] = mvnrnd(mus[sample, :], self.sigma)
return W
def mean(self, S):
"""Return the upper-level policy mean given the context features.
The mean of the distribution is N(w | a + As, sigma)
Parameters
----------
S: numpy.ndarray, shape (n_samples, n_context_features)
Context features
Returns
-------
W: numpy.ndarray, shape (n_samples, n_lower_policy_weights)
Distribution mean for contexts
"""
return self.a + S.dot(self.A)
def update(self, w, F, p):
"""Update the upper-level policy parametersself.
Update is done using weighted maximum likelihood.
Parameters
----------
w: numpy.ndarray, shape (n_samples, n_lower_policy_weights)
Lower-level policy weights
F: numpy.ndarray, shape (n_samples, n_context_features)
Context features
p: numpy.ndarray, shape (n_samples,)
Sample weights
"""
n_samples = w.shape[0]
n_lower_policy_weights = self.a.shape[1]
assert(w.shape[1] == n_lower_policy_weights and
F.shape[0] == n_samples and
F.shape[1] == self.n_context and
p.shape[0] == n_samples and
p.ndim == 1
), "Incorrect parameter size"
S = np.concatenate((np.ones((p.size, 1)), F), axis = 1)
P = np.diag(p)
# Compute new mean
bigA = np.linalg.pinv(S.T.dot(P).dot(S)).dot(S.T).dot(P).dot(w)
a = bigA[0, :].reshape(1, -1)
# Compute new covariance matrix
wd = w - a
sigma = (p * wd.T).dot(wd)
# Update policy parameters
self.set_parameters(a, bigA[1:, :], sigma)
if self.verbose:
print('Policy update: a, A, mean of sigma')
print(self.a)
print(self.A)
print(self.sigma.mean())