In tensorflow what is the difference between tf.add and operator ?

In tensorflow what is the difference between tf.add and operator ?

Asked on December 26, 2018 in Tensorflow.
Add Comment


  • 1 Answer(s)

        The a+b and tf.add(a, b) has no difference in precision. The a+b translates to a.__add__(b) this will mapped to tf.add.

    _OverrideBinaryOperatorHelper(gen_math_ops.add, "add")
    

        The Difference is that their node name in the hidden graph is add rather than Add. Just compare things by referring the hidden graph representation like the below code:

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

        Look this directly by examine the __add__ method.

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

        This shows output like this which means that they call same underlying function

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

        Below are some of the Python special methods which is potentially overloaded by suitable TensorFlow versions see it from tf.Tensor.OVERLOADABLE_OPERATORS.

    {'__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__'}
    
    Answered on December 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.