首页 > 解决方案 > XLA 编译错误:操作没有名为“_XlaCompile”的属性

问题描述

我正在运行以下代码。我收到如下所述的 XLA 编译器错误。我有 Tensorflow GPU 1.8 版本,但我确实尝试了 CPU 版本,但我收到了错误。但是当我将 Tensorflow (CPU) 降级到 1.3 时,我没有收到任何错误。Tensorflow 已使用带有 Pip 的虚拟环境安装。

请有人可以帮助我。

代码:

import tensorflow as tf
input_tensor = tf.placeholder(dtype=tf.float32, shape=[None, 16, 16, 3])
print(input_tensor.shape)

conv_filter = tf.get_variable('conv_filter', shape=[2, 2, 3, 6], dtype=tf.float32)
conv1 = tf.nn.conv2d(input_tensor, conv_filter, strides=[1, 2, 2, 1], padding='SAME')
print(conv1.shape)

deconv_filter = tf.get_variable('deconv_filter', shape=[2, 2, 6, 3], dtype=tf.float32)

deconv = tf.nn.conv2d_transpose(input_tensor, filter=deconv_filter,
    output_shape=tf.shape(input_tensor),
    strides=[1, 2, 2, 1],
    padding='SAME')
print(deconv.shape)

t = tf.reduce_mean(deconv)
g = tf.train.AdamOptimizer(0.01).minimize(t)

错误日志:

(?, 16, 16, 3)
(?, 8, 8, 6)
(?, 16, 16, 3)     # <<<<< This should have printed (?, ?, ?, ?)

Traceback (most recent call last):
  File "/my/path/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2327, in get_attr
    c_api.TF_OperationGetAttrValueProto(self._c_op, name, buf)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Operation 'conv2d_transpose' has no attr named '_XlaCompile'.

Traceback (most recent call last):
  File "/my/path/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py", line 380, in _MaybeCompile
    xla_compile = op.get_attr("_XlaCompile")
  File "/my/path/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2331, in get_attr
    raise ValueError(str(e))
ValueError: Operation 'conv2d_transpose' has no attr named '_XlaCompile'.

错误即将到来g = tf.train.AdamOptimizer(0.01).minimize(t)

标签: tensorflowtensorflow-xla

解决方案


问题的发生是因为过滤器的形状不匹配,以及与之相关的步幅不匹配,以产生具有 V​​ALID 填充的所需输出形状。我应该将反卷积滤波器形状设置为 [1, 1, 3, 3] 并在 [1, 1, 1, 1] 处设置步幅。

所需代码:

deconv_filter = tf.get_variable('deconv_filter', shape=[1, 1, 6, 3], dtype=tf.float32)

deconv = tf.nn.conv2d_transpose(input_tensor, filter=deconv_filter,
    output_shape=tf.shape(input_tensor),
    strides=[1, 1, 1, 1],
    padding='VALID')

推荐阅读