首页 > 解决方案 > TFX:在评估器中使用转换后的数据

问题描述

TLDR;

我面临着评估者模型的问题。所有使用 Evaluator 组件的示例都使用原始 ExampleGen 数据中的标签作为标签源。但我想给它在管道期间计算的标签。

有没有一种方法可以在将标签提供给评估者之前对标签进行一次热编码?另一种方法是在转换组件中对数据进行一次热编码,然后使用 ImportExampleGen 组件再次加载它,但这对于时间和内存来说非常昂贵。


长版:

我正在运行一个语言建模管道,我有一个文本作为输入,我想训练一个基于 LSTM 的 LM。到目前为止,我的步骤是:

output = example_gen_pb2.Output(
    split_config=example_gen_pb2.SplitConfig(
        splits=[
            example_gen_pb2.SplitConfig.Split(name="train", hash_buckets=45),
            example_gen_pb2.SplitConfig.Split(name="eval", hash_buckets=5),
        ]
    )
)


# Load the data from our prepared TFDS folder
example_gen = ImportExampleGen(input_base=str(data_root), output_config=output)

context.run(example_gen)

这是改造后的样子:

{'label_sentence': array([17843,  1863, 30003,    32,     4, 30003, 30003, 30003, 30003,
       30003, 12551, 30003, 22696, 30003, 30003, 30003, 30003, 30003,
       30003,   210, 29697, 30003,  3813,  2262, 30003,   313,   370,
         667, 27087,   186,   182, 30003,   370, 10500,   186,   182,
       30003,   370,  8366,   186,   182, 30003,  9949,  1789, 30003,
       30003,   158,  1863, 30003,     8,  5169,     3,    67,  4229,
           3,   239,  3843, 30003,     5,   682,  1887, 28241, 30003,
       16798, 30003,   116,     4,   207,  1320,  1529, 30003,     2,]),

 'training_sentence': array([    1, 17843,  1863, 30003,    32,     4, 30003, 30003, 30003,
       30003, 30003, 12551, 30003, 22696, 30003, 30003, 30003, 30003,
       30003, 30003,   210, 29697, 30003,  3813,  2262, 30003,   313,
         370,   667, 27087,   186,   182, 30003,   370, 10500,   186,
         182, 30003,   370,  8366,   186,   182, 30003,  9949,  1789,
       30003, 30003,   158,  1863, 30003,     8,  5169,     3,    67,
        4229,     3,   239,  3843, 30003,     5,   682,  1887, 28241,
       30003, 16798, 30003,   116,     4,   207,  1320,  1529, 30003])}

这是训练代码的那一部分:

    train_dataset = train_dataset.map(lambda x, y: (x, tf.one_hot(y, depth=NUM_CLASSES)))
    eval_dataset = eval_dataset.map(lambda x, y: (x, tf.one_hot(y, depth=NUM_CLASSES)))

    
    mirrored_strategy = tf.distribute.MirroredStrategy()
    with mirrored_strategy.scope():
        model = get_model()

    tensorboard_callback = keras.callbacks.TensorBoard(
        log_dir=fn_args.model_run_dir, update_freq="batch"
    )

    model.fit(
        train_dataset,
        steps_per_epoch=fn_args.train_steps,
        validation_data=eval_dataset,
        validation_steps=fn_args.eval_steps,
        callbacks=[tensorboard_callback],
    )

有没有一种方法可以在将标签提供给评估者之前对标签进行一次热编码?另一种方法是在转换组件中对数据进行一次热编码,然后使用 ImportExampleGen 组件再次加载它,但这对于时间和内存来说非常昂贵。

标签: tfx

解决方案


推荐阅读