Skip to content

tfa.metrics.F1Score gives ValueError for binary classification with shape (n,) #746

Open
@MadsAdrian

Description

@MadsAdrian

System information

  • OS: Ubuntu 18.04.3 LTS (Bionic Beaver)
  • TensorFlow: 2.0.0 via tf docker image ('v2.0.0-rc2-26-g64c3d38')
  • TensorFlow-Addons: 0.6.0 via pip
  • Python version: 3.6.8 [GCC8.3.0] on linux
  • Is GPU used? (yes/no): tried both

Describe the bug
tfa.metrics.F1Score gives a ValueError for a binary classification problem encoded with shape (n,).

ValueError: Shapes must be equal rank, but are 1 and 0 for 'AssignAddVariableOp' (op: 'AssignAddVariableOp') with input shapes: [], [].

Code to reproduce the issue

import tensorflow as tf
import tensorflow_addons as tfa

f1 = tfa.metrics.F1Score(num_classes=2, average=None)

y_true = tf.constant([1,0,0,1,0])
y_pred = tf.constant([1,1,0,0,1])
f1.update_state(y_true, y_pred)

Other info / logs
Would expect this input to work, as it does with tfa.metrics.CohensKappa. If it walks like a tfa.metric and quacks like a tfa.metric, it is a tfa.metric.

E.g

import tensorflow as tf
import tensorflow_addons as tfa

k = tfa.metrics.CohenKappa(num_classes=2)

y_true = tf.constant([1,0,0,1,0])
y_pred = tf.constant([1,1,0,0,1])
k.update_state(y_true, y_pred)

yields

<tf.Tensor: id=196, shape=(2, 2), dtype=int32, numpy=
array([[1, 2], 
       [1, 1]], dtype=int32)>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions