【发布时间】:2017-01-27 02:46:48
【问题描述】:
我在 tensorflow 中运行 CNN。我正在使用 tf.device(/gpu:0) 将所有变量放入 gpu,但其中一些似乎仍在 cpu 中。当我运行我的代码时,我正在查看我的 gpu 实用程序,它上升到 100%,然后下降到 0%。
我知道如果我使用 config.log_device_placement = True 我可以看到哪个变量分配给了哪个设备。但是由于我的代码中变量的数量很多,我无法找出cpu中的变量。
那么,有什么方法可以让我看到哪些变量被固定到 cpu 上? 或者,您知道为什么我使用 tf.device 将某些变量分配给 gpu 时应该将它们固定到 cpu 吗?
顺便说一句,在我通过以下代码更改上采样器(简单插值器 tf.image.resize_images)后,这个问题发生了:
def unravel_argmax(argmax, shape):
with tf.device(gpu_n):
argmax_shape = argmax.get_shape()
new_1dim_shape = tf.shape(tf.constant(0, shape=[tf.Dimension(4), argmax_shape[0]*argmax_shape[1]*argmax_shape[2]*argmax_shape[3]]))
batch_shape = tf.constant(0, dtype=tf.int64, shape=[argmax_shape[0], 1, 1, 1]).get_shape()
b = tf.multiply(tf.ones_like(argmax), tf.reshape(tf.range(shape[0]), batch_shape))
y = argmax // (shape[2] * shape[3])
x = argmax % (shape[2] * shape[3]) // shape[3]
c = tf.ones_like(argmax) * tf.range(shape[3])
pack = tf.stack([b, y, x, c])
pack = tf.reshape(pack, new_1dim_shape)
pack = tf.transpose(pack)
return pack
def unpool_layer2x2_batch(updates, mask, ksize=[1, 2, 2, 1]):
with tf.device(gpu_n):
input_shape = updates.get_shape()
new_dim_y = input_shape[1] * ksize[1]
new_dim_x = input_shape[2] * ksize[2]
output_shape = tf.to_int64((tf.constant(0, dtype=tf.int64, shape=[input_shape[0], new_dim_y, new_dim_x, input_shape[3]]).get_shape()))
indices = unravel_argmax(mask, output_shape)
new_1dim_shape = tf.shape(tf.constant(0, shape=[input_shape[0] * input_shape[1] * input_shape[2] * input_shape[3]]))
values = tf.reshape(updates, new_1dim_shape)
ret = tf.scatter_nd(indices, values, output_shape)
return ret
我从here 获得此代码用于取消池化。
【问题讨论】:
-
您是否真的有任何证据表明 CPU 正在计算某些操作?
-
这是我的问题!如何确定我的某些变量是否固定在 cpu 中?目前,当我查看我的 gpu 实用程序时,似乎有些变量在 CPU 上,因为它上下波动(在 100% 和 0% 之间)。似乎一些变量在 cpu 和 gpu 之间传递。
-
一个比较常见的问题是计算中间的一些放在cpu上。您的 GPU 也可能正在等待数据被读取/输入到 TF 中。你可以通过查看时间线来解决这个问题
-
是的,我认为中间的一些变量放在 cpu 上,我想知道如何理解哪些变量放在 cpu 中?我确定这不是因为读取 TF 的输入。
-
使用时间线。所需的所有信息都很容易在谷歌上搜索到。
标签: python tensorflow