TensorFlow:一个网络,两个GPU?
问题描述:
我有两个不同的输出流的卷积神经网络:TensorFlow:一个网络,两个GPU?
input
|
(...) <-- several convolutional layers
|
_________
(several layers) | | (several layers)
fully-connected | | fully-connected
output stream 1 -> | | <- output stream 2
我想计算上/gpu:1
上/gpu:0
流1和流2。不幸的是我无法正确设置它。
此尝试:
...placeholders...
...conv layers...
with tf.device("/gpu:0"):
...stream 1 layers...
nn_out_1 = tf.matmul(...)
with tf.device("/gpu:1"):
...stream 2 layers...
nn_out_2 = tf.matmul(...)
运行死慢(比仅基于1个GPU训练慢),有时会产生在输出NaN值。我想这可能是因为with
语句可能无法正确同步。所以我加了control_dependencies
并置于CONV层上/gpu:0
明确:
...placeholders... # x -> input, y -> labels
with tf.device("/gpu:0"):
with tf.control_dependencies([x, y]):
...conv layers...
h_conv_flat = tf.reshape(h_conv_last, ...)
with tf.device("/gpu:0"):
with tf.control_dependencies([h_conv_flat]):
...stream 1 layers...
nn_out_1 = tf.matmul(...)
with tf.device("/gpu:1"):
with tf.control_dependencies([h_conv_flat]):
...stream 2 layers...
nn_out_2 = tf.matmul(...)
...但这种方法的网络甚至没有运行。不管是什么我已经试过了,抱怨没有被初始化的输入:
tensorflow.python.framework.errors.InvalidArgumentError:
You must feed a value for placeholder tensor 'x'
with dtype float
[[Node: x = Placeholder[dtype=DT_FLOAT, shape=[],
_device="/job:localhost/replica:0/task:0/cpu:0"]()]]
没有with
报表网络训练只/gpu:0
和运行良好 - 火车合理的东西,没有任何错误。
我在做什么错? TensorFlow是否无法将一个网络中的不同层流分成不同的GPU?我是否总是必须拆分完成网络在不同塔?
答
有一个如何在一个网络上使用许多gpus的例子 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/cifar10/cifar10_multi_gpu_train.py 可能你可以复制代码。 也可以得到这样的
# Creates a graph.
c = []
for d in ['/gpu:2', '/gpu:3']:
with tf.device(d):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
sum = tf.add_n(c)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(sum)
综观:https://www.tensorflow.org/versions/r0.7/how_tos/using_gpu/index.html#using-multiple-gpus
问候
它可以依靠从许多不同的因素。是相同的gpus?你的数据有多大? – fabrizioM
是的,这两个GPU是相同的,它们在一张卡上。这是一张来自NVIDIA [双核] K80 Tesla卡[http://www.nvidia.com/object/tesla-k80.html]。它具有24 GB VRAM,并且数据完全适合一个GPU(12GB)的VRAM。 – daniel451
您确定该计算的瓶颈是GPU速度吗? GPU带宽*的瓶颈是非常常见的,而不是实际的计算;如果你发送一个很大的张量到另一个GPU上,那么在那种情况下它只会让事情变得更糟。 – Peteris