其他分享
首页 > 其他分享> > Tensorflow如何使用GPU训练(笔记)

Tensorflow如何使用GPU训练(笔记)

作者:互联网

参考:https://blog.csdn.net/qq_31554953/article/details/107302404

Tensorflow和tf.keras 模型可以在单个GPU上透明运行,而无需更改。

注意:(1)需要使用tf.config.experimental.list_physical_devices('GPU')确认使用的tensorflow可以使用GPU。

(2)在一台机器上运行多个GPU,或者在多台机器上运行,最简单的方法是使用分布策略

确保你的机器已经安装TensorflowGPU版。

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

运行结果:

Num GPUs Available:  2

TensorFlow支持在各种类型的设备上运行计算,包括CPU和GPU。 它们用字符串标识符表示,例如:

(1)"/device:CPU:0": The CPU of your machine.

(2)"/GPU:0": Short-hand notation for the first GPU of your machine that is visible to TensorFlow.

(3)"/job:localhost/replica:0/task:0/device:GPU:1": 你的机器的第二个可用的GPU的全称。

如果TensorFlow操作同时具有CPU和GPU实施,则默认情况下,将操作分配给设备时,GPU设备将获得优先级。 例如,tf.matmul同时具有CPU和GPU内核。 在具有CPU:0和GPU:0的设备的系统上,除非您明确要求在另一台设备上运行,否则将选择GPU:0设备运行tf.matmul。

记录操作分配给哪些设备

要找出操作和张量分配给哪些设备,请将tf.debugging.set_log_device_placement(True)作为程序的第一条语句。 启用设备放置日志记录将导致打印任何张量分配或操作。

tf.debugging.set_log_device_placement(True)
 
# Create some tensors
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
c = tf.matmul(a, b)
 
print(c)

运行结果:

Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
tf.Tensor(
[[22. 28.]
 [49. 64.]], shape=(2, 2), dtype=float32)

标签:笔记,device,Tensorflow,tf,GPU,CPU,运行,设备
来源: https://www.cnblogs.com/java-six/p/16669055.html