|
| 1 | +"""Tests for the NormalizedMSELoss class.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | +from yoke.losses.NormMSE import NormalizedMSELoss |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def norm_mse() -> NormalizedMSELoss: |
| 10 | + """Fixture for NormalizedMSELoss.""" |
| 11 | + return NormalizedMSELoss() |
| 12 | + |
| 13 | + |
| 14 | +def test_norm_mse_loss_zero(norm_mse: NormalizedMSELoss) -> None: |
| 15 | + """Test the NormalizedMSELoss with zero input and target.""" |
| 16 | + inp = torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]]]) |
| 17 | + target = torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]]]) |
| 18 | + loss = norm_mse(inp, target) |
| 19 | + assert torch.all(loss == 0.0) |
| 20 | + |
| 21 | + |
| 22 | +def test_norm_mse_loss_positive(norm_mse: NormalizedMSELoss) -> None: |
| 23 | + """Test the NormalizedMSELoss with positive input and target.""" |
| 24 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]], dtype=torch.float32) |
| 25 | + target = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]], dtype=torch.float32) |
| 26 | + loss = norm_mse(inp, target) |
| 27 | + assert torch.all(loss == 0.0) |
| 28 | + |
| 29 | + |
| 30 | +def test_norm_mse_loss_non_zero(norm_mse: NormalizedMSELoss) -> None: |
| 31 | + """Test the NormalizedMSELoss with non-zero input and target.""" |
| 32 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]]) |
| 33 | + target = torch.tensor([[[[4.0, 5.0], [6.0, 7.0]]]]) |
| 34 | + loss = norm_mse(inp, target) |
| 35 | + expected_loss = torch.mean( |
| 36 | + ( |
| 37 | + (inp - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 38 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 39 | + - (target - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 40 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 41 | + ) |
| 42 | + ** 2, |
| 43 | + dim=(0, 2, 3), |
| 44 | + ) |
| 45 | + assert torch.all(loss == expected_loss) |
| 46 | + |
| 47 | + |
| 48 | +def test_norm_mse_loss_negative(norm_mse: NormalizedMSELoss) -> None: |
| 49 | + """Test the NormalizedMSELoss with negative input and target.""" |
| 50 | + inp = torch.tensor([[[[-1.0, -2.0], [-3.0, -4.0]]]]) |
| 51 | + target = torch.tensor([[[[-4.0, -5.0], [-6.0, -7.0]]]]) |
| 52 | + loss = norm_mse(inp, target) |
| 53 | + expected_loss = torch.mean( |
| 54 | + ( |
| 55 | + (inp - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 56 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 57 | + - (target - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 58 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 59 | + ) |
| 60 | + ** 2, |
| 61 | + dim=(0, 2, 3), |
| 62 | + ) |
| 63 | + assert torch.all(loss == expected_loss) |
| 64 | + |
| 65 | + |
| 66 | +def test_norm_mse_loss_mean_reduction() -> None: |
| 67 | + """Test the mean reduction of the NormalizedMSELoss.""" |
| 68 | + norm_mse = NormalizedMSELoss(reduction="mean") |
| 69 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]]) |
| 70 | + target = torch.tensor([[[[4.0, 5.0], [6.0, 7.0]]]]) |
| 71 | + loss = norm_mse(inp, target) |
| 72 | + expected_loss = torch.mean( |
| 73 | + ( |
| 74 | + (inp - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 75 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 76 | + - (target - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 77 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 78 | + ) |
| 79 | + ** 2 |
| 80 | + ).mean() |
| 81 | + assert torch.all(loss == expected_loss) |
| 82 | + |
| 83 | + |
| 84 | +def test_norm_mse_loss_sum_reduction() -> None: |
| 85 | + """Test the sum reduction of the NormalizedMSELoss.""" |
| 86 | + norm_mse = NormalizedMSELoss(reduction="sum") |
| 87 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]]) |
| 88 | + target = torch.tensor([[[[4.0, 5.0], [6.0, 7.0]]]]) |
| 89 | + loss = norm_mse(inp, target) |
| 90 | + expected_loss = torch.sum( |
| 91 | + ( |
| 92 | + (inp - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 93 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 94 | + - (target - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 95 | + / (target.std(dim=(0, 2, 3), keepdim=True) + norm_mse.eps) |
| 96 | + ) |
| 97 | + ** 2 |
| 98 | + ).sum() |
| 99 | + assert loss == expected_loss |
| 100 | + |
| 101 | + |
| 102 | +def test_norm_mse_loss_different_shapes(norm_mse: NormalizedMSELoss) -> None: |
| 103 | + """Test the NormalizedMSELoss with different input and target shapes.""" |
| 104 | + inp = torch.tensor([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]]]) |
| 105 | + target = torch.tensor([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]]]) |
| 106 | + loss = norm_mse(inp, target) |
| 107 | + assert torch.all(loss == 0.0) |
| 108 | + |
| 109 | + |
| 110 | +def test_norm_mse_loss_batch_size(norm_mse: NormalizedMSELoss) -> None: |
| 111 | + """Test the NormalizedMSELoss with different batch sizes.""" |
| 112 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]], [[[1.0, 2.0], [3.0, 4.0]]]]) |
| 113 | + target = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]], [[[1.0, 2.0], [3.0, 4.0]]]]) |
| 114 | + loss = norm_mse(inp, target) |
| 115 | + assert torch.all(loss == 0.0) |
| 116 | + |
| 117 | + |
| 118 | +def test_norm_mse_loss_different_eps() -> None: |
| 119 | + """Test the NormalizedMSELoss with different eps values.""" |
| 120 | + norm_mse = NormalizedMSELoss(eps=1e-5) |
| 121 | + inp = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]]) |
| 122 | + target = torch.tensor([[[[4.0, 5.0], [6.0, 7.0]]]]) |
| 123 | + loss = norm_mse(inp, target) |
| 124 | + expected_loss = ( |
| 125 | + (inp - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 126 | + / (target.std(dim=(0, 2, 3), keepdim=True) + 1e-5) |
| 127 | + - (target - target.mean(dim=(0, 2, 3), keepdim=True)) |
| 128 | + / (target.std(dim=(0, 2, 3), keepdim=True) + 1e-5) |
| 129 | + ) ** 2 |
| 130 | + assert torch.all(loss == expected_loss) |
0 commit comments