I'm developing an input pipeline with TensorFlow Datasets, the dataset has just two columns, I want to filter based in a list of values, but I just can filter the dataset using the operator equal "==", when I tried to use the membership operator "in" I received the following error.
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.
Below my code:
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'
)
This filter works:
@tf.function
def filter_fn(features):
return features['department'] == 'MOVEIS'
ds = raw_data.unbatch()
ds = ds.filter(filter_fn)
ds = ds.batch(2)
Output:
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)>)])
This filter doesn't work:
@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)
Error:
---------------------------------------------------------------------------
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.
Here the link to Colab where it possible to run and check the error: