-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctrnn_imitator.py
287 lines (201 loc) · 10.2 KB
/
ctrnn_imitator.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import tensorflow as tf
import os
import numpy as np
def affine_layer(inputs,activation=None,name=""):
input_size = int(inputs.shape[-1])
k = tf.get_variable('affine_k_{}'.format(name), [input_size],initializer=tf.initializers.constant(1.0))
d = tf.get_variable("affine_d_{}".format(name), [input_size],initializer=tf.initializers.constant(0.0))
y = inputs*k + d
if(not activation is None):
y = activation(y)
return y
class CTRNN_Cell(tf.nn.rnn_cell.RNNCell):
def __init__(self, num_units,cell_clip=-1):
self._num_units = num_units
self._num_unfolds = 6
self._delta_t = 1.0/self._num_unfolds
self.tau = 1.0
self.cell_clip = cell_clip
def _dense(self,units,inputs,activation,name,bias_initializer=tf.constant_initializer(0.0)):
input_size = int(inputs.shape[-1])
W = tf.get_variable('W_{}'.format(name), [input_size, units])
b = tf.get_variable("bias_{}".format(name), [units],initializer=bias_initializer)
y = tf.matmul(inputs,W) + b
if(not activation is None):
y = activation(y)
return y
def linear(self,units,inputs,name=""):
input_size = int(inputs.shape[-1])
W = tf.get_variable('W_linear'+name, [input_size, units])
y = tf.matmul(inputs,W)
return y,W
@property
def state_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
def build(self,input_shape):
pass
def __call__(self, inputs, state, scope=None):
# CTRNN ODE is: df/dt = NN(x) - f
# where x is the input, and NN is a MLP.
# Input could be: 1: just the input of the RNN cell
# or 2: input of the RNN cell merged with the current state
self._input_size = int(inputs.shape[-1])
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("RNN",reuse=tf.AUTO_REUSE):
for i in range(self._num_unfolds):
fused_input = tf.concat([state,inputs],axis=-1)
input_f_prime = self._dense(inputs=fused_input,units=self._num_units,activation=tf.nn.tanh,name="l")
# df/dt
f_prime = -state/self.tau + input_f_prime
# If we solve this ODE with explicit euler we get
# f(t+deltaT) = f(t) + deltaT * df/dt
state = state + self._delta_t * f_prime
if(self.cell_clip > 0):
state = tf.clip_by_value(state,-self.cell_clip,self.cell_clip)
return state,state
class CTRNN_Imitator:
def __init__(self,num_units,clip_value,lidar_size,state_size):
self.lidar_size = lidar_size
self.state_size = state_size
self.num_units = num_units
self.x_lidar = tf.placeholder(tf.float32, shape=[None, None, self.lidar_size],name='lidar')
self.x_state = tf.placeholder(tf.float32, shape=[None, None, self.state_size],name='state')
self.target_y = tf.placeholder(tf.float32, shape=[None,None,1])
head = tf.reshape(self.x_lidar,[-1,self.lidar_size,1])
head = tf.keras.layers.Conv1D(12,5,strides=3,activation="relu")(head)
head = tf.keras.layers.Conv1D(16,5,strides=3,activation="relu")(head)
head = tf.keras.layers.Conv1D(24,5,strides=2,activation="relu")(head)
head = tf.keras.layers.Conv1D(1,1,strides=1,activation=None)(head)
head = tf.keras.layers.Flatten()(head)
head = tf.reshape(head,shape=[tf.shape(self.x_lidar)[0],tf.shape(self.x_lidar)[1],head.shape[-1]])
estim_in = self.x_state
estim_in = tf.clip_by_value(estim_in,-1.0,1.0)
head = tf.concat([head,estim_in],axis=-1)
print("head shape: ",str(head.shape))
self.init_state = tf.placeholder(tf.float32,[None,self.num_units],name="initial_state")
self.fused_cell = CTRNN_Cell(self.num_units,cell_clip=clip_value)
cell_out,self.final_state = tf.nn.dynamic_rnn(self.fused_cell,head,initial_state = self.init_state,time_major=True)
cell_out = tf.reshape(cell_out,[-1,self.num_units])
y = tf.keras.layers.Dense(1,activation=None,name="ct_out")(cell_out)
# Reshape back to sequenced batch form
self.y = tf.reshape(y,shape=[tf.shape(self.x_state)[0],tf.shape(self.x_state)[1],1])
#Output
tf.identity(self.y,name='prediction')
tf.identity(self.final_state,name='final_state')
self.loss = tf.reduce_mean(tf.square(tf.subtract(self.target_y, self.y)))
# Loss, error and training algorithm
self.mean_abs_error = tf.reduce_mean(tf.abs(tf.subtract(self.y, self.target_y)))
optimizer = tf.train.AdamOptimizer(0.0001)
self.train_step = optimizer.minimize(self.loss)
def zero_state(self,batch_size):
return np.zeros([batch_size,self.num_units],dtype=np.float32)
def share_sess(self, sess):
self.sess = sess
def predict_step(self, x_state, x_lidar,init_state=None):
if(init_state is None):
init_state = self.zero_state(1)
# Reshape sequence into a batch of 1 sequence
x_state = x_state.reshape([1,1,self.state_size])
x_lidar = x_lidar.reshape([1,1,self.lidar_size])
feed_dict = {
self.x_state: x_state,
self.x_lidar: x_lidar,
self.init_state: init_state}
prediction,next_state = self.sess.run([self.y,self.final_state], feed_dict=feed_dict)
return float(prediction.flatten()),next_state
def evaluate(self, batch_x_state, batch_x_lidar,batch_y):
feed_dict = {
self.x_state: batch_x_state,
self.x_lidar: batch_x_lidar,
self.target_y: batch_y,
self.init_state:self.zero_state(batch_x_state.shape[1])
}
loss,mae = self.sess.run([self.loss,self.mean_abs_error], feed_dict=feed_dict)
return loss,mae
def train_iter(self, batch_x_state, batch_x_lidar,batch_y):
feed_dict = {
self.x_state: batch_x_state,
self.x_lidar: batch_x_lidar,
self.target_y: batch_y,
self.init_state:self.zero_state(batch_x_state.shape[1])
}
(_,loss,mae) = self.sess.run([self.train_step, self.loss,self.mean_abs_error], feed_dict=feed_dict)
return loss,mae
def create_checkpoint(self, path, name='model'):
if not os.path.exists(path):
os.makedirs(path)
checkpoint_path = os.path.join(path, '-'+name)
self.saver = tf.train.Saver()
filename = self.saver.save(self.sess, checkpoint_path)
def restore_from_checkpoint(self, path):
self.saver = tf.train.Saver()
self.saver.restore(self.sess, os.path.join(path,'-model'))
class CTRNN_Cheetah:
def __init__(self,num_units,clip_value,obs_size,action_size):
self.obs_size = obs_size
self.action_size = action_size
self.num_units = num_units
self.x_obs = tf.placeholder(tf.float32, shape=[None, None, self.obs_size],name='state')
self.target_y = tf.placeholder(tf.float32, shape=[None,None,self.action_size])
head = self.x_obs
head = tf.reshape(self.x_obs,[-1,self.obs_size])
head = tf.keras.layers.Dense(128,activation="relu")(head)
head = tf.keras.layers.Dense(128,activation=None)(head)
head = tf.reshape(head,shape=[tf.shape(self.x_obs)[0],tf.shape(self.x_obs)[1],head.shape[-1]])
self.init_state = tf.placeholder(tf.float32,[None,self.num_units],name="initial_state")
self.fused_cell = CTRNN_Cell(self.num_units,cell_clip=clip_value)
cell_out,self.final_state = tf.nn.dynamic_rnn(self.fused_cell,head,initial_state = self.init_state,time_major=True)
cell_out = tf.reshape(cell_out,[-1,self.num_units])
y = tf.keras.layers.Dense(self.action_size,activation=None,name="ct_out")(cell_out)
# Reshape back to sequenced batch form
self.y = tf.reshape(y,shape=[tf.shape(self.x_obs)[0],tf.shape(self.x_obs)[1],self.action_size])
#Output
tf.identity(self.y,name='prediction')
tf.identity(self.final_state,name='final_state')
self.loss = tf.reduce_mean(tf.reduce_sum(tf.square(tf.subtract(self.target_y, self.y)),axis=-1))
# Loss, error and training algorithm
self.mean_abs_error = tf.reduce_mean(tf.abs(tf.subtract(self.y, self.target_y)))
optimizer = tf.train.AdamOptimizer(0.0001)
self.train_step = optimizer.minimize(self.loss)
def zero_state(self,batch_size):
return np.zeros([batch_size,self.num_units],dtype=np.float32)
def share_sess(self, sess):
self.sess = sess
def predict_step(self, x_obs,init_state=None):
if(init_state is None):
init_state = self.zero_state(1)
# Reshape sequence into a batch of 1 sequence
x_obs = x_obs.reshape([1,1,self.obs_size])
feed_dict = {
self.x_obs: x_obs,
self.init_state: init_state}
prediction,next_state = self.sess.run([self.y,self.final_state], feed_dict=feed_dict)
return prediction.flatten(),next_state
def evaluate(self, batch_x_obs, batch_action):
feed_dict = {
self.x_obs: batch_x_obs,
self.target_y: batch_action,
self.init_state:self.zero_state(batch_x_obs.shape[1])
}
loss,mae = self.sess.run([self.loss,self.mean_abs_error], feed_dict=feed_dict)
return loss,mae
def train_iter(self, batch_x_obs, batch_action):
feed_dict = {
self.x_obs: batch_x_obs,
self.target_y: batch_action,
self.init_state:self.zero_state(batch_x_obs.shape[1])
}
(_,loss,mae) = self.sess.run([self.train_step, self.loss,self.mean_abs_error], feed_dict=feed_dict)
return loss,mae
def create_checkpoint(self, path, name='model'):
if not os.path.exists(path):
os.makedirs(path)
checkpoint_path = os.path.join(path, '-'+name)
self.saver = tf.train.Saver()
filename = self.saver.save(self.sess, checkpoint_path)
def restore_from_checkpoint(self, path):
self.saver = tf.train.Saver()
self.saver.restore(self.sess, os.path.join(path,'-model'))