votes up 6

Threshold values must be in [0, 1]. Invalid values: (param1)

Package:
Exception Class:
ValueError

Raise code

rn inner


def assert_thresholds_range(thresholds):
  if thresholds is not None:
    invalid_thresholds = [t for t in thresholds if t is None or t < 0 or t > 1]
    if invalid_thresholds:
      raise ValueError(
          'Threshold values must be in [0, 1]. Invalid values: {}'.format(
              invalid_thresholds))


def parse_init_thresholds(thresholds, default_threshold=0.5):
  if thresholds is not None:
    assert_thresholds_range(to_list(thresholds))
  thre
🙏 Scream for help to Ukraine
Today, 25th May 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 1 votes down

When instantiating the Recall class, the threshold parameter should be in the range [0,1].

Reproducing the error:

pipenv install tensorflow

import tensorflow as tf
m = tf.keras.metrics.Recall(thresholds=-0.7)
m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
print(m.result().numpy())

The output error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-5220c73949a6> in <module>()
      1 import tensorflow as tf
----> 2 m = tf.keras.metrics.Recall(thresholds=-0.7)
      3 m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
      4 print(m.result().numpy())

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/metrics.py in __init__(self, thresholds, top_k, class_id, name, dtype)
   1426     default_threshold = 0.5 if top_k is None else metrics_utils.NEG_INF
   1427     self.thresholds = metrics_utils.parse_init_thresholds(
-> 1428         thresholds, default_threshold=default_threshold)
   1429     self.true_positives = self.add_weight(
   1430         'true_positives',

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/metrics_utils.py in parse_init_thresholds(thresholds, default_threshold)
    190 def parse_init_thresholds(thresholds, default_threshold=0.5):
    191   if thresholds is not None:
--> 192     assert_thresholds_range(to_list(thresholds))
    193   thresholds = to_list(default_threshold if thresholds is None else thresholds)
    194   return thresholds

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/metrics_utils.py in assert_thresholds_range(thresholds)
    185       raise ValueError(
    186           'Threshold values must be in [0, 1]. Invalid values: {}'.format(
--> 187               invalid_thresholds))
    188 
    189 

ValueError: Threshold values must be in [0, 1]. Invalid values: [-0.7]

The fixed version of the code:

Make sure the threshold parameter (if given) is with in the range [0,1].

import tensorflow as tf
m = tf.keras.metrics.Recall(thresholds=0.7)
m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
print(m.result().numpy())


Output:

0.6666667

Jul 16, 2021 kellemnegasi answer
kellemnegasi 29.0k

Add a possible fix

Please authorize to post fix