【问题标题】:Delete a column from TFRecord Dataset (for feature selection)从 TFRecord 数据集中删除一列(用于特征选择)
【发布时间】:2021-11-11 22:18:18
【问题描述】:

我正在尝试根据以下计划实施功能选择组件:

实现

  • 组件将InputArtifact[Example] 作为输入
  • 由于数据以 TFRecords 的形式存储在输入工件的 URI 中,因此我将其转换为兼容的 numpy 字典并使用 sklearn 得出所选特征的列表
  • 直接从输入示例中删除所需的特征以将其放入OutputArtifact[Example](具有相同的结构但列更少)

我已经完成了第一点和第二点,但我无法弄清楚如何直接在 TFRecord 数据集本身中删除选定的列(我正在使用 tf.data.TFRecordDataset(train_uri, compression_type='GZIP')

【问题讨论】:

  • 能否请您参考此blog.Thanks

标签: python tensorflow tensorflow-datasets tfrecord tfx


【解决方案1】:

我花了一些时间才弄清楚(借助 TensorFlow Support 在 cmets 中链接的博客),但这里有一个解决方法!

split_dataset = tf.data.TFRecordDataset("path_to_original_dataset.gzip", compression_type='GZIP')
with tf.io.TFRecordWriter(path = "path_to_new_TFRecord.gzip", options="GZIP") as writer:
      for split_record in split_dataset.as_numpy_iterator():
        example = tf.train.Example()
        example.ParseFromString(split_record)

        updated_example = update_example(selected_features, example)

        writer.write(updated_example.SerializeToString())

这里,updated_example 是我使用的自定义函数,它获取解析后的示例,对其进行处理并返回处理后的示例!

# update example with selected features
def update_example(selected_features, orig_example):
  result = {}
  for key, feature in orig_example.features.feature.items():
    if key in selected_features:
      result[key] = orig_example.features.feature[key]
    
    new_example = tf.train.Example(features=tf.train.Features(feature=result))
    return new_example

我没有删除该列(因为我找不到这样做的方法),而是逐个功能地创建了一个新示例并将其返回!

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 2014-02-05
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多