Additional recommended reading:

1. “Take you to AI” takes you to AI and TensorFlow2 Combat introduction: How to fast deep Learning development

· Baseline (Based on Keras Val_ACC: 0.88)

·DC race Bearing fault detection Baseline (based on Keras1D convolution VAL_ACC :0.99780)

4. The author’s in-depth study public account “Minimalist AI” :

Minimalist AI public account

 

0 foreword

Updated the latest installation method, now supports TensorFlow1.13.1 -> TensorFlow2.4.0: see section 1.3

This article will guide you to install the official TF2.0 version (CPU and GPU) in the simplest way. I will step on the pit to facilitate you to experience the official TF2.0 version.

Without further ado, let’s begin the tutorial.

 

1 Environment Preparation

I am currently on Windows10, using a python environment managed by conda, installing cuda and cudnn via conda (GPU support), and tensorflow2.0 via PIP. After trying only the simplest way to install, do not need to configure complex environment.

(The installation of Ubuntu and MAC versions can follow this method, because Conda supports multiple platforms, there should be no problem, if you have more questions, please feel free to comment, I will update the Ubuntu installation tutorial later)

1.0 ConDA Environment Preparation

Conda is a great Python management tool that makes it easy to set up and manage multiple Python environments. I will also introduce some common conda directives in the following installation steps.

Conda I recommend installing Miniconda, which can be understood as a stripped-down version of Anaconda, with only the necessary components, so it will be much faster to install, and it will also be able to manage our Python environment. (Anaconda typically takes a few gigabytes of memory and takes 1-2 hours to install on a SOLID state drive. Miniconda typically takes a few hundred megabytes and can be installed in 10 minutes.)

Miniconda recommended source download: tsinghua mirrors.tuna.tsinghua.edu.cn/anaconda/mi…

Just pick the version that works for you,

  • Windows recommend address: mirrors.tuna.tsinghua.edu.cn/anaconda/mi…
  • Ubuntu recommended address: mirrors.tuna.tsinghua.edu.cn/anaconda/mi…
  • Mac OS recommended address: mirrors.tuna.tsinghua.edu.cn/anaconda/mi…

Install Miniconda for Windows as a demonstration. Download the appropriate version from above, open it as administrator and click Install.

Note that both are checked, one is to allow us to use the conda directive directly in CMD, and the other is to use Miniconda’s python3.7 as system Python.

After the installation, you can use the conda command in CMD, CMD to open the way, Windows key +R key, pop up the input box, enter CMD to enter. You can also directly search for CMD in Windows and hit Run.

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

  1. Check the conda environment: conda env list
  2. To create a conda environment (env_name is the name of the created environment and can be customized) : conda create -n env_name
  3. Activate the conda environment (Ubuntu and Macos replace conda with source) : conda activate env_name
  4. Exit the conda environment: conda deactivate
  5. Conda install numpy # conda uninstall numpy
  6. To view the list of installed Python, run the conda list -n env_name command

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

In order to speed up the installation, you can change the source of conda and PIP.

Now change the “conda” source to tsinghua source to speed up, copy and paste the following command in “CMD” and press 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
Copy the code

At the same time, we also change “PIP” to Tsinghua source to speed up, copy and paste the following command in “CMD”, and press Enter:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Copy the code

 

1.1 TF2.0 CPU version Installation

TF CPU installation is relatively easy, because there is no need to configure GPU, so Windows Ubuntu macOS installation method is similar, the disadvantage is that the running speed is slow, but it can be used for daily learning.

The following is a demonstration of the Windows version: All operations are performed on the command line

1.1.0 Create TF2.0 CPU environment (use conda python==3.6 to indicate python3.6 at the same time)

Conda create -n TF_2C python=3.6Copy the code

Proceed ([y]/n)? Type y enter

When you’re done, you can enter the environment

1.1.1 Entering the TF_2C Environment

conda activate TF_2C
Copy the code

After entering, we can find :(TF_2C) before the previous path, indicating entering the environment. To exit, use conda deactivate.

Enter conda activate TF_2C again to execute the following command

1.1.2 Installing TF2.0 CPU version (the -i after means download from Domestic Tsinghua source, which is much faster than the default source)

PIP install tensorflow = = 2.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simpleCopy the code

If the network is bad, perform several times. And then it’s all set up after a while. So 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 tf
version = tf.__version__
gpu_ok = tf.test.is_gpu_available()
print("tf version:",version,"\nuse GPU",gpu_ok)
Copy the code

If there is no problem, the output is as follows: tf version 2.0.0 is CPU version, so GPU is False

1.2 TF2.0 GPU Installation

GPU version is similar to CPU, but there will be an additional step for GPU support installation. Let’s do it step by step. Make sure your PC has an Nvidia GPU before installing

1.2.0 New TF2.0 GPU environment (use conda new environment instruction python==3.6 to indicate python3.6 at the same time)

Conda create -n TF_2G python=3.6Copy the code

Proceed ([y]/n)? Type y enter

When you’re done, you can enter the environment

1.1.1 Entering the TF_2G Environment

conda activate TF_2G
Copy the code

