In [1]:
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
import sys
sys.path.append('../')
import numpy as np
import deep_control as dc
import pandas
import seaborn as sns
Previously generated trajectories can be loaded with dc.data.load_trajectories
In [4]:
import glob
import pickle
from tqdm import tqdm
files = glob.glob('../data/rw/*pic')
total = 0
trajs = []
for f in tqdm(files, leave=True):
rw = pickle.load(open(f,'rb'))
for rwi in rw:
traj = np.hstack((rwi[0], rwi[1]))
df = pandas.DataFrame(data=traj)
col_names = ['t', 'x', 'y', 'z', 'vz', 'theta', 'm', 'u1', 'u2']
df.columns = col_names
trajs.append(df)
In [5]:
ini_ps = np.vstack([t.values[0,:] for t in trajs])
In [6]:
for i in range(3):
for j in range(2):
plt.subplot(2,3,i*2+j+1)
plt.hist(ini_ps[:,i*2+j+1],59)
plt.locator_params(nbins=5)
plt.tight_layout()
In [7]:
plot_idx_t = [(x,list(range(1,x)))for x in range(1,7)]
In [8]:
plot_idx = []
In [9]:
for p in plot_idx_t:
for pi in p[1]:
plot_idx.append((p[0],pi))
In [10]:
plt.rcParams['figure.figsize'] = [30,20]
In [11]:
for i in range(5):
for j in range(3):
plt.subplot(5,3,i*3+j+1)
plt.scatter(ini_ps[:,plot_idx[i*3+j][0]], ini_ps[:,plot_idx[i*3+j][1]], s=5, alpha=0.1)
plt.locator_params(nbins=4)
From the trajectories we can generate the training sets:
In [12]:
train_p = 0.9 # proportion of training data
x_train, y_train, x_test, y_test, idx_train = dc.data.create_training_data(trajs, train_p = train_p, n_outputs=2)
In [13]:
dc.nn.save_training_data([x_train, y_train, x_test, y_test, idx_train], "rw")
We specify a model to train
In [107]:
model_description = {"data": "rw",
"control": dc.nn.DTHETA,
"nlayers": 3,
"units": 32,
"output_mode": dc.nn.OUTPUT_LOG,
"dropout": False,
"batch_size": 8,
"epochs": 32,
"lr": 0.001,
"input_vars" : 6,
"hidden_nonlinearity": "ReLu"}
In [108]:
dc.nn.train(model_description)
In [14]:
model_description = {"data": "rw",
"control": dc.nn.THRUST,
"nlayers": 3,
"units": 32,
"output_mode": dc.nn.OUTPUT_LOG,
"dropout": False,
"batch_size": 8,
"epochs": 32,
"lr": 0.001,
"input_vars" : 6,
"hidden_nonlinearity": "ReLu"}
dc.nn.train(model_description)
In [15]:
model_th = dc.nn.load_model('nets/rw/0/ReLu_outputLog_3_32.model')
network_th = dc.nn.load_network(model_th, base_dir='')
In [16]:
model_dth = dc.nn.load_model('nets/rw/1/ReLu_outputLog_3_32.model')
network_dth = dc.nn.load_network(model_dth, base_dir='')
networks = [(model_th, network_th),
(model_dth, network_dth)]
In [17]:
data = dc.nn.load_training_data(model_dth, '')
In [ ]:
In [ ]:
In [33]:
plt.rcParams['figure.figsize'] = [20,5*len(networks)]
fig = plt.figure()
batch = 1000
b = 20
traj_length = 100
for s, dset in enumerate(['train', 'test']):
for i,(model,network) in enumerate(networks):
plt.subplot(len(networks), 2, i*2+s+1)
u = np.zeros((batch,len(networks)))
network_input = data['X_'+dset][b*batch:(b+1)*batch,:]
ui = network['pred'](network_input)
u[:,i] = ui[:,0]
u = dc.nn.postprocess(model, u)
u_gt = data['Y_'+dset][b*batch:(b+1)*batch,:].copy()
u_gt = dc.nn.postprocess(model, u_gt)
for j in range(5):
label_1, = plt.plot(np.arange(traj_length)+j*traj_length,u_gt[j*traj_length:(j+1)*traj_length,i], c=sns.color_palette()[0])
plt.gca().get_xaxis().set_ticks([])
plt.ylabel(r'$' + 'u_'+str(i+1)+'$')
ylims = plt.ylim()
plt.subplot(len(networks), 2, i*2+s+1)
for j in range(5):
label_2, = plt.plot(np.arange(traj_length)+j*traj_length, u[j*traj_length:(j+1)*traj_length,i], c=sns.color_palette()[1])
#plt.locator_params(axis='y', nbins=1)
plt.gca().get_xaxis().set_ticks([])
plt.ylim(ylims)
plt.xlabel(dset)
plt.figlegend([label_1,label_2], ['Optimal control', 'DNN predictions'], loc='upper center', ncol=2)
Out[33]:
In [ ]: