-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAleatoric_mle.py
56 lines (49 loc) · 1.31 KB
/
Aleatoric_mle.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
import numpy as np
import torch
from torch import nn
from matplotlib import pyplot as plt
w0 = 0.125
b0 = 5.
x_range = [-20, 60]
def load_dataset(n=150, n_tst=150):
np.random.seed(43)
def s(x):
g = (x - x_range[0]) / (x_range[1] - x_range[0])
return 3 * (0.25 + g**2.)
x = (x_range[1] - x_range[0]) * np.random.rand(n) + x_range[0]
eps = np.random.randn(n) * s(x)
y = (w0 * x * (1. + np.sin(x)) + b0) + eps
y = (y - y.mean()) / y.std()
idx = np.argsort(x)
x = x[idx]
y = y[idx]
return y[:, None], x[:, None]
y, x = load_dataset()
# Make a variable
X = torch.tensor(x, dtype=torch.float)
Y = torch.tensor(y, dtype=torch.float)
class MaximumLikelihood(nn.Module):
def __init__(self):
super().__init__()
self.out = nn.Sequential(
nn.Linear(1, 20),
nn.ReLU(),
nn.Linear(20, 1)
)
def forward(self, x):
return self.out(x)
epochs = 200
m = MaximumLikelihood()
optim = torch.optim.Adam(m.parameters(), lr=0.01)
for epoch in range(epochs):
optim.zero_grad()
y_pred = m(X)
loss = (0.5 * (y_pred - Y)**2).mean()
loss.backward()
optim.step()
m.eval()
y_estimate = m(X)
plt.figure(figsize=(10, 5))
plt.plot(x,y, 'b.', alpha=0.8)
plt.plot(x, y_estimate.detach().numpy(), 'r', alpha=0.6)
plt.show()