首页 > 解决方案 > 在 TensorFlow 数据集上应用预处理需要很长时间

问题描述

我正在尝试应用预处理器从 Tensorflow 数据集的文件中删除 html 标签。我在执行t5which uses时使用它segio

my_preprocessor如果我使用,它可以正常工作,text = row[field_index]并且在训练中,它会打印几行并继续,但是如果我使用text = normalize_text(row[field_index])它,它会挂在无限或长循环上,就好像它想将它应用于整个数据集行一样!

def normalize_text(text):
    """Lowercase and remove quotes and tags from a TensorFlow string."""
    text = tf.strings.lower(text)
    text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
    text = tf.strings.regex_replace(text,"<[^>]+>", " ")
    return text

def make_add_field_names_preprocessor(
    field_names: Sequence[str], field_indices: Optional[Sequence[int]] = None,
) -> Callable:

   def my_preprocessor(ds):
        def to_inputs_and_targets(*row):
            ret = {}
            count=0
            tf.print("=======================")
            for field_name, field_index in zip(field_names, field_indices):
                # if I use this, it works okay
                text = row[field_index]
                # if I use the following it falls to a long or endless loop
                text = normalize_text(row[field_index])
                tf.print(count, "Row:",text)
                ret[field_name] = text
                count+=1
            return ret
        return ds.map(to_inputs_and_targets,
                    num_parallel_calls=tf.data.experimental.AUTOTUNE)
    return my_preprocessor

更新:预处理器获取两个字段名称,行 [1] 的“输入”和行 [2] 的“目标”。normalize_text当我打电话时,我注意到问题存在很多row[2]

标签: pythontensorflow

解决方案


推荐阅读