win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中

使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中

  • 1.打开Anaconda Prompt
  • 2.建立一个名为tf2.1(该名称可随便取)的环境,使用Python3.7
  • 3.进入tf2.1环境
  • 4.安装cuda10.1(因为这里安的TensorFlow是2.1版本,对应的cuda版本是10.1,cudnn版本是7.6,如果要安装别的版本的TensorFlow,需安装对应的cuda和cudnn版本。)
  • 5.安装cudnn7.6
  • 6.安装TensorFlow-GPU2.1
  • 2021.6.30补充
    • 使用nvcc -V 可以查看系统环境下(本地)的cuda版本。
    • 查看conda支持的cuda和cudnn版本
    • 测试

  因为之前在电脑上安装了PyTorch,装了cuda10.2,网上有各种说法,有说cuda10.2没有对应的TensorFlow版本,所以就用不了TensorFlow了,还有的说可以在本地装两个cuda,一个cuda10.2,一个cuda10.0然后就能使用cuda10.0版本对应的TensorFlow-GPU版了,但我按照方法安装之后,安装是可以安装,但是不能使用GPU加速。
  跑测试代码会显示GPU:false
  测试代码如下:

import tensorflow as tf
import os

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

a = tf.constant(1.)
b = tf.constant(2.)
print(a+b)

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

报如下错误:

D:\Anaconda3\envs\tf2.1.0\python.exe "C:/Users/1/Desktop/Gesture Recognition/EDenseNet/test2.py"
2021-05-15 17:20:13.842076: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-15 17:20:13.842233: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-05-15 17:20:15.299935: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-05-15 17:20:16.004743: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce MX350 computeCapability: 6.1
coreClock: 1.468GHz coreCount: 5 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 52.21GiB/s
2021-05-15 17:20:16.005776: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-15 17:20:16.088248: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-15 17:20:16.128756: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-15 17:20:16.142859: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-15 17:20:16.194405: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
tf.Tensor(3.0, shape=(), dtype=float32)
2021-05-15 17:20:16.225556: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-15 17:20:16.226581: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2021-05-15 17:20:16.226706: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2021-05-15 17:20:16.227360: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2021-05-15 17:20:16.227895: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-15 17:20:16.227996: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      
WARNING:tensorflow:From C:/Users/1/Desktop/Gesture Recognition/EDenseNet/test2.py:10: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2021-05-15 17:20:16.230236: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce MX350 computeCapability: 6.1
coreClock: 1.468GHz coreCount: 5 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 52.21GiB/s
2021-05-15 17:20:16.231241: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-15 17:20:16.231361: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-15 17:20:16.231450: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-15 17:20:16.231538: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-15 17:20:16.231627: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-15 17:20:16.231719: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-15 17:20:16.232770: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2021-05-15 17:20:16.232900: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1592] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
**GPU: False**
2021-05-15 17:20:16.746028: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-15 17:20:16.746122: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2021-05-15 17:20:16.746174: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 

Process finished with exit code 0

  由上图看出虽然装的是GPU版,但是程序并不能调用到GPU。
  后来发现可以一个方法可以安装TensorFlow-GPU版:之前我一直都是在本地计算机安装cuda和cudnn,后面发现不在本地安装cuda和cudnn,而是在anaconda新建的虚拟环境中安装cuda和cudnn,然后再安装对应的TensorFlow-GPU版本,可以work。现记录下来,给别人一个参考,也方便以后自己查看。

1.打开Anaconda Prompt

2.建立一个名为tf2.1(该名称可随便取)的环境,使用Python3.7

conda create --name tf2.1 python=3.7

win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中_第1张图片

3.进入tf2.1环境

activate tf2.1

4.安装cuda10.1(因为这里安的TensorFlow是2.1版本,对应的cuda版本是10.1,cudnn版本是7.6,如果要安装别的版本的TensorFlow,需安装对应的cuda和cudnn版本。)

conda install cudatoolkit=10.1

5.安装cudnn7.6

conda install cudnn=7.6

6.安装TensorFlow-GPU2.1

pip install tensorflow-gpu==2.1

安装完成就可以使用TensorFlow2.1GPU版本啦!
win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中_第2张图片
  因为我们是将TensorFlow-GPU2.1装在虚拟环境中的,cuda和cudnn也是装在虚拟环境中的,所以理论上我们可以再新建一个虚拟环境,装不同版本的TensorFlow,同理pytorch也一样,当然我认为是这样的,我自己也没试过。有需要的朋友可以试一下。

2021.6.30补充

使用nvcc -V 可以查看系统环境下(本地)的cuda版本。

nvcc -V

win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中_第3张图片

查看conda支持的cuda和cudnn版本

  打开Anaconda Prompt,输入下列命令查看conda目前支持的cudatoolkit版本。(cudatoolkit即cuda)。

conda search cudatoolkit --info

win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中_第4张图片
输入下列命令查看conda目前支持的cudnn版本。

conda search cudnn --info

(注:安装cudnn时,如果conda install cudnn不指定版本,系统会自动安装和当前cuda匹配的cudnn)

测试

用conda list看下上面在虚拟环境下安装的包。

conda list

win10使用anaconda将TensorFlow2.1GPU版安装在虚拟环境中_第5张图片
  可以看到虚拟环境里的cuda版本为10.1,而本地的cuda版本是10.2。说明虚拟环境中的cuda和本地的是相互独立的。

参考链接:https://blog.csdn.net/qq_41719643/article/details/109777042

你可能感兴趣的:(深度学习,tensorflow,gpu,anaconda,cuda)