为什么tfdv.display_schema不支持SchemaGen?

问题描述

关于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要求我们使用内部函数才能从中获得价值。我假设它是为交互式管道与非交互式方案提供的,但是我的Google搜索使我不清楚。

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

TIA

解决方法

我也是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)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...