Skip to content
Advertisement

tensorflow: InvalidArgumentError while find the AUC score

I have a dataset with labels 0’s and 1’s, which is binary classification problem. Getting error while try to find AUC score using tf.keras.metrics.AUC() as metrics in model.compile(.. function.

Code:

initializer = tf.keras.initializers.RandomUniform(minval=0., maxval=1.)

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(2,)))
model.add(tf.keras.layers.Dense(4, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(8, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(16, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(8, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(4, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(1))

model.compile(loss= tf.keras.losses.BinaryCrossentropy(),
                      optimizer = tf.optimizers.SGD(momentum=0.9,), 
            metrics=['accuracy', tf.keras.metrics.AUC()])

model.fit(X, y, validation_data=(X_val, y_val), epochs=10)

If I removed AUC from metrics, the code runs fine.

Error:

InvalidArgumentError: assertion failed: [predictions must be >= 0] [Condition x >= y did not hold element-wise:] [x (sequential_48/dense_293/BiasAdd:0) = ] [[2.12408257][2.12408257][-2.12408257]…] [y (Cast_10/x:0) = ] [0]
[[{{node assert_greater_equal/Assert/AssertGuard/else/_1/assert_greater_equal/Assert/AssertGuard/Assert}}]] [Op:__inference_train_function_120642]

Function call stack: train_function

How to find the AUC score/how to set AUC score as metrics in compile?

Advertisement

Answer

You are missing activation function in the last dense layer. If we don’t mention by default it will consider a linear activation, that means you can get any values including negative values.

InvalidArgumentError: assertion failed: [predictions must be >= 0] [Condition x >= y did not hold element-wise:] [x (sequential_48/dense_293/BiasAdd:0) = ] [[2.12408257][2.12408257][-2.12408257]…]

If you can check above error, it is saying the same (i.e predictions for your model must be >=0), but your model return [[2.12408257][2.12408257][-2.12408257]...] values.

To avoid this, you should add sigmoid activation function to the last dense layer as shown below.

initializer = tf.keras.initializers.RandomUniform(minval=0., maxval=1.)
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(2,)))
model.add(tf.keras.layers.Dense(4, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(8, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(16, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(8, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(4, activation='tanh', kernel_initializer=initializer))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model.compile(loss= tf.keras.losses.BinaryCrossentropy(),
                      optimizer = tf.optimizers.SGD(momentum=0.9,), 
            metrics=['accuracy', tf.keras.metrics.AUC()])

model.fit(X, y, validation_data=(X_val, y_val), epochs=10)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement