| import torch |
| import torch.nn as nn |
|
|
| class KL_Loss(nn.Module): |
| def __init__(self, logvar_init=0.0, kl_weight=1.0, nll_loss_type='l1'): |
|
|
| super().__init__() |
| self.kl_weight = kl_weight |
| |
| self.logvar = nn.Parameter(torch.ones(size=()) * logvar_init) |
| if nll_loss_type == 'l1': |
| self.nll_loss = torch.nn.L1Loss(reduction='none') |
| elif nll_loss_type == 'l2': |
| self.nll_loss = torch.nn.MSELoss(reduction='none') |
| else: |
| self.nll_loss = None |
| |
| print("Warning: nll_loss_type not recognized, no nll_loss will be used.") |
| def calculate_adaptive_weight(self, nll_loss, g_loss, last_layer=None): |
| if last_layer is not None: |
| nll_grads = torch.autograd.grad(nll_loss, last_layer, retain_graph=True)[0] |
| g_grads = torch.autograd.grad(g_loss, last_layer, retain_graph=True)[0] |
| else: |
| nll_grads = torch.autograd.grad(nll_loss, self.last_layer[0], retain_graph=True)[0] |
| g_grads = torch.autograd.grad(g_loss, self.last_layer[0], retain_graph=True)[0] |
|
|
| d_weight = torch.norm(nll_grads) / (torch.norm(g_grads) + 1e-4) |
| d_weight = torch.clamp(d_weight, 0.0, 1e4).detach() |
| d_weight = d_weight * self.discriminator_weight |
| return d_weight |
|
|
| def forward(self, inputs, reconstructions, posteriors, split="train", weights=None): |
| if self.nll_loss is None: |
| rec_loss = torch.tensor(0.0) |
| nll_loss = torch.tensor(0.0) |
| else: |
| rec_loss = self.nll_loss(inputs.contiguous(), reconstructions.contiguous()) |
|
|
| nll_loss = rec_loss / torch.exp(self.logvar) + self.logvar |
| weighted_nll_loss = nll_loss |
| if weights is not None: |
| weighted_nll_loss = weights*nll_loss |
| weighted_nll_loss = torch.sum(weighted_nll_loss) / weighted_nll_loss.shape[0] |
| nll_loss = torch.sum(nll_loss) / nll_loss.shape[0] |
| |
| kl_loss = posteriors.kl() |
| kl_loss = torch.sum(kl_loss) / kl_loss.shape[0] |
| |
| if self.nll_loss is None: |
| loss = self.kl_weight * kl_loss |
| else: |
| loss = weighted_nll_loss + self.kl_weight * kl_loss |
|
|
| log = {"{}/total_loss".format(split): loss.clone().detach().mean(), "{}/logvar".format(split): self.logvar.detach(), |
| "{}/kl_loss".format(split): kl_loss.detach().mean(), "{}/nll_loss".format(split): nll_loss.detach().mean(), |
| "{}/rec_loss".format(split): rec_loss.detach().mean(), |
| } |
| return loss, log |