繁体   English   中英

什么时候为tensorflow中的多GPU训练设置重用= True?

[英]When to set reuse=True for multi GPU training in tensorflow?

我正在尝试使用带有多个塔的张量流训练网络。 我为所有塔设置了reuse = True 但是在tensorflow教程cifar10 multi gpu系列中 ,在创建第一个塔之后设置了重用变量:

with tf.variable_scope(tf.get_variable_scope()):
  for i in xrange(FLAGS.num_gpus):
    with tf.device('/gpu:%d' % i):
      with tf.name_scope('%s_%d' % (cifar10.TOWER_NAME, i)) as scope:
        # Dequeues one batch for the GPU
        image_batch, label_batch = batch_queue.dequeue()
        # Calculate the loss for one tower of the CIFAR model. This function
        # constructs the entire CIFAR model but shares the variables across
        # all towers.
        # Actually the logits (whole network) is defined in tower_loss
        loss = tower_loss(scope, image_batch, label_batch)

        # Reuse variables for the next tower.
        tf.get_variable_scope().reuse_variables()

有什么区别吗? 如果我们事先设置了reuse = True,会发生什么?

在第一次运行时,您需要reuse=False来生成变量。 如果reuse = True,但尚未构造变量,则会出现错误。

如果您使用新版本的tensorflow(我认为> 1.4),则可以使用reuse=tf.AUTO_REUSE ,它将为您带来魔力。

我不确定它如何与您的多设备设置互动。 仔细检查变量名是否不被设备作为前缀。 在这种情况下,没有重用,每个设备都有一个不同的变量。

有两种共享变量的方法

任一版本1:

with tf.variable_scope("model"):
  output1 = my_image_filter(input1)
with tf.variable_scope("model", reuse=True):
  output2 = my_image_filter(input2)

或版本2:

with tf.variable_scope("model") as scope:
  output1 = my_image_filter(input1)
  scope.reuse_variables()
  output2 = my_image_filter(input2)

两种方法共享变量。 Cifar10教程中使用了第二种方法,因为它更清洁(这只是我的观点)。 您可以尝试使用版本1重建它,该代码可能可读性较低。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM