简体   繁体   English

为什么“NumPy 操作会自动将张量转换为 numpy arrays”? 这个功能是如何实现的?

[英]why “NumPy operations convert Tensors to numpy arrays automatically”? how does this feature been implemented?

Reading TensorFlow docs: https://www.tensorflow.org/tutorials/customization/basics#numpy_compatibility阅读 TensorFlow 文档: 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.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. TF API 支持 NumPy 对象作为输入,这很容易理解,因为 TF API 只是由 TensorFlow 团队实现来处理 NumPy 对象。

But

print(np.add(tensor, 1))打印(np.add(张量,1))

On the contrary, why NumPy APIs could handle a tf.Tensor is quite amazing to me.相反,为什么 NumPy API 可以处理 tf.Tensor 让我很惊讶。 Is this a mechanism that NumPy provided to handle any type of objects?这是 NumPy 提供的用于处理任何类型对象的机制吗? Or just supported by Python in language level?还是仅在语言级别受 Python 支持? (I am new to Python) (我是 Python 新手)

TensorFlow's API revolves around tensors, which flow from operation to operation—hence the name TensorFlow. TensorFlow 的 API 围绕张量展开,张量在操作之间流动——因此命名为 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 ).张量通常是一个多维数组(就像 NumPy ndarray 一样),但它也可以保存一个标量(一个简单的值,例如 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 Nd Array Libraries. Numpy 和 TensorFlow 都是 Nd 阵列库。 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 Nd array support but does not have methods to create tensor functions, can't automatically compute derivatives and it can't take advantage of GPU. Numpy 支持 Nd 数组,但没有创建张量函数的方法,不能自动计算导数,也不能利用 GPU。

TensorFlow provides primitives for defining functions on tensors and automatically computing their derivatives. TensorFlow 提供了用于定义张量函数并自动计算其导数的原语。 TensorFlow computations define a graph that has no numerical value until evaluated. TensorFlow 计算定义了一个在评估之前没有数值的图。 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.张量与 NumPy 配合得很好:您可以从 NumPy 数组创建张量,反之亦然。 You can even apply TensorFlow operations to NumPy arrays and NumPy operations to tensors:您甚至可以将 TensorFlow 操作应用于 NumPy arrays 和 Z3B7F949B2343F9E5E5390E29F6EF 操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM