QNA > 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

Di Martz

Windows 10 funziona perfettamente con 2GB di RAM? :: Questa fascia Fitbit Flex rotta può essere riparata o sostituita?
Link utili