Ensure tf.linspace(start, stop, num)[-1] == stop
The contract of tf.linspace says "A sequence of num evenly-spaced values are generated beginning at start. If num > 1, the values in the sequence increase by stop - start / num - 1, so that the last one is exactly stop." But the existing logic computes the last value as `start + (num - 1) * ((start - stop) / (num - 1))`. This exactly equals `stop` for real numbers, but not for floating point numbers. In particular, for start = 0.0, stop = 1.0, num = 42, this reduces to (1.0/41.0)*41.0, and for IEEE 754 32-bit floats, that resolves to ~0.99999994, not 1.0 (binary values 0x3F7FFFFF vs 0x3F800000). After this change, `tf.linspace(start, stop, num)[-1] == stop` holds for all values assuming num > 1, because we just set it directly to `stop`. This matches what numpy does here: https://github.com/numpy/numpy/blob/v1.15.0/numpy/core/function_base.py#L144-L145 PiperOrigin-RevId: 228204335
Loading
Please sign in to comment