TF.变量无法固定到 GPU?



我的代码:

import tensorflow as tf
def main():
    with tf.device('/gpu:0'):
        a = tf.Variable(1)
        init_a = tf.global_variables_initializer()
        with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
           sess.run(init_a)
if __name__ == '__main__':
    main()

错误:

InvalidArgumentError(有关回溯,请参见上文(:无法分配设备 对于操作"变量":无法满足显式设备 规格 '/设备:GPU:0'因为没有支持 GPU 的内核 设备可用。

这是否意味着 tf 无法将Variable固定到 GPU?

这是与此主题相关的另一个线程。

int32类型(截至 2018 年 1 月(在 GPU 上并不全面支持。我相信完整的错误会说:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'Variable': Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Colocation Debug Info:
Colocation group had the following types and devices: 
Assign: CPU 
Identity: CPU 
VariableV2: CPU 
         [[Node: Variable = VariableV2[container="", dtype=DT_INT32, shape=[], shared_name="", _device="/device:GPU:0"]()]]
正是

那里的DT_INT32给您带来了麻烦,因为您明确要求将变量放置在 GPU 上,但没有用于相应操作和 dtype 的 GPU 内核。

如果这只是一个测试程序,而实际上您需要其他类型的变量,例如 float32,您应该没问题。例如:

import tensorflow as tf
with tf.device('/gpu:0'):
  # Providing 1. instead of 1 as the initial value will result
  # in a float32 variable. Alternatively, you could explicitly
  # provide the dtype argument to tf.Variable()
  a = tf.Variable(1.)
  init_a = tf.global_variables_initializer()
  with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    sess.run(init_a)

或者,您可以选择在 CPU 上显式放置 int32 变量,或者根本不指定任何设备,让 TensorFlow 的设备放置选择适当的 GPU。例如:

import tensorflow as tf
v_int = tf.Variable(1, name='intvar')
v_float = tf.Variable(1., name='floatvar')
init = tf.global_variables_initializer()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
  sess.run(init)

这将显示"intvar"放置在CPU上,而"floatvar"放置在GPU上使用一些日志行,例如:

floatvar: (VariableV2)/job:localhost/replica:0/task:0/device:GPU:0
intvar: (VariableV2)/job:localhost/replica:0/task:0/device:CPU:0

希望有帮助。

这意味着 Tensorflow 找不到您指定的设备。

我假设您想指定您的代码在 GPU 0 上执行。

正确的语法是:

with tf.device('/device:GPU:0'):

您正在使用的简写形式只允许用于 CPU。

你也可以在这里查看这个答案:如何在张量流中获取当前可用的 GPU?
它演示如何列出 TF 识别的 GPU 设备。

这列出了语法:https://www.tensorflow.org/tutorials/using_gpu

最新更新