Tensorflow:在Multigpu培训中将变量固定到CPU无法正常工作

时间:2016-11-12 13:08:50

标签: tensorflow multi-gpu

我正在使用tensorflow训练我的第一个multi-gpu模型。 正如教程所述,变量固定在CPU上,并使用name_scope在每个GPU上进行操作。

当我正在运行一个小测试并记录设备放置时,我可以看到ops被放置在各自的GPU上,并带有TOWER_1 / TOWER_0前缀,但变量没有放在CPU上。

我错过了什么或者我是否错误地理解了设备放置日志。

附加测试代码,这里是device placement log

由于

测试代码

with tf.device('cpu:0'):  
    imgPath=tf.placeholder(tf.string)
    imageString=tf.read_file(imgPath)
    imageJpeg=tf.image.decode_jpeg(imageString, channels=3)
    inputImage=tf.image.resize_images(imageJpeg, [299,299])
    inputs  = tf.expand_dims(inputImage, 0)
    for i in range(2):
        with tf.device('/gpu:%d' % i):
            with tf.name_scope('%s_%d' % ('TOWER', i)) as scope:
                with slim.arg_scope([tf.contrib.framework.python.ops.variables.variable], device='/cpu:0'):
                    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
                        logits,endpoints = inception_v3.inception_v3(inputs, num_classes=1001, is_training=False)
                tf.get_variable_scope().reuse_variables()

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=True)) as sess:
    tf.initialize_all_variables().run()
exit(0)

修改 基本上'与slim.arg_scope([tf.contrib.framework.python.ops.variables.variable],device ='/ cpu:0')的行:'应该强制cpu上的所有变量,但它们是在'GPU:0'

1 个答案:

答案 0 :(得分:0)

尝试:

with slim.arg_scope([slim.model_variable, slim.variable], device='/cpu:0'):

这取自: model_deploy