警告:tensorflow:AutoGraph 无法转换 <function format_example at ...> 并将按原样运行

问题描述 投票:0回答:3

我的问题与这里的thisthis有关。我在 Windows 和 Python 3.7.8 和 Tensorflow 2.2.0 上使用 PyCharm:

print (sys.version)
3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)]

print(tf.__version__)
2.2.0

当我从这个 colab 教程运行此代码时:

import os

import numpy as np

import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds

(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)
IMG_SIZE = 160 # All images will be resized to 160x160

def format_example(image, label):
  image = tf.cast(image, tf.float32)
  image = (image/127.5) - 1
  image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
  return image, label

然后尝试运行此行:

train = raw_train.map(format_example)

我收到警告:

WARNING:tensorflow:AutoGraph could not transform <function format_example at 0x00000265A2DB4E58> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Unable to locate the source code of <function format_example at 0x00000265A2DB4E58>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert

我仅在本地电脑上使用 PyCharm 时收到此警告。当我在 colab 中运行它时没有问题。这里有什么问题,这是否相关,或者我可以忽略警告吗?

python tensorflow
3个回答
2
投票

像 OP 一样,我在 JupyterLab 中收到此警告,但在 Colab 中则没有。但这只有在我使用 lambda 时才会发生。

train = raw_train.map(lambda x: x)

如果我使用像OP在这里所做的那样的命名函数,警告就会消失。

train = raw_train.map(format_example)
即使您处于 eager 模式,

DataSet.map()
也始终会构建图表。我认为图形生成器遇到了某种混乱。


0
投票

按照警告消息中的建议,您可以将

format_example
函数装饰为

train = raw_train.map(tf.autograph.experimental.do_not_convert(format_example))

或者

train = raw_train.map(tf.autograph.experimental.do_not_convert(lambda x: x*2))

0
投票

如果有帮助的话,当我的单元格以

%%time
开头时,我也遇到了同样的问题。我删除了它,警告不再显示。

© www.soinside.com 2019 - 2024. All rights reserved.