Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install CPU and GPU versions of TensorFlow2.x

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to install the CPU and GPU versions of TensorFlow2.x, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

0 preface

Updated the latest version of the installation method, currently supports TensorFlow1.13.1-> TensorFlow2.4.0: see Section 1.3

This article will lead you to install the official version of TF2.0 (CPU and GPU) in the easiest way, and I will step on it to facilitate you to experience the official version of TF2.0.

Let's cut the crap and now officially start the tutorial.

1 Environmental preparation

I am currently on Windows10, using the python environment managed by conda, installing cuda and cudnn through conda (GPU support), and installing tensorflow2.0 through pip. After trying, it is the easiest way to install, without the need to configure a complex environment.

1.0 conda environment preparation

Conda is an easy-to-use python management tool that facilitates the establishment and management of multiple python environments. I will also introduce some commonly used conda instructions in the following installation steps.

Conda I recommend installing miniconda, which can be understood as a stripped-down version of anaconda, which only retains some necessary components, so the installation will be much faster and can also meet our needs for managing the python environment. (anaconda usually takes a few gigabytes of memory to install on a solid-state disk, which takes 1-2 hours. Miniconda can be installed in a few hundred Ms in 10 minutes.)

Miniconda recommends using Tsinghua Source to download: https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/

Just choose the version that suits you.

Windows recommended address: https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.7.10-Windows-x86_64.exe

Ubuntu recommended address: https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.7.10-Linux-x86_64.sh

Mac os recommended address: https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.7.10-MacOSX-x86_64.pkg

Use the windows version to install miniconda as a demonstration, download the appropriate version from the above, and open it with administrator privileges and click install.

Note that both should be checked, one is to allow us to use the conda instruction directly in cmd, and the second is to use the python3.7 that comes with miniconda as the system python.

After installation, you can use the conda command in cmd, cmd open mode, windows key + R key, pop-up input box, enter cmd to enter. You can also directly search for cmd in windows and click to run.

Here are some cmd conda directives (env_name stands for environment name):

View the conda environment: conda env list

Create a new conda environment (env_name is the name of the environment created and can be customized): conda create-n env_name

Activate the conda environment (ubuntu and Macos replace conda with source): conda activate env_name

Exit the conda environment: conda deactivate

Install and uninstall the python package: conda install numpy # conda uninstall numpy

View the list of installed python: conda list-n env_name

Knowing these instructions, you can start using conda to create a new environment to install TF2.0.

At the same time, to speed up the installation, you can change the sources of conda and pip.

Below, change the "conda" source to Tsinghua source for acceleration, copy and paste the following instructions in "CMD", and enter:

Conda config-add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/conda config-add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/conda config-add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/conda config-append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/conda config-append channels https://mirrors.tuna. Tsinghua.edu.cn/anaconda/cloud/pytorch/conda config-append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/conda config-set show_channel_urls yes

At the same time, we also change "pip" to Tsinghua source for acceleration, copy and paste the following instructions in "CMD", and enter:

Pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple1.1 TF2.0 CPU version installation

TF CPU installation is relatively simple, because there is no need to configure GPU, so windows ubuntu macOS installation methods are similar, but the disadvantage is that the running speed is slow, but it can be used for daily learning and use.

The following is demonstrated with the windows version: all of the following are operated on the command line

1.1.0 create a new TF2.0 CPU environment (use the conda new environment directive python==3.6 to python3.6 at the same time as the environment is created)

Conda create-n TF_2C python=3.6

When popping up: Proceed ([y] / n)? Enter y enter

When you are finished, you can enter this environment.

1.1.1 enter the TF_2C environment

Conda activate TF_2C

After entering, we can find: (TF_2C) in front of the previous path, indicating that you have entered the environment. You can exit using conda deactivate.

We re-enter conda activate TF_2C to facilitate the execution of the following command

1.1.2 install the TF2.0 CPU version (the following-I indicates that it is downloaded from a domestic Tsinghua source, which is much faster than the default source)

Pip install tensorflow==2.0.0-I https://pypi.tuna.tsinghua.edu.cn/simple

If the network is not good, execute it several times more. And then it will be installed after a while. Let's do a simple test.

1.1.3 Test the TF2.0 CPU version (save the following code to demo.py and run it using TF_2C python)

Import tensorflow as tfversion = tf.__version__gpu_ok = tf.test.is_gpu_available () print ("tfversion:", version, "\ nuse GPU", gpu_ok)

If there is no problem, the output is as follows: you can see that the tf version is 2.0.0 because it is the cpu version, so the gpu is False

1.2 TF2.0 GPU version installation

The GPU version is similar to CPU, but there will be one more step in the installation of GPU support. Let's do it step by step. Make sure your computer has Nvidia GPU before installation

