anaconda tensorflow-gpu配置验证

目录

tensorflow对应版本cuda和cudnn版本对照参考
anaconda安装
设定anaconda镜像源
安装CUDA
安装cudnn
配置环境变量
tensorflow-gpu安装
pycharm安装
附上MNIST代码
conda 常用命令

首先确认自己需要安装哪个版本的tensorflow再安装对应版本CUDA及cudnn!!!!!!!!
本教程实例为:
tensorflow CUDA cuDNN Python Anaconda
2.4.0 11.0 8.0 Python3.8 Anaconda 2020.11
需要的可以自取
链接:https://pan.baidu.com/s/1UcvTWXQSgId4p9mEpccrkg 
提取码:dhcc 

anaconda tensorflow-gpu配置验证_第1张图片

返回目录

anaconda安装

从官网中下载windows版本anaconda。

下载个人版本够用了,下面放的截止至文档编写完成当日最新版的anaconda下载链接

  • anacondawindows64位下载链接
  • anacondaLinux64位下载链接

点击next
anaconda tensorflow-gpu配置验证_第2张图片

选择 All user

anaconda tensorflow-gpu配置验证_第3张图片

安装路径选择D:\ProgramFIles\anaconda

anaconda tensorflow-gpu配置验证_第4张图片

全部勾选

第一个将Anaconda添加至系统环境变量
Anaconda3中的Python3.8设定为系统默认Python环境
anaconda tensorflow-gpu配置验证_第5张图片

设定anaconda镜像源

返回目录

打开Anaconda prompt
首先创建.condarc文件
.condarc文件默认是不存在的,但当用户第一次运行 conda config命令时,将会在windows系统中的C:\Users\dhcc路径下创建该文件。
用记事本打开该文件将下面任一脚本代码替代原有代码复制并保存。

#beiji
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.bfsu.edu.cn/anaconda
default_channels:
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/free
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/pro
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
channels:
 - defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
#在conda prompt输入使config生效
conda config --set show_channel_urls yes

安装CUDA

返回目录
CUDA官网下载

安装路径选择``

anaconda tensorflow-gpu配置验证_第6张图片
同意并继续
anaconda tensorflow-gpu配置验证_第7张图片
选择自定义安装

去掉Visual Studio Integration

Driver components中的Display Driver当前版本大于新版本也将钩去掉
anaconda tensorflow-gpu配置验证_第8张图片
选择安装位置选择默认即可, 记住安装路径。配置环境变量需要用到。
anaconda tensorflow-gpu配置验证_第9张图片
完成安装anaconda tensorflow-gpu配置验证_第10张图片

安装cudnn

返回目录
这里放一个cudnn官网链接,下载cudnn需要注册Nvidia账号,可能需要科学上网。

解压cudnn并将cuda文件夹名改为cudnn放在C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0目录下
anaconda tensorflow-gpu配置验证_第11张图片

配置环境变量

CUDAcuDNN库添加到环境变量中

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\cudnn\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\extras\CUPTI\lib64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\libnvvp

anaconda tensorflow-gpu配置验证_第12张图片

配置环境变量后重启电脑

tensorflow-gpu安装

返回目录

  1. 打开Anaconda Navigator
  2. 点击Environments
    anaconda tensorflow-gpu配置验证_第13张图片
    点击creat
    anaconda tensorflow-gpu配置验证_第14张图片
    4.输入环境变量名,选择python版本,点击create。稍等一会便创建成功
    anaconda tensorflow-gpu配置验证_第15张图片
    打开conda prompt
    输入conda update conda
    然后输入pip install tensorflow-gpu==2.4.0安装tensorflow2.4.0

验证tensorflow-gpu是否安装成功

(tensorflow1) C:\Users\dhcc>python
Python 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 22:54:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-02-27 16:24:32.243502: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
>>> tf.test.is_gpu_available()
2021-02-27 16:27:25.706998: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:83:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.88GiB deviceMemoryBandwidth: 836.37GiB/s
2021-02-27 16:27:25.719334: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-27 16:27:25.725227: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-27 16:27:25.731455: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-27 16:27:25.737729: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-27 16:27:25.744441: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-27 16:27:25.752021: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-27 16:27:25.759194: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-27 16:27:25.766735: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-27 16:27:25.782919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2021-02-27 16:27:26.528352: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-02-27 16:27:26.534888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267]      0
2021-02-27 16:27:26.539445: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0:   N
2021-02-27 16:27:26.558320: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/device:GPU:0 with 14827 MB memory) -> physical GPU (device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:83:00.0, compute capability: 7.0)
2021-02-27 16:27:26.569970: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
True

或者输入

