2

关于 TFX 的 tensorflow-data-validation,我试图了解何时应该使用 *Gen 组件与使用 TFDV 提供的方法。

具体来说,让我感到困惑的是,我有这个作为我的 ExampleGen:

output = example_gen_pb2.Output(
         split_config=example_gen_pb2.SplitConfig(splits=[
             example_gen_pb2.SplitConfig.Split(name='train', hash_buckets=7),
             example_gen_pb2.SplitConfig.Split(name='test', hash_buckets=2),
             example_gen_pb2.SplitConfig.Split(name='eval', hash_buckets=1)
         ]))
example_gen = CsvExampleGen(input_base=os.path.join(base_dir, data_dir), 
output_config=output)
context.run(example_gen)

所以我想,我想从我的火车分割中生成我的统计数据,而不是从原始火车文件中,所以我尝试了:

statistics_gen = StatisticsGen(
  examples=example_gen.outputs['examples'],
  exclude_splits=['eval']
)
context.run(statistics_gen)

并且运行良好。但后来,我尝试推断我的模式(插入蜂鸣器声音):

schema = tfdv.infer_schema(statistics=statistics_gen)

并且故意这会引发下面的错误。我完全期望它不是正确的类型,但我无法弄清楚如何从 StatsGen 对象中提取正确的输出以提供给 infer_schema() 方法

或者,如果我追求一个完全基于 *Gen 的组件结构,它会构建,但我看不到如何正确可视化架构、统计信息等。最后,我在这里使用 tfdv.infer_schema() 调用的原因如果您尝试将 SchemaGen 传递给同样命运多舛的“display_schema()”调用该错误。

上面的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-93ceafbcb04a> in <module>
----> 1 schema = tfdv.infer_schema(statistics=validate_stats)
      2 tfdv.write_schema_text(schema, schema_location)
      3 
      4 tfdv.display(infer_schema)

/usr/local/lib/python3.6/dist-packages/tensorflow_data_validation/api/validation_api.py in infer_schema(statistics, infer_feature_shape, max_string_domain_size, schema_transformations)
     95     raise TypeError(
     96         'statistics is of type %s, should be '
---> 97         'a DatasetFeatureStatisticsList proto.' % type(statistics).__name__)
     98 
     99   # This will raise an exception if there are multiple datasets, none of which

TypeError: statistics is of type ExampleValidator, should be a DatasetFeatureStatisticsList proto.

我真正想了解的是为什么我们有组件,例如 SchemaGen 和 StatisticsGen 只是为了让 TFDV 要求我们使用内部函数才能从中获得价值。我假设它提供交互式管道与非交互式场景,但我的谷歌搜索让我不清楚。

如果有一种方法可以根据我的数据拆分而不是依赖文件阅读器来生成和查看统计信息,我也很想知道这一点。(如果不是很明显,是的,我是 TFX 的新手)。

TIA

4

1 回答 1

1

我也是 TFX 的新手。你的帖子ExampleValidator帮助了我,希望这能回答你的问题。

仅使用组件来可视化架构

 statistics_gen = StatisticsGen(
  examples=example_gen.outputs['examples'],
  exclude_splits=['eval']
)
context.run(statistics_gen)

schema_gen = SchemaGen(
    statistics=statistics_gen.outputs['statistics'],
    infer_feature_shape=True
)
context.run(schema_gen)

context.show(schema_gen.outputs['schema']) # this should allow you to to visualize your schema 

使用组件 + TFDV 可视化模式

看来我们不能StatisticsGen直接使用。我们需要知道统计数据生成工件的保存位置,然后使用加载该工件tfdv.load_statistics

# get the stats artifact
stats_artifact = statistics_gen.outputs.statistics._artifacts[0]

# get base path 
base_path = stats_artifact.uri 

# get path to file 
train_stats_file = os.path.join(base_path, 'train/stats_tfrecord') #only showing training as an example

# load stats 
loaded_stats = tfdv.load_statistics(train_stats_file)

# generic and show schema
schema = tfdv.infer_schema(loaded_stats)

tfdv.display_schema(schema)
于 2020-10-04T19:59:34.900 回答