-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain_darcy.py
94 lines (63 loc) · 2.5 KB
/
train_darcy.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from functools import reduce
from functools import partial
import random
import gc
from timeit import default_timer
from utilities3 import *
from Adam import Adam
def train_model(model,train_loader, val_loader, test_loader,ntrain,nval,ntest,s,wieght_path,T_f=10,step=1,batch_size=20,epochs=150,learning_rate= 0.0001,\
scheduler_step= 50,scheduler_gamma= 0.5,device = 'cuda', weight_decay = 1e-3):
optimizer = Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay,amsgrad = False)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=scheduler_step, gamma=scheduler_gamma)
best_error = 100000.0
myloss = LpLoss(size_average=False)
for ep in range(epochs):
model.train()
t1 = default_timer()
train_l2 = 0
for x, y in train_loader:
x, y = x.cuda(), y.cuda()
batch_size = x.shape[0]
optimizer.zero_grad()
out = model(x).reshape(batch_size, s, s)
loss = myloss(out.view(batch_size,-1), y.view(batch_size,-1))
loss.backward()
optimizer.step()
train_l2 += loss.item()
del x,y,out,loss
gc.collect()
torch.cuda.empty_cache()
scheduler.step()
model.eval()
val_l2 = 0.0
with torch.no_grad():
for x, y in val_loader:
x, y = x.cuda(), y.cuda()
batch_size = x.shape[0]
out = model(x).reshape(batch_size, s, s)
val_l2 += myloss(out.view(batch_size,-1), y.view(batch_size,-1)).item()
train_l2/= ntrain
val_l2 /= nval
t2 = default_timer()
if best_error > val_l2:
print("..Saving Model..", best_error - val_l2)
best_error = val_l2
torch.save(model.state_dict(), wieght_path)
print(ep, t2-t1, train_l2, val_l2)
model.load_state_dict(torch.load(wieght_path))
model.eval()
test_l2 = 0.0
with torch.no_grad():
for x, y in test_loader:
x, y = x.cuda(), y.cuda()
batch_size = x.shape[0]
out = model(x).reshape(batch_size, s, s)
test_l2 += myloss(out.view(batch_size,-1), y.view(batch_size,-1)).item()
test_l2 /= ntest
t2 = default_timer()
print(ep, t2-t1, "Test Error", test_l2)