How to save a TensorFlow tensor as a NumPy array
You can call .numpy() method to explicitly transform a TensorFlow tensor to a NumPy array.
Example:
- >>> tensor = tf.ones((3, 2))
- >>> tensor
- array([[1., 1.],
- [1., 1.],
- [1., 1.]], dtype=float32)>
- >>> array = tensor.numpy()
- >>> array
- array([[1., 1.],
- [1., 1.],
- [1., 1.]], dtype=float32)
Also, you can use TensorFlow operations with NumPy arrays and NumPy arrays are automatically converted to TensorFlow tensors. It also works with NumPy operations. So, there is no need to transform since it is automatically handled.
Example 2:
- >>> tensor = tf.ones((3, 2))
- >>> tensor
- array([[1., 1.],
- [1., 1.],
- [1., 1.]], dtype=float32)>
- >>> array = np.add(tensor, 3)
- >>> array
- array([[4., 4.],
- [4., 4.],
- [4., 4.]], dtype=float32)
These convertions are computationally cheap if they share underlying memory representation.
To understand better, you can check the original documentation of Tensorflow:
Customization basics: tensors and operations | TensorFlow Core
Artigos semelhantes
- Como podemos converter um array unidimensional para um array bidimensional em Python?
- O que significa o 'mesmo' parâmetro de preenchimento em convolução no TensorFlow?
- O Linux é melhor do que o Windows para usar o TensorFlow?
- Por que eu recebo "objeto numpy.ndarray não tem erro de atributo em anexo"?