Install the gpu version of tensorflow

The tensorflow official website prompts that Anaconda needs to install CUDA and cuDNN when installing the GPU version of tensorflow. The installation steps are as follows:

  1. Check the computing power of your own graphics card . It is recommended to install the gpu version with a computing power greater than 3.5

  2. Pay attention to the correspondence between the CUDA version and the graphics card, and the correspondence between tensorflow-gpu and cuDNN. For version correspondence, please refer to the official website of Tensorflow . Some data looks like this:

    Version Python version translater build tool cuDNN CUDA
    tensorflow_gpu-2.6.0 3.6-3.9 MSVC 2019 Basel 3.7.2 8.1 11.2
    tensorflow_gpu-2.5.0 3.6-3.9 MSVC 2019 Basel 3.7.2 8.1 11.2
    tensorflow_gpu-2.4.0 3.6-3.8 MSVC 2019 Basel 3.1.0 8.0 11.0
    tensorflow_gpu-2.3.0 3.5-3.8 MSVC 2019 Basel 3.1.0 7.6 10.1
    tensorflow_gpu-2.2.0 3.5-3.8 MSVC 2019 Basel 2.0.0 7.6 10.1
    tensorflow_gpu-2.1.0 3.5-3.7 MSVC 2019 Basel 0.27.1-0.29.1 7.6 10.1
    tensorflow_gpu-2.0.0 3.5-3.7 MSVC 2017 Basel 0.26.1 7.4 10
    tensorflow_gpu-1.15.0 3.5-3.7 MSVC 2017 Basel 0.26.1 7.4 10
    tensorflow_gpu-1.14.0 3.5-3.7 MSVC 2017 Basel 0.24.1-0.25.2 7.4 10
    tensorflow_gpu-1.13.0 3.5-3.7 MSVC 2015 update 3 Basel 0.19.0-0.21.0 7.4 10
    tensorflow_gpu-1.12.0 3.5-3.6 MSVC 2015 update 3 Basel 0.15.0 7.2 9.0
  3. Create a virtual environment:

    conda create -n tensorflow_gpu python=3.8
    
  4. In our specific use, in fact, what is really needed is not the whole CUDA, but cudatoolkit , so we install cudatoolkit directly here, without downloading more than 3 G of CUDA for local installation.

    > conda activate tensorflow_gpu
    > conda install cudatoolkit=11.0
    
  5. Install cuDNN, pay attention to check the version relationship between cuDNN and CUDA.

    > conda install cudnn=8
    

    There is a problem here. The CUDA installed before is 11.0, and the cuDNN version checked should be 8.0. 8.0 should have been installed, but the problem is that an error is prompted when installing 8.0. The corresponding package could not be found. The solution is to directly install the latest version of the current major version (8) without adding the minor version number after the dot.
    (There are two speculations here, one is because I changed the domestic source, maybe the domestic source does not have this installation package, or there may be no such version of the software package.) Check which versions are available
    conda search cudnn. It was found that there was only version 8.2.1, so using 8.0 found that it could not be found at all.

  6. Install tensorflow-gpu. I can't find it through conda installation here. It is said to be installed through pip on the Internet. The gpu version corresponding to the previous cudnn and CUDA version is 2.4.0

    pip install tensorflow-gpu==2.4.0
    
  7. Check whether the gpu version of your tensorflow is installed successfully:

    # 在新的tensorflow版本中,这里会有警告,说这个函数将要被淘汰
    > import tensorflow as tf	# 导入tensorflow包,并命名为tf
    > tf.test.is_built_with_cuda() # 判断是否可以调用CUDA
    > tf.test.is_gpu_available() # 判断是否可以调用GPU
    
    # 上面代码实现可能会出现很多提示,提示说函数将淘汰啥的,可以直接输出看看效果。
    > print(tf.test.is_gpu_available())
    
    # 对于新版本中,提示使用tf.config.list_physical_devices()和tf.config.list_logical_devices()来查看是否有GPU
    > tf.config.list_physical_devices()
    Out[]:
     [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
      PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    

Relationship with keras

When Google released TensorFlow 2.0 in June 2019, it announced that Keras is TensorFlow's official high-level API for fast and easy model design and training. And with the release of Keras2.3.0, it declares that this version is the first version of Keras synchronized with tf.keras.
That is to say, in the Tensorflow2.0 version, it is synchronized Keraswith . tf.kerasThen for TensorFlow2.0 version, it is more recommended to usetf.keras

First understand the history of Keras and TensorFlow. Keras was originally created and developed by Google AI developer/researcher Francois Chollet. Keras was developed to facilitate his own research and experiments. However, with the popularity of deep learning, many developers, programmers, and machine learning practitioners are flocking to Keras because of its easy-to-use API. There weren't many deep networks available at the time, and Keras, in order to train its own custom neural network, needed a computational engine (called the backend) to build the network's graph and topology, run the optimizer, and perform the actual number crunching. . For Keras, before v1.1.0, the default backend of Keras was Theano. At the same time, Google released TensorFlow, a symbolic math library for machine learning and neural network training. At this time, Keras began to support TensorFlow as the backend. Gradually, TensorFlow became the most popular backend, which made TensorFlow the default backend for Keras starting from the Kerasv1.1.0 release. Generally speaking, once TensorFlow becomes the default backend of Keras, the usage of TensorFlow and Keras will grow together, that is, Keras cannot be used without TensorFlow, so if Keras is installed on the system, then it must Install TensorFlow. Similarly, TensorFlow users are increasingly attracted by the ease of use of the high-level Keras API. tf.keras was introduced in TensorFlow v1.10.0, which is the first step to integrate keras directly into the TensorFlow package. At this time, the f.keras package is separate from the keras package you installed via pip (ie pipinstallkeras). To ensure compatibility, the original keras packages are not included in tensorflow, so their development is in order.因此这段时间内开发的keras版本与TensorFlow版本需要有个对应关系,否则可能会报错. However, TensorFlow 2.0 released by Google in June 2019 declared that Keras is the official high-level API of TensorFlow. And with the release of Keras2.3.0, it is declared that this version is the first version of Keras synchronized with tf.keras. And for this release, Francois states:

  1. This is the first version of Keras to sync with tf.keras;
    it is also the final version of Keras that supports multiple backends (ie Theano, CNTK, etc.).
    Most importantly, all deep learning practitioners should convert their code to TensorFlow 2.0 and the tf.keras package.
    The original keras package will still receive bugs and fixes, but moving forward, you should start using tf.keras.

Therefore, if it is the Kears module started with tensorflow2.0, you can learn it directly, but for tensorflow1.0 who has already learned deep learning, you can refer to automatically upgrade the code to TensorFlow 2 . Of course, there are some differences, you can go to the official website to find information.

Pit encountered when using Keras

In the process of using Keras, I used Tensorflow2.6.0 version, and when I used it from tensorflow.keras.applications.resnet50 import ResNet50, an error was reported cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental. The problem is caused by incompatibility between tensorflow and Keras versions. Then the solution will be immediate, just match the version.

# 首先查看一下tensorflow的版本
import tensorflow as tf
print(tf.__version__)

# 查看一下keras版本
import tensorflow.keras as keras
print(keras.__version__)

After version 2.3.0, there is a one-to-one correspondence between Keras and Tensorflow versions, so just install the keras version as the tensorflow version. (Of course, you can also change the tensorflow version to the Keras version, but the installation package to be replaced is much larger than the replacement of keras).

Install the keras version as tensorflow: here is the keras version 2.6 as an example

pip install keras==2.6

There is another problem. In the process of using keras, some data sets and weight information may need to be downloaded. The default location is C:\Users\用户名\.kerasin the directory. Therefore, when more keras models are running and the c disk is not enough, you can regularly clean up the directory.

Guess you like

Origin blog.csdn.net/weixin_41012765/article/details/124973351