In [4]:
import matplotlib.pyplot as plt
%matplotlib inline
In [5]:
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 [6]:
import glob
import pickle
from tqdm import tqdm
files = glob.glob('../data/simple/*pic')
In [7]:
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', 'm', 'u1', 'u2']
df.columns = col_names
trajs.append(df)
In [8]:
ini_ps = np.vstack([t.values[0,:] for t in trajs])
In [9]:
for i in range(3):
for j in range(2):
if i!=0 or j!= 0:
plt.subplot(2,3,i*2+j)
plt.hist(ini_ps[:,i*2+j],59)
plt.locator_params(nbins=4)
plt.tight_layout()
From the trajectories we can generate the training sets:
In [10]:
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 [11]:
dc.nn.save_training_data([x_train, y_train, x_test, y_test, idx_train], "indirect_simple")
We specify a model to train
In [12]:
model_description = {"data": "indirect_simple",
"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" : 5,
"hidden_nonlinearity": "ReLu"}
In [13]:
dc.nn.train(model_description)
In [14]:
model_description = {"data": "indirect_simple",
"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" : 5,
"hidden_nonlinearity": "ReLu"}
dc.nn.train(model_description)
In [19]:
model_th = dc.nn.load_model('nets/indirect_simple/0/ReLu_outputLog_3_32.model')
network_th = dc.nn.load_network(model_th, base_dir='')
In [20]:
model_dth = dc.nn.load_model('nets/indirect_simple/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 [21]:
data = dc.nn.load_training_data(model_dth, '')
In [22]:
plt.rcParams['figure.figsize'] = [20,5*len(networks)]
fig = plt.figure()
batch = 1000
b = 10
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[22]:
In [248]:
from lasagne import layers
params = layers.get_all_params(network_dth['layers'])
In [249]:
params = [(params[2*p].get_value(),
params[2*p+1].get_value()) for p in range(int(len(params)/2))]
In [250]:
f = open('nn.params', 'wt')
f.write('L{0}\n'.format(len(params)))
for i, layer in enumerate(params):
f.write('W{0},{1},{2}\n'.format(i,layer[0].shape[1],layer[0].shape[0]))
for j in range(layer[0].shape[1]):
for k in range(layer[0].shape[0]):
f.write('{0:.16}\t'.format(layer[0][k,j]))
f.write('\n')
f.write('b{0},{1},{2}\n'.format(i,1,layer[1].shape[0]))
for w in layer[1]:
f.write('{0:.16}\t\n'.format(w))
f.close()
In [276]:
x = np.atleast_2d([0,2,0,1,0])
In [277]:
network_dth['pred'](x)
Out[277]: