【发布时间】:2019-06-23 22:38:27
【问题描述】:
我有一个用 PyTorch 编写的神经网络,它在 GPU 上输出一些张量 a。我想继续使用高效的 TensorFlow 层处理a。
据我所知,这样做的唯一方法是将a 从 GPU 内存移动到 CPU 内存,转换为 numpy,然后将其输入 TensorFlow。一个简化的例子:
import torch
import tensorflow as tf
# output of some neural network written in PyTorch
a = torch.ones((10, 10), dtype=torch.float32).cuda()
# move to CPU / pinned memory
c = a.to('cpu', non_blocking=True)
# setup TensorFlow stuff (only needs to happen once)
sess = tf.Session()
c_ph = tf.placeholder(tf.float32, shape=c.shape)
c_mean = tf.reduce_mean(c_ph)
# run TensorFlow
print(sess.run(c_mean, feed_dict={c_ph: c.numpy()}))
这可能有点牵强,但有办法做到这一点
-
a永远不会离开 GPU 内存,或者 -
a从 GPU 内存到固定内存再到 GPU 内存。
我尝试 2. 在上面使用 non_blocking=True 剪切的代码中,但我不确定它是否符合我的预期(即将其移动到固定内存)。
理想情况下,我的 TensorFlow 图会直接在 PyTorch 张量占用的内存上运行,但我认为这是不可能的?
【问题讨论】:
标签: tensorflow pytorch gpu