Different datatypes in creation of linspace from either integers or floats

Dear TensorFlow community,

I’ve noticed that when I create a linspace object, if I do so using integers as my min-and-max values, the created object is a float64 object. If I do this using floats for my min-and-max, the created object is a float32 dtype.

As this seems inconsistent to me I was wondering if there was a reason for the different datatype constructions?

I have pasted minimal code for a demo and the output of the script below.

Best.

Script:

import tensorflow as tf

linspace_from_integers = tf.linspace(0, 1, 10)
linspace_from_floats = tf.linspace(0.0, 1.0, 10)

print(f"linspace_from_integers dtype: {linspace_from_integers.dtype}“)
print(f"linspace_from_floats dtype: {linspace_from_floats.dtype}”)

Output:

linspace_from_integers dtype: <dtype: ‘float64’>
linspace_from_floats dtype: <dtype: ‘float32’>

Hi @Zak_Williams, If you see the source code of the linspace particularly at this code line

upon calculating the delta value the start and end datatype is being cast to delta datatype. so when passing start and end as an integer the delta dtype is float64 as the denominator is an integer.

If you pass float values to start and end the delta dtype is float32 as the denominator is an float32, because the initially passing float values is considered as float32 dtype.

But if you convert float32 to float64 and then pass that float64 to start and end values the delta dtype is float64 as the denominator is an float64. For example,

import tensorflow as tf
start=tf.constant(0,dtype=tf.float64)
end=tf.constant(1,dtype=tf.float64)
linspace_from_floats = tf.linspace(start, end, 10)
print(f"linspace_from_floats dtype: {linspace_from_floats.dtype}”)

#output: linspace_from_floats dtype: <dtype: 'float64'>

please refer to this gist for code explanation. Thank You.