简体   繁体   中英

Tensorflow after 1.15 - No need to install tensorflow-gpu package

Question

Please confirm that to use both CPU and GPU with TensorFlow after 1.15 , install tensorflow package is enough and tensorflow-gpu is no more required.

Background

Still see articles stating to install tensorflow-gpu eg pip install tensorflow-gpu==2.2.0 and the PyPi repository for tensorflow-gpu package is active with the latest tensorflow-gpu 2.4.1 .

The Annaconda document also refers to tensorflow-gpu package still.

TensorFlow is a general machine learning library, but most popular for deep learning applications. There are three supported variants of the tensorflow package in Anaconda, one of which is the NVIDIA GPU version. This is selected by installing the meta-package tensorflow-gpu:

However, according to the TensorFlow v2.4.1 (as of Apr 2021) Core document GPU support - Older versions of TensorFlow

For releases 1.15 and older, CPU and GPU packages are separate:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

According to the TensorFlow Core Guide Use a GPU .

TensorFlow code, and tf.keras models will transparently run on a single GPU with no code changes required.

According to Difference between installation libraries of TensorFlow GPU vs CPU .

Just a quick (unnecessary?) note... from TensorFlow 2.0 onwards these are not separated, and you simply install tensorflow (as this includes GPU support if you have an appropriate card/CUDA installed).

Hence would like to have a definite confirmation that the tensorflow-gpu package would be for convenience (legacy script which has specified tensorflow-gpu, etc) only and no more required. There is no difference between tensorflow and tensorflow-gpu packages now.

It's reasonable to get confused here about the package naming. However, here is my understanding. For tf 1.15 or older , the CPU and GPU packages are separate:

pip install tensorflow==1.15      # CPU
pip install tensorflow-gpu==1.15  # GPU

So, if I want to work entirely on the CPU version of tf , I would go with the first command and otherwise, if I want to work entirely on the GPU version of tf , I would go with the second command.


Now, in tf 2.0 or above, we only need one command that will conveniently work on both hardware. So, in the CPU and GPU based system, we need the same command to install tf , and that is:

pip install tensorflow

Now, we can test it on a CPU based system ( no GPU )

import tensorflow as tf 
print(tf.__version__)

print('1: ', tf.config.list_physical_devices('GPU'))
print('2: ', tf.test.is_built_with_cuda)
print('3: ', tf.test.gpu_device_name())
print('4: ', tf.config.get_visible_devices())

2.4.1
1:  []
2:  <function is_built_with_cuda at 0x7f2ce91415f0>
3:  
4:  [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

or also test it on a CPU based system ( with GPU )

2.4.1
1:  [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2:  <function is_built_with_cuda at 0x7fb6affd0560>
3:  /device:GPU:0
4:  [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
     PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

So, as you can see this is just a single command for both CPU and GPU cases. Hope it's clear now more. But until now (in tf > = 2 ) we can also use -gpu / -cpu postfix while installing tf that delicately use for GPU / CPU respectively.

!pip install tensorflow-gpu 
....
Installing collected packages: tensorflow-gpu
Successfully installed tensorflow-gpu-2.4.1

# -------------------------------------------------------------

!pip install tensorflow-cpu
....
Installing collected packages: tensorflow-cpu
Successfully installed tensorflow-cpu-2.4.1

Check: Similar response from tf-team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM