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 _
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

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 31.6k

Add a possible fix

Please authorize to post fix