Install anaconda, tensorflow2.0, pytorch in linux system, simple code comparison pytorch, tensorflow1.0, tensorflow2.0

After installing the ubuntu system, we will start the process of deep learning. The first is to configure the python language, a variety of deep learning frameworks, and the environment of various installation packages.

This article will introduce the following:

  • Install anaconda in linux system
  • Install tensorflow2.0 in linux system
  • Install pytorch in linux system
  • Simple comparison of pytorch, tensorflow1.0, tensorflow2.0

The installation of this article relies on anaconda (because it is very simple). If you want to install the application yourself, you can refer to this article: Installation process of CUDA, cuDNN and tensorflow-gpu under ubuntu16.04

One, install anaconda in linux system

1. Download anaconda from the website

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.2.0-Linux-x86_64.sh

2. Install anaconda
bash Anaconda3-5.2.0-Linux-x86_64.sh
3. Update conda download source

Enter the following in the terminal:

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 --set show_channel_urls yes
4. Anaconda commonly used commands
conda --version           # 获取conda版本号   
conda info --envs         # 获取当前所有虚拟环境
source activate 【your_env_name】# 进入某个环境
source deactivate         # 退出当前环境
conda create --name 【new_env_name 】--clone 【old_env_name】 # 复制某个环境
conda remove --name 【your_env_name 】--all  # 删除某个环境
conda list # 查看当前环境中有哪些安装包

Second, install tensorflow2.0 in linux system

1. Install nvidia graphics driver

Before starting to install tensorflow2.0, we first determine whether the linux system has installed the nvidia graphics driver.
I will not repeat them here, recommend the following URL for detailed operation: ubuntu16.04 system run nvidia graphics driver installed
How to view graphics driver specific information, I can refer to another article: Linux-Ubuntu system to view video card model, graphics Detailed information, Graphics ladder diagram

2. Install tensorflow2.0
1) Use anaconda to install tensorflow2.0

Use conda to create a python virtual environment (-clone is recommended here, spyder4.1.2 is recommended)

conda create --name 【new_env_name 】

The command to install tensorflow2.0 is as follows ( this command will automatically install the CUDA and cuDNN packages ):

conda install tensorflow-gpu==2.2.0
2) Manually download the tensorflow2.0 installation package and install directly

Many times use anaconda to install tensorflow2.0 because the file is large, and the download will be interrupted. At this time, it is recommended to download and install manually.

Enter the following command in the ubuntu terminal to view the required version information of cuda and cudnn

conda install tensorflow-gpu==2.1.0

The version information and download addresses required by cuda and cudnn are as follows:
Insert picture description here
enter the two links corresponding to cuda and cudnn, search and download according to the package name, and
Insert picture description here
use the conda install command to directly install cuda and cudnn

conda install --offline cudnn-7.6.5-cuda10.1_0.tar.bz2
conda install --offline cudatoolkit-10.1.243-h6bb024c_0.tar.bz2

Using Douban source to download tensorflow
has been verified many times, and this method is fast, has a high success rate, and is effective.

pip install tensorflow-gpu==2.1.0 -i https://pypi.doubanio.com/simple

Use the following method to verify whether the installed tensorflow is the gpu version

Enter python3the python command interface in the terminal , enter the following command, if the return result is True, it is the gpu version, otherwise it is not.

import tensorflow as tf
tf.test.is_gpu_available()

The specific process is as follows:
Insert picture description here
After installation, the following error message may appear when the spyder editor is opened. You can click the link below to solve the
error message as follows:

TypeError: handle_get_file_code() got an unexpected keyword argument ‘save_all‘

The solution link is as follows:
https://blog.csdn.net/TFATS/article/details/110424064

Three, install pytorch in linux system

1. Direct installation of pytorch official website command

Enter the pytorch official website and download the following picture:
Insert picture description here
Because sometimes the automatic download speed is too slow, you can use the following methods to download and install

2. Install after downloading on the official website

Or you can go directly to the download website to download https://download.pytorch.org/whl/torch_stable.html .
According to the download command generated after selecting the torch, python, torchvision, and cuda version numbers on the official website, find these two download files and download them.
As shown in the figure below: The
Insert picture description here
download file name with parameters is explained as follows:
Insert picture description here

3. Install pytorch

The two download files after downloading are as follows:
Insert picture description here

pip install torch-1.6.0+cu101-cp36-cp36m-linux_x86_64.whl
pip install torchvision-0.7.0+cu101-cp36-cp36m-linux_x86_64.whl

Fourth, simply compare pytorch, tensorflow1.0, tensorflow2.0

1,tensorflow1.0
# 1 + 1/2 + 1/2^2 + 1/2^3 + ... + 1/2^50

import tensorflow as tf 
print(tf.__version__)

x = tf.Variable(0.)
y = tf.Variable(1.)

print(x)
print(y)

# x = x + y 
add_op = x.assign(x + y)
# y = y / 2
div_op = y.assign(y / 2)

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer())
    for iteration in range(50):
        sess.run(add_op)
        sess.run(div_op)
    print(x.eval())   # sess.eval(x)
    
# -----output-------
1.15.0
<tf.Variable 'Variable_12:0' shape=() dtype=float32_ref>
<tf.Variable 'Variable_13:0' shape=() dtype=float32_ref>
2.0
2,pytorch
import torch 
assert torch.cuda.is_available()

print(torch.__version__)

x = torch.Tensor([0.])
y = torch.Tensor([1.])

for iteration in range(50):
    x = x + y 
    y = y / 2 
    
print(x)

# -----output--------
1.6.0+cu101
tensor([2.])
3,tensorflow2.0
import tensorflow as tf 
# tf.enable_eager_execution()     # tensorflow在1.6版本后添加了此启用动态图机制功能;2.0版本移除了该方法。
tf.compat.v1.enable_eager_execution()	# tensorflow在2.0版本后默认自动开启动态图机制功能;手动开启方法如上。

print(tf.__version__)

x = tf.constant(0.)
y = tf.constant(1.)

for iteration in range(50):
    x = x + y 
    y = y / 2 
    
print(x.numpy())

# ------output-------
2.2.0
2.0

Guess you like

Origin blog.csdn.net/TFATS/article/details/109177575