I am very new to TensorFlow. So I came across this different types of division in TensorFlow from here. Code printed below:
a = tf.constant([2, 2], name='a')
b = tf.constant([[0, 1], [2, 3]], name='b')
with tf.Session() as sess:
print(sess.run(tf.div(b, a))) ⇒ [[0 0] [1 1]]
print(sess.run(tf.divide(b, a))) ⇒ [[0. 0.5] [1. 1.5]]
print(sess.run(tf.truediv(b, a))) ⇒ [[0. 0.5] [1. 1.5]]
print(sess.run(tf.floordiv(b, a))) ⇒ [[0 0] [1 1]]
print(sess.run(tf.realdiv(b, a))) ⇒ # Error: only works for real values
print(sess.run(tf.truncatediv(b, a))) ⇒ [[0 0] [1 1]]
print(sess.run(tf.floor_div(b, a))) ⇒ [[0 0] [1 1]]
Since I am a noob in programming languages I could not understand some of their documentation which included things like "computes division python style", etc. If someone can explain the differences between all and their practical aspect, I would be grateful.
Types of division operators in Python/ : Divides the number on its left by the number on its right and returns a floating point value. // : Divides the number on its left by the number on its right, rounds down the answer, and returns a whole number.
In Python, there are two kinds of division: integer division and float division. Integer division returns the floor of the division.
The float division operation / is used when you want a more precise division result, as it does not round off the value to whole numbers. It must be noted that for recurring decimal values, there is a limit to how many digits a float variable can store, so it is not always possible to store the exact value.
tf.div - Enforces python v2 division semantics e.g. uses integer division (also known as "floor division") if both arguments are integers and normal floating point division if arguments are float or complex numbers. The result is integer if both arguments are integers, float otherwise.
tf.div(7, 5)
# >>> 1
tf.div(-7, 5)
# >>> -2
tf.div(7.0, 5.0)
# >>> 1.4
tf.truediv - enforces python v3 division semantics, e.g. if both arguments are integers they are first cast into float type (the documentation web page specifies which type of integer is converted to which type of float) and then the normal floating point division is applied:
tf.truediv(7, 5)
# >>> 1.4
tf.truediv(-7, 5)
# >>> -1.4
tf.truediv(7.0, 5.0)
# >>> 1.4
tf.divide(x,y) essentially calls x/y
, therefore the result depends on the behavior of the /
operator in the environment in which the division is performed.
tf.floordiv and tf.floor_div return the same result as tf.div if both arguments are integers and tf.floor(tf.div(x,y))
if both arguments are floating point numbers:
tf.floordiv(7, 5)
# >>> 1
tf.floordiv(-7, 5)
# >>> -2
tf.floordiv(7.0, 5.0)
# >>> 1.0
tf.floordiv(-7.0, 5.0)
# >>> -2.0
tf.realdiv - normal floating point division, this is the operation that tf.truediv calls after it casts its arguments.
tf.truncatediv - rounds the result of division towards zero:
tf.truncatediv(7, 5)
# >>> 1
tf.truncatediv(-7, 5)
# UNLIKE tf.floordiv and tf.div
# >>> -1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With