-
Notifications
You must be signed in to change notification settings - Fork 74.1k
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 2.0] How to globally force CPU? #31135
Comments
There might be a better way, but you can use |
This can help you set all operations on CPU; import tensorflow as tf
print(tf.__version__)
# Set CPU as available physical device
my_devices = tf.config.experimental.list_physical_devices(device_type='CPU')
tf.config.experimental.set_visible_devices(devices= my_devices, device_type='CPU')
# To find out which devices your operations and tensors are assigned to
tf.debugging.set_log_device_placement(True)
# Create some tensors and perform an operation
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
print(c) Output 2.0.0-beta1
Executing op MatMul in device /job:localhost/replica:0/task:0/device:CPU:0
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32) |
That works, thank you. |
for anyone who is using tf 2.1, the above comment does not seems to work. I use |
@henrysky this is indeed part of the breaking changes introduced in 2.1 (as documented in the release notes), which promoted some former experimental functions to the public scope; thank you for pointing the new correct syntax out :) |
tf.config.set_visible_devices([], 'GPU') |
I managed to limit cpus by using
The later call of |
I also wanted to use TF 2.10 with CPU only as it defaults to using my GPU. However the above can not be used in Jupyter Labs due to this error message "RuntimeError: Visible devices cannot be modified after being initialized" So is there any other way to force tensorflow to use CPU to perform all calculations ? |
In TF 1.x it was possible to force CPU only by using:
However,
ConfigProto
doesn't exist in TF 2.0 and changing a OS environment variable seems very clunky.What's the TF 2.0 way of doing this?
The text was updated successfully, but these errors were encountered: