-
Notifications
You must be signed in to change notification settings - Fork 52
/
utils.py
210 lines (174 loc) · 6.27 KB
/
utils.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
import numpy as np
import tensorflow as tf
import random
import scipy.signal
import prettytensor as pt
seed = 1
random.seed(seed)
np.random.seed(seed)
tf.set_random_seed(seed)
dtype = tf.float32
def discount(x, gamma):
assert x.ndim >= 1
return scipy.signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
def rollout(env, agent, max_pathlength, n_timesteps):
paths = []
timesteps_sofar = 0
while timesteps_sofar < n_timesteps:
obs, actions, rewards, action_dists = [], [], [], []
ob = env.reset()
agent.prev_action *= 0.0
agent.prev_obs *= 0.0
for _ in xrange(max_pathlength):
action, action_dist, ob = agent.act(ob)
obs.append(ob)
actions.append(action)
action_dists.append(action_dist)
res = env.step(action)
ob = res[0]
rewards.append(res[1])
if res[2]:
path = {"obs": np.concatenate(np.expand_dims(obs, 0)),
"action_dists": np.concatenate(action_dists),
"rewards": np.array(rewards),
"actions": np.array(actions)}
paths.append(path)
agent.prev_action *= 0.0
agent.prev_obs *= 0.0
break
timesteps_sofar += len(path["rewards"])
return paths
class VF(object):
coeffs = None
def __init__(self, session):
self.net = None
self.session = session
def create_net(self, shape):
print(shape)
self.x = tf.placeholder(tf.float32, shape=[None, shape], name="x")
self.y = tf.placeholder(tf.float32, shape=[None], name="y")
self.net = (pt.wrap(self.x).
fully_connected(64, activation_fn=tf.nn.relu).
fully_connected(64, activation_fn=tf.nn.relu).
fully_connected(1))
self.net = tf.reshape(self.net, (-1, ))
l2 = (self.net - self.y) * (self.net - self.y)
self.train = tf.train.AdamOptimizer().minimize(l2)
self.session.run(tf.initialize_all_variables())
def _features(self, path):
o = path["obs"].astype('float32')
o = o.reshape(o.shape[0], -1)
act = path["action_dists"].astype('float32')
l = len(path["rewards"])
al = np.arange(l).reshape(-1, 1) / 10.0
ret = np.concatenate([o, act, al, np.ones((l, 1))], axis=1)
return ret
def fit(self, paths):
featmat = np.concatenate([self._features(path) for path in paths])
if self.net is None:
self.create_net(featmat.shape[1])
returns = np.concatenate([path["returns"] for path in paths])
for _ in range(50):
self.session.run(self.train, {self.x: featmat, self.y: returns})
def predict(self, path):
if self.net is None:
return np.zeros(len(path["rewards"]))
else:
ret = self.session.run(self.net, {self.x: self._features(path)})
return np.reshape(ret, (ret.shape[0], ))
def cat_sample(prob_nk):
assert prob_nk.ndim == 2
N = prob_nk.shape[0]
csprob_nk = np.cumsum(prob_nk, axis=1)
out = np.zeros(N, dtype='i')
for (n, csprob_k, r) in zip(xrange(N), csprob_nk, np.random.rand(N)):
for (k, csprob) in enumerate(csprob_k):
if csprob > r:
out[n] = k
break
return out
def var_shape(x):
out = [k.value for k in x.get_shape()]
assert all(isinstance(a, int) for a in out), \
"shape function assumes that shape is fully known"
return out
def numel(x):
return np.prod(var_shape(x))
def flatgrad(loss, var_list):
grads = tf.gradients(loss, var_list)
return tf.concat(0, [tf.reshape(grad, [numel(v)])
for (v, grad) in zip(var_list, grads)])
class SetFromFlat(object):
def __init__(self, session, var_list):
self.session = session
assigns = []
shapes = map(var_shape, var_list)
total_size = sum(np.prod(shape) for shape in shapes)
self.theta = theta = tf.placeholder(dtype, [total_size])
start = 0
assigns = []
for (shape, v) in zip(shapes, var_list):
size = np.prod(shape)
assigns.append(
tf.assign(
v,
tf.reshape(
theta[
start:start +
size],
shape)))
start += size
self.op = tf.group(*assigns)
def __call__(self, theta):
self.session.run(self.op, feed_dict={self.theta: theta})
class GetFlat(object):
def __init__(self, session, var_list):
self.session = session
self.op = tf.concat(0, [tf.reshape(v, [numel(v)]) for v in var_list])
def __call__(self):
return self.op.eval(session=self.session)
def slice_2d(x, inds0, inds1):
inds0 = tf.cast(inds0, tf.int64)
inds1 = tf.cast(inds1, tf.int64)
shape = tf.cast(tf.shape(x), tf.int64)
ncols = shape[1]
x_flat = tf.reshape(x, [-1])
return tf.gather(x_flat, inds0 * ncols + inds1)
def linesearch(f, x, fullstep, expected_improve_rate):
accept_ratio = .1
max_backtracks = 10
fval = f(x)
for (_n_backtracks, stepfrac) in enumerate(.5**np.arange(max_backtracks)):
xnew = x + stepfrac * fullstep
newfval = f(xnew)
actual_improve = fval - newfval
expected_improve = expected_improve_rate * stepfrac
ratio = actual_improve / expected_improve
if ratio > accept_ratio and actual_improve > 0:
return xnew
return x
def conjugate_gradient(f_Ax, b, cg_iters=10, residual_tol=1e-10):
p = b.copy()
r = b.copy()
x = np.zeros_like(b)
rdotr = r.dot(r)
for i in xrange(cg_iters):
z = f_Ax(p)
v = rdotr / p.dot(z)
x += v * p
r -= v * z
newrdotr = r.dot(r)
mu = newrdotr / rdotr
p = r + mu * p
rdotr = newrdotr
if rdotr < residual_tol:
break
return x
class dict2(dict):
def __init__(self, **kwargs):
dict.__init__(self, kwargs)
self.__dict__ = self
def explained_variance(ypred, y):
assert y.ndim == 1 and ypred.ndim == 1
vary = np.var(y)
return np.nan if vary==0 else 1 - np.var(y-ypred)/vary