windows10下TensorFlow 2+cuda+cuDNN配置

anaconda安装

anaconda下载地址:
https://www.anaconda.com/products/individual
清华大学镜像站(推荐,国内站点)
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
直接下一步安装

CUDA安装

查看显卡驱动版本号:
在这里插入图片描述
官方查看对应的CUDA版本号地址:https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
在这里插入图片描述
我的456.81,选择11.1版本。

CUDA下载地址:
https://developer.nvidia.com/cuda-toolkit-archive
在这里插入图片描述
点下一步
在这里插入图片描述
选自定义,取消勾选vs integration
在这里插入图片描述

这里看版本,我的驱动版本已经比新版本更高,所以取消选择,点下一步,选择安装位置(记录下安装路径),我默认位置(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1)。点下一步
在这里插入图片描述

打钩,下一步
在这里插入图片描述
等绿条不转了安装结束。

cuDNN安装

cuDNN下载地址:
https://developer.nvidia.com/rdp/cudnn-archive
7.6版本可以通过以下地址免费下载:https://download.csdn.net/download/stone_tigerLI/16528461?spm=1001.2014.3001.5503
解压后如下:
在这里插入图片描述
将三个文件夹拷贝到CUDA根目录下
然后配置环境变量

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\libnvvp

测试:nvcc -V
在这里插入图片描述

TensorFlow 2安装

1. 创建虚拟环境
打开anaconda Prompt

  • 创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 .\venv 目录来存放它
python -m venv --system-site-packages .\venv

在这里插入图片描述

  • 激活虚拟环境
.\venv\Scripts\activate

在这里插入图片描述

  • 升级pip
pip install --upgrade pip

pip list  # show packages installed within the virtual environment
  • 退出虚拟环境:
deactivate  # don't exit until you're done using TensorFlow

2. 安装TensorFlow pip安装包

  • 系统安装
pip install --user --upgrade tensorflow
  • 验证安装效果
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
  • 返回结果如下,即返回张量,则表示安装成功:
2021-04-07 17:48:35.023098: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-04-07 17:48:35.070487: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x18380aeaa90 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-04-07 17:48:35.076893: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tf.Tensor(-716.05505, shape=(), dtype=float32)

测试

打开anaconda prompt,依次输入:

python

import tensorflow as tf 

tf.test.gpu_device_name()

我的测试结果如下:

2021-04-12 15:01:54.111274: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1660 computeCapability: 7.5
coreClock: 1.815GHz coreCount: 22 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 178.86GiB/s
2021-04-12 15:01:54.119479: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-04-12 15:01:54.122894: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-04-12 15:01:54.126258: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-04-12 15:01:54.130257: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-04-12 15:01:54.133702: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-04-12 15:01:54.137873: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-04-12 15:01:54.142229: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-04-12 15:01:54.146727: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-04-12 15:01:54.150634: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
2021-04-12 15:01:54.154061: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1261] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-04-12 15:01:54.158806: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1267]      0
2021-04-12 15:01:54.161329: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1280] 0:   N
2021-04-12 15:01:54.164296: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/device:GPU:0 with 4616 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1660, pci bus id: 0000:01:00.0, compute capability: 7.5)
2021-04-12 15:01:54.172058: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
'/device:GPU:0'

查看使用哪个GPU,依次输入代码:

from tensorflow.python.client import device_lib

device_lib.list_local_devices()

结果如下:
出现的最后一句:‘/device:GPU:0’ 则说明安装成功了。

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 8036385388359947959
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4841183641
locality {
  bus_id: 1
  links {
  }
}
incarnation: 3631306616704755276
physical_device_desc: "device: 0, name: GeForce GTX 1660, pci bus id: 0000:01:00.0, compute capability: 7.5"
]

另外测试:

import tensorflow as tf
tf.test.is_gpu_available()

输出结果:

GPU:0 with 4616 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1660, pci bus id: 0000:01:00.0, compute capability: 7.5)
2021-04-12 15:19:25.740823: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
True
}
incarnation: 8036385388359947959
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4841183641
locality {
  bus_id: 1
  links {
  }
}
incarnation: 3631306616704755276
physical_device_desc: "device: 0, name: GeForce GTX 1660, pci bus id: 0000:01:00.0, compute capability: 7.5"
]

到这里全部安装完成!!

若分别出现以下情况,则说明没有安装成功,没成功调用GPU。

可能的问题原因,CUDA安装的版本、TensorFlow版本、cuDNN的版本不对应,无法识别或找到对应的dll文件。
我的解决办法如下:
1、从新安装cuda和cuDNN,确保版本对应。
2、百度相应.dll文件下载放到CUDA对应目录下。

2021-04-07 18:08:06.142769: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-04-07 18:08:06.158718: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1d4006f0560 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-04-07 18:08:06.165391: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
''

上述其中第一行可通过以下代码取消:

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '1' # 默认,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '2' # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' # 只显示 Error
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 10723846442634604676
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 12741821707617156847
physical_device_desc: "device: XLA_CPU device"
]

计算时间测试代码:

import tensorflow as tf
import timeit

with tf.device('/cpu:0'):
    cpu_a = tf.random.normal([10000, 1000])
    cpu_b = tf.random.normal([1000, 2000])
    print(cpu_a.device, cpu_b.device)

with tf.device('/gpu:0'):
    gpu_a = tf.random.normal([10000, 1000])
    gpu_b = tf.random.normal([1000, 2000])
    print(gpu_a.device, gpu_b.device)

def cpu_run():
    with tf.device('/cpu:0'):
        c = tf.matmul(cpu_a, cpu_b)
    return c

def gpu_run():
    with tf.device('/gpu:0'):
        c = tf.matmul(gpu_a, gpu_b)
    return c

# warm up	这里就当是先给gpu热热身了
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('warmup:', cpu_time, gpu_time)


cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print('run time:', cpu_time, gpu_time)


在这里插入图片描述

资料

TensorFlow 2安装官方指导
https://tensorflow.google.cn/install/pip

windows下TensorFlow、cuDNN、CUDA版本对应表:
https://tensorflow.google.cn/install/source_windows#gpu

英伟达GPU计算力:https://developer.nvidia.com/cuda-gpus#compute

anaconda安装TensorFlow官方文档:https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow/

参考资料:https://blog.csdn.net/weixin_42642296/article/details/112565119

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值