我们经常在许多TensorFlow教程中看到这种情况:
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True))allow_soft_placement和log_device_placement是什么意思?
发布于 2017-07-02 17:10:45
如果您查看ConfigProto API,在第278行,您将看到以下内容:
// Whether soft placement is allowed. If allow_soft_placement is true,
// an op will be placed on CPU if
// 1. there's no GPU implementation for the OP
// or
// 2. no GPU devices are known or registered
// or
// 3. need to co-locate with reftype input(s) which are from CPU.
bool allow_soft_placement = 7;这实际上意味着,如果您在没有allow_soft_placement=True的情况下这样做,TensorFlow会抛出一个错误。
with tf.device('/gpu:0'):
# some op that doesn't have a GPU implementation就在它下面,你会看到281线:
// Whether device placements should be logged.
bool log_device_placement = 8;当log_device_placement=True时,您将得到如下内容的详细输出:
2017-07-03 01:13:59.466748: I tensorflow/core/common_runtime/simple_placer.cc:841] Placeholder_1: (Placeholder)/job:localhost/replica:0/task:0/cpu:0
Placeholder: (Placeholder): /job:localhost/replica:0/task:0/cpu:0
2017-07-03 01:13:59.466765: I tensorflow/core/common_runtime/simple_placer.cc:841] Placeholder: (Placeholder)/job:localhost/replica:0/task:0/cpu:0
Variable/initial_value: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-07-03 01:13:59.466783: I tensorflow/core/common_runtime/simple_placer.cc:841] Variable/initial_value: (Const)/job:localhost/replica:0/task:0/cpu:0您可以看到每个操作映射到哪里。在本例中,它们都映射到/cpu:0,但是如果您处于分布式设置中,则会有更多的设备。
发布于 2017-07-02 21:19:28
除了tensorflow/core/protobuf/config.proto (安放,安放)中的注释外,TF的使用GPU教程中也解释了这一点。
要找出您的操作和张量分配给哪些设备,请创建将
log_device_placement配置选项设置为True的会话。
这对调试很有帮助。对于图中的每个节点,您将看到分配给它的设备。
如果希望TensorFlow自动选择现有和受支持的设备来运行指定的操作,则可以在创建会话时在configuration选项中将
allow_soft_placement设置为True。
如果您不小心手动指定了错误的设备或不支持特定op的设备,这将对您有所帮助。如果您编写的代码可以在您不知道的环境中执行,这是非常有用的。您仍然可以提供有用的缺省值,但在失败的情况下,则是一个优雅的退步。
发布于 2018-11-24 13:30:07
allow_soft_placement
此选项允许弹性设备分配,但只有当您的tensorflow未编译GPU时才能工作。如果您的tensorflow支持GPU,则无论是否设置allow_soft_placement,即使您将设备设置为CPU,这些操作始终在GPU上执行。但是,如果您将其设置为false,而设备设置为GPU,但在您的计算机中找不到GPU,则会引发错误。
log_device_placement
此配置告诉您在构建图形时分配了哪个设备的操作。它总能在你的机器上找到性能最好的优先设备。它似乎只是忽略你的设置。
https://stackoverflow.com/questions/44873273
复制相似问题