TensorFlow:一个网络,两个GPU?

2024-05-14 15:05:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个具有两个不同输出流的卷积神经网络:

                         input
                           |
                         (...) <-- several convolutional layers
                           |
                       _________
    (several layers)   |       |    (several layers)
    fully-connected    |       |    fully-connected
    output stream 1 -> |       | <- output stream 2

我想计算/gpu:0上的流1和/gpu:1上的流2。不幸的是,我不能正确地设置它。在

这次尝试:

^{pr2}$

运行速度非常慢(比单独在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?我是否总是要在不同的塔台上分割完整的网络?在


Tags: 网络inputstreamgpudevicelayerstfwith
1条回答
网友
1楼 · 发布于 2024-05-14 15:05:58

有一个例子说明如何在一个网络上使用多个gpu 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

谨致问候

相关问题 更多 >

    热门问题