forked from kumar-shridhar/PyTorch-BayesianCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
29 lines (19 loc) · 836 Bytes
/
metrics.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
import numpy as np
import torch.nn.functional as F
from torch import nn
import torch
class ELBO(nn.Module):
def __init__(self, train_size):
super(ELBO, self).__init__()
self.train_size = train_size
def forward(self, input, target, kl, kl_weight=1.0):
assert not target.requires_grad
return F.nll_loss(input, target, size_average=True) * self.train_size + kl_weight * kl
def lr_linear(epoch_num, decay_start, total_epochs, start_value):
if epoch_num < decay_start:
return start_value
return start_value*float(total_epochs-epoch_num)/float(total_epochs-decay_start)
def acc(outputs, targets):
return np.mean(outputs.cpu().numpy().argmax(axis=1) == targets.data.cpu().numpy())
def calculate_kl(log_alpha):
return 0.5 * torch.sum(torch.log1p(torch.exp(-log_alpha)))