I am trying to apply a filter to a tf.data.Dataset
which removes any strings where one group > 50% of the string. Here is my Dataset
:
import tensorflow as tf
strings = [
["ABCDEFGABCDEFG\tUseless\tLabel1"],
["AAAAAAAADEFGAB\tUseless\tLabel2"],
["HIJKLMNHIJKLMN\tUseless\tLabel3"],
["HIJKLMMMMMMMNH\tUseless\tLabel4"],
]
ds = tf.data.Dataset.from_tensor_slices(strings)
def _clean(x):
x = tf.strings.split(x, "\t")
return x[0], x[2]
def _filter(x):
s = tf.strings.bytes_split(x)
_, _, count = tf.unique_with_counts(s)
percent = tf.reduce_max(count) / tf.shape(s)[0]
return tf.less_equal(percent, 0.5)
ds = ds.map(_clean)
ds = ds.filter(lambda x, y: _filter(x))
for x, y in ds:
tf.print(x, y)
This creates the following error:
TypeError: Failed to convert elements of tf.RaggedTensor(values=Tensor("StringsByteSplit/StringSplit:1", shape=(None,), dtype=string), row_splits=Tensor("StringsByteSplit/RaggedFromValueRowIds/RowPartitionFromValueRowIds/concat:0", shape=(None,), dtype=int64)) to Tensor. Consider casting elements to a supported type.
Any way to solve this problem in a tf.data.Dataset
graph?