- tf.division() - On dividing two tensors / a tensor and a integer
Produces a tensor of datatype float64 as default. - Doesn’t int32 / int32 results with a int32 value ?
- Should I use tf.cast( , tf.int32) always to get this done ?
- Sadly < 2 > is right acc. to tensorFlows library,
- Which is against a general equation in mathematics.
x * (y/y) = x [where both x and y variables are of type int in LHS]
In terms of tensorFlow
tensor = tf.multiply(tf.divide(x, y), y) #x - tensor(DT - int32), y - tensor or a variable (DT- int32)
print(tensor) - Should I stop overthinking over this and just stick with this rules ??
a.) tf.division(tensor of DT int, tensor or variable of DT int) = tensor of value < tensor/tensor or variable > of DT float64 always
b.) The same rule < a > while dividing a tensor with a variable or another tensor of DT int gives a tensor < tensor/tensor or variable > of DT float64 always. - If any agree this has to be addressed and I was right do support this to let this addressed by TensorFlow team. & to help me move on to further studies. !
ThankYou Dev_Friends !!
Hello Saravanan_R,
According to the Tensorflow doc,
1.If the given x,y are int32 output of tf.devide() is float64 datatype default.
-
For example, if x = tf.constant([16, 11]) and y = tf.constant([4, 2]),
tf.devide output:
16/4 = 4 and 11/2 =5.5 which is float value.
Thats reason tf.devide output is set datatype of float64 as default. -
If you are looking for the Tensorflow api which takes integer input and result in an integer.
Use tf.math.floordiv(x, y), Where x and y are of datatype int32. Result will have datatype of int32. For more details refer this linkimport tensorflow as tf
x = tf.constant([16, 12, 11])
y = tf.constant([4, 6, 2])
tf.math.floordiv(x, y)
Output:
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 5], dtype=int32)>
-
In case of tf.devide, if the inputs are of float32, output will be of float32. LHS and RHS datatype are same.
import tensorflow as tf
x = tf.constant([16.0, 12.0, 11.0])
y = tf.constant([4.0, 6.0, 2.0])
print(x)
tf.divide(x,y)
Output:
> > tf.Tensor([16. 12. 11.], shape=(3,), dtype=float32) > > <tf.Tensor: shape=(3,), dtype=float32, numpy=array([4. , 2. , 5.5], dtype=float32)>