-
Notifications
You must be signed in to change notification settings - Fork 1
/
bma.py
70 lines (53 loc) · 2.21 KB
/
bma.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
import numpy as np
import torch
def threshold_loss(Y_reference, Y_model, threshold):
cost = ((Y_reference[Y_reference>threshold]-Y_model[Y_reference>threshold])**2).mean()
return cost
def bma(data, models: list, reference: str):
'''
Bayesian model averaging with loss =
'''
y_T = torch.tensor(data[reference])
m=[]
for i, model in enumerate(models):
m.append(torch.tensor(data[model]).reshape(-1,1))
m_k = torch.cat(m, axis=1)
n_m = m_k.shape[1]
log_s = torch.nn.Parameter(torch.ones(n_m))
log_w = torch.nn.Parameter(torch.ones(n_m))
softmax = torch.nn.Softmax(dim=0)
optimizer = torch.optim.Adam([log_s, log_w], lr=0.01)
for i in range(500):
optimizer.zero_grad()
w = softmax(log_w)
mixture_distribution = torch.distributions.Categorical(logits=log_w)
component_distribution = torch.distributions.Normal(m_k, log_s.exp())
gmm = torch.distributions.MixtureSameFamily(mixture_distribution, component_distribution)
log_post = gmm.log_prob(y_T).sum() + torch.distributions.Dirichlet(torch.ones(n_m)).log_prob(w)
loss = -log_post
loss.backward()
optimizer.step()
return w
def bma_threshold(data, models: list, reference, threshold):
y_T = torch.tensor(data[reference])
m=[]
for i, model in enumerate(models):
m.append(torch.tensor(data[model]).reshape(-1,1))
m_k = torch.cat(m, axis=1)
n_m = m_k.shape[1]
log_s = torch.nn.Parameter(torch.ones(n_m))
log_w = torch.nn.Parameter(torch.ones(n_m))
softmax = torch.nn.Softmax(dim=0)
optimizer = torch.optim.Adam([log_s, log_w], lr=0.01)
#loss_plot = []
for i in range(500):
optimizer.zero_grad()
w = softmax(log_w)
mixture_distribution = torch.distributions.Categorical(logits=log_w)
component_distribution = torch.distributions.Normal(m_k, log_s.exp())
gmm = torch.distributions.MixtureSameFamily(mixture_distribution, component_distribution)
y_predicted=(w*m_k).sum(axis=1)
loss = threshold_loss(y_T, y_predicted, threshold)
loss.backward()
optimizer.step()
return w