【问题标题】:Tensorflow 2 - OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph is disabled in this functionTensorflow 2 - OperatorNotAllowedInGraphError:不允许使用 `tf.Tensor` 作为 Python `bool`:此函数中禁用了 AutoGraph
【发布时间】:2020-07-31 04:10:52
【问题描述】:

我正在为 Tensorflow 2 中的数据集编写映射函数。 数据集包含几张图像和相应的标签,更具体地说,标签只有三个可能的值,13、17 和 34。 映射函数应该获取标签并将它们转换为分类标签。

可能有更好的方法来实现这个功能(请随意提出建议),但这是我的实现:

def map_labels(dataset):

    def convert_labels_to_categorical(image, labels):
        labels = [1.0, 0., 0.] if labels == 13 else [0., 1.0, 0.] if labels == 17 else [0., 0., 1.0]

        return image, labels

        categorical_dataset = dataset.map(convert_labels_to_categorical)

    return categorical_dataset

主要问题是我收到以下错误:

OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph is disabled in this function. Try decorating it directly with @tf.function.

我真的不知道这个错误是什么意思,而且互联网上没有那么多其他来源记录同样的错误。有什么想法吗?

编辑(新的非工作实现):

def map_labels(dataset):

    def convert_labels_to_categorical(image, labels):
        labels = tf.Variable([1.0, 0., 0.]) if tf.reduce_any(tf.math.equal(labels, tf.constant(0,dtype=tf.int64))) \
        else tf.Variable([0., 1.0, 0.]) if tf.reduce_any(tf.math.equal(labels, tf.constant(90,dtype=tf.int64))) \
        else tf.Variable([0., 0., 1.0])

        return image, labels

    categorical_dataset = dataset.map(convert_labels_to_categorical)

    return categorical_dataset

【问题讨论】:

    标签: tensorflow2.0 tensorflow-datasets tensorflow2.x


    【解决方案1】:

    您的问题来自您将 Python 代码与 TensorFlow 代码混合在一起的事实。

    实际上,在您的 map 函数中,您可以使用任意 Python 代码,而不是专门优化的 TensorFlow 代码

    ma​​p 函数中,您只能使用属于 tf* 类别的函数。如果您仍想使用任意 Python 代码,则需要使用 tf.py_function() 库。

    您可能需要查阅此主题以获得更好的概览:

    Is there an alternative to tf.py_function() for custom Python code?

    要解决您的问题,您需要专门使用 tf 模块中的函数,例如 tf.stringstf.bool 等。

    【讨论】:

    • 谢谢,我稍微修改了函数以考虑您的建议。据我了解,我摆脱了任何 Python 函数,但仍然缺少一些东西,因为我仍然得到同样的错误。我绝对想使用纯 TensorFlow 2 代码,而不是 Python。
    • 另外,我不明白为什么错误提到“不允许使用 tf.Tensor 作为 Python bool”。那个 Python 布尔值在哪里?
    • 您是否尝试过使用诸如:tf.math.equal() 之类的函数?
    • tf.math.equal(labels,tf.constant[1,0,0])
    • 谢谢,我忘了。我插入了 tf.math.equal() 并添加了 tf.reduce_any(),同样的错误信息。还是有些不对劲。
    【解决方案2】:

    我找到了一个可行的解决方案。首先,我创建了一个字典,然后是数据集:

    dictionary = {"data":data, "labels":labels.astype('int32')}
    dataset = tf.data.Dataset.from_tensor_slices(dict(dictionary))
    

    这使我可以轻松访问数据集中的数据和标签。可能还有其他不需要使用字典的方法,但这对我有用。对于我使用的映射:

    def map_labels(dataset):
    
        def convert_labels_to_categorical(dataset):
            if dataset['labels'] ==  tf.constant(13):
                dataset['labels'] = tf.constant([1, 0, 0]) 
    
            elif dataset['labels'] == tf.constant(17):
                dataset['labels'] =  tf.constant([0, 1, 0])
    
            elif dataset['labels'] == tf.constant(34):
                dataset['labels'] = tf.constant([0, 0, 1])
    
            return dataset
    
        categorical_dataset = dataset.map(convert_labels_to_categorical)
    
        return categorical_dataset
    

    映射数据集后,如果我使用categorical_dataset.element_spec 检查它,我会得到:

    {'data': TensorSpec(shape=(32, 32, 3), dtype=tf.uint8, name=None), 'labels': TensorSpec(shape=(None,), dtype=tf.int32, name=None)}
    

    如果我打印元素,新的分类标签会正确分配给相应的图像。 总之,=== 仍然适用于 tf 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2018-07-12
      • 2018-03-20
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      相关资源
      最近更新 更多