win10 +Tensorflow-gpu环境搭建及使用(cuda10.1+python3.8+tensorflow2.3.1)【详细】

1、准备工作

首先我们要确定电脑所能支持的tensorflow版本,根据tensorflow官网所给配置,我们要去检查电脑gpu的cuda支持版本,再去对应下载python版本和tensorflow版本。
右键桌面>NVIDIA控制面板>帮助>系统信息>组件
在这里插入图片描述

上图说明我的显卡所支持的cuda版本为11.1(向下兼容)

官网配置截图
在这里插入图片描述

cuda安装

由于tensorflow最高版本对应的是cuda10.1版本,那我们下载cuda10.1就可以

到英伟达官网下载cuda(注意:电脑上一开始没有配置cuda,以上只是在查版本,所以要去下载)https://developer.nvidia.com/cuda-toolkit-archive
点击CUDA Toolkit 10.1在这里插入图片描述
在这里插入图片描述
下载完后安装,自定义安装,地址默认即可
在这里插入图片描述
记下安装的路径(配置环境用得到)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1

cuDNN安装

登录英伟达官网https://developer.nvidia.com/rdp/cudnn-download
在这里插入图片描述
下载完后将解压文件夹中的三个文件夹全部复制进cuda安装目录下,没有文件会覆盖
在这里插入图片描述

cuda+cuDNN环境配置

系统应该自动添加了以下环境变量
在这里插入图片描述
以及在这里插入图片描述
我们需要在path中额外添加

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\lib64

  
  
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述
win+R cmd 中输入

nvcc -V

  
  
  • 1

在这里插入图片描述

python安装

win+R cmd 中输入

python

系统原先没有的话,会弹出window应用商店提供安装(一般是python3.8版本)
在这里插入图片描述

2、Tensorflow 安装

tensorflow的安装需要很多依赖库,为了方便依赖库的安装,直接用Anaconda的环境

Anaconda安装

下载地址:https://www.anaconda.com/products/individual
在这里插入图片描述
在这里插入图片描述
安装好后配置环境,在path中添加

D:\Software\Anaconda3\Scripts
D:\Software\Anaconda3\Library\bin
D:\Software\Anaconda3

在这里插入图片描述

TensorFlow安装

打开Anaconda Powershell Prompt,在Anaconda的终端下输入

conda activate tensoflow-gpu

  
  
  • 1

创建环境,环境名为tensoflow-gpu
在这里插入图片描述

接着输入

pip install -U tensorflow-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple

  
  
  • 1

通过清华源下载tensorflow,当然没有版本限制的话直接下的是2.3.1的最新版本,若选择版本则添加版本限定

pip install -U tensorflow-gpu==2.3.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

  
  
  • 1

下载完后我们进行测试tensorflow的GPU版本是否安装成功
输入ipython进入ipython交互式终端,再输入命令

import tensorflow as tf 

  
  
  • 1

没有报错继续输入,测试GPU是否可用

tf.test.is_gpu_available()

  
  
  • 1

在这里插入图片描述
在这里插入图片描述
如果为true,则TensorFlow GPU版本安成功
可用以下命令查看tensorflow版本号

tf.__version__

  
  
  • 1

在这里插入图片描述

退出ipython交互式终端

exit()

  
  
  • 1

顺便安装python常用库

python -m pip install  -U numpy matplotlib pillow pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

  
  
  • 1

在这里插入图片描述
退出tensoflow-gpu环境

conda deactivate

  
  
  • 1

在这里插入图片描述完成

测试

进入环境conda activate tensoflow-gpu>ipython>粘贴测试代码

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这里插入图片描述
输出
在这里插入图片描述

3、TensorFlow IDE(集成开发环境)

推荐使用PyCharm(免费)
下载官方网址:https://download.jetbrains.com/python/pycharm-professional-2020.2.3.exe
下载安装后点击右下角设置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
ok,然后apply
新建
在这里插入图片描述
运行测试程序(!!!注意:不要在anaconda终端环境没有退出的情况下在pycharm运行,不然会报错显存不足,failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED)
在这里插入图片描述
在这里插入图片描述
OK
如有错误,欢迎指正

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.无mkl支持; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 TI 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]://home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: bazel build --config=opt --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值