[go: nahoru, domu]

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.keras.backend.set_floatx does not change the default dtype in tensor initialisation #15594

Closed
FHof opened this issue Nov 5, 2021 · 2 comments

Comments

@FHof
Copy link
FHof commented Nov 5, 2021

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): I didn't execute an official example script.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Arch Linux and Ubuntu 20.04
  • TensorFlow installed from (source or binary): binaries from conda-forge and pip3
  • TensorFlow version (use command below): intel_tensorflow 2.6.0, tensorflow 2.6.0 from conda-forge, tensorflow-gpu 2.4.1 from conda-forge and tensorflow 2.6.1 from pip3
  • Python version: 3.9.7
  • Bazel version (if compiling from source):
  • GPU model and memory: no nvidia GPU and Nvidia GeForce RTX 3080
  • Exact command to reproduce: python3 -c 'import tensorflow as tf; tf.keras.backend.set_floatx("float64"); print(tf.keras.backend.floatx(), tf.constant(1.0))'

Describe the problem.

I would like to change the default float precision for initialising tensors and found the tf.keras.backend.set_floatx function in the documentation.
When I use this function, tf.keras.backend.floatx() shows the configured float type but unexpectedly this type does not affect the type of created tensors:

python3 -c 'import tensorflow as tf; tf.keras.backend.set_floatx("float64"); print(tf.keras.backend.floatx(), tf.constant(1.0))'

The command outputs float64 tf.Tensor(1.0, shape=(), dtype=float32) when I execute it.
I have tested it with intel_tensorflow 2.6.0, tensorflow 2.6.0 from conda-forge, tensorflow-gpu 2.4.1 from conda-forge and tensorflow 2.6.1 from pip3. The type of the created tensor never matched the configured default type.

Issue at tensorflow: tensorflow/tensorflow#52941
More related issues: #15226, tensorflow/tensorflow#51379

The documentation only shows Sets the default float type., so I assume that the floatx is supposed to affect both keras and tensorflow.
If this is not the case, I would like to know if it is possible at all to globally change the default dtype in tensorflow tensor initialisations such as tf.zeros and tf.constant.

Describe the current behavior.

The default float precision is used when creating a tensor.

Describe the expected behavior.

If I don't explicitly specify a dtype argument, the dtype is float32 and not the current keras backend floatx.

Contributing.

  • Do you want to contribute a PR? (yes/no): no
  • If yes, please read this page for instructions
  • Briefly describe your candidate solution(if contributing):

Standalone code to reproduce the issue.

python3 -c 'import tensorflow as tf; tf.keras.backend.set_floatx("float64"); print(tf.keras.backend.floatx(), tf.constant(1.0))'
@sanatmpa1
Copy link
Contributor

@FHof,

tf.keras.backend.set_floatx supports only for Keras and it doesn't have any effect on Tensorflow, Can you take a look at this issue? where a similar feature has been proposed and it is still open. Thanks!

@FHof
Copy link
Author
FHof commented Nov 9, 2021

Thank you for linking to the issue. That is what I was looking for.

@FHof FHof closed this as completed Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants