python - 反正有在 GPU 上运行 TensorFlow 代码吗?
问题描述
这是我的代码,我想将 GPU 用于我的代码。目前它在 CPU 上运行。
elf.graph = tf.Graph()
with self.graph.as_default():
self.face_graph = tf.GraphDef()
fid=tf.gfile.GFile(self.facenet_model, 'rb')
serialized_graph = fid.read()
self.face_graph.ParseFromString(serialized_graph)
tf.import_graph_def(self.face_graph, name='')
self.facenet_sess = tf.Session(graph=self.graph)
self.images_placeholder = self.graph.get_tensor_by_name("input:0")
self.embeddings = self.graph.get_tensor_by_name("embeddings:0")
解决方案
tensorflow 的网站上有相当完整的指南:https ://www.tensorflow.org/guide/gpu
确认 tf 可以看到你的 gpu:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
查看您的 GPU 名称:
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
做一些手动放置:
tf.debugging.set_log_device_placement(True)
gpus = tf.config.experimental.list_logical_devices('GPU')
if gpus:
# Replicate your computation on multiple GPUs
c = []
for gpu in gpus:
with tf.device(gpu.name):
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.append(tf.matmul(a, b))
with tf.device('/CPU:0'):
matmul_sum = tf.add_n(c)
print(matmul_sum)
推荐阅读
- ocaml - 如何在 ocaml 中创建查找功能?
- django - Fixtures - 使用多对多的中间模型
- javascript - 如何通过 zapier webhook 通过 twilio 发送 whatsapp 消息
- javascript - 检查是否未按下组合键
- android - 如何在 viewpager 中使用 recyclerview smothScrollToPosition
- php - MySQL Select Query 17000 行慢
- java - PDFBox IllegalArgumentException:字体 Webdings 中没有字形
- qt - AttributeRead 从未由 QOpcUaNode 发送,无法读取节点但能够写入
- python - TypeError:列表索引必须是整数或切片,而不是 str(在 VS Code 中可以正常工作)
- alias - 用于 UVM 报告的使用定义的详细级别(别名)(使用 uvm_info)