Home  ›  Tensorflow  ›  配置  ›  深度学习
Tensorflow 环境搭建
admin  •  2020-02-17 22:59:53  •  117 views

概述

本文的目的是指导读者在 Windows 10 系统上安装 tensorflow 2.1.0。

成功安装的关键在于一定要下载正确的软件版本,此处涉及到操作系统版本,python 版本,pip 版本,CUDA 版本,显卡驱动版本,cuDNN 版本,tensorflow 版本。

前提条件

  1. 操作系统:Windows 10 64-bit
  2. 显卡:支持 CUDA 的 Nvidia 独立显卡,可在此处查看自己的显卡是否支持 CUDA,笔者此处使用的显卡为 GTX 1050ti。
  3. Anaconda 3,可在镜像站下载

安装 CUDA

在此处下载 CUDA 10.1 Windows 10 64位。 之后双击进行安装,保持默认的安装路径,注意安装选项中有些选择框可以取消勾选,例如 Visual Studio 扩展。如果安装失败,就重启再试几次。我当时第一次也是报错,第二次就可以正常安装了。 安装结束后需要重启系统。 重启后检查环境变量 CUDA_PATH 是否已经正确设置,我这里得值为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1

安装 cuDNN

在此处下载,注意需要登录以及同意协议,之后下载对应 CUDA 10.1 的版本。

下载好之后是一个压缩包,解压缩后: image.png

将对应文件夹复制到此路径:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 image.png

创建虚拟环境并安装 tensorflow

conda create -n tensorflow python=3.7 -y

之后激活该环境,注意 powershell 下不能直接激活环境,需切换到 cmd: activate tensorflow

之后直接 pip install tensorflow==2.1.0

PS C:\Users\Song> cmd
Microsoft Windows [Version 10.0.18363.592]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\Song>activate tensorflow

(tensorflow) C:\Users\Song>pip install tensorflow
Collecting tensorflow

验证安装是否成功

python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

从输出中我们可以发现 tensorflow 已经识别出了 GPU:

(tensorflow) C:\Users\Song>python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2020-01-18 16:49:37.235829: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cudart64_101.dll81: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor
2020-01-18 16:49:40.343442: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library nvcuda.dll5.719187: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N
2020-01-18 16:49:40.445183: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: loc                                pciBusID: 0000:01:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1 1050 Ti, pci bus id: 0000:01:00.0, compute capab
coreClock: 1.62GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-01-18 16:49:40.455461: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cudart64_101.dllng>
2020-01-18 16:49:40.464788: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cublas64_10.dll
2020-01-18 16:49:40.473574: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cufft64_10.dll
2020-01-18 16:49:40.479816: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library curand64_10.dll
2020-01-18 16:49:40.488185: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cusolver64_10.dll
2020-01-18 16:49:40.496774: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cusparse64_10.dll
2020-01-18 16:49:40.560866: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic                                 library cudnn64_7.dll
2020-01-18 16:49:40.573027: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-01-18 16:49:40.578171: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that thi                                s TensorFlow binary was not compiled to use: AVX2
2020-01-18 16:49:40.587584: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.62GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-01-18 16:49:40.601792: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-01-18 16:49:40.606484: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-01-18 16:49:40.610985: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-01-18 16:49:40.614911: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-01-18 16:49:40.619218: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll       2020-01-18 16:49:40.623819: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll       2020-01-18 16:49:40.627825: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-01-18 16:49:40.632180: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-01-18 16:49:41.291048: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:    2020-01-18 16:49:41.295451: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0
2020-01-18 16:49:41.298248: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N
2020-01-18 16:49:41.301383: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2990 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
tf.Tensor(3203.9128, shape=(), dtype=float32)

(tensorflow) C:\Users\Song>

至此,配置结束。