I'm using the Dataset APIs in TF 2.4 . Currently I have a working code piece like
def map_func(a:int, b:int) -> typing.Tuple[typing.List[float],typing.List[int]]:
# some complex logics here, for example, protobuf message deserialization
return [0.0],[0] if some_condition() else [1.0],[1]
some_dataset \
.map(lambda a, b: tf.numpy_function(map_func, inp=[a,b], Tout=(tf.float32, tf.int32))) \
.filter(lambda features, labels: any(labels)) \ # filter out results whose labels are all zeros, regardless whatever features are
.some_other_apis()
The map_func function defined above return a tuple of (features, labels) , where labels might contain zeros or non-zeros. By chaining a filter call, I filter out samples whose labels are all 0s.
What's the problem
I'm wondering if it is possible to "integrate" the filter logic inside the map_func, because the current implementation looks somehow ugly and redundant. I tried to return a tuple of ([],[]) or (None, None) when I want to abandon the results, but TF would complain return types mismatching.