why "NumPy operations convert Tensors to numpy arrays automatically"? how does this feature been implemented?

Viewed 610

Reading TensorFlow docs: https://www.tensorflow.org/tutorials/customization/basics#numpy_compatibility

import numpy as np

ndarray = np.ones([3, 3])

print("TensorFlow operations convert numpy arrays to Tensors automatically")
tensor = tf.multiply(ndarray, 42)
print(tensor)


print("And NumPy operations convert Tensors to numpy arrays automatically")
print(np.add(tensor, 1))

print("The .numpy() method explicitly converts a Tensor to a numpy array")
print(tensor.numpy())

tensor = tf.multiply(ndarray, 42)

TF APIs support NumPy objects as inputs and this is easy to understand because TF APIs are just implemented to handle NumPy objects by TensorFlow Team.

But

print(np.add(tensor, 1))

On the contrary, why NumPy APIs could handle a tf.Tensor is quite amazing to me. Is this a mechanism that NumPy provided to handle any type of objects? Or just supported by Python in language level ? (I am new to Python)

1 Answers

TensorFlow’s API revolves around tensors, which flow from operation to operation—hence the name TensorFlow. A tensor is usually a multidimensional array (exactly like a NumPy ndarray ), but it can also hold a scalar (a simple value, such as 42 ). These tensors will be important when we create custom cost functions, custom metrics, custom layers, and more, so let’s see how to create and manipulate them.

Both Numpy and TensorFlow are N-d Array Libraries. Tensors are multilinear maps from Vector spaces to real numbers. Scalar, Vector and Matrix are all tensors. So a tensor could be represented as a multi-dimensional array.

Numpy has N-d array support but does not have methods to create tensor functions, can’t automatically compute derivatives and it can’t take advantage of GPU.

TensorFlow provides primitives for defining functions on tensors and automatically computing their derivatives. TensorFlow computations define a graph that has no numerical value until evaluated. This graph can be seen as having each operation as node and Tensors being transformed at each node and propagating to the next operation in the graph.

Tensors play nice with NumPy: you can create a tensor from a NumPy array, and vice versa. You can even apply TensorFlow operations to NumPy arrays and NumPy operations to tensors:

>>> a = np.array([2., 4., 5.])
>>> tf.constant(a)
<tf.Tensor: id=111, shape=(3,), dtype=float64, numpy=array([2., 4., 5.])>>>> t.numpy() # or np.array(t)
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)
>>> tf.square(a)
<tf.Tensor: id=116, shape=(3,), dtype=float64, numpy=array([4., 16.,
25.])>
>>> np.square(t)
array([[ 1., 4., 9.],
[16., 25., 36.]], dtype=float32)
Related