Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

Time:2020-10-27

Write it at the front

Recently, I’m going to try to run tensorflow GPU training model. The commonly used MBP is that AMD only supports CPU training, but there are idle win10 laptops. NVIDIA supports CUDA.

Tensorflow GPU training acceleration needs a GPU graphics card that supports CUDA. The CUDA mentioned here refers to a parallel computing platform and programming model developed by NVIDIA. It can greatly improve the computing performance by utilizing the processing power of GPU. For more information, please seeRelated contents

The process of building the environment is relatively simple as a whole. It takes a while to find and download the relevant toolkit.

1 hardware conditions

First of all, check whether your graphics card model meets the requirements, and tensorflow official documenthardware requirementsIt is mentioned that CUDA? Architecture is NVIDIA? GPU card with 3.5, 3.7, 5.2, 6.0, 6.1, 7.0 or higher.

1.1 check the graphics card model

The first step is to check your video card model, enter NVIDIA control panel, and click system information.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

The graphics card model is geforce GTX 850m, and the computer is quite old.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

1.2 check whether the video card supports CUDA

The video card model isGeForce GTX 850M, click to viewGPU card supporting CUDA +List to see if the graphics card model is in the supported list.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

The computing power is 5. The graphics card supports CUDA. Next, check the CUDA driver version, NVIDIA control panel, and system information components.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

The version is 11.1.96. If the version is too low, you can check the software download mentioned below to install and upgrade the latest driver.

2 software download

2.1 download and install NVIDIA? GPU driver

Input information query graphics card driver, download and install the latestNVIDIA? GPU driverTo facilitate CUDA toolkit 10.1 installation, CUDA toolkit 10.1 needs driver version 418. X or higher.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

The version number is 456.71, which meets the requirements. Then download it and install the driver.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

2.2 download and installCUDA® Toolkit

CUDA toolkit provides a development environment for creating high performance GPU accelerated applications. Tensorflow (tensorflow 2.1.0 and later) supports CUDA 10.1’sCUDA® Toolkit

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

Select the temporary file directory, where the default settings are selected.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

Then continue to the next step until here. If visual studio is not installed on the computer, you will be prompted to install visual studio. If you plan to build tensorflow from the source code, you need to install it. The specific installation content can be viewedBuild from source code in Windows Environment

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

After the installation is completed, several environment variables will be added to the system automatically. Open the terminal for inputnvcc -VVerify the installation results.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

2.3 download cudnn SDK 7.6

Cudnn is a GPU accelerated deep neural network library. To download cudnn SDK, you need to register NVIDIA Developer program. After entering the download page, select cudnn 7.6 of CUDA 10.1.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

After the download is successful, unzip the compressed package and put the copy into the CUDA directoryC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\Next, copy here refers to putting the files under these three directories into the corresponding directory of CUDA v10.1.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

2.4 TensorRT 6.0(optional)

Can be used to reduce the delay time of inference with some models and improve the throughputZipMode, optional here.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

3 install anaconda

useAnacondaIf you think Anaconda takes up too much disk space, you can use itminicondaThe minionda is usually developed on the computer. For the installation and use of minionda, you can clickthis

3.1 download Anaconda

3.2 installation of Anaconda

Installation process is also relatively simple, need to pay attention to the following two places.

Anaconda occupies a large amount of disk space after installation. If Disk C has limited space, it is not recommended to install it on disk C.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

There are two options. The first is to add Anaconda to the environment variable. If another version of anaconda is installed on the computer, the use of Anaconda will be affected. The advantage of adding Anaconda to the environment variable is that you can directly open the terminal to execute the relevant command management package. If you do not add it to the environment variable, you can also find Anaconda in the windows start menu Prompt, and then execute the relevant command management package on the open terminal, so you can not add it to the system environment variables here.

The second is to set the python 3.7 version of Anaconda as the default Python on my computer, so that your IDE and other editors will detect this version, so that you can use this version when debugging code in the IDE later. If Python has been installed on the computer before, you can choose it according to your situation.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

3.3 configure Anaconda

Anaconda official packet image, download is slow, you can consider using Tsinghua imagehttps://mirrors.tuna.tsinghua…

Configure the new mirror address to.condarcFile, configuration image content can be viewedAnaconda Tsinghua image help

4 test

4.1 installing tensorflow

Open the installed Anaconda navigator and create a virtual environment for testing tensorflow GPU.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

It should be noted that if you use pip to install tensorflow, you can directly install tensorflow packagepip install --upgrade tensorflowFor tensorflow 1. X, the CPU and GPU packages are separate, while 2. X packages them together. Other 2. X GPU packages are also releasedpip install --upgrade tensorflow-gpuInstall the GPU package.

When using CONDA installation, Anaconda cloud officially split tensorflow 2. X CPU and GPU packages, which should reduce the package installation volume.

Use CONDA to install tensorflow GPU, enter tensorflow GPU in the input field, select tensorflow GPU, and click apply.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

4.2 run the code to test the installation results

Open the terminal of the current environment or open jupytarlab or notebook and run the following code.

import tensorflow as tf
from tensorflow.python.client import device_lib

//Print existing GPU 
print(tf.config.list_physical_devices('GPU'))
//Print GPU device name
print(tf.test.gpu_device_name())
//Print devices on a computer that can be used for computing
print(device_lib.list_local_devices())

Running in jupytarlab, the result is

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

The devices that can be used for computing are CPU and GPU, and print their information and GPU computing power respectively. After the installation is successful, you can run the code to train the neural network.

4.3 a simple neural network test

Finally, let’s take a look at hello world demo, a common entry point. According to a simple formula and constructing a neural network, we can simply predict the price of a house.

import tensorflow as tf
import numpy as np
from tensorflow import keras

model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float)
ys = np.array([5.0, 6.5, 8.0, 9.5, 11.0, 12.5], dtype=float)
model.fit(xs, ys, epochs=1000)
print(model.predict([7.0]))

If the price of the house is very simple, if a house with a bedroom is 150000 + 350000, that is, a house with master bedroom is 500000, a house with two bedrooms is 650000, and so on.

Input 6 pieces of data to predict how much a 7-bedroom house costs. Here, the unit is reduced to facilitate the neural network to process the data. Input data 1 represents a house with a bedroom, and output 5 represents 500000.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

After 1000 times of training, the screenshot of predicted operation results is shown below.

Accelerating support of tensorflow 2.1 + GPU training model in Windows Environment

Reference link

Original addresshttps://github.com/liuvigongzuoshi/blog