3

I'm running tf2.0 and simply can not print the confusion matrix values. The problem is described below.

  @tf.function
  def test_step(self, x , y):
    predictions = model(x, training=False)
    loss = self.loss(y, predictions)

    y, predictions = tf.reshape(y,[-1,]), tf.reshape(predictions, [-1,])

    # Cast into class labels
    predictions = math_ops.cast(predictions > 0.5, predictions.dtype)

    ....

    self.test_conf_matrix = tf.math.confusion_matrix(y, predictions, num_classes=2) <--- important line!

Everything is going well so far, and the confusion matrix will be computed properly.

But it is simply not possible to print it out in the end like:

print(str(self.test_conf_matrix.numpy()))

The error I get is:

AttributeError: 'Tensor' object has no attribute 'numpy'

But since tf2 and eagerExecution this should be done this way, right? See: TF2.0 Tutorial

2
  • What's math_ops. Where did you import that from? Commented Jan 19, 2020 at 21:56
  • It is from 'from tensorflow.python.ops import math_ops'. Because e.g. the F1-Score implementation of tfa does not work with probabilities. So I had a look at tensorflows accuracy implementation and that is the way they cast the probabilities into class labels. Commented Jan 20, 2020 at 15:15

2 Answers 2

2

According to the definition of tf.function,

"Compiles a function into a callable TensorFlow graph".

Since the tf.function imposes a TensorFlow graph, you cannot use anything outside of the tf.* methods.

That means that any arbitrary python code cannot be used inside a tf.function, only what is already available in the tf.* methods.

The same exact phenomenon happens when you want to iterate on a tf.data.Dataset with a map function. That map function you want to use on tf.data.Dataset cannot contain arbitrary python code, unless you specifically use a tf.py_function.

These operation are specifically executed in graph mode for performance reasons, and thus, you cannot call methods that belong to the 'eager execution' category, such as .numpy().

Sign up to request clarification or add additional context in comments.

10 Comments

So, thanks but I don't really get it. The documentation of tf.math.confusion_matrix says it returns a Tensor, and that is what it does. So when I have a look at the doc of a Tensor there is the .numpy() function described. Do you have a suggestion how to overcome the issue?
You cannot. You just cannot use .numpy() inside a function that is used in a TensorFlow graph.
The only way you can use .numpy() or so is to call/create a specific py_fuunction. The fact that .numpy() exists in the documentation for a tensor does not mean you can apply it everywhere :D
Haben Sie verstanden?
No, not completely. Because I can process any other evaluation metrics like tf.keras.metrics.BinaryAccuracy. So do you have a solution how to get the result from the confusion matrix? I don't get the difference between one of those evaluation metrics and the confusion matrix. Why can I print / evaluate all other metrics but the confusion matrix?
|
0

Look at : https://www.tensorflow.org/api_docs/python/tf/numpy_function

def my_numpy_func(x):
  # x will be a numpy array with the contents of the input to the
  # tf.function
  return np.sinh(x)

@tf.function(input_signature=[tf.TensorSpec(None, tf.float32)])
def tf_function(input):
  y = tf.numpy_function(my_numpy_func, [input], tf.float32)
  return y * y
tf_function(tf.constant(1.))

This works for me fine.

You make a function something like

def print_cm(cm):
  print(cm)

@tf.function()
 def test_step(self, x , y):
   
    ....

    self.test_conf_matrix = tf.math.confusion_matrix(y, predictions, num_classes=2) # <--- important line!
    # make print as numpy array under tf.function decorator 
    print_cm(test_conf_matrix))  # just call function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.