1.2.0 create a new TF2.0 GPU environment (use the conda new environment directive python==3.6 to python3.6 at the same time when the environment is created)

Conda create-n TF_2G python=3.6

When popping up: Proceed ([y] / n)? Enter y enter

When you are finished, you can enter this environment.

1.1.1 enter the TF_2G environment

Conda activate TF_2G

1.1.2 installation of GPU version is supported. Windows with Nvidia generally has a default driver. You only need to install cudatoolkit and cudnn package. Note that you need to install cudatoolkit version 10.0. Note that if the cudatoolkit of the system is less than 10.0, you need to update it to 10.0.

Conda install cudatoolkit=10.0 cudnn

1.1.3 install the TF2.0 GPU version (the following-I means downloading from the domestic Tsinghua source, which is much faster than the default source)

Pip install tensorflow-gpu==2.0.0-I https://pypi.tuna.tsinghua.edu.cn/simple

If the network is not good, execute it several times more. And then it will be installed after a while. Let's do a simple test.

1.1.3 Test the TF2.0 GPU version (save the following code to demo.py and run it using TF_2G python)

Import tensorflow as tfversion = tf.__version__gpu_ok = tf.test.is_gpu_available () print ("tfversion:", version, "\ nuse GPU", gpu_ok)

If there is no problem, the output is as follows: you can see that the tf version is 2.0.0 because it is the gpu version, so the gpu is True, which means that the GPU version is installed.

Tf version: install other versions of 2.0.0use GPU True1.3 TensorFlow (TensorFlow1.13.1-> TensorFlow2.4.0)

First of all, you can use this link to see the dependencies for the TensorFlow version:

Https://tensorflow.google.com/install/source#linux

As you can see, if you install TensorFlow2.4, it corresponds to CUDA=11.0,cuDNN=8.0;TensorFlow2.1-2.3, CUDA=10.0,cuDNN=7.6;TensorFlow1.13.1-1.15.0 and CUDA=10.0,cuDNN=7.6. Note that the version number of Nvidia Driver should be > = CUDA version number.

It should be noted that the wrong version will cause the installation of GPU to fail.

The following is explained for different versions of TensorFlow.

Starting with TensorFlow 2.1, the pip package tensorflow also includes GPU support, and there is no need to install the GPU version through the specific pip package tensorflow-gpu. If you are sensitive to the size of the pip package, you can use the tensorflow-cpu package to install only the TensorFlow version of CPU.

TensorFlow2.4/2.3 cpu version installation

Pip install tensorflow-cpu==2.4#orpip install tensorflow-cpu==2.3

TensorFlow2.4 gpu version installation (since cudnn8 has not been updated in conda, you need to install cudnn8 with pytorch:)

Conda install pytorch=1.7.1 torchvision torchaudio cudatoolkit=11.0pip install tensorflow==2.4

TensorFlow2.3/2.2/2.1 gpu version installation

Conda install cudatoolkit=10.1 cudnn=7pip install tensorflow==2.3

TensorFlow2.0 gpu version installation

Conda install cudatoolkit=10.0 cudnn=7pip install tensorflow-gpu==2.0

TensorFlow1.15/1.14/1.13.1 gpu version installation

Finally, conda install cudatoolkit=10.0 cudnn=7pip install tensorflow-gpu==1.151.4, we tested a linear fitting code written in the TF2.0 version.

Save the following code as main.py

Import tensorflow as tfX = tf.constant ([[1.0,2.0,3.0], [4.0,5.0,6.0]]) y = tf.constant ([[10.0], [20.0]]) class Linear (tf.keras.Model): def _ init__ (self): super (). _ init__ () self.dense = tf.keras.layers.Dense (units=1, activation=None) Kernel_initializer=tf.zeros_initializer (), bias_initializer=tf.zeros_initializer () def call (self Input): output = self.dense (input) return output# the following code structure is similar to the previous section model = Linear () optimizer = tf.keras.optimizers.SGD (learning_rate=0.01) for i in range (100): with tf.GradientTape () as tape: y_pred = model (X) # call model y_pred = model (X) instead of explicitly writing y_pred = a * X + b Loss = tf.reduce_mean (tf.square (y_pred-y)) grads = tape.gradient (loss Model.variables) # use the attribute model.variables to directly obtain all variables in the model optimizer.apply_gradients (grads_and_vars=zip (grads, model.variables)) if I% 10 = 0: print (I, loss.numpy ()) print (model.variables) output is as follows: 0 250.010 0.7364813720 0.617234930 0.517295640 0.433538950 0.3633426460 0.304512470 0.2552081680 0.213886590 0.17925593 [ Thank you for reading this article carefully. I hope the article "how to install the CPU and GPU versions of TensorFlow2.x" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report