何时为张量流中的多 GPU 训练设置 reuse=True?
When to set reuse=True for multi GPU training in tensorflow?
我正在尝试使用具有多个塔的张量流来训练网络。我为所有塔设置了 reuse = True
。但在 cifar10 multi gpu train of tensorflow tutorials 中,重用变量已在创建第一个塔后设置:
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
,它会为您带来神奇效果。
我不确定这如何与您的多设备设置交互。仔细检查变量名称是否没有成为设备的前缀。在那种情况下没有重用,每个设备都有不同的变量。
有两种方式share variables。
任一版本 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 tutorial 中,因为它更干净(这只是我的意见)。您可以尝试使用版本 1 重新构建它,代码的可读性可能会降低。
我正在尝试使用具有多个塔的张量流来训练网络。我为所有塔设置了 reuse = True
。但在 cifar10 multi gpu train of tensorflow tutorials 中,重用变量已在创建第一个塔后设置:
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
,它会为您带来神奇效果。
我不确定这如何与您的多设备设置交互。仔细检查变量名称是否没有成为设备的前缀。在那种情况下没有重用,每个设备都有不同的变量。
有两种方式share variables。
任一版本 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 tutorial 中,因为它更干净(这只是我的意见)。您可以尝试使用版本 1 重新构建它,代码的可读性可能会降低。