Esempio n. 1
0
    def test_givenAPerfectPredictionTensor_whenNLLLossPerTag_thenLossIs0(self):
        # Tags prediction value are 'inverted' to mimic the log
        first_token_first_element_of_the_batch = [
            0., 1.
        ]  # the predicted token is the first class
        first_token_second_element_of_the_batch = [
            1., 0.
        ]  # the predicted token is the second class
        second_token_first_element_of_the_batch = [
            1., 0.
        ]  # the predicted token is the second class
        second_token_second_element_of_the_batch = [
            0., 1.
        ]  # the predicted token is the first class
        predict_tensor = torch.tensor(
            [[
                first_token_first_element_of_the_batch,
                first_token_second_element_of_the_batch
            ],
             [
                 second_token_first_element_of_the_batch,
                 second_token_second_element_of_the_batch
             ]],
            device=self.a_device)

        actual = nll_loss(predict_tensor,
                          self.a_short_ground_truth).detach().tolist()
        expected = torch.tensor(1, device=self.a_device).tolist()
        self.assertAlmostEqual(expected, actual, delta=5)
Esempio n. 2
0
    def test_givenACompletelyWrongPredictionTensor_whenNLLLossPerTag_thenLossIsMinus2(
        self, ):
        # Tags prediction value are "inverted" to mimic the log
        first_token_first_element_of_the_batch = [
            1.0,
            0.0,
        ]  # the predicted token is the second class
        first_token_second_element_of_the_batch = [
            0.0,
            1.0,
        ]  # the predicted token is the first class
        second_token_first_element_of_the_batch = [
            0.0,
            1.0,
        ]  # the predicted token is the first class
        second_token_second_element_of_the_batch = [
            1.0,
            0.0,
        ]  # the predicted token is the second class
        predict_tensor = torch.tensor(
            [
                [
                    first_token_first_element_of_the_batch,
                    first_token_second_element_of_the_batch,
                ],
                [
                    second_token_first_element_of_the_batch,
                    second_token_second_element_of_the_batch,
                ],
            ],
            device=self.a_device,
        )

        actual = nll_loss(predict_tensor,
                          self.a_short_ground_truth).detach().tolist()
        expected = torch.tensor(0.0, device=self.a_device)
        self.assertAlmostEqual(expected, actual, delta=5)
Esempio n. 3
0
 def test_givenAPredictionTensor_whenNLLLossPerTag_thenLossIsOk(self):
     # need to convert to list and use float since not working almost equal for tensor
     actual = nll_loss(self.a_prediction_tensor,
                       self.ground_truth).detach().tolist()
     expected = self.a_loss.tolist()
     self.assertAlmostEqual(expected, actual, delta=5)