TensorFlow 2.0致力于简化使用和提高易用性。本文基于Windows10操作系统,使用PyCharm搭建基于GPU加速的TesorFlow开发平台。
安装过程主要包含三个步骤:
-
安装Anaconda
下载 anaconda
Anaconda官网下载Windows操作系统安装包
运行安装包开始安装,勾选Advanceed Options第一项
anaconda安装 -
安装CUDA
下载CUDA
2.1 NVIDIA官网下载CUDA Toolkit 10 点击这里下载。注意:必须NVIDIA显卡才支持CUDA
2.2 安装需选择自定义安装。
自定义安装
2.3 取消GeForce这个选项
取消GeForce选项
2.4 取消Visual Studio选项
取消Visual Studio选项
2.5 查看Display Driver选项,左边为安装包中的驱动版本,右边显示为系统当前版本。如果安装包中驱动版本高于系统当前版本这勾选这个选项,否则不勾选这个选项
Display Driver选项 -
安装PyCharm配置TensorFlow环境
PyCharm安装教程很多这里就不过多说明了,主要讲述如何配置TensorFlow环境。
PyCharm中新建Pure Python项目tf2,Project Interpreter中选择创建新虚拟环境,使用Conda。
py016.png
项目创建好后打开PyCharm的终端,安装TensorFlow2.0
pip install tensorflow-gpu==2.0.0-alpha0
终端
安装cudnn,cudatoolkit numba
conda install cudnn cudatoolkit numba
安装完成后运行gpu_accelerate.py进行性能测试。
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([100000, 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=100)
gpu_time = timeit.timeit(gpu_run, number=100)
print('warm up:', cpu_time, gpu_time)
cpu_time = timeit.timeit(cpu_run, number=100)
gpu_time = timeit.timeit(gpu_run, number=100)
print('run time:', cpu_time, gpu_time)
运行测试结果如下
测试结果
可以看到左边为使用CPU的计算花费了19秒多的时间,而右边的GPU运算只用了0.01秒。采用测试设备为ThinkPad T470P笔记本 CPU i7-7700HQ,显卡为比较差的GeForce 940MX。
网友评论