在 tensorflow 中,tf.add 和运算符 (+) 之间有什么区别? [英] In tensorflow what is the difference between tf.add and operator (+)?

查看:76
本文介绍了在 tensorflow 中,tf.add 和运算符 (+) 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 tensorflow 教程中,我看到了两个代码,如 tf.add(tf.matmul(X, W), b)tf.matmul(X, W) + bcode>,使用数学函数tf.add()tf.assign()等和运算符+有什么区别和 = 等,在精度或其他方面?

In tensorflow tutorials, I see both codes like tf.add(tf.matmul(X, W), b) and tf.matmul(X, W) + b, what is the difference between using the math function tf.add(), tf.assign(), etc and the operators + and =, etc, in precision or other aspects?

推荐答案

a+btf.add(a, b) 在精度上没有区别.前者转换为 a.__add__(b) ,后者通过 在 math_ops.py 中的以下行

There's no difference in precision between a+b and tf.add(a, b). The former translates to a.__add__(b) which gets mapped to tf.add by means of following line in math_ops.py

_OverrideBinaryOperatorHelper(gen_math_ops.add, "add")

唯一的区别是底层Graph中的节点名称是add而不是Add.您通常可以通过查看这样的底层 Graph 表示来比较事物

The only difference is that node name in the underlying Graph is add instead of Add. You can generally compare things by looking at the underlying Graph representation like this

tf.reset_default_graph()
dtype = tf.int32
a = tf.placeholder(dtype)
b = tf.placeholder(dtype)
c = a+b
print(tf.get_default_graph().as_graph_def())

您也可以通过检查 __add__ 方法直接看到这一点.有一个额外的间接级别,因为它是一个闭包,但是你可以得到下面的底层函数

You could also see this directly by inspecting the __add__ method. There's an extra level of indirection because it's a closure, but you can get the underlying function as follows

real_function = tf.Tensor.__add__.im_func.func_closure[0].cell_contents
print(real_function.__module__ + "." + real_function.__name__)
print(tf.add.__module__ + "." + tf.add.__name__)

你会在下面看到输出,这意味着它们调用了相同的底层函数

And you'll see output below which means that they call same underlying function

tensorflow.python.ops.gen_math_ops.add
tensorflow.python.ops.gen_math_ops.add

您可以从 tf.Tensor.OVERLOADABLE_OPERATORS 中看到,以下 Python 特殊方法可能会被适当的 TensorFlow 版本重载

You can see from tf.Tensor.OVERLOADABLE_OPERATORS that following Python special methods are potentially overloaded by appropriate TensorFlow versions

{'__abs__',
 '__add__',
 '__and__',
 '__div__',
 '__floordiv__',
 '__ge__',
 '__getitem__',
 '__gt__',
 '__invert__',
 '__le__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__neg__',
 '__or__',
 '__pow__',
 '__radd__',
 '__rand__',
 '__rdiv__',
 '__rfloordiv__',
 '__rmod__',
 '__rmul__',
 '__ror__',
 '__rpow__',
 '__rsub__',
 '__rtruediv__',
 '__rxor__',
 '__sub__',
 '__truediv__',
 '__xor__'}

这些方法在 Python 参考 3.3.7:模拟数字类型.请注意,Python 数据模型不提供重载赋值运算符 = 的方法,因此赋值始终使用本机 Python 实现.

Those methods are described in Python reference 3.3.7: emulating numeric types. Note that Python data model does not provide a way to overload assignment operator = so assignment always uses native Python implementation.

这篇关于在 tensorflow 中,tf.add 和运算符 (+) 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