tensorflow 快速安装入门教程

经过几年的完善发展,tensorflow 安装已经非常简单。本文介绍 windows 环境下安装的关键步骤。

1. 安装 anaconda

anaconda 是一个快速安装 python 环境的程序,到该程序官网下载安装即可。安装完毕后,在开始菜单里找到 anaconda prompt 或者 anaconda powershell prompt 快捷方式,启动命令行界面。

anaconda 内置了 pip 命令,可快速对 python 配套程序模块进行管理。我们这里关心如下几个命令:

  • pip install :安装程序模块
  • pip uninstall :卸载程序模块
  • pip list :查看已安装的程序模块

2. 安装 tensorflow

安装 cpu 版本的 tensorflow 命令如下:

pip install tensorflow

安装 gpu 版本的 tensorflow 命令如下:

pip install tensorflow-gpu

上述命令默认安装最新版本的 tensorflow。如果安装特定版本,可用下面的命令

pip install tensorflow==1.2.3

其中,1.2.3是安装包的版本号。如果该版本号不存在,该命令返回一个可用的版本清单。因此,也可以通过输入一个不存在的版本号,查询安装包的可用版本号。

3. 安装 gpu 驱动程序

去英伟达官网,https://www.geforce.cn/drivers,下载安装驱动程序。硬件型号匹配完全自动搞定。

4. 安装 cuda

去 cuda 官网,https://developer.nvidia.com/cuda-toolkit,下载,安装过程全自动。如果需要在 visual studio 中使用 cuda,请先安装 visual studio。

5. 安装 cuDNN 库

输入 https://developer.nvidia.com/cudnn 网址来到下载页面,得到相关包后解压,直接复制到 cuda 路径对应的文件夹下面就行。

6.验证安装

在 anaconda prompt 命令行环境下,键入 python,进入 python 系统,键入下面命令验证cpu版本正确性:

>>>import tensorflow as tf
>>>hello = tf.constant('Hello, World!')
>>>sess = tf.Session()
>>>sess.run(hello)
b'Hello, World!'

tensorflow-gpu 版本,可用下面程序验证:

import tensorflow as tf

with tf.compat.v1.Session() as sess: 
    with tf.device("/gpu:0"): 
        a = tf.compat.v1.placeholder(tf.int32) 
        b = tf.compat.v1.placeholder(tf.int32) 
        add = tf.add(a, b)
        print(sess.run(add, feed_dict={a: 3, b: 4}))

正常情况,上述步骤一次搞定。如果存在细节问题,百度一下即可。主要步骤如本文所述,没有太多玄乎的安装技巧。


附. 可能遇到的问题

1. 找不到 xxx.dll 文件

安装完成后碰到的问题:运行tensorflow时,出现ImportError: DLL load failed:找不到指定的模块:cublas64_100.dll、cusolver64_100.dll、cudart_64_100.dll。

解决办法:先进入路径‘C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin’,复制下面三个文件到桌面,改名字为上述找不到指定模块的名称,改后后,再粘贴回去,tensorflow就能成功运行了。

cublas64_10.dll
cusolver64_10.dll
cudart64_101.dll
curand64_10.dll

2. 找不到 GPU 设备

执行下面代码时,提示找不到 GPU 设备:

import tensorflow as tf

with tf.Session() as sess: 
    with tf.device("/GPU:0"): 
        a = tf.placeholder(tf.int16) 
        b = tf.placeholder(tf.int16) 
        add = tf.add(a, b)
        print(sess.run(add, feed_dict={a: 3, b: 4}))

错误信息为:
InvalidArgumentError: Cannot assign a device for operation Add_1: node Add_1 (defined at C:\Users\xuyeping\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.

检查后发现,忘记安装 cuDNN 库了,安装后即消除此问题。

3. 清华大学镜像库

如果下载速度慢,可尝试清华大学镜像库。

  • 临时使用
    可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple
    例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gevent,这样就会从清华这边的镜像去安装gevent库。

  • 永久修改
    Linux下,修改 ~/.pip/pip.conf (没有就创建一个), 修改 index-url至tuna,内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

windows下,直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini,内容如下

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

我用下面的方法安装了 tensorflow:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==1.13.2
发布了174 篇原创文章 · 获赞 80 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/quicmous/article/details/102505370