>>> tf.config.list_physical_devices('GPU')
2021-02-27 16:40:04.176880: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-27 16:40:04.187789: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:83:00.0 name: Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.88GiB deviceMemoryBandwidth: 836.37GiB/s
2021-02-27 16:40:04.199739: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-02-27 16:40:04.205925: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-02-27 16:40:04.211750: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-02-27 16:40:04.218167: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-02-27 16:40:04.224249: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-02-27 16:40:04.229874: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-02-27 16:40:04.235953: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-02-27 16:40:04.242297: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-02-27 16:40:04.259506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

安装Pycharm

返回目录
安装Pycharm一直点下一步就可以
安装完成后需要将anaconda环境变量添加进pycharm中

  1. 打开pycharm软件,点击Create New Project选项
  2. 在弹出的界面中点击右侧的Interpreter下拉框后面的按钮
  3. 进入解释器设置界面,我们选择Conda Environment后面的按钮
  4. 选择Exiting environment
  5. 选择anaconda所需要的环境中的python.exe
  6. anaconda tensorflow-gpu配置验证_第16张图片
  7. 点击OK完成配置

附上MNIST代码

返回目录

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

import  tensorflow as tf
from    tensorflow import keras
from    tensorflow.keras import layers, optimizers, datasets



(x, y), (x_val, y_val) = datasets.mnist.load_data() 
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.
y = tf.convert_to_tensor(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
train_dataset = train_dataset.batch(200)

 


model = keras.Sequential([ 
    layers.Dense(512, activation='relu'),
    layers.Dense(256, activation='relu'),
    layers.Dense(10)])

optimizer = optimizers.Adam(learning_rate=0.001)


def train_epoch(epoch):

    # Step4.loop
    for step, (x, y) in enumerate(train_dataset):


        with tf.GradientTape() as tape:
            # [b, 28, 28] => [b, 784]
            x = tf.reshape(x, (-1, 28*28))
            # Step1. compute output
            # [b, 784] => [b, 10]
            out = model(x)
            # Step2. compute loss
            loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]

        # Step3. optimize and update w1, w2, w3, b1, b2, b3
        grads = tape.gradient(loss, model.trainable_variables)
        # w' = w - lr * grad
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

        if step % 100 == 0:
            print(epoch, step, 'loss:', loss.numpy())



def train():

    for epoch in range(30):

        train_epoch(epoch)






if __name__ == '__main__':
    train()

运行过程展示

anaconda tensorflow-gpu配置验证_第17张图片

conda 常用命令

返回目录

  • Conda是没有重命名环境的功能的, 要实现这个基本需求, 只能通过克隆-删除的过程。
  • 切记不要直接mv移动环境的文件夹来重命名, 会导致一系列无法想象的错误的发生!
1. conda --version #查看conda版本,验证是否安装

2. conda update conda #更新至最新版本,也会更新其它相关包

3. conda update --all #更新所有包

4. conda update package_name #更新指定的包

5. conda create -n env_name package_name #创建名为env_name的新环境,并在该环境下安装名为package_name 的包
可以指定新环境的版本号,例如:
conda create -n python2 python=python2.7 numpy pandas,
创建了python2环境,python版本为2.7,同时还安装了numpy pandas包

6. source activate env_name #切换至env_name环境

7. source deactivate #退出环境

8. conda info -e #显示所有已经创建的环境

9. conda create --name new_env_name --clone old_env_name #复制old_env_name为new_env_name

10. conda remove --name env_name –all #删除环境

11. conda list #查看所有已经安装的包

12. conda install package_name #在当前环境中安装包

13. conda install --name env_name package_name #在指定环境中安装包

14. conda remove -- name env_name package #删除指定环境中的包

15. conda remove package #删除当前环境中的包

16. conda create -n tensorflow_env tensorflow

conda activate tensorflow_env #conda 安装tensorflow的CPU版本

17. conda create -n tensorflow_gpuenv tensorflow-gpu

conda activate tensorflow_gpuenv #conda安装tensorflow的GPU版本

18. conda env remove -n env_name #采用第10条的方法删除环境失败时,可采用这种方法

19. conda install --channel https://conda.anaconda.org/anaconda tensorflow=1.8.0 
提供一个下载地址,使用上面命令就可安装1.8.0版本tensorflow

conda clean -p 删除一些没用的包,这个命令会检查哪些包没有在包缓存中被硬依赖到其他地方,并删除它们

conda clean -t可以将conda保存下来的tar打包。

conda clean -y -all //删除所有的安装包及cache

这里给出tensorflow对应版本cuda和cudnn版本对照参考。

你可能感兴趣的:(机器学习,tensorflow,python)