tensorflow detailed installation tutorial (Win10, Anaconda, Python3.9)
tensorflow detailed installation tutorial (Win10, Anaconda, Python3.9)
1. Preparations for the tensorflow version
The main difference between the CPU version and the GPU version is the running speed. The GPU version runs faster, so if the computer graphics card supports cuda, it is recommended to install the gpu version.
The operation is not complicated. At first, I felt that it was very troublesome to download so many things, and I didn’t want to do it, but for the sake of speed, I finally tried to install it and found that it was not that difficult to do.
1.1 CPU version, no additional preparation required
The CPU version can be installed on general computers, and there is no need to prepare additional graphics card content.
1.2 GPU version, you need to download cuda and cudnn in advance
According to the instructions on installing TensorFlow on Windows (juejin.im) on the webpage, the following four conditions need to be met.
-
Check the computer's graphics card, this computer → right click and select Manage → Device Manager → Display Adapters.
The core display UHD Graphics 630 and the independent display GeForce GTX 1050, mainly look at the independent display GeForce GTX 1050.
Select the NVDIA GeForce GTX 1050 right-click and select Properties → Driver, you can see that the driver has been installed. One of the four conditions is met.
-
Check out CUDA Compute Capability at CUDA GPUs | NVIDIA Developer . Taking some screenshots, we can see that the Compute Capability of the GeForce GTX 1050 is 6.1 to meet one of the four conditions.
-
Check your computer's CUDA version. Right-click NVDIA Control Panel → System Information → Components at the icon , and in the red box, you can see that the version of CUDA is 11.1.
-
Download cuda and cudnn. Download the corresponding cuda and cudnn on the official website. The version can be lower than but not higher than the version supported by the computer. cuda download address: CUDA Toolkit Archive | NVIDIA Developer , cudnn download address: cuDNN Archive | NVIDIA Developer
I downloaded CUDA Toolkit 11.0.0, select the corresponding system, version and other options, Download the installation package.
Download the corresponding version of cuDNN. The cuDNN v8.0.5 for CUDA 11.0 is selected here.
-
CUDA installation: select custom installation→default installation path→installation end
After the installation is complete, two system variables are generated by default.
View system variables: This computer → Right-click Properties → Advanced System Settings → Environment Variables → System Variables
Here you can find the path in the system variable, then edit and add some paths, and finally there are four in total. If necessary, add additional paths in the future. , can be added here. -
cuDNN installation: Unzip → Copy the three folders to the cuda installation directory, and directly select the overwrite file.
After the installation is complete, test the cuda version.
Open cmd and enter the command:
nvcc -V
- 1
So far, all four conditions have been satisfied.
2. Download Anaconda
2.1 Download and install Anaconda
The download address is Anaconda | Anaconda Distribution . The version I installed is Python 3.9. During the installation process, remember to select automatic configuration of environment variables.
After the installation is complete, open Anaconda Prompt and enter the command:
conda --version to view the installed version
conda env list to view the installed environment, the "*" on the right indicates the currently used environment
- 1
- 2
- 3
2.2 Create an environment
-
Create a tensorflow environment and enter the command: conda create -n tensorflow python=3.9, which means to create an environment named tensorflow, the python version used in this environment is version 3.9
-
After the creation is successful, enter the command: conda env list, you can see that the tensorflow environment has been created, and the asterisk is the current environment (base environment base).
-
Enter the environment, enter the command: activate tensorflow, you can enter the tensorflow environment.
Because my conda environment is in the D drive, I changed the path to the following. If anaconda is installed with the default path, this step is not required.
-
Install the default version of tensorflow-cpu or tensorflow-gpu.
If cuda is not configured, to install the tensorflow-cpu version, you can enter the command: pip install --ignore-installed --upgrade tensorflow
If cuda is configured and the tensorflow-gpu version is installed, you can enter the command: pip install --ignore-installed --upgrade tensorflow-gpu
Then after downloading for a while, an error will be reported, which is the reason for the network speed.
The solution is: find the file tensorflow_gpu-2.8.0-cp39-cp39-win_amd64.whl (438.0 MB).
Log in to https://pypi.org/, search for tensorflow_gpu, and click the desired package name.
URL tensorflow-gpu · PyPI , download the file to the directory D:\Anaconda3\envs\tensorflow\.
Enter the command: pip install tensorflow_gpu-2.8.0-cp39-cp39-win_amd64.whl
At this time, ERROR: Could not find a version that satisfies the requirement XXX
Solution: This problem can be solved by directly selecting the pip source and trusting its source
pip install library package -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com Here, it is applicable to replace the pip source with Tsinghua source, Ali source, etc. -i https://pypi.tuna.tsinghua.edu.cn/simple pip install tensorflow_gpu-2.8.0-cp39-cp39-win_amd64.whl -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
- 1
- 2
- 3
- 4
induction 1.pip install --ignore-installed --upgrade tensorflow-gpu 2. Download the tensorflow_gpu-2.8.0-cp39-cp39-win_amd64.whl file 3.pip install tensorflow_gpu-2.8.0-cp39-cp39-win_amd64.whl -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
- 1
- 2
- 3
- 4
This will install successfully.
Enter the command: pip show tensorflow-gpu, you can view the version information of tensorflow
-
Exit the environment: conda deactivate
3. Test whether tensorflow-gpu is successfully installed
-
Open Anaconda, select the tensorflow environment, and open spyder. The first time you open it, you need to install Spyder, just click install below.
-
test code
import tensorflow as tf a = tf.constant(1.) b = tf.constant(2.) print(a+b) print(tf.__version__) print(tf.test.gpu_device_name()) print('GPU:',tf.config.list_physical_devices(device_type='GPU')) print('CPU:',tf.config.list_physical_devices(device_type='CPU')) print(tf.test.is_gpu_available())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
At this point there is an error:
Could not load dynamic library ‘cusolver64_11.dll‘; dlerror: cusolver64_11.dll not found
Solution:
Link: https://pan.baidu.com/s/1W9fR2N_hoVD-7_ODtOiJhg
Extraction code: u65iDownload the file and add the file cusolver64_11.dll to the created environment\Library\bin
The program runs normally and outputs the result
Simply test the difference between the running speed of cpu and gpu
import tensorflow as tf import timeit #Specify to run on cpu def cpu_run ( ) : with tf . device ( '/cpu:0' ) : cpu_a = tf . random . normal ( [ 10000 , 1000 ] ) cpu_b = tf . random . normal ( [ 1000 , 2000 ] ) cpu_c = tf.matmul(cpu_a, cpu_b) # print( "cpu_a: ", cpu_a.device) # print( "cpu_b: ", cpu_b.device) # print("cpu_c:", cpu_c.device) return cpu_c #Specify to run on gpu def gpu_run(): with tf.device( '/gpu:0'): gpu_a = tf.random. normal([ 10000,1000]) gpu_b = tf.random. normal([ 1000, 2000]) gpu_c = tf.matmul(gpu_a, gpu_b) # print( "gpu_a: ", gpu_a.device) # print("gpu_b: ", gpu_b.device) # print("gpu_c: ", gpu_c.device) return gpu_c cpu_time = timeit.timeit(cpu_run, number = 10) gpu_time = timeit.timeit(gpu_run, number = 10) print('cpu:',cpu_time, 'gpu:',gpu_time)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
The speed difference is obvious.
Reference link:
Install Anaconda/Python3.9/Tensorflow_Miska_Muska's blog-CSDN blog_anaconda install tensorflow
Install and run tensorflow_w_66666's blog in Anaconda - CSDN blog_anaconda how to install tensorflow
Anaconda under conda, pip install tensorflow-gpu_AnnnnnJie's blog - CSDN blog
Anaconda installs tensorflow-gpu hands-on tutorial