1.1.2 Installing THE GPU version support, Windows with Nvidia GPU generally has the default driver, just need to install CudatoolKit and CUDNN package, note that you need to install CudatoolKit 10.0, note that If cudatoolKit is smaller than 10.0, update to 10.0

Conda install cudatoolkit cudnn = 10.0Copy the code

1.1.3 Installing TF2.0 GPU version (the -i after means download from 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/simpleCopy the code

If the network is bad, perform several times. And then it’s all set up after a while. So 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 tf
version = tf.__version__
gpu_ok = tf.test.is_gpu_available()
print("tf version:",version,"\nuse GPU",gpu_ok)
Copy the code

If there is no problem, the output is as follows: It can be seen that tf version is 2.0.0 because it is GPU version, so GPU is True, which means that the GPU version has been installed.

Tf version: 2.0.0 use GPU TrueCopy the code

 

1.3 Other TensorFlow versions Installed (TensorFlow1.13.1 -> TensorFlow2.4.0)

 

You can start by following this link to see the dependencies for the TensorFlow version:

https://tensorflow.google.c om /install/source#linux

 

 

As can be seen, if TensorFlow2.4 is installed, CUDA=11.0, cuDNN=8.0; Tensorflow2.1-2.3 corresponds to CUDA=10.1, cuDNN=7.6; TensorFlow2.0, corresponding to CUDA=10.0, cuDNN=7.6; Tensorflow1.13.1-1.15.0, corresponding to CUDA=10.0, cuDNN=7.6. Note the Nvidia Driver version number must be greater than =CUDA version number.

Pay attention to this, the wrong version will cause GPU installation failure.

The following describes different versions of TensorFlow.

Starting with TensorFlow 2.1, the PIP package TensorFlow also includes GPU support, without the need to install the GPU version through the specific PIP package TensorFlow-GPU. If you are sensitive to the size of the PIP package, install the CPU-only version of TensorFlow using the TensorFlow-CPU package.

 

  • Tensorflow 2.4/2.3 CPU version installed
PIP install tensorflow - CPU = = 2.4#orPIP install tensorflow - CPU = = 2.3Copy the code

 

  • Install CUDNn8 using PyTorch since CUDn8 has not been updated in Conda:
Conda install PyTorch =1.7.1 TorchVision TorchAudio CudatoolKit =11.0 PIP install TensorFlow ==2.4Copy the code

 

  • Tensorflow 2.3/2.2/2.1 GPU version installed
Conda install CUDatoolkit =10.1 CUDNN =7 PIP install tensorFlow ==2.3Copy the code

 

  • TensorFlow2.0 gpu version installed
Conda install cudatoolkit=10.0 CUDNN =7 PIP install tensorflow- GPU ==2.0Copy the code

 

  1. TensorFlow1.15/1.14/1.13.1 gpu version installed
Conda install cudatoolkit=10.0 CUDNN =7 PIP install tensorflow- GPU ==1.15Copy the code

 

1.4 Finally, we tested a linear fitting code written in version TF2.0

Save the following code as main.py

import tensorflow as tf

X = 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)    Get all the variables in the model directly using the model.variables property
    optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
    if i % 10= =0:
        print(i, loss.numpy())
print(model.variables)
Copy the code

The following output is displayed:

0 250.0 10 0.73648137 20 0.6172349 30 0.5172956 40 0.4335389 50 0.36334264 60 0.3045124 70 0.25520816 80 0.2138865 90 0.17925593 [< tf. The Variable'linear/dense/kernel:0'Shape =(3, 1) dType = Float32, NumPy = Array ([[0.40784496], [1.191065], [1.9742855]], DType =float32)>, < tf.variable'linear/dense/bias:0'Shape = (1,) dtype = float32, numpy = array ([0.78322077], dtype = float32) >]Copy the code

– 1. The postscript

Replies to two more questions in the comments section:

Is it necessary to install the TF environment after it is created? I’ve installed it directly from root several times

The default environment installation is used when you install without creating a new environment. This is not recommended, there may be conflicts after installing new modules in the default environment. You u are advised to use different environments for different tasks.

Using Conda Install eliminates the need to pre-configure CudatoolKit and CUDNN. (CudatoolKit and CUDNN version issues)

Conda install is not currently supported in TF2.0, only PIP install can be used. For Windows, conda install CudatoolKit cudnn is available. Note that cudatoolKit >= 10.0 is used for tf1.14. Because windows10 cudatoolkit is version 9 by default, you need to manually install version 10. If you install version 10, you can install version 9, 8, and 7 with Conda

 

Here is a collection of PDF books for getting started, covering TensorFlow, PyTorch, and MXNet. The reason recommended is easy to understand, suitable for beginners to study. The list of books is as follows: Simple and crude TensorFlow2 Latest Chinese version, Hands-on Deep Learning PyTorch Latest Chinese version, Hands-on deep Learning MXNet latest Chinese version

Based on the sharing of theoretical learning and application development technology of deep learning, the author will often share the dry contents of deep learning. When learning or applying deep learning, you can also communicate with me on this page if you have any questions.

From CSDN blog expert & Zhihu deep learning columnist @Xiaosong yes