【发布时间】:2020-06-03 20:45:06
【问题描述】:
我正在使用 TensorFlow 数据集开发输入管道,数据集只有两列,我想根据值列表进行过滤,但我只能使用运算符等于“==”过滤数据集,当我尝试使用会员运算符“in”我收到以下错误。
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
在我的代码下面:
import numpy as np
import tensorflow as tf
# Load file
file_path = 'drive/My Drive/Datasets/category_catalog.csv.gz'
def get_dataset(file_path, batch_size=5, num_epochs=1, **kwargs):
return tf.data.experimental.make_csv_dataset(
file_path,
batch_size=batch_size,
na_value="?",
num_epochs=num_epochs,
ignore_errors=True,
**kwargs
)
raw_data = get_dataset(
file_path,
select_columns=['description', 'department'],
compression_type='GZIP'
)
此过滤器有效:
@tf.function
def filter_fn(features):
return features['department'] == 'MOVEIS'
ds = raw_data.unbatch()
ds = ds.filter(filter_fn)
ds = ds.batch(2)
输出:
next(iter(ds))
OrderedDict([('description', <tf.Tensor: shape=(2,), dtype=string, numpy=
array([b'KIT DE COZINHA KITS PARANA 8 PORTAS GOLDEN EM MDP LINHO BRANCO E LINHO PRETO',
b'ARMARIO AEREO PARA COZINHA 1 PORTA HORIZONTAL EXCLUSIVE ITATIAIA PRETO MATTE'],
dtype=object)>),
('department',
<tf.Tensor: shape=(2,), dtype=string, numpy=array([b'MOVEIS', b'MOVEIS'], dtype=object)>)])
此过滤器不起作用:
@tf.function
def filter_fn(features):
return features['department'] in ['FERRAMENTAS', 'MERCEARIA', 'MOVEIS']
ds = raw_data.unbatch()
ds = ds.filter(filter_fn)
ds = ds.batch(2)
错误:
---------------------------------------------------------------------------
OperatorNotAllowedInGraphError Traceback (most recent call last)
<ipython-input-52-52131b5369b6> in <module>()
6
7 ds = raw_data.unbatch()
----> 8 ds = ds.filter(filter_fn)
9 ds = ds.batch(2)
18 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
OperatorNotAllowedInGraphError: in user code:
<ipython-input-52-52131b5369b6>:5 filter_fn *
return features['department'] in ['FERRAMENTAS', 'MERCEARIA', 'MOVEIS']
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:778 __bool__
self._disallow_bool_casting()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:545 _disallow_bool_casting
"using a `tf.Tensor` as a Python `bool`")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:532 _disallow_when_autograph_enabled
" decorating it directly with @tf.function.".format(task))
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.
这里是 link 到 Colab,可以运行并检查错误:
【问题讨论】:
-
嗨!你能给我们提供一个最小的可重现的例子吗?这里的问题是我们无权访问您的数据,因此我们很难重现错误。如果您可以与所有这些共享一个 google colab,那就更好了。
-
嗨@ZaccharieRamzi 我已经添加了带有代码的colab,感谢您的帮助
标签: python tensorflow tensorflow-datasets