-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoisson_model.py
More file actions
53 lines (43 loc) · 1.56 KB
/
Copy pathpoisson_model.py
File metadata and controls
53 lines (43 loc) · 1.56 KB
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
import torch
from torch.utils.data import DataLoader
from math import *
from deep_ritz import *
from data_sample import uniform
from plot import plotFunction
if __name__ == "__main__":
# Hyperparameters
rep_size = 64
capacity = 5
dataset_size = 100000
batch_size = 64
interval = [0, 1]
it = torch.Tensor(interval)
epochs = 20
learning_rate = 1e-3
#f = lambda x: torch.sin(x)
f = lambda x: torch.exp(x)
bound = torch.Tensor([1, 2])
decay = 1e-3
alpha = 1e1
# Model setup
dataset = torch.Tensor(uniform(interval[0], interval[1], dataset_size))
dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
print(f"Using {device} device")
model = NeuralNetwork(interval, rep_size, capacity).to(device)
loss_fn = PoisonLoss(f, bound, alpha=alpha)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=decay)
# Training
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train(dataloader, model, loss_fn, optimizer, it)
print("Done!")
torch.save(model.state_dict(), "poisson_model.pth")
print("Saved PyTorch Model State to poisson_model.pth")
model.eval()
g = lambda x: - exp(x) + e * x + 2
#g = lambda x: x + 1
#g = lambda x: sin(x)
print(model(it[0].unsqueeze(dim=0)))
print(model(it[1].unsqueeze(dim=0)))
plotFunction(model, g, interval=interval, image=[0.5, 3])