简体   繁体   English

Tensorflow2.0 - 如何将张量转换为 numpy() 数组

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

I'm running tf2.0 and simply can not print the confusion matrix values.我正在运行 tf2.0 并且根本无法打印混淆矩阵值。 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?但是既然 tf2 和 eagerExecution 这应该是这样做的,对吧? See: TF2.0 Tutorial参见: TF2.0 教程

According to the definition of tf.function ,根据tf.function的定义,

"Compiles a function into a callable TensorFlow graph". “将函数编译为可调用的 TensorFlow 图”。

Since the tf.function imposes a TensorFlow graph, you cannot use anything outside of the tf.* methods.由于tf.function强加了 TensorFlow 图,因此您不能使用tf.*方法之外的任何内容。

That means that any arbitrary python code cannot be used inside a tf.function , only what is already available in the tf.* methods.这意味着任何 Python 代码都不能在tf.function中使用,只能在tf.*方法中使用。

The same exact phenomenon happens when you want to iterate on a tf.data.Dataset with a map function.当您想使用 map 函数迭代tf.data.Dataset时,会发生同样的现象。 That map function you want to use on tf.data.Dataset cannot contain arbitrary python code, unless you specifically use a tf.py_function .您要在tf.data.Dataset上使用的map函数不能包含任意 python 代码,除非您专门使用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() .出于性能原因,这些操作专门在图形模式下执行,因此,您不能调用属于“急切执行”类别的方法,例如.numpy()

Look at : https://www.tensorflow.org/api_docs/python/tf/numpy_function看: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从本地目录将图像加载到 TensorFlow2.0:ValueError:无法将 NumPy 数组转换为张量(不支持 object 类型 int) - Loading Images Into TensorFlow2.0 From Local Directory: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int) 如何在张量流中将图像张量转换为 numpy 数组? - How to convert image tensor in to numpy array in tensorflow? 如何在 tensorflow 中转换张量中的 numpy 数组? - how to convert a numpy array in tensor in tensorflow? 如何在 tensorflow 中将“张量”转换为“numpy”数组? - How to convert "tensor" to "numpy" array in tensorflow? 将张量转换为 Tensorflow 中的 numpy 数组? - Convert a tensor to numpy array in Tensorflow? 如何在不转换为 Numpy 数组的情况下将 TensorFlow 张量转换为 PyTorch 张量? - How to convert TensorFlow tensor to PyTorch tensor without converting to Numpy array? 如何将 tensorflow 图像张量转换为数据集中的 Numpy 数组? - How to convert tensorflow image Tensor to Numpy array inside Dataset? Tensorflow 2.0 使用 tf.compat.v1.disable_eager_execution() 时无法将张量转换为 numpy 数组 - Tensorflow 2.0 Cannot convert tensor to numpy array when using tf.compat.v1.disable_eager_execution() 如何将Tensor转换成NumPy数组 - How to convert Tensor into NumPy array 如何将张量转换为numpy数组 - How to convert tensor to numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM