-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss_functions.py
More file actions
162 lines (143 loc) · 5.51 KB
/
loss_functions.py
File metadata and controls
162 lines (143 loc) · 5.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from typing import Callable
import torch
import torch.distributions.beta as dist_beta
from torch import Tensor, lgamma, nn
class PonderLoss(nn.Module):
def __init__(
self,
task_loss_fn: Callable,
scale_reg: float,
lambda_prior: float,
max_ponder_steps: int,
):
"""
Args:
scale_reg: Weight for the regularization loss term.
lambda_reg: Parameterizes the (Bernoulli) prior.
task_loss_fn: Loss function for the actual task (e.g. MSE or CE).
max_ponder_steps
"""
super().__init__()
self.task_loss_fn = task_loss_fn
self.scale_reg = scale_reg
self.lambda_prior = lambda_prior
self.KL = nn.KLDivLoss(reduction="batchmean")
prior = lambda_prior * (1 - lambda_prior) ** torch.arange(max_ponder_steps)
prior = prior / prior.sum()
self.register_buffer("log_prior", prior.log())
def forward(
self,
preds: Tensor,
p: Tensor,
halted_at: Tensor,
targets: Tensor,
regularization_warmup_factor: float,
**kwargs
):
"""
Args:
`preds`: Predictions of shape (ponder_steps, batch_size, logits)
`p`: Cumulative probability of reaching and then stopping at each
step of shape (step, batch)
`halted_at`: Indices of steps where each sample actually stopped of
shape (batch)
`targets`: Targets of shape (batch_size)
`regularization_warmup_factor`: Factor used to warm up
regularization loss term.
"""
n_steps, batch_size, _ = preds.shape
# Reconstruction term
task_losses = self.task_loss_fn(
preds.view(
-1, preds.size(-1)
), # View pred steps as individual classifications.
targets.repeat(n_steps), # Repeat targets as needed to match.
reduction="none",
).view(n_steps, batch_size)
l_rec = (task_losses * p).sum(0).mean()
# Regularization term
p_t = p.transpose(1, 0)
l_reg = self.KL(self.log_prior[:n_steps].expand_as(p_t), p_t)
return l_rec, regularization_warmup_factor * self.scale_reg * l_reg
class PonderBayesianLoss(nn.Module):
def __init__(
self,
task_loss_fn: Callable,
beta_prior: tuple[float, float],
max_ponder_steps: int,
scale_reg: float,
):
super().__init__()
self.task_loss_fn = task_loss_fn
self.beta_prior = beta_prior
self.KL = nn.KLDivLoss(reduction="none")
self.scale_reg = scale_reg
self.prior = dist_beta.Beta(beta_prior[0], beta_prior[1])
def forward(
self,
preds: Tensor,
p: Tensor,
halted_at: Tensor,
targets: Tensor,
regularization_warmup_factor: float,
**kwargs
):
"""
Args:
`preds`: Predictions of shape (ponder_steps, batch_size, logits)
`p`: Cumulative probability of reaching and then stopping at each
step of shape (step, batch)
`halted_at`: Indices of steps where each sample actually stopped of
shape (batch)
`targets`: Targets of shape (batch_size)
'lambdas': Lambdas of shape (step, batch_size)
`regularization_warmup_factor`: Factor used to warm up
regularization loss term.
"""
assert "lambdas" in kwargs, "Must provide lambdas!"
lambdas = kwargs["lambdas"]
alphas, betas = kwargs["beta_params"]
n_steps, batch_size, _ = preds.shape
# Reconstruction term
task_losses = self.task_loss_fn(
preds.view(
-1, preds.size(-1)
), # View pred steps as individual classifications.
targets[
torch.arange(targets.size(0)).repeat(n_steps)
], # Repeat targets as needed to match.
reduction="none",
).view(n_steps, batch_size)
l_rec = torch.einsum("ij,ij->j", p, task_losses).mean()
# Regularization term
# TODO : Make hyperparameter to decide if you want to use approximation
# l_reg_alt = (
# self.KL(
# self.prior.rsample(sample_shape=(batch_size, n_steps))
# .to(lambdas.device)
# .log(),
# lambdas.transpose(1, 0), # type: ignore
# )
# .sum(1)
# .mean()
# ) # Sum over the number of steps, then mean over the batch.
def lbeta(x, y):
# As derivable from:
# https://en.wikipedia.org/wiki/Beta_function#:~:text=.%5B1%5D-,A%20key%20property,-of%20the%20beta
return lgamma(x) + lgamma(y) - lgamma(x + y)
a_prime, b_prime = torch.Tensor(self.beta_prior).to(lambdas.device)
a, b = alphas, betas
# Analytically computing KL-divergence, according to formula in
# https://en.wikipedia.org/wiki/Beta_distribution#:~:text=The%20relative%20entropy%2C%20or%20Kullback%E2%80%93Leibler%20divergence%20DKL(X1%20%7C%7C%20X2)
l_reg = (
(
lbeta(a_prime, b_prime)
- lbeta(a, b)
+ (a - a_prime) * torch.digamma(a)
+ (b - b_prime) * torch.digamma(b)
+ (a_prime - a + b_prime - b) * torch.digamma(a + b)
)
.sum(0)
.mean()
)
return l_rec, regularization_warmup_factor * self.scale_reg * l_reg