floating point issue

89 views
Skip to first unread message

Edoh Kodjo

unread,
Jun 20, 2019, 6:36:29 AM6/20/19
to TensorFlow.js Discussion
Hi,

Before filling it as an issue on github, I would like to post it here.

tf.scalar(12045).mul(tf.scalar(12045)).print(); will give a wrong value (145082032 instead of 145082025). It does not seem to be a webgl limitation.

Best

Daniel Smilkov

unread,
Jun 20, 2019, 8:59:19 AM6/20/19
to Edoh Kodjo, TensorFlow.js Discussion
Hi Edoh,

That's just a side-effect of IEEE-754 32-bit floats.

To reproduce: new Float32Array([12045*12045])

While native Javascript numbers are stored in 64bit IEEE-754, TF.js doesn't support float64 dtype yet since there hasn't been much of a need for it in the domain of neural nets.

Daniel


--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/CAF5wq7nByzH85feDJ0LVSpQ265TR6_H0HBX7NpRa9RPD_e_Kvw%40mail.gmail.com.

Daniel Smilkov

unread,
Jun 20, 2019, 9:01:46 AM6/20/19
to Edoh Kodjo, TensorFlow.js Discussion
Also our WebGL backend is limited to 32bit floats, so if we ever add float64 dtype, we would have to use the cpu backend, which is significantly slower.

Daniel

Edoh Kodjo

unread,
Jun 20, 2019, 9:11:45 AM6/20/19
to Daniel Smilkov, TensorFlow.js Discussion
Got it, thanks Daniel for the explanation.

Edoh Kodjo

unread,
Jun 20, 2019, 9:45:04 AM6/20/19
to Daniel Smilkov, TensorFlow.js Discussion
It is true that it is a float32 precision issue. Does it mean that under the hood all the dtype of tfjs are float32 even if the dtype of the tensor is 'int32' ? What I mean is that new Float32Array([12045*12045]) will give a wrong output whereas new Int32Array([12045*12045]) will give the correct output. But it does not make any difference with tf.scalar(12045).mul(tf.scalar(12045)).print() and tf.scalar(12045, 'int32').mul(tf.scalar(12045, 'int32')).print(). The two will output the same wrong value

Daniel Smilkov

unread,
Jun 20, 2019, 10:03:01 AM6/20/19
to Edoh Kodjo, TensorFlow.js Discussion
Correct, unfortunately we can't do end-to-end int arithmetic in WebGL (it gets turned into 32 bit float along the pipeline).

If you need precise int arithmetic, tf.setBackend('cpu') and do tf.scalar(12045, 'int32').mul(tf.scalar(12045, 'int32')).print()

Daniel

Edoh Kodjo

unread,
Jun 20, 2019, 10:12:24 AM6/20/19
to Daniel Smilkov, TensorFlow.js Discussion
Ok, thanks for the clarification. 
Reply all
Reply to author
Forward
0 new messages