文章目录

  • ​​前提:配置好Cuda和CuDnn​​
  • ​​一、查看版本需求​​
  • ​​二、安装​​
  • ​​三、验证tensorflow-gpu是否安装成功​​
  • ​​四、不用配置cuda和cudnn​​
  • ​​1-1. 「方法一」半自动安装​​
  • ​​1-2. 「方法二」全自动安装​​
  • ​​验证​​

前提:配置好Cuda和CuDnn

一、查看版本需求

​https://tensorflow.google.cn/install/source_windows#gpu​windows安装tensorflow-gpu_python

二、安装

pip install tensorflow==2.6.0

1.15以上的新版本的tensorflow,安装不再区分gpu还是cpu

windows安装tensorflow-gpu_tensorflow_02

三、验证tensorflow-gpu是否安装成功

import tensorflow as tf
tf.test.is_gpu_available()

>>> 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')]

四、不用配置cuda和cudnn

使用conda安装


1-1. 「方法一」半自动安装

先安装 cudnn 和 cudatoolkit 包,再安装 tensorflow 包

conda create -n tensorflow-38-1 python=3.8.13
conda activate tensorflow-38-1

# 会自动安装 cudatoolkit
conda install -c conda-forge cudnn
pip install tensorflow

1-2. 「方法二」全自动安装

缺点是版本相对 pip 稍低
如:目前 pip 版本为 2.8,conda 的版本为 2.6
conda install tensorflow-gpu 和 pip install tensorflow-gpu 安装的内容是不同的

conda create -n tensorflow-38-2 python=3.8.13
conda activate tensorflow-38-2

# 会自动安装 cudnn 和 cudatoolkit 包
conda install tensorflow-gpu -c conda-forge

验证

import tensorflow as tf
# 查看 tensorflow 版本
print(tf.__version__)
# 判断是否使用 GPU 构建
print(tf.test.is_built_with_cuda())
# 查看GPU列表
print(tf.config.list_physical_devices('GPU'))
# 查看驱动名称
if tf.test.gpu_device_name():
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF")