【问题标题】:Why isn't SchemaGen supported in tfdv.display_schema()?为什么 tfdv.display_schema() 不支持 SchemaGen?
【发布时间】:2020-09-25 21:34:43
【问题描述】:

关于 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( ) 这里的调用是针对同样命运多舛的“display_schema()”调用,如果您尝试将其传递给 SchemaGen,则会出错。

上面的错误:

---------------------------------------------------------------------------
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

【问题讨论】:

    标签: tensorflow2.0 tfx tensorflow-data-validation


    【解决方案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)
    

    【讨论】:

    • 谢谢@sleepyowl,这与我最初提出这个问题以来的发现一致。不过,我仍然犹豫不决的是,“这就是我们发现的……但是,这是“正确的方法”吗?”感谢您的洞察力。
    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2011-03-17
    • 2018-07-21
    • 2013-01-04
    • 2017-10-07
    • 2018-08-24
    • 2011-09-16
    相关资源
    最近更新 更多