-
Notifications
You must be signed in to change notification settings - Fork 6
/
main_simulation_simple_no.py
235 lines (199 loc) · 10.1 KB
/
main_simulation_simple_no.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
import argparse
from argparse import Namespace
import torch
import torch.utils.data
from simulation.dataset_simple import NBodyDynamicsDataset as SimulationDataset
from model.egno import EGNO
from utils import EarlyStopping
import os
from torch import nn, optim
import json
import random
import numpy as np
parser = argparse.ArgumentParser(description='EGNO')
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='exp_results', metavar='N',
help='folder to output the json log file')
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='hidden dim')
parser.add_argument('--model', type=str, default='hier', metavar='N')
parser.add_argument('--n_layers', type=int, default=4, metavar='N',
help='number of layers for the autoencoder')
parser.add_argument('--max_training_samples', type=int, default=3000, metavar='N',
help='maximum amount of training samples')
parser.add_argument('--weight_decay', type=float, default=1e-12, metavar='N',
help='timing experiment')
parser.add_argument('--data_dir', type=str, default='',
help='Data directory.')
parser.add_argument('--dropout', type=float, default=0.5,
help='Dropout rate (1 - keep probability).')
parser.add_argument("--config_by_file", default=None, nargs="?", const='', type=str, )
parser.add_argument('--lambda_link', type=float, default=1,
help='The weight of the linkage loss.')
parser.add_argument('--n_cluster', type=int, default=3,
help='The number of clusters.')
parser.add_argument('--flat', action='store_true', default=False,
help='flat MLP')
parser.add_argument('--interaction_layer', type=int, default=3,
help='The number of interaction layers per block.')
parser.add_argument('--pooling_layer', type=int, default=3,
help='The number of pooling layers in EGPN.')
parser.add_argument('--decoder_layer', type=int, default=1,
help='The number of decoder layers.')
parser.add_argument('--norm', action='store_true', default=False,
help='Use norm in EGNO')
parser.add_argument('--num_timesteps', type=int, default=1,
help='The number of time steps.')
parser.add_argument('--time_emb_dim', type=int, default=32,
help='The dimension of time embedding.')
parser.add_argument('--num_modes', type=int, default=2,
help='The number of modes.')
time_exp_dic = {'time': 0, 'counter': 0}
args = parser.parse_args()
if args.config_by_file is not None:
if len(args.config_by_file) == 0:
job_param_path = './configs/config_simulation_simple_no.json'
else:
job_param_path = args.config_by_file
with open(job_param_path, 'r') as f:
hyper_params = json.load(f)
# Only update existing keys
args = vars(args)
args.update((k, v) for k, v in hyper_params.items() if k in args)
args = Namespace(**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(reduction='none')
print(args)
try:
os.makedirs(args.outf)
except OSError:
pass
try:
os.makedirs(args.outf + "/" + args.exp_name)
except OSError:
pass
def main():
# fix seed
seed = args.seed
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
dataset_train = SimulationDataset(partition='train', max_samples=args.max_training_samples,
data_dir=args.data_dir, num_timesteps=args.num_timesteps)
loader_train = torch.utils.data.DataLoader(dataset_train, batch_size=args.batch_size, shuffle=True, drop_last=True,
num_workers=0)
dataset_val = SimulationDataset(partition='val',
data_dir=args.data_dir, num_timesteps=args.num_timesteps)
loader_val = torch.utils.data.DataLoader(dataset_val, batch_size=args.batch_size, shuffle=False, drop_last=False,
num_workers=0)
dataset_test = SimulationDataset(partition='test',
data_dir=args.data_dir, num_timesteps=args.num_timesteps)
loader_test = torch.utils.data.DataLoader(dataset_test, batch_size=args.batch_size, shuffle=False, drop_last=False,
num_workers=0)
if args.model == 'egno':
model = EGNO(n_layers=args.n_layers, in_node_nf=1, in_edge_nf=2, hidden_nf=args.nf, device=device,
with_v=True, flat=args.flat, activation=nn.SiLU(), norm=args.norm, use_time_conv=True,
num_modes=args.num_modes, num_timesteps=args.num_timesteps, time_emb_dim=args.time_emb_dim)
else:
raise NotImplementedError('Unknown model:', args.model)
print(model)
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
model_save_path = args.outf + '/' + args.exp_name + '/' + 'saved_model.pth'
print(f'Model saved to {model_save_path}')
early_stopping = EarlyStopping(patience=50, verbose=True, path=model_save_path)
results = {'eval epoch': [], 'val loss': [], 'test loss': [], 'train loss': []}
best_val_loss = 1e8
best_test_loss = 1e8
best_epoch = 0
best_train_loss = 1e8
for epoch in range(0, args.epochs):
train_loss = train(model, optimizer, epoch, loader_train)
results['train loss'].append(train_loss)
if epoch % args.test_interval == 0:
val_loss = train(model, optimizer, epoch, loader_val, backprop=False)
test_loss = train(model, optimizer, epoch, loader_test, backprop=False)
results['eval epoch'].append(epoch)
results['val loss'].append(val_loss)
results['test loss'].append(test_loss)
if val_loss < best_val_loss:
best_val_loss = val_loss
best_test_loss = test_loss
best_train_loss = train_loss
best_epoch = epoch
# Save model is move to early stopping.
print("*** Best Val Loss: %.5f \t Best Test Loss: %.5f \t Best epoch %d"
% (best_val_loss, best_test_loss, best_epoch))
early_stopping(val_loss, model)
if early_stopping.early_stop:
print("Early Stopping.")
break
json_object = json.dumps(results, indent=4)
with open(args.outf + "/" + args.exp_name + "/loss.json", "w") as outfile:
outfile.write(json_object)
return best_train_loss, 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, 'counter': 0, 'lp_loss': 0}
for batch_idx, data in enumerate(loader):
data = [d.to(device) for d in data]
loc, vel, edge_attr, charges, loc_end = data
n_nodes = 5
loc_mean = loc.mean(dim=1, keepdim=True).repeat(1, n_nodes, 1).view(-1, loc.size(2)) # [BN, 3]
loc = loc.view(-1, loc.shape[-1])
vel = vel.view(-1, vel.shape[-1])
edge_attr = edge_attr.view(-1, edge_attr.shape[-1])
batch_size = loc.shape[0] // n_nodes
loc_end = loc_end.view(batch_size * n_nodes, args.num_timesteps, 3).transpose(0, 1).contiguous().view(-1, 3)
edges = loader.dataset.get_edges(batch_size, n_nodes)
edges = [edges[0].to(device), edges[1].to(device)]
optimizer.zero_grad()
if args.model == 'egno':
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, vel_pred, _ = model(loc, nodes, edges, edge_attr, v=vel, loc_mean=loc_mean)
else:
raise Exception("Wrong model")
losses = loss_mse(loc_pred, loc_end).view(args.num_timesteps, batch_size * n_nodes, 3)
losses = torch.mean(losses, dim=(1, 2))
loss = torch.mean(losses)
if backprop:
loss.backward()
optimizer.step()
res['loss'] += losses[-1].item() * batch_size
res['counter'] += batch_size
if not backprop:
prefix = "==> "
else:
prefix = ""
print('%s epoch %d avg loss: %.5f avg lploss: %.5f'
% (prefix+loader.dataset.partition, epoch, res['loss'] / res['counter'], res['lp_loss'] / res['counter']))
return res['loss'] / res['counter']
if __name__ == "__main__":
best_train_loss, best_val_loss, best_test_loss, best_epoch = main()
print("best_train = %.6f" % best_train_loss)
print("best_val = %.6f" % best_val_loss)
print("best_test = %.6f" % best_test_loss)
print("best_epoch = %d" % best_epoch)
print("best_train = %.6f, best_val = %.6f, best_test = %.6f, best_epoch = %d"
% (best_train_loss, best_val_loss, best_test_loss, best_epoch))