forked from vgsatorras/egnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_nbody.py
274 lines (229 loc) · 11.8 KB
/
main_nbody.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
import argparse
import torch
from n_body_system.dataset_nbody import NBodyDataset
from n_body_system.model import GNN, EGNN, Baseline, Linear, EGNN_vel, Linear_dynamics, RF_vel
import os
from torch import nn, optim
import json
import time
parser = argparse.ArgumentParser(description='VAE MNIST Example')
parser.add_argument('--exp_name', type=str, default='exp_1', metavar='N', help='experiment_name')
parser.add_argument('--batch_size', type=int, default=100, metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=10000, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log_interval', type=int, default=1, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--test_interval', type=int, default=5, metavar='N',
help='how many epochs to wait before logging test')
parser.add_argument('--outf', type=str, default='n_body_system/logs', metavar='N',
help='folder to output vae')
parser.add_argument('--lr', type=float, default=5e-4, metavar='N',
help='learning rate')
parser.add_argument('--nf', type=int, default=64, metavar='N',
help='learning rate')
parser.add_argument('--model', type=str, default='egnn_vel', metavar='N',
help='available models: gnn, baseline, linear, linear_vel, se3_transformer, egnn_vel, rf_vel, tfn')
parser.add_argument('--attention', type=int, default=0, metavar='N',
help='attention in the ae model')
parser.add_argument('--n_layers', type=int, default=4, metavar='N',
help='number of layers for the autoencoder')
parser.add_argument('--degree', type=int, default=2, metavar='N',
help='degree of the TFN and SE3')
parser.add_argument('--max_training_samples', type=int, default=3000, metavar='N',
help='maximum amount of training samples')
parser.add_argument('--dataset', type=str, default="nbody_small", metavar='N',
help='nbody_small, nbody')
parser.add_argument('--sweep_training', type=int, default=0, metavar='N',
help='0 nor sweep, 1 sweep, 2 sweep small')
parser.add_argument('--time_exp', type=int, default=0, metavar='N',
help='timing experiment')
parser.add_argument('--weight_decay', type=float, default=1e-12, metavar='N',
help='timing experiment')
parser.add_argument('--div', type=float, default=1, metavar='N',
help='timing experiment')
parser.add_argument('--norm_diff', type=eval, default=False, metavar='N',
help='normalize_diff')
parser.add_argument('--tanh', type=eval, default=False, metavar='N',
help='use tanh')
time_exp_dic = {'time': 0, 'counter': 0}
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if args.cuda else "cpu")
loss_mse = nn.MSELoss()
print(args)
try:
os.makedirs(args.outf)
except OSError:
pass
try:
os.makedirs(args.outf + "/" + args.exp_name)
except OSError:
pass
def get_velocity_attr(loc, vel, rows, cols):
diff = loc[cols] - loc[rows]
norm = torch.norm(diff, p=2, dim=1).unsqueeze(1)
u = diff/norm
va, vb = vel[rows] * u, vel[cols] * u
va, vb = torch.sum(va, dim=1).unsqueeze(1), torch.sum(vb, dim=1).unsqueeze(1)
return va
def main():
dataset_train = NBodyDataset(partition='train', dataset_name=args.dataset,
max_samples=args.max_training_samples)
loader_train = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, drop_last=True)
dataset_val = NBodyDataset(partition='val', dataset_name="nbody_small")
loader_val = torch.utils.data.DataLoader(dataset_val, batch_size=args.batch_size, shuffle=False, drop_last=False)
dataset_test = NBodyDataset(partition='test', dataset_name="nbody_small")
loader_test = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=False, drop_last=False)
if args.model == 'gnn':
model = GNN(input_dim=6, hidden_nf=args.nf, n_layers=args.n_layers, device=device, recurrent=True)
elif args.model == 'egnn_vel':
model = EGNN_vel(in_node_nf=1, in_edge_nf=2, hidden_nf=args.nf, device=device, n_layers=args.n_layers, recurrent=True, norm_diff=args.norm_diff, tanh=args.tanh)
elif args.model == 'baseline':
model = Baseline()
elif args.model == 'linear_vel':
model = Linear_dynamics(device=device)
elif args.model == 'linear':
model = Linear(6, 3, device=device)
elif args.model == 'rf_vel':
model = RF_vel(hidden_nf=args.nf, edge_attr_nf=2, device=device, act_fn=nn.SiLU(), n_layers=args.n_layers)
elif args.model == 'se3_transformer' or args.model == 'tfn':
from n_body_system.se3_dynamics.dynamics import OurDynamics as SE3_Transformer
model = SE3_Transformer(n_particles=5, n_dimesnion=3, nf=int(args.nf/args.degree), n_layers=args.n_layers, model=args.model, num_degrees=args.degree, div=1)
if torch.cuda.is_available():
model = model.cuda()
else:
raise Exception("Wrong model specified")
print(model)
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
results = {'epochs': [], 'losess': []}
best_val_loss = 1e8
best_test_loss = 1e8
best_epoch = 0
for epoch in range(0, args.epochs):
train(model, optimizer, epoch, loader_train)
if epoch % args.test_interval == 0:
#train(epoch, loader_train, backprop=False)
val_loss = train(model, optimizer, epoch, loader_val, backprop=False)
test_loss = train(model, optimizer, epoch, loader_test, backprop=False)
results['epochs'].append(epoch)
results['losess'].append(test_loss)
if val_loss < best_val_loss:
best_val_loss = val_loss
best_test_loss = test_loss
best_epoch = epoch
print("*** Best Val Loss: %.5f \t Best Test Loss: %.5f \t Best epoch %d" % (best_val_loss, best_test_loss, best_epoch))
json_object = json.dumps(results, indent=4)
with open(args.outf + "/" + args.exp_name + "/losess.json", "w") as outfile:
outfile.write(json_object)
return best_val_loss, best_test_loss, best_epoch
def train(model, optimizer, epoch, loader, backprop=True):
if backprop:
model.train()
else:
model.eval()
res = {'epoch': epoch, 'loss': 0, 'coord_reg': 0, 'counter': 0}
for batch_idx, data in enumerate(loader):
batch_size, n_nodes, _ = data[0].size()
data = [d.to(device) for d in data]
data = [d.view(-1, d.size(2)) for d in data]
loc, vel, edge_attr, charges, loc_end = data
edges = loader.dataset.get_edges(batch_size, n_nodes)
edges = [edges[0].to(device), edges[1].to(device)]
optimizer.zero_grad()
if args.time_exp:
torch.cuda.synchronize()
t1 = time.time()
if args.model == 'gnn':
nodes = torch.cat([loc, vel], dim=1)
loc_pred = model(nodes, edges, edge_attr)
elif args.model == 'egnn':
nodes = torch.ones(loc.size(0), 1).to(device) # all input nodes are set to 1
rows, cols = edges
loc_dist = torch.sum((loc[rows] - loc[cols])**2, 1).unsqueeze(1) # relative distances among locations
vel_attr = get_velocity_attr(loc, vel, rows, cols).detach()
edge_attr = torch.cat([edge_attr, loc_dist, vel_attr], 1).detach() # concatenate all edge properties
loc_pred = model(nodes, loc.detach(), edges, edge_attr)
elif args.model == 'egnn_vel':
nodes = torch.sqrt(torch.sum(vel ** 2, dim=1)).unsqueeze(1).detach()
rows, cols = edges
loc_dist = torch.sum((loc[rows] - loc[cols])**2, 1).unsqueeze(1) # relative distances among locations
edge_attr = torch.cat([edge_attr, loc_dist], 1).detach() # concatenate all edge properties
loc_pred = model(nodes, loc.detach(), edges, vel, edge_attr)
elif args.model == 'baseline':
backprop = False
loc_pred = model(loc)
elif args.model == 'linear':
loc_pred = model(torch.cat([loc, vel], dim=1))
elif args.model == 'linear_vel':
loc_pred = model(loc, vel)
elif args.model == 'se3_transformer' or args.model == 'tfn':
loc_pred = model(loc, vel, charges)
elif args.model == 'rf_vel':
rows, cols = edges
vel_norm = torch.sqrt(torch.sum(vel ** 2, dim=1).unsqueeze(1)).detach()
loc_dist = torch.sum((loc[rows] - loc[cols]) ** 2, 1).unsqueeze(1)
edge_attr = torch.cat([edge_attr, loc_dist], 1).detach()
loc_pred = model(vel_norm, loc.detach(), edges, vel, edge_attr)
else:
raise Exception("Wrong model")
if args.time_exp:
torch.cuda.synchronize()
t2 = time.time()
time_exp_dic['time'] += t2 - t1
time_exp_dic['counter'] += 1
print("Forward average time: %.6f" % (time_exp_dic['time'] / time_exp_dic['counter']))
loss = loss_mse(loc_pred, loc_end)
if backprop:
loss.backward()
optimizer.step()
res['loss'] += loss.item()*batch_size
res['counter'] += batch_size
if batch_idx % args.log_interval == 0 and (args.model == "se3_transformer" or args.model == "tfn"):
print('===> {} Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(loader.dataset.partition,
epoch, batch_idx * batch_size, len(loader.dataset),
100. * batch_idx / len(loader),
loss.item()))
if not backprop:
prefix = "==> "
else:
prefix = ""
print('%s epoch %d avg loss: %.5f' % (prefix+loader.dataset.partition, epoch, res['loss'] / res['counter']))
return res['loss'] / res['counter']
def main_sweep():
training_samples = [100, 200, 400, 800, 1600, 3200, 6400, 12800, 25000, 50000]
n_epochs = [2000, 2000, 4000, 5000, 8000, 10000, 8000, 6000, 4000, 2000]
if args.model == 'egnn_vel':
n_epochs = [4000, 4000, 2000, 2000, 2000, 1500, 1500, 1500, 1000, 1000] # up to the 5th updated
elif args.model == 'kholer_vel':
n_epochs = [8000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 4000, 2000] # up to the 5th
if args.sweep_training == 2:
training_samples = training_samples[0:5]
n_epochs = n_epochs[0:5]
elif args.sweep_training == 3:
training_samples = training_samples[6:]
n_epochs = n_epochs[6:]
elif args.sweep_training == 4:
training_samples = training_samples[8:]
n_epochs = n_epochs[8:]
results = {'tr_samples': [], 'test_loss': [], 'best_epochs': []}
for epochs, tr_samples in zip(n_epochs, training_samples):
args.epochs = epochs
args.max_training_samples = tr_samples
args.test_interval = max(int(10000/tr_samples), 1)
best_val_loss, best_test_loss, best_epoch = main()
results['tr_samples'].append(tr_samples)
results['best_epochs'].append(best_epoch)
results['test_loss'].append(best_test_loss)
print("\n####### Results #######")
print(results)
print("Results for %d epochs and %d # training samples \n" % (epochs, tr_samples))
if __name__ == "__main__":
if args.sweep_training:
main_sweep()
else:
main()