Casa > H > How To Save A Tensorflow Tensor As A Numpy Array

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:

  1. >>> tensor = tf.ones((3, 2))  
  2. >>> tensor  
  3. array([[1., 1.],  
  4. [1., 1.],  
  5. [1., 1.]], dtype=float32)>  
  6. >>> array = tensor.numpy()  
  7. >>> array  
  8. array([[1., 1.],  
  9. [1., 1.],  
  10. [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:

  1. >>> tensor = tf.ones((3, 2))  
  2. >>> tensor  
  3. array([[1., 1.],  
  4. [1., 1.],  
  5. [1., 1.]], dtype=float32)>  
  6. >>> array = np.add(tensor, 3)  
  7. >>> array  
  8. array([[4., 4.],  
  9. [4., 4.],  
  10. [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

De Palestine Ladson

Qual é o melhor gamepad móvel disponível na Índia? :: O que é melhor: Netflix, Hulu, ou Amazon Prime Video?