CyberPlayerOne
CyberPlayerOne

Reputation: 3178

For tensorflow 2.x, how to switch between the CPU and GPU version?

I have updated anaconda to its latest version. It has both tensorflow 2.2 and tensorflow-gpu 2.2 installed. But when I import tensorflow, tensorflow-gpu is the default one to be used. Is there a way to switch between them?

The similar questions asked on this website are all for 1.x version. I have tried the solutions and it seems it doesn't work for 2.x version. Tensor flow toggle between CPU/GPU

Upvotes: 2

Views: 750

Answers (2)

DachuanZhao
DachuanZhao

Reputation: 1349

Another way is :

#use gpu 0
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.set_visible_devices(physical_devices[0], 'GPU')

#use cpu
tf.config.set_visible_devices([], 'GPU')

Upvotes: 0

SchelmEntwicklerJustin
SchelmEntwicklerJustin

Reputation: 125

This should fix your Problem:

with tf.device('/gpu:0'):
    # YOUR def main() OR model.fit()
with tf.device('/cpu:0'):
    # YOUR def main() OR model.fit()

This should work with TF2 without sessions.

Upvotes: 2

Related Questions