Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 715422c

Browse files
abhishek0318matt-gardner
authored andcommitted
Fix error in BooleanAccuracy when total count is 0 (#2991)
* Fix error when count is 0 * Update boolean_accuracy.py
1 parent 9e52e0f commit 715422c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

allennlp/tests/training/metrics/boolean_accuracy_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ def test_incorrect_mask_shape_catches_exceptions(self):
6464
incorrect_shape_mask = torch.randint(0, 2, [5, 8])
6565
with pytest.raises(ValueError):
6666
accuracy(predictions, labels, incorrect_shape_mask)
67+
68+
def test_does_not_divide_by_zero_with_no_count(self):
69+
accuracy = BooleanAccuracy()
70+
self.assertAlmostEqual(accuracy.get_metric(), 0.0)

allennlp/training/metrics/boolean_accuracy.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ def get_metric(self, reset: bool = False):
8484
-------
8585
The accumulated accuracy.
8686
"""
87-
accuracy = float(self._correct_count) / float(self._total_count)
87+
if self._total_count > 0:
88+
accuracy = float(self._correct_count) / float(self._total_count)
89+
else:
90+
accuracy = 0.0
8891
if reset:
8992
self.reset()
9093
return accuracy

0 commit comments

Comments
 (0)