mse_loss.py 750 Bytes
Newer Older
1
from losses import ReconstructionBase
Paul Primus's avatar
initial  
Paul Primus committed
2
3
4
5
6
7
8
9
10
11
import torch.nn.functional as F


class MSE(ReconstructionBase):

    def __init__(self, weight=1.0, p=0.1, **kwargs):
        super().__init__(weight=weight)

        self.p = p

Paul Primus's avatar
initial    
Paul Primus committed
12
    def loss(self, batch, *args, **kwargs):
13
        bce = F.mse_loss(batch['losses'], batch['observations'], reduction='mean')
Paul Primus's avatar
Paul Primus committed
14
        batch['reconstruction_loss'] = self.weight * bce # / (batch['observations'].shape[0])
Paul Primus's avatar
initial  
Paul Primus committed
15
16
17
18
19
        return batch['reconstruction_loss']

    def forward(self, batch):

        batch['visualizations'] = batch['pre_reconstructions']
20
21
        batch['losses'] = batch['pre_reconstructions']
        batch['scores'] = (batch['losses'] - batch['observations']).pow(2).mean(axis=(1, 2, 3))
Paul Primus's avatar
initial  
Paul Primus committed
22
        return batch