为什么设置初始化值会阻止在 TensorFlow 中将变量放置在 GPU 上?
Why does setting an initialization value prevent placing a variable on a GPU in TensorFlow?
当我尝试 运行 以下非常简单的 TensorFlow 代码时出现异常,尽管我实际上是从文档中复制它的:
import tensorflow as tf
with tf.device("/gpu:0"):
x = tf.Variable(0, name="x")
sess = tf.Session()
sess.run(x.initializer) # Bombs!
例外情况是:
tensorflow.python.framework.errors.InvalidArgumentError: Cannot assign a device to
node 'x': Could not satisfy explicit device specification '/device:GPU:0' because
no supported kernel for GPU devices is available.
如果我将变量的初始值改为 tf.zeros([1])
,一切正常:
import tensorflow as tf
with tf.device("/gpu:0"):
x = tf.Variable(tf.zeros([1]), name="x")
sess = tf.Session()
sess.run(x.initializer) # Works fine
知道发生了什么事吗?
出现这个错误是因为tf.Variable(0, ...)
定义了元素类型tf.int32
的变量,而标准TensorFlow分布中没有在GPU上实现int32
变量的内核。当您使用 tf.Variable(tf.zeros([1]))
时,您定义的是元素类型 tf.float32
的变量,GPU 支持 。
TensorFlow 中 GPU 上 tf.int32
的故事很长。虽然在 GPU 上支持整数运算 运行 在技术上很容易,但我们的经验是,大多数整数运算实际上发生在张量的 元数据 上,并且该元数据存在于CPU,所以在那里操作效率更高。作为短期解决方法,删除了 GPU 上 int32
的几个内核注册。但是,如果这些对您的模型有用,则可以 add them as custom ops.
来源: 在TensorFlow 0.10中,变量相关的内核是registered using the TF_CALL_GPU_NUMBER_TYPES()
宏。当前"GPU number types"是tf.float16
、tf.float32
和tf.float64
。
当我尝试 运行 以下非常简单的 TensorFlow 代码时出现异常,尽管我实际上是从文档中复制它的:
import tensorflow as tf
with tf.device("/gpu:0"):
x = tf.Variable(0, name="x")
sess = tf.Session()
sess.run(x.initializer) # Bombs!
例外情况是:
tensorflow.python.framework.errors.InvalidArgumentError: Cannot assign a device to
node 'x': Could not satisfy explicit device specification '/device:GPU:0' because
no supported kernel for GPU devices is available.
如果我将变量的初始值改为 tf.zeros([1])
,一切正常:
import tensorflow as tf
with tf.device("/gpu:0"):
x = tf.Variable(tf.zeros([1]), name="x")
sess = tf.Session()
sess.run(x.initializer) # Works fine
知道发生了什么事吗?
出现这个错误是因为tf.Variable(0, ...)
定义了元素类型tf.int32
的变量,而标准TensorFlow分布中没有在GPU上实现int32
变量的内核。当您使用 tf.Variable(tf.zeros([1]))
时,您定义的是元素类型 tf.float32
的变量,GPU 支持 。
TensorFlow 中 GPU 上 tf.int32
的故事很长。虽然在 GPU 上支持整数运算 运行 在技术上很容易,但我们的经验是,大多数整数运算实际上发生在张量的 元数据 上,并且该元数据存在于CPU,所以在那里操作效率更高。作为短期解决方法,删除了 GPU 上 int32
的几个内核注册。但是,如果这些对您的模型有用,则可以 add them as custom ops.
来源: 在TensorFlow 0.10中,变量相关的内核是registered using the TF_CALL_GPU_NUMBER_TYPES()
宏。当前"GPU number types"是tf.float16
、tf.float32
和tf.float64
。