-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeepFCL2.py
419 lines (354 loc) · 18.7 KB
/
DeepFCL2.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# -*- coding: utf-8 -*-
"""
Formation control learning from raw visual observation
Created on Mon Dec 11 10:10:18 2017
@author: jesse
"""
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
#import tensorflow.contrib.slim as slim
class DeepFCL:
def __init__(self, obs_dim1, obs_dim2, act_dim, img_chnl):
# observation and state space dimension
self.obs_dim = obs_dim1*obs_dim2*img_chnl # height * width * channel
self.act_dim = act_dim
self.batchsize = 256 #32
# input variables
self.obs_var = tf.placeholder(shape=[None,obs_dim1*obs_dim2*img_chnl], dtype=tf.float32, name="obs_var")
#self.obs_var = tf.placeholder(tf.float32, shape=(None,obs_dim,obs_dim))
self.pre_ctrl = tf.placeholder(shape=[None,2], dtype=tf.float32, name="pre_ctrl")
#self.is_training = tf.placeholder(shape=[], dtype=tf.bool, name="train_cond")
# ------- Define Observation-State Mapping Using Convolutional Network -----------------------
# network parameters
conv1_num = 32
conv2_num = 16
conv3_num = 16
fc1_num = 32
# resize the array of flattened input
self.imageIn = tf.reshape(self.obs_var, shape=[-1,obs_dim1,obs_dim2,img_chnl])
# convolutions acti: ReLU and spatial softmax
self.conv1 = tf.contrib.layers.convolution2d(inputs=self.imageIn,#tf.expand_dims(self.obs_var,3),
num_outputs=conv1_num,
kernel_size=[8,8],
stride=[4,4],
padding='VALID',
biases_initializer=None)
# max pooling
#self.conv1 = tf.layers.max_pooling2d(self.conv1,2,1)
self.conv2 = tf.contrib.layers.convolution2d(inputs=self.conv1,
num_outputs=conv2_num,
kernel_size=[4,4],
stride=[2,2],
padding='VALID',
biases_initializer=None)
#self.conv2 = tf.layers.max_pooling2d(self.conv2,2,1)
self.conv3 = tf.contrib.layers.convolution2d(inputs=self.conv2,
num_outputs=conv3_num,
kernel_size=[3,3],
stride=[1,1],
padding='VALID',
biases_initializer=None)
#self.conv3 = tf.layers.max_pooling2d(self.conv3,2,1)
# output layer
#self.convout = tf.contrib.layers.flatten(self.conv3)
self.convout = tf.concat([tf.contrib.layers.flatten(self.conv3), self.pre_ctrl], 1)
# fully-connected (acti: ReLU)
self.W1 = tf.Variable(tf.random_normal([self.convout.get_shape().as_list()[1], fc1_num]))
self.b1 = tf.Variable(tf.random_normal([fc1_num]))
self.fc1 = tf.nn.relu(tf.matmul(self.convout, self.W1) + self.b1)
self.W2 = tf.Variable(tf.random_normal([fc1_num, act_dim]))
self.b2 = tf.Variable(tf.random_normal([act_dim]))
# output layer (acti: linear)
self.out = tf.matmul(self.fc1, self.W2) + self.b2
#self.out = 0.5*tf.tanh(tf.matmul(self.fc1, self.W2) + self.b2)
# ------- Define Loss Function ---------------------------
self.targetOut = tf.placeholder(shape=[None,2], dtype=tf.float32)
self.out_error = tf.norm(self.targetOut - self.out, ord=2, axis=1)
# total loss
self.loss = tf.reduce_mean(self.out_error)
# Training Functions
self.optimizer = tf.train.AdamOptimizer(learning_rate = 0.001)
self.train_op = self.optimizer.minimize(self.loss)
self.saver = tf.train.Saver()
current_dir = os.getcwd()
self.save_path = os.path.join(current_dir + '/models/')
self.sess = tf.Session()
def learn(self, observations, actions, pre_actions, epi_starts):
# Prepre Training Data -------------------------------------------
# normalize observation input
self.mean_obs = np.mean(observations, axis=0, keepdims=True)
self.std_obs = np.std(observations, ddof=1)
observations = (observations - self.mean_obs) / self.std_obs
# number of samples in total
num_samples = observations.shape[0] - 1
# indices for all time steps where the episode continues
indices = np.array([i for i in range(num_samples) if not epi_starts[i + 1]], dtype='int32')
np.random.shuffle(indices)
# split indices into minibatches
minibatchlist = [np.array(sorted(indices[start_idx:start_idx + self.batchsize]))
for start_idx in range(0, num_samples - self.batchsize + 1, self.batchsize)]
# Training -------------------------------------------------------
init = tf.global_variables_initializer()
num_epochs = 150
# saver = tf.train.Saver()
# current_dir = os.getcwd()
# save_path = os.path.join(current_dir + '/models/')
with tf.Session() as sess:
sess.run(init)
loss_hist = []
for epoch in range(num_epochs):
epoch_loss = 0
epoch_batches = 0
enumerated_minibatches = list(enumerate(minibatchlist))
np.random.shuffle(enumerated_minibatches)
for i, batch in enumerated_minibatches:
_ , tmp_loss = sess.run([self.train_op,self.loss], feed_dict = {
self.obs_var: observations[batch],
self.targetOut: actions[batch] })
epoch_loss += tmp_loss
epoch_batches += 1
loss_hist.append(epoch_loss / epoch_batches)
# print results for this epoch
if (epoch+1) % 5 ==0:
print("Epoch {:3}/{}, loss:{:.4f}".format(epoch+1, num_epochs, epoch_loss / epoch_batches))
# save the updated model
print('saving learned model')
self.saver.save(sess, os.path.join(self.save_path, 'model_epi' + str(epoch)))
predicted_action = sess.run(self.out, feed_dict={self.obs_var: observations,
self.pre_ctrl: pre_actions })
plt.close("Learned Policy")
return predicted_action, loss_hist
def init_test(self):
# norm_para = np.load('norm_para2.npz')
# self.mean_obs = norm_para['arr_0']
# self.std_obs = norm_para['arr_1']
norm_para = np.load('train_rslt.npz')
self.mean_obs = norm_para['mean_obs']
self.std_obs = norm_para['std_obs']
self.saver.restore(self.sess, os.path.join(self.save_path, 'model_epi' + str(150-1)))
def test(self, observations = None, pre_actions = None):
if observations is None:
return 1
observations = (observations - self.mean_obs) / self.std_obs
# saver = tf.train.Saver()
# current_dir = os.getcwd()
# save_path = os.path.join(current_dir, 'models')
# with tf.Session() as sess:
# load the model and output action
# self.saver.restore(sess, os.path.join(self.save_path, 'model_epi' + str(150-1)))
act_output = self.sess.run(self.out, feed_dict = {self.obs_var: observations,
self.pre_ctrl: pre_actions})
return act_output
if __name__ == '__main__':
print('\nFormation Control Task\n')
print('Loading and displaying training data ... ')
training_data = np.load('fcl_data11.npz')
#plot_observations(training_data['observations'], name="Observation Samples (Subset of Training Data) -- Simple Navigation Task")
print('Learning a policy ... ')
fcl = DeepFCL(50, 50, 2, 2)
[training_ctrls, loss_hist] = fcl.learn(training_data['observations'],training_data['actions'],training_data['actions_1'],training_data['epi_starts'])
# plot_representation(training_states, training_data['rewards'],
# name='Observation-State-Mapping Applied to Training Data -- Simple Navigation Task',
# add_colorbar=True)
# print('Loading and displaying testing data ... ')
# testing_data = np.load('fcl_data1.npz')
#
# print('Testing a policy ... ')
# testing_ctrls = fcl.test(testing_data['observations'])
#----------------------------------------------------------------------------------------------------
## -*- coding: utf-8 -*-
#"""
#Formation control learning from raw visual observation
#
#Created on Mon Dec 11 10:10:18 2017
#
#@author: jesse
#"""
#
#import numpy as np
#import matplotlib.pyplot as plt
#
#import tensorflow as tf
#
#import os
##import tensorflow.contrib.slim as slim
#
#class DeepFCL:
#
# def __init__(self, obs_dim1, obs_dim2, act_dim, img_chnl):
#
# # observation and state space dimension
# self.obs_dim = obs_dim1*obs_dim2*img_chnl # height * width * channel
# self.act_dim = act_dim
# self.batchsize = 256 #32
#
# # input variables
# self.obs_var = tf.placeholder(shape=[None,obs_dim1*obs_dim2*img_chnl], dtype=tf.float32, name="obs_var")
# #self.obs_var = tf.placeholder(tf.float32, shape=(None,obs_dim,obs_dim))
# #self.goal_trj = tf.placeholder(shape=[None,2], dtype=tf.float32, name="goal_trj")
# #self.is_training = tf.placeholder(shape=[], dtype=tf.bool, name="train_cond")
#
# # ------- Define Observation-State Mapping Using Convolutional Network -----------------------
#
# # network parameters
# conv1_num = 32
# conv2_num = 16
# conv3_num = 16
# fc1_num = 32
#
# # resize the array of flattened input
# self.imageIn = tf.reshape(self.obs_var, shape=[-1,obs_dim1,obs_dim2,img_chnl])
#
# # convolutions acti: ReLU and spatial softmax
# self.conv1 = tf.contrib.layers.convolution2d(inputs=self.imageIn,#tf.expand_dims(self.obs_var,3),
# num_outputs=conv1_num,
# kernel_size=[8,8],
# stride=[4,4],
# padding='VALID',
# biases_initializer=None)
# # max pooling
# #self.conv1 = tf.layers.max_pooling2d(self.conv1,2,1)
#
# self.conv2 = tf.contrib.layers.convolution2d(inputs=self.conv1,
# num_outputs=conv2_num,
# kernel_size=[4,4],
# stride=[2,2],
# padding='VALID',
# biases_initializer=None)
#
# #self.conv2 = tf.layers.max_pooling2d(self.conv2,2,1)
#
# self.conv3 = tf.contrib.layers.convolution2d(inputs=self.conv2,
# num_outputs=conv3_num,
# kernel_size=[3,3],
# stride=[1,1],
# padding='VALID',
# biases_initializer=None)
#
# #self.conv3 = tf.layers.max_pooling2d(self.conv3,2,1)
#
# # output layer
# self.convout = tf.contrib.layers.flatten(self.conv3)
#
# # fully-connected (acti: ReLU)
# self.W1 = tf.Variable(tf.random_normal([self.convout.get_shape().as_list()[1], fc1_num]))
# self.b1 = tf.Variable(tf.random_normal([fc1_num]))
# self.fc1 = tf.nn.relu(tf.matmul(self.convout, self.W1) + self.b1)
#
# self.W2 = tf.Variable(tf.random_normal([fc1_num, act_dim]))
# self.b2 = tf.Variable(tf.random_normal([act_dim]))
# # output layer (acti: linear)
# self.out = tf.matmul(self.fc1, self.W2) + self.b2
# #self.out = tf.nn.relu(tf.matmul(self.fc1, self.W2) + self.b2)
#
# # ------- Define Loss Function ---------------------------
# self.targetOut = tf.placeholder(shape=[None,2], dtype=tf.float32)
# self.out_error = tf.norm(self.targetOut - self.out, ord=2, axis=1)
#
# # total loss
# self.loss = tf.reduce_mean(self.out_error)
#
# # Training Functions
# self.optimizer = tf.train.AdamOptimizer(learning_rate = 0.001)
# self.train_op = self.optimizer.minimize(self.loss)
#
#
# def learn(self, observations, actions): #, epi_starts):
#
# # Prepre Training Data -------------------------------------------
# # normalize observation input
# self.mean_obs = np.mean(observations, axis=0, keepdims=True)
# self.std_obs = np.std(observations, ddof=1)
# observations = (observations - self.mean_obs) / self.std_obs
#
# # number of samples in total
# num_samples = observations.shape[0] - 1
#
# # indices for all time steps where the episode continues
# #indices = np.array([i for i in range(num_samples) if not epi_starts[i + 1]], dtype='int32')
# indices = np.array([i for i in range(num_samples)], dtype='int32')
# np.random.shuffle(indices)
#
# # split indices into minibatches
# minibatchlist = [np.array(sorted(indices[start_idx:start_idx + self.batchsize]))
# for start_idx in range(0, num_samples - self.batchsize + 1, self.batchsize)]
#
# # Training -------------------------------------------------------
# init = tf.global_variables_initializer()
# num_epochs = 150
# saver = tf.train.Saver()
# current_dir = os.getcwd()
# save_path = os.path.join(current_dir + '/models/')
#
# with tf.Session() as sess:
# sess.run(init)
# loss_hist = []
#
# for epoch in range(num_epochs):
# epoch_loss = 0
# epoch_batches = 0
# enumerated_minibatches = list(enumerate(minibatchlist))
# np.random.shuffle(enumerated_minibatches)
#
# for i, batch in enumerated_minibatches:
# _ , tmp_loss = sess.run([self.train_op,self.loss], feed_dict = {
# self.obs_var: observations[batch],
# self.targetOut: actions[batch] })
# epoch_loss += tmp_loss
# epoch_batches += 1
# loss_hist.append(epoch_loss / epoch_batches)
#
# # print results for this epoch
# if (epoch+1) % 5 ==0:
# print("Epoch {:3}/{}, loss:{:.4f}".format(epoch+1, num_epochs, epoch_loss / epoch_batches))
#
# # save the updated model
# print('saving learned model')
# saver.save(sess, os.path.join(save_path, 'model_epi' + str(epoch)))
# predicted_action = sess.run(self.out, feed_dict={self.obs_var: observations})
# plt.close("Learned Policy")
#
# return predicted_action, loss_hist
#
#
# def test(self, observations):
## observations = (observations - self.mean_obs) / self.std_obs
# saver = tf.train.Saver()
# current_dir = os.getcwd()
# save_path = os.path.join(current_dir + '/models/')
# num_epochs = 150
# with tf.Session() as sess:
# # load the model and output action
# saver.restore(sess, os.path.join(save_path, 'model_epi' + str(num_epochs-1)))
# act_output = sess.run(self.out, feed_dict = {self.obs_var: observations})
#
# return act_output
#
#
#if __name__ == '__main__':
#
# print('\nFormation Control Task\n')
#
# print('Loading and displaying training data ... ')
# current_dir = os.getcwd()
# training_data = np.load(current_dir + '/data/data1_lf.npz')
# a = training_data['observations']
# b = training_data['actions']
#
# #plot_observations(training_data['observations'], name="Observation Samples (Subset of Training Data) -- Simple Navigation Task")
#
# print('Learning a policy ... ')
# fcl = DeepFCL(50, 50, 2, 1)
# #[training_ctrls, loss_hist] = fcl.learn(training_data['observations'],training_data['actions']) #,training_data['epi_starts'])
# [training_ctrls, loss_hist] = fcl.learn(a[0:10000,:],b[0:10000,:])
## plot_representation(training_states, training_data['rewards'],
## name='Observation-State-Mapping Applied to Training Data -- Simple Navigation Task',
## add_colorbar=True)
#
## print('Loading and displaying testing data ... ')
## testing_data = np.load('fcl_data1.npz')
##
## print('Testing a policy ... ')
## testing_ctrls = fcl.test(testing_data['observations'])