Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf.cast on native python float to dtype=tf.float64 leads to loss of precision #35938

Closed
st-- opened this issue Jan 16, 2020 · 3 comments · Fixed by #35961
Closed

tf.cast on native python float to dtype=tf.float64 leads to loss of precision #35938

st-- opened this issue Jan 16, 2020 · 3 comments · Fixed by #35961
Assignees
Labels
comp:ops OPs related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug

Comments

@st--
Copy link

st-- commented Jan 16, 2020

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): no
  • OS Platform and Distribution: Linux Ubuntu 18.04
  • TensorFlow installed from (source or binary): binary (pip install)
  • TensorFlow version (use command below): v2.1.0-rc2-17-ge5bf8de 2.1.0, v2.0.0-rc2-26-g64c3d38 2.0.0
  • Python version: 3.7

Describe the current behavior

tf.cast() on a python float (e.g, a literal float constant, not a numpy float/array type) does an implicit conversion to the TensorFlow default dtype tf.float32, which can result in loss of precision when intending to cast to tf.float64:

>>> tf.cast(0.2, tf.float64)
<tf.Tensor: id=37, shape=(), dtype=float64, numpy=0.20000000298023224>

Describe the expected behavior

>>> tf.cast(0.2, tf.float64)
<tf.Tensor: id=37, shape=(), dtype=float64, numpy=0.2>

Code to reproduce the issue
See above.

Other info / logs
This was discovered as a bug in GPflow, which we built a work-around for in GPflow/GPflow#1211 - but this is a pervading issue, and it would be good to fix this upstream instead of having to write and use a gpflow.cast() everywhere just to work around having potentially passed in a python float.

@st--
Copy link
Author

st-- commented Jan 16, 2020

The simple workaround would be to add

    if not tf.is_tensor(value):
        return tf.convert_to_tensor(value, dtype)

to the beginning of the definition of tf.cast().

This is related to tensorflow#26033 - not having to support our own "default dtype" implementation would remove the need for a lot of the calls to tf.cast.

@yongtang
Copy link
Member

Added PR #35961 for the fix.

@oanush oanush self-assigned this Jan 17, 2020
@oanush oanush added comp:ops OPs related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug labels Jan 17, 2020
@oanush oanush assigned gowthamkpr and unassigned oanush Jan 17, 2020
@tensorflow-bot
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:ops OPs related issues TF 2.0 Issues relating to TensorFlow 2.0 type:bug Bug
Projects
Development

Successfully merging a pull request may close this issue.

4 participants