Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

python - TensorFlow operator overloading

What is the difference between

   tf.add(x, y)

and

   x + y

in TensorFlow? What would be different in your computation graph when you construct your graph with + instead of tf.add()?

More generally, are + or other operations overloaded for tensors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If at least one of x or y is a tf.Tensor object, the expressions tf.add(x, y) and x + y are equivalent. The main reason you might use tf.add() is to specify an explicit name keyword argument for the created op, which is not possible with the overloaded operator version.

Note that if neither x nor y is a tf.Tensor—for example if they are NumPy arrays—then x + y will not create a TensorFlow op. tf.add() always creates a TensorFlow op and converts its arguments to tf.Tensor objects. Therefore, if you are writing a library function that might accept both tensors and NumPy arrays, you might prefer to use tf.add().

The following operators are overloaded in the TensorFlow Python API:

  • __neg__ (unary -)
  • __abs__ (abs())
  • __invert__ (unary ~)
  • __add__ (binary +)
  • __sub__ (binary -)
  • __mul__ (binary elementwise *)
  • __div__ (binary / in Python 2)
  • __floordiv__ (binary // in Python 3)
  • __truediv__ (binary / in Python 3)
  • __mod__ (binary %)
  • __pow__ (binary **)
  • __and__ (binary &)
  • __or__ (binary |)
  • __xor__ (binary ^)
  • __lt__ (binary <)
  • __le__ (binary <=)
  • __gt__ (binary >)
  • __ge__ (binary >=)

Please note, __eq__ ( binary == ) is not overloaded. x == y will simply return a Python boolean whether x and y refer to the same tensor. You need to use tf.equal() explicitly to check for element-wise equality. Same goes for not equal, __ne__ ( binary != ).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...