Tensorflow – Converting Tensors to Numpy Arrays
You can convert a tensor to a numpy array with ease by using the tensor.numpy() method or the np.array() function.
Example:
import numpy as np
import tensorflow as tf
# Create demo tensors
ts1 = tf.constant([
[1, 2, 3],
[4, 5, 6]
], dtype=tf.int16)
ts2 = tf.constant([0.2, 1.1, 3.3, 4.4], dtype=tf.float32)
# Using numpy() method
arr1 = ts1.numpy()
# Using np.array() function
arr2 = np.array(ts2)
print(type(arr1))
print(type(arr2))
Output:
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
Hope this helps!
Subscribe
0 Comments