Doubt over Tf.math.division()

  1. tf.division() - On dividing two tensors / a tensor and a integer
    Produces a tensor of datatype float64 as default.
  2. Doesn’t int32 / int32 results with a int32 value ?
  3. Should I use tf.cast( , tf.int32) always to get this done ?
  4. Sadly < 2 > is right acc. to tensorFlows library,
  5. 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)
  6. 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.
  7. 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.

  1. 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.

  2. 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 link

    import tensorflow as tf
    x = tf.constant([16, 12, 11])
    y = tf.constant([4, 6, 2])
    tf.math.floordiv(x, y)


<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 5], dtype=int32)>

  1. 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])


>     > tf.Tensor([16. 12. 11.], shape=(3,), dtype=float32)
>     > <tf.Tensor: shape=(3,), dtype=float32, numpy=array([4. , 2. , 5.5], dtype=float32)>
1 Like