votes up 2

`num_labels` is needed only when `multi_label` is True.

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

._built = False
    if self.multi_label:
      if num_labels:
        shape = tf.TensorShape([None, num_labels])
        self._build(shape)
    else:
      if num_labels:
        raise ValueError(
            '`num_labels` is needed only when `multi_label` is True.')
      self._build(None)

  @property
  def thresholds(self):
    """The thresholds used for evaluating AUC."""
    return list(self._thresholds)

  def _
🙏 Scream for help to Ukraine
Today, 2nd July 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 4 votes down

Summary: when using the AUC (Area under the curve) we have to make sure multi_label and the num_labels parameters agree in value. As pointed out in the documentation The number of labels, is used when multi_label is True.

Code to reproduce the error (Usage with compile() API method):

import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(
    optimizer='adam',
    loss='mean_squared_error',
    metrics=[
        tf.keras.metrics.MeanSquaredError(),
        tf.keras.metrics.AUC(multi_label=False,num_labels=3),
    ]
)

Working (fixed) version of the code:

import tensorflow as tf



inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)


# set multi_label to True
model.compile(
    optimizer='adam',
    loss='mean_squared_error',
    metrics=[
        tf.keras.metrics.MeanSquaredError(),
        tf.keras.metrics.AUC(multi_label=True, num_labels=3),
    ]
)

For more usage guides checkout the official Keras decumentation here.

May 19, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix