-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_simple_joints_lstm_fl_real3_bullet_nosim.py
executable file
·221 lines (173 loc) · 6.39 KB
/
train_simple_joints_lstm_fl_real3_bullet_nosim.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
import shutil
import numpy as np
from torch import nn, optim, torch
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torch.nn.functional as F
from simple_joints_lstm.dataset_real_smol_bullet_nosim import DatasetRealSmolBulletNoSim
from simple_joints_lstm.lstm_net_real_v3 import LstmNetRealv3
try:
from hyperdash import Experiment
hyperdash_support = True
except:
hyperdash_support = False
HIDDEN_NODES = 128
LSTM_LAYERS = 3
EXPERIMENT = 1
EPOCHS = 5
MODEL_PATH = "./trained_models/lstm_real_nosim_v1_exp{}_l{}_n{}.pt".format(
EXPERIMENT,
LSTM_LAYERS,
HIDDEN_NODES
)
MODEL_PATH_BEST = "./trained_models/lstm_real_nosim_v1_exp{}_l{}_n{}_best.pt".format(
EXPERIMENT,
LSTM_LAYERS,
HIDDEN_NODES
)
# TRAIN = True
# CONTINUE = False
BATCH_SIZE = 1
VIZ = False
dataset_train = DatasetRealSmolBulletNoSim(train=True)
dataset_test = DatasetRealSmolBulletNoSim(train=False)
# batch size has to be 1, otherwise the LSTM doesn't know what to do
dataloader_train = DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=False, num_workers=1)
dataloader_test = DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=False, num_workers=1)
net = LstmNetRealv3(
n_input_state_sim=0,
n_input_state_real=12,
n_input_actions=6,
nodes=HIDDEN_NODES,
layers=LSTM_LAYERS)
if torch.cuda.is_available():
net = net.cuda()
net = net.float()
def extract(dataslice):
x, y, epi = (Variable(dataslice["x"]).float(),
Variable(dataslice["y"]).float(),
dataslice["epi"].numpy()[0])
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
return x, y, epi
def printEpochLoss(epoch_idx, epoch_len, loss_epoch, diff_epoch):
loss_avg = round(float(loss_epoch) / epoch_len, 2)
diff_avg = round(float(diff_epoch) / epoch_len, 2)
print("epoch {}, "
"loss: {}, loss avg: {}, "
"diff: {}, diff avg: {}".format(
epoch_idx,
round(loss_epoch, 2),
loss_avg,
round(diff_epoch, 2),
diff_avg
))
if hyperdash_support:
exp.metric("epoch", epoch_idx)
exp.metric("loss train epoch avg", loss_avg)
exp.metric("diff train epoch avg", diff_avg)
def saveModel(state, epoch, loss_epoch, diff_epoch, is_best, epoch_len):
torch.save({
"epoch": epoch,
"epoch_len": epoch_len,
"state_dict": state,
"epoch_avg_loss": float(loss_epoch) / epoch_len,
"epoch_avg_diff": float(diff_epoch) / epoch_len
}, MODEL_PATH)
if is_best:
shutil.copyfile(MODEL_PATH, MODEL_PATH_BEST)
loss_function = nn.MSELoss()
if hyperdash_support:
exp = Experiment("[sim2real] lstm - real v3 - nosim")
exp.param("exp", EXPERIMENT)
exp.param("layers", LSTM_LAYERS)
exp.param("nodes", HIDDEN_NODES)
optimizer = optim.Adam(net.parameters())
loss_history = [np.inf] # very high loss because loss can't be empty for min()
for epoch in np.arange(EPOCHS):
loss_epoch = 0
diff_epoch = 0
epi_x_old = 0
x_buf = []
y_buf = []
for epi, data in enumerate(dataloader_train):
x, y, epi_x = extract(data)
net.zero_grad()
net.zero_hidden()
optimizer.zero_grad()
if epi_x != epi_x_old or epi == len(dataset_train) - 1:
x_cat = torch.cat(x_buf, 0).unsqueeze(1)
y_cat = torch.cat(y_buf, 0).unsqueeze(1)
delta = net.forward(x_cat)
# for idx in range(len(x_cat)):
# print(idx, "=")
# print("real t1_x:", np.around(x_cat[idx, 0, 12:24].cpu().data.numpy(), 2))
# print("sim_ t2_x:", np.around(x_cat[idx, 0, :12].cpu().data.numpy(), 2))
# print("action__x:", np.around(x_cat[idx, 0, 24:].cpu().data.numpy(), 2))
# print("real t2_x:",
# np.around(x_cat[idx, 0, :12].cpu().data.numpy() + y_cat[idx, 0].cpu().data.numpy(), 2))
# print("real t2_y:",
# np.around(x_cat[idx, 0, :12].cpu().data.numpy() + delta[idx, 0].cpu().data.numpy(), 2))
# print("delta___x:",
# np.around(y_cat[idx, 0].cpu().data.numpy(), 3))
# print("delta___y:",
# np.around(delta[idx, 0].cpu().data.numpy(), 3))
# print("===")
loss = loss_function(delta, y_cat)
loss.backward()
optimizer.step()
x_buf = []
y_buf = []
epi_x_old = epi_x
loss_episode = loss.clone().cpu().data.numpy()[0]
diff_episode = F.mse_loss(x_cat[:, :, :12], x_cat[:, :, :12]+y_cat).clone().cpu().data.numpy()[0]
loss.detach_()
net.hidden[0].detach_()
net.hidden[1].detach_()
if exp is not None:
exp.metric("loss episode", loss_episode)
exp.metric("diff episode", diff_episode)
exp.metric("epoch", epoch)
loss_epoch += loss_episode
diff_epoch += diff_episode
x_buf.append(x)
y_buf.append(y)
printEpochLoss(epoch, epi_x_old, loss_epoch, diff_epoch)
saveModel(
state=net.state_dict(),
epoch=epoch,
epoch_len=epi_x_old,
loss_epoch=loss_epoch,
diff_epoch=diff_epoch,
is_best=(loss_epoch < min(loss_history))
)
loss_history.append(loss_epoch)
# Validation step
loss_total = []
diff_total = []
epi_x_old = 0
x_buf = []
y_buf = []
for epi, data in enumerate(dataloader_test):
x, y, epi_x = extract(data)
net.zero_hidden()
if epi_x != epi_x_old or epi == len(dataset_test) - 1:
x_cat = torch.cat(x_buf, 0).unsqueeze(1)
y_cat = torch.cat(y_buf, 0).unsqueeze(1)
delta = net.forward(x_cat)
loss = loss_function(delta, y_cat)
loss_total.append(loss.clone().cpu().data.numpy()[0])
diff_total.append(F.mse_loss(x_cat[:, :, :12], x_cat[:, :, :12]+y_cat).clone().cpu().data.numpy()[0])
x_buf = []
y_buf = []
epi_x_old = epi_x
x_buf.append(x)
y_buf.append(y)
if hyperdash_support:
exp.metric("loss test mean", np.mean(loss_total))
exp.metric("diff test mean", np.mean(diff_total))
exp.metric("epoch", epoch)
# Cleanup and mark that the experiment successfully completed
if hyperdash_support:
exp.end()