File size: 621 Bytes
fe8202e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys

import torch

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))

from gliomasam3_moe.losses.brats_losses import LossComputer


def test_loss_backward():
    logits = torch.randn(2, 3, 8, 32, 32, requires_grad=True)
    labels = torch.randint(0, 4, (2, 1, 8, 32, 32))
    # map {0,1,2,3} -> {0,1,2,4}
    labels = labels.clone()
    labels[labels == 3] = 4

    aux = {
        "pi_et": torch.sigmoid(torch.randn(2)),
        "moe_gamma": torch.softmax(torch.randn(2, 5), dim=-1),
    }

    loss_fn = LossComputer()
    loss, _ = loss_fn(logits, aux, labels)
    loss.backward()