예제 #1
0
 def test_soft_accuracy(self):
     """Tests that soft accuracy is computed correctly."""
     domain = domains.FixedLengthDiscreteDomain(
         vocab=domains.Vocabulary(tokens=['A', 'B', 'C']), length=2)
     targets = np.array([[0, 1]])
     logits = np.log([[[0.9, 0.1], [0.6, 0.4]]])
     freq_dict = {
         'A': {
             'A': 5,
             'B': 3,
             'C': 1
         },
         'B': {
             'A': 3,
             'B': 5,
             'C': 1
         },
         'C': {
             'A': 1,
             'B': 1,
             'C': 1
         }
     }
     accuracy, denominator = utils.compute_weighted_soft_accuracy(
         logits,
         targets,
         weights=None,
         matrix=utils.get_normalized_matrix(domain, freq_dict))
     self.assertEqual(accuracy / denominator, 0.75)
예제 #2
0
 def test_get_normalized_matrix(self):
     """Tests that the normalized matrix is computed correctly."""
     domain = domains.FixedLengthDiscreteDomain(
         vocab=domains.Vocabulary(tokens=['A', 'B', 'C']), length=2)
     freq_dict = {
         'A': {
             'A': 5,
             'B': 3,
             'C': 1
         },
         'B': {
             'A': 3,
             'B': 5,
             'C': 1
         },
         'C': {
             'A': 1,
             'B': 1,
             'C': 1
         }
     }
     matrix = utils.get_normalized_matrix(domain, freq_dict)
     expected_matrix = [[1, 0.5, 0], [
         0.5,
         1,
         0,
     ], [0, 0, 0]]
     self.assertAllEqual(matrix, expected_matrix)