【问题标题】:How to add in a new data to a TfRecord by performing a join如何通过执行连接将新数据添加到 TfRecord
【发布时间】:2020-07-14 17:20:32
【问题描述】:

我有一堆 tensorflow 记录(这些不是我创建的)。但是,我需要通过加入另一个数据源(一个大查询表)来添加更多功能。如何编辑 tf 记录,将其转换为 TfDataset 还是需要以某种方式将其转换为 pandas 数据帧并加入数据并作为 tfrecord 写出?谁有例子?

【问题讨论】:

    标签: tensorflow apache-beam tfrecord


    【解决方案1】:

    假设您在 TfRecord 中有 TFExample,那么一种方法是使用 beam.io.tfrecordio.ReadFromTFRecord 和 tf 解析函数:

    ...

       pipeline
            | "ReadMetadata" >> beam.io.tfrecordio.ReadFromTFRecord(
                    file_pattern=<file pattern>)
            | "ParseMetadataFile" >> beam.ParDo(_parseExamples())
    
    class _parseExamples(beam.DoFn):
        def __init__(self):
            beam.DoFn.__init__(self)
    
        def process(self, element, feature_dict) -> tf.train.Example:
            example = tf.train.Example.FromString(element)
            # Or something like...
            # example = tf.io.parse_single_example(element, feature_dict)
            # Then something like 
            # t = example.features.feature['new']
            # t = tf.train.Feature(int64_list=tf.train.Int64List(value=[1]))
            yield example
    

    关于 TFRecord 和 tf.Example 的其他非光束特定说明

    https://www.tensorflow.org/tutorials/load_data/tfrecord

    【讨论】:

    • 我如何将其他功能添加到该示例中?就像我想加入一个大查询表中的数据一样?
    • 一旦你有了 Example 对象,你可以用其他数据改变它,然后使用对 TFRecord 的写入来执行相反的读取操作。如果 BigQuery 中的查找值很小,那么您可以使用光束 SideInput ,如果它们很大,则使用光束管道中的连接。
    • 在任何地方都有这样的例子吗?我想看看您如何使用外部数据源对其进行变异。 (也加入键)。当你重写它时,你是否必须重新序列化 tf 记录?这是否意味着重新定义所有功能和类型?
    • 侧输入:beam.apache.org/documentation/programming-guide/#side-inputs 加入 beam.apache.org/documentation/programming-guide/#cogroupbykey 对于输出,您将示例重新序列化为字节并使用 TFRecordIO 写入。
    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 2011-05-18
    • 2020-05-08
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多