-
Notifications
You must be signed in to change notification settings - Fork 67
/
train.py
205 lines (168 loc) · 6.76 KB
/
train.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
import argparse
import torch
import utils
import datetime
import os
import pickle
import numpy as np
import logging
from torch.utils import data
import torch.nn.functional as F
import modules
parser = argparse.ArgumentParser()
parser.add_argument('--batch-size', type=int, default=1024,
help='Batch size.')
parser.add_argument('--epochs', type=int, default=100,
help='Number of training epochs.')
parser.add_argument('--learning-rate', type=float, default=5e-4,
help='Learning rate.')
parser.add_argument('--encoder', type=str, default='small',
help='Object extrator CNN size (e.g., `small`).')
parser.add_argument('--sigma', type=float, default=0.5,
help='Energy scale.')
parser.add_argument('--hinge', type=float, default=1.,
help='Hinge threshold parameter.')
parser.add_argument('--hidden-dim', type=int, default=512,
help='Number of hidden units in transition MLP.')
parser.add_argument('--embedding-dim', type=int, default=2,
help='Dimensionality of embedding.')
parser.add_argument('--action-dim', type=int, default=4,
help='Dimensionality of action space.')
parser.add_argument('--num-objects', type=int, default=5,
help='Number of object slots in model.')
parser.add_argument('--ignore-action', action='store_true', default=False,
help='Ignore action in GNN transition model.')
parser.add_argument('--copy-action', action='store_true', default=False,
help='Apply same action to all object slots.')
parser.add_argument('--decoder', action='store_true', default=False,
help='Train model using decoder and pixel-based loss.')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disable CUDA training.')
parser.add_argument('--seed', type=int, default=42,
help='Random seed (default: 42).')
parser.add_argument('--log-interval', type=int, default=20,
help='How many batches to wait before logging'
'training status.')
parser.add_argument('--dataset', type=str,
default='data/shapes_train.h5',
help='Path to replay buffer.')
parser.add_argument('--name', type=str, default='none',
help='Experiment name.')
parser.add_argument('--save-folder', type=str,
default='checkpoints',
help='Path to checkpoints.')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
now = datetime.datetime.now()
timestamp = now.isoformat()
if args.name == 'none':
exp_name = timestamp
else:
exp_name = args.name
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
exp_counter = 0
save_folder = '{}/{}/'.format(args.save_folder, exp_name)
if not os.path.exists(save_folder):
os.makedirs(save_folder)
meta_file = os.path.join(save_folder, 'metadata.pkl')
model_file = os.path.join(save_folder, 'model.pt')
log_file = os.path.join(save_folder, 'log.txt')
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger()
logger.addHandler(logging.FileHandler(log_file, 'a'))
print = logger.info
pickle.dump({'args': args}, open(meta_file, "wb"))
device = torch.device('cuda' if args.cuda else 'cpu')
dataset = utils.StateTransitionsDataset(
hdf5_file=args.dataset)
train_loader = data.DataLoader(
dataset, batch_size=args.batch_size, shuffle=True, num_workers=4)
# Get data sample
obs = train_loader.__iter__().next()[0]
input_shape = obs[0].size()
model = modules.ContrastiveSWM(
embedding_dim=args.embedding_dim,
hidden_dim=args.hidden_dim,
action_dim=args.action_dim,
input_dims=input_shape,
num_objects=args.num_objects,
sigma=args.sigma,
hinge=args.hinge,
ignore_action=args.ignore_action,
copy_action=args.copy_action,
encoder=args.encoder).to(device)
model.apply(utils.weights_init)
optimizer = torch.optim.Adam(
model.parameters(),
lr=args.learning_rate)
if args.decoder:
if args.encoder == 'large':
decoder = modules.DecoderCNNLarge(
input_dim=args.embedding_dim,
num_objects=args.num_objects,
hidden_dim=args.hidden_dim // 16,
output_size=input_shape).to(device)
elif args.encoder == 'medium':
decoder = modules.DecoderCNNMedium(
input_dim=args.embedding_dim,
num_objects=args.num_objects,
hidden_dim=args.hidden_dim // 16,
output_size=input_shape).to(device)
elif args.encoder == 'small':
decoder = modules.DecoderCNNSmall(
input_dim=args.embedding_dim,
num_objects=args.num_objects,
hidden_dim=args.hidden_dim // 16,
output_size=input_shape).to(device)
decoder.apply(utils.weights_init)
optimizer_dec = torch.optim.Adam(
decoder.parameters(),
lr=args.learning_rate)
# Train model.
print('Starting model training...')
step = 0
best_loss = 1e9
for epoch in range(1, args.epochs + 1):
model.train()
train_loss = 0
for batch_idx, data_batch in enumerate(train_loader):
data_batch = [tensor.to(device) for tensor in data_batch]
optimizer.zero_grad()
if args.decoder:
optimizer_dec.zero_grad()
obs, action, next_obs = data_batch
objs = model.obj_extractor(obs)
state = model.obj_encoder(objs)
rec = torch.sigmoid(decoder(state))
loss = F.binary_cross_entropy(
rec, obs, reduction='sum') / obs.size(0)
next_state_pred = state + model.transition_model(state, action)
next_rec = torch.sigmoid(decoder(next_state_pred))
next_loss = F.binary_cross_entropy(
next_rec, next_obs,
reduction='sum') / obs.size(0)
loss += next_loss
else:
loss = model.contrastive_loss(*data_batch)
loss.backward()
train_loss += loss.item()
optimizer.step()
if args.decoder:
optimizer_dec.step()
if batch_idx % args.log_interval == 0:
print(
'Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data_batch[0]),
len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.item() / len(data_batch[0])))
step += 1
avg_loss = train_loss / len(train_loader.dataset)
print('====> Epoch: {} Average loss: {:.6f}'.format(
epoch, avg_loss))
if avg_loss < best_loss:
best_loss = avg_loss
torch.save(model.state_dict(), model_file)