forked from echowve/meshGraphNets_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
54 lines (39 loc) · 1.79 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
from dataset import FPC
from model.simulator import Simulator
import torch
from utils.noise import get_velocity_noise
import time
from utils.utils import NodeType
from torch_geometric.loader import DataLoader
import torch_geometric.transforms as T
dataset_dir = "/home/jlx/dataset/data"
batch_size = 20
noise_std=2e-2
print_batch = 10
save_batch = 200
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
simulator = Simulator(message_passing_num=15, node_input_size=11, edge_input_size=3, device=device)
optimizer= torch.optim.Adam(simulator.parameters(), lr=1e-4)
print('Optimizer initialized')
def train(model:Simulator, dataloader, optimizer):
for batch_index, graph in enumerate(dataloader):
graph = transformer(graph)
graph = graph.cuda()
node_type = graph.x[:, 0] #"node_type, cur_v, pressure, time"
velocity_sequence_noise = get_velocity_noise(graph, noise_std=noise_std, device=device)
predicted_acc, target_acc = model(graph, velocity_sequence_noise)
mask = torch.logical_or(node_type==NodeType.NORMAL, node_type==NodeType.OUTFLOW)
errors = ((predicted_acc - target_acc)**2)[mask]
loss = torch.mean(errors)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_index % print_batch == 0:
print('batch %d [loss %.2e]'%(batch_index, loss.item()))
if batch_index % save_batch == 0:
model.save_checkpoint()
if __name__ == '__main__':
dataset_fpc = FPC(dataset_dir=dataset_dir, split='train', max_epochs=50)
train_loader = DataLoader(dataset=dataset_fpc, batch_size=batch_size, num_workers=10)
transformer = T.Compose([T.FaceToEdge(), T.Cartesian(norm=False), T.Distance(norm=False)])
train(simulator, train_loader, optimizer)