为什么 tfdv.display_schema() 不支持 SchemaGen?



关于TFX的tensorflow数据验证,我正在努力了解何时应该使用*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.inder_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-

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

最新更新