Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supress value error when label contains one label #157

Merged
merged 3 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions chainer_chemistry/training/extensions/roc_auc_evaluator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
from logging import getLogger

import numpy

Expand Down Expand Up @@ -73,6 +74,10 @@ class ROCAUCEvaluator(Evaluator):
are considered as negative.
ignore_labels (int or list or None): labels to be ignored.
`None` is used to not ignore all labels.
raise_value_error (bool): If False, `ValueError` caused by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enclose False with backquotes.

`roc_auc_score` calculation is suppressed and ignored with warning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with a warning message.

message.
logger:

Attributes:
converter: Converter function.
Expand All @@ -86,13 +91,16 @@ class ROCAUCEvaluator(Evaluator):

def __init__(self, iterator, target, converter=convert.concat_examples,
device=None, eval_hook=None, eval_func=None, name=None,
pos_labels=1, ignore_labels=None):
pos_labels=1, ignore_labels=None, raise_value_error=True,
logger=None):
super(ROCAUCEvaluator, self).__init__(
iterator, target, converter=converter, device=device,
eval_hook=eval_hook, eval_func=eval_func)
self.name = name
self.pos_labels = _to_list(pos_labels)
self.ignore_labels = _to_list(ignore_labels)
self.raise_value_error = raise_value_error
self.logger = logger or getLogger()

def evaluate(self):
iterator = self._iterators['main']
Expand Down Expand Up @@ -132,7 +140,19 @@ def evaluate(self):
# --- set positive labels to 1, negative labels to 0 ---
pos_indices = numpy.in1d(t_total, self.pos_labels)
t_total = numpy.where(pos_indices, 1, 0)
roc_auc = metrics.roc_auc_score(t_total, y_total)
try:
roc_auc = metrics.roc_auc_score(t_total, y_total)
except ValueError as e:
if self.raise_value_error:
raise e
else:
# This is usually caused by the following
# Only one class present in y_true.
# ROC AUC score is not defined in that case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It describe when roc_auc_score is raised. So I think we should move to l.145.

self.logger.warning(
'ValueError detected during roc_auc_score calculation. {}'
.format(e.args))
roc_auc = numpy.nan

observation = {}
with reporter.report_scope(observation):
Expand Down
36 changes: 36 additions & 0 deletions tests/training_tests/extensions_tests/test_roc_auc_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def data1():
return y, t


@pytest.fixture
def data2():
# Example of bad example case
# `t` only contains correct label, `y` is dummy predict value by predictor
t = numpy.array([0, 0, 0, 0], dtype=numpy.int32)[:, None]
y = numpy.array([0.1, 0.4, 0.35, 0.8], dtype=numpy.float32)[:, None]
return y, t


class DummyPredictor(chainer.Chain):

def __call__(self, y):
Expand Down Expand Up @@ -95,5 +104,32 @@ def _test_roc_auc_evaluator_with_labels(data1):
assert result['val/main/roc_auc'] == expected_roc_auc


def test_roc_auc_evaluator_raise_value_error(data2):
with pytest.raises(ValueError):
_test_roc_auc_evaluator_raise_error(data2, raise_value_error=True)

res = _test_roc_auc_evaluator_raise_error(data2, raise_value_error=False)
assert numpy.isnan(res)


def _test_roc_auc_evaluator_raise_error(data, raise_value_error=True):

predictor = DummyPredictor()
dataset = NumpyTupleDataset(*data)

iterator = SerialIterator(dataset, 2, repeat=False, shuffle=False)
evaluator = ROCAUCEvaluator(
iterator, predictor, name='train',
pos_labels=1, ignore_labels=None,
raise_value_error=raise_value_error
)
repo = chainer.Reporter()
repo.add_observer('target', predictor)
with repo:
observation = evaluator.evaluate()

return observation['target/roc_auc']


if __name__ == '__main__':
pytest.main([__file__, '-v', '-s'])