Example #1
0
def test_compare_with_without_logits():
    x1 = torch.rand(10, 10) * 3
    x2 = torch.sigmoid(x1)
    y = torch.rand(10, 10).round()
    with_logits = focal_loss_with_logits(x1, y, gamma=1.0, pos_weight=0.6)
    without_logits = focal_loss(x2, y, gamma=1.0, pos_weight=0.6)
    assert_tensors_close(with_logits, without_logits)
Example #2
0
 def test_label_smoothing(self, fn, true_fn):
     x = torch.rand(10, 10)
     y = torch.rand(10, 10).round()
     smooth_factor = 0.2
     smoothed = y.clamp(smooth_factor, 1 - smooth_factor)
     smoothed_loss = fn(x, y, gamma=0.0, label_smoothing=smooth_factor)
     expected_loss = fn(x, smoothed, gamma=0.0)
     assert_tensors_close(expected_loss, smoothed_loss)
Example #3
0
 def test_equals_ce_when_gamma_zero(self, cls, true_cls):
     x = torch.rand(1, 5, 10, 10)
     y = torch.randint(0, 5, (1, 10, 10))
     criterion = cls(gamma=0, reduction="none")
     true_criterion = CrossEntropyLoss(reduction="none")
     true_loss = true_criterion(x, y)
     loss = criterion(x, y)
     assert_tensors_close(loss, true_loss)
Example #4
0
 def test_label_smoothing(self, cls, true_cls):
     x = torch.rand(10, 10)
     y = torch.rand(10, 10).round()
     smooth_factor = 0.2
     smoothed = y.clamp(smooth_factor, 1 - smooth_factor)
     smoothed_loss = cls(gamma=0.0, label_smoothing=smooth_factor)(x, y)
     expected_loss = cls(gamma=0.0)(x, smoothed)
     assert_tensors_close(expected_loss, smoothed_loss)
Example #5
0
def test_assert_tensors_close():
    x = torch.rand(10, 10)
    x_clone = x.clone()
    y = torch.rand_like(x)

    assert_tensors_close(x, x_clone)
    with pytest.raises(AssertionError):
        assert_tensors_close(x, y)
Example #6
0
    def test_traced_forward_call(self, model, data):
        r"""Calls :func:`torch.jit.trace` on the given model and tests that a :class:`torch.jit.ScriptModule`
        is returned.

        Because of the size of some models, this test is only run when a GPU is available.
        """
        traced = torch.jit.trace(model, data)
        output = model(data)
        traced_output = traced(data)
        if isinstance(output, Tensor):
            assert_tensors_close(output, traced_output)
        elif isinstance(output, Iterable):
            for out, traced_out in zip(output, traced_output):
                assert_tensors_close(out, traced_out)
        else:
            pytest.skip()
Example #7
0
 def test_equals_bce_when_gamma_zero(self, fn, true_fn):
     x = torch.rand(10, 10)
     y = torch.rand(10, 10).round()
     true_loss = true_fn(x, y)
     loss = fn(x, y, gamma=0)
     assert_tensors_close(loss, true_loss)
Example #8
0
 def test_equals_bce_when_gamma_zero(self, cls, true_cls):
     x = torch.rand(10, 10)
     y = torch.rand(10, 10).round()
     true_loss = true_cls()(x, y)
     loss = cls(gamma=0)(x, y)
     assert_tensors_close(loss, true_loss)