SamSampleman
SamSampleman

Reputation: 65

Tensorflow2.0 - How to convert Tensor to numpy() array

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

Upvotes: 3

Views: 6074

Answers (2)

megamachine
megamachine

Reputation: 1

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

Upvotes: 0

Timbus Calin
Timbus Calin

Reputation: 14983

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().

Upvotes: 2

Related Questions