segmamba / source_code /gliomasam3_moe /tests /test_loss_backward.py
ChipYTY's picture
Add files using upload-large-folder tool
fe8202e verified
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()