forked from kvfrans/parallel-trpo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
190 lines (163 loc) · 6.16 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
import tensorflow as tf
import numpy as np
import scipy.signal
def xrange(x):
return iter(range(x))
# KL divergence with itself, holding first argument fixed
def gauss_selfKL_firstfixed(mu, logstd):
mu1, logstd1 = map(tf.stop_gradient, [mu, logstd])
mu2, logstd2 = mu, logstd
return gauss_KL(mu1, logstd1, mu2, logstd2)
# probability to take action x, given paramaterized guassian distribution
def gauss_log_prob(mu, logstd, x):
var = tf.exp(2*logstd)
gp = -tf.square(x - mu)/(2*var) - .5*tf.log(tf.constant(2*np.pi)) - logstd
return tf.reduce_sum(gp, [1])
# KL divergence between two paramaterized guassian distributions
def gauss_KL(mu1, logstd1, mu2, logstd2):
var1 = tf.exp(2*logstd1)
var2 = tf.exp(2*logstd2)
kl = tf.reduce_sum(logstd2 - logstd1 + (var1 + tf.square(mu1 - mu2))/(2*var2) - 0.5)
return kl
# Shannon entropy for a paramaterized guassian distributions
def gauss_ent(mu, logstd):
h = tf.reduce_sum(logstd + tf.constant(0.5*np.log(2*np.pi*np.e), tf.float32))
return h
def discount(x, gamma):
assert x.ndim >= 1
return scipy.signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
def cat_sample(prob_nk):
assert prob_nk.ndim == 2
# prob_nk: batchsize x actions
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 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 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
class Filter:
def __init__(self, filter_mean=True):
self.m1 = 0
self.v = 0
self.n = 0.
self.filter_mean = filter_mean
def __call__(self, o):
self.m1 = self.m1 * (self.n / (self.n + 1)) + o * 1/(1 + self.n)
self.v = self.v * (self.n / (self.n + 1)) + (o - self.m1)**2 * 1/(1 + self.n)
self.std = (self.v + 1e-6)**.5 # std
self.n += 1
if self.filter_mean:
o1 = (o - self.m1)/self.std
else:
o1 = o/self.std
o1 = (o1 > 10) * 10 + (o1 < -10)* (-10) + (o1 < 10) * (o1 > -10) * o1
return o1
filter = Filter()
filter_std = Filter()
def numel(x):
return np.prod(var_shape(x))
def flatgrad(loss, var_list):
grads = tf.gradients(loss, var_list)
return tf.concat([tf.reshape(grad, [numel(v)]) for (v, grad) in zip(var_list, grads)], 0)
def conjugate_gradient(f_Ax, b, cg_iters=10, residual_tol=1e-10):
# in numpy
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
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
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(tf.float32, [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([tf.reshape(v, [numel(v)]) for v in var_list], 0)
def __call__(self):
return self.op.eval(session=self.session)
class GetPolicyWeights(object):
def __init__(self, session, var_list):
self.session = session
self.op = [var for var in var_list if 'policy' in var.name]
def __call__(self):
return self.session.run(self.op)
class SetPolicyWeights(object):
def __init__(self, session, var_list):
self.session = session
self.policy_vars = [var for var in var_list if 'policy' in var.name]
self.placeholders = {}
self.assigns = []
for var in self.policy_vars:
self.placeholders[var.name] = tf.placeholder(tf.float32, var.get_shape())
self.assigns.append(tf.assign(var,self.placeholders[var.name]))
def __call__(self, weights):
feed_dict = {}
count = 0
for var in self.policy_vars:
feed_dict[self.placeholders[var.name]] = weights[count]
count += 1
self.session.run(self.assigns, feed_dict)
def xavier_initializer(self, shape):
dim_sum = np.sum(shape)
if len(shape) == 1:
dim_sum += 1
bound = np.sqrt(6.0 / dim_sum)
return tf.random_uniform(shape, minval=-bound, maxval=bound)
def fully_connected(input_layer, input_size, output_size, weight_init, bias_init, scope):
with tf.variable_scope(scope):
w = tf.get_variable("w", [input_size, output_size], initializer=weight_init)
# w = tf.Variable(xavier_initializer([input_size, output_size]), name="w")
b = tf.get_variable("b", [output_size], initializer=bias_init)
return tf.matmul(input_layer,w) + b