在 Windows 上通过 DirectML 启用 TensorFlow

此版本通过使用 TensorFlow 1.15 的 TensorFlow with DirectML 包,为学生、初学者和专业人士提供了在其现有硬件上运行机器学习 (ML) 训练的方法。 要在 TensorFlow 2 上使用 DirectML,请查看 TensorFlow-DirectML-Plugin。 设置后,可以使用现有的示例模型脚本,或查看 DirectML 存储库中的一些示例

检查 Windows 版本

本机 Windows 上的 TensorFlow with DirectML 包从 Windows 10 版本 1709(内部版本 16299 或更高版本)开始运行。 可以查看内部版本号,方法是通过 Run 命令(Windows 徽标键 + R)运行 winver

检查 GPU 驱动程序更新

确保已安装最新的 GPU 驱动程序。 在“设置”应用的“Windows 更新”部分中,选择“检查是否有更新”

设置 TensorFlow with DirectML

建议在 Windows 内设置虚拟 Python 环境。 可以使用许多工具来设置虚拟 Python 环境 - 对于这些说明,我们将使用 Anaconda 的 Miniconda。 此设置的其余部分假定你使用 Miniconda 环境。

设置 Python 环境

在系统上下载并安装 Miniconda Windows 安装程序。 Anaconda 网站上提供了关于设置的附加指南。 安装 Miniconda 后,使用名为 directml 的 Python 创建环境并通过以下命令将其激活。

注意

在下面的命令中,我们使用 Python 3.6。 但是,tensorflow-directml 包适用于 Python 3.5、3.6 或 3.7 环境。

conda create --name directml python=3.6 

conda activate directml 

安装 TensorFlow with DirectML 包

通过运行以下命令,通过 pip 安装 TensorFlow with DirectML 包。

注意

tensorflow-directml 包仅支持 TensorFlow 1.15。

pip install tensorflow-directml

安装 tensorflow-directml 包后,可以通过添加两个张量来验证其是否正确运行。 将以下行复制到交互式 Python 会话中。

import tensorflow.compat.v1 as tf 

tf.enable_eager_execution(tf.ConfigProto(log_device_placement=True)) 

print(tf.add([1.0, 2.0], [3.0, 4.0])) 

应会看到类似于以下内容的输出,其中 add 运算符位于 DML 设备上。

2020-06-15 11:27:18.235973: I tensorflow/core/common_runtime/dml/dml_device_factory.cc:45] DirectML device enumeration: found 1 compatible adapters. 

2020-06-15 11:27:18.240065: I tensorflow/core/common_runtime/dml/dml_device_factory.cc:32] DirectML: creating device on adapter 0 (AMD Radeon VII) 

2020-06-15 11:27:18.323949: I tensorflow/stream_executor/platform/default/dso_loader.cc:60] Successfully opened dynamic library DirectMLba106a7c621ea741d2159d8708ee581c11918380.dll 

2020-06-15 11:27:18.337830: I tensorflow/core/common_runtime/eager/execute.cc:571] Executing op Add in device /job:localhost/replica:0/task:0/device:DML:0 

tf.Tensor([4. 6.], shape=(2,), dtype=float32) 

TensorFlow with DirectML 示例和反馈

查看我们的示例,或使用你现有的模型脚本。 如果遇到问题或有关于 TensorFlow with DirectML 包的反馈,请与我们的团队联系