-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_gp.py
58 lines (46 loc) · 1.43 KB
/
test_gp.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
import numpy as np
import torch
from torch.optim import SGD
from gp import GP
from binary_laplace_gpc import BinaryLaplaceGPC
def test_gp():
X = torch.randn(10, 1)
f = torch.sin(X * 2 * np.pi / 4).flatten()
y = f + torch.randn_like(f) * 0.1
y = y[:, None]
grid = torch.linspace(-5, 5, 20)[:, None]
gp = GP()
gp.fit(X, y)
mu, var = gp.forward(grid)
mu = mu.detach().numpy().flatten()
std = torch.sqrt(var).detach().numpy().flatten()
def test_gp_opt():
X = torch.randn(10, 1)
f = torch.sin(X * 2 * np.pi / 4).flatten()
y = f + torch.randn_like(f) * 0.1
y = y[:, None]
grid = torch.linspace(-5, 5, 20)[:, None]
gp = GP()
opt = SGD(gp.parameters(), lr=0.01)
for i in range(2):
d_train = gp.train_step(X, y, opt)
def test_gpc():
X = torch.randn(10, 1)
f = torch.sin(X * 3 * np.pi / 4)
y = (f > 0.).int() * 2 - 1
grid = torch.linspace(-5, 5, 20)[:, None]
gp = BinaryLaplaceGPC()
gp.fit(X, y)
mu, var, pi = gp.forward(grid)
mu = mu.detach().numpy().flatten()
std = torch.sqrt(var).detach().numpy().flatten()
pi = pi.detach().numpy().flatten()
def test_gpc_opt():
X = torch.randn(10, 1)
f = torch.sin(X * 3 * np.pi / 4)
y = (f > 0.).int() * 2 - 1
grid = torch.linspace(-5, 5, 20)[:, None]
gp = BinaryLaplaceGPC()
opt = SGD(gp.parameters(), lr=0.0001)
for i in range(2):
d_train = gp.train_step(X, y, opt)