0 foreword
TensorFlow 2.0 was officially released early this morning.
TensorFlow 2.0 is much better than PyTorch and is ready for a full shift to the new and improved deep learning framework.
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:
- Check the conda environment: conda env list
- To create a conda environment (env_name is the name of the created environment 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
- Conda install numpy # conda uninstall numpy
- 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.
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)
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 cudatoolKit needs to be updated to 10.0 if it is smaller than 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)
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.
1.2 Finally, we tested a linear fitting code written in version TF2.0
Save the following code as main.py
X = tf. Constant ([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) y = tf. The constant ([[10.0], [20.0]]) class Linear (tf) keras) Model) : self.dense = tf.keras.layers.Dense( kernel_initializer=tf.zeros_initializer(), bias_initializer=tf.zeros_initializer() output = self.dense(input) optimizer = Tf.keras.optimizers.SGD(learning_rate=0.01) with tF.gradienttape () : loss = tf.reduce_mean(tf.square(y_pred - y)) grads = tape.gradient(loss, model.variables) optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))Copy the code
The following output is displayed:
[< tf.variable 'linear/dense/kernel:0' shape=(3, 1) dType =float32, NUMPY = [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
Welcome to xiao Song’s official account ** “Minimalist AI” ** Take you to learn deep learning:
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.