当我尝试运行以下非常简单的 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
,并且没有内核实现int32
标准 TensorFlow 分布中 GPU 上的变量。当你使用tf.Variable(tf.zeros([1]))
,您正在定义一个元素类型的变量tf.float32
, which isGPU 上支持。
的故事tf.int32
关于 TensorFlow 中的 GPU 的内容是一篇很长的文章。虽然支持在 GPU 上运行的整数运算在技术上很容易,但我们的经验是大多数整数运算实际上发生在 GPU 上metadata张量的元数据存在于 CPU 上,因此在 CPU 上对其进行操作会更高效。作为短期解决方法,需要进行多个内核注册int32
GPU 上的内容已被删除。但是,如果这些对您的模型有用,则可以将它们添加为自定义操作 https://www.tensorflow.org/versions/r0.10/how_tos/adding_an_op/index.html.
Source:在 TensorFlow 0.10 中,与变量相关的内核是挂号的 https://github.com/tensorflow/tensorflow/blob/854146110b5ec6b087fc6f2e18f35cc3b98e7186/tensorflow/core/kernels/variable_ops.cc#L55使用TF_CALL_GPU_NUMBER_TYPES() https://github.com/tensorflow/tensorflow/blob/854146110b5ec6b087fc6f2e18f35cc3b98e7186/tensorflow/core/framework/register_types.h#L162宏。当前的“GPU 编号类型”是tf.float16
, tf.float32
, and tf.float64
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)