[python] python 虚拟环境构建 & GPU环境

2020-10-29 10:20:34 浏览数 (1)

GPU/python环境配置与验证。

(1)GPU加速型实例安装NVIDIA GPU驱动及CUDA工具包

(2)华为云linux服务器部署TensorFlow-gpu全攻略:https://www.cnblogs.com/zxyza/p/10535939.html

(3) Ubuntu安装Anaconda3: https://www.jianshu.com/p/d9fb4e65483c

(4)添加环境变量: vim ~/.bashrc

代码语言:javascript复制
        export PATH="/root/anaconda3/bin:$PATH"
        export PATH=/usr/local/cuda/bin{PATH: :{PATH}}
        export LD_LIBRARY_PATH=/usr/local/cuda/lib64{LD_LIBRARY_PATH: :{LD_LIBRARY_PATH}}
        export CUDA_HOME=/usr/local/cuda

(5)source ~/.bashrc

(6)创建虚拟环境:

conda create -n py37 python=3.7

进入环境

source activate py37

conda activate py37

退出环境

source deactivate

conda deactivate

(7)source activate py37

(8)安装tensorflow-gpu:pip install tensorflow-gpu==1.13.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

(9)测试:

import tensorflow as tf

import os

# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

print('GPU>>>>>>', tf.test.is_gpu_available())

a = tf.constant(2.0)

b = tf.constant(4.0)

print(a b)

(10) 结果:

GPU>>>>>> True

Tensor("add:0", shape=(), dtype=float32)

(11) 不同版本torch安装:

代码语言:javascript复制
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0

conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit=10.0

conda install pytorch==1.5.0 torchvision==0.6.0 cudatoolkit=10.1

上述命令直接安装太太太慢了,可以通过更换conda源来加速下载。

代码语言:javascript复制
# 修改conda配置
vim .condarc

# 在配置钟添加清华源
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - default
show_channel_urls: true


# 安装pytorch和对应版本的cudatoolkit
conda install pytorch=1.4.0 torchvision cudatoolkit=10.1

0 人点赞