앞서 2번에 걸처서 알아본 TFDV를 머신러닝 파이프라인에 통합해보겠다.
TFX는 StatisticsGen이라는 파이프라인 컴포넌트를 제공한다. StatisticsGen은 데이터 수집 때 사용한 ExampleGen 컴포넌트의 출력을 입력으로 수락한 다음 통계 생성을 수행한다.
from tfx.components import StatisticsGen
statistics_gen = StatisticsGen(examples=example_gen.outputs['examples'])
context.run(statistics_gen)
데이터 수집에서 살펴봤듯이, 아래와 같이 대화형 콘텍스트에서 출력을 시각화할 수 있다.
context.show(statistics_gen.outputs['statistics'])
스키마 생성은 통계 생성만큼 쉽다.
from tfx.components import SchemaGen
schema_gen = SchemaGen(statistics=stat丄stics_gen.outputs['statistics'],
infer_feature_shape=True)
context.run(schema_gen)
SchemaGen 컴포넌트는 스키마가 없을 때만 스키마를 생성한다. 이 컴포넌트를 처음 실행할 때는 스키마를 검토한 후 (전 포스팅 '스키마 업데이트'에서 살펴본 대로) 필요시 수동으로 조정해주면 좋다. 그렇게 하면 새 피처를 추가하는 등 변경이 필요하기 전까지 해당 스키마를 사용할 수 있다.
이제 통계와 스키마를 사용하여 새로운 데이터셋을 검증할 수 있다.
from tfx.components import ExampleValidator
example_validator = ExampleValidator(statistics=statistics_gen.outputs['statistics'],
schema=schema_gen.outputs[ schema])
context.run(example_validator)
ExampleValidator는 이전 포스팅에서 설명한 스큐 및 드리프트 비교기를 사용하여 스키마 관련 이상치를 자동으로 탐지한다. 그러나 데이터의 모든 잠재적 이상치를 포함하지는 않을 것이다. 특정 이상치를 탐지해야 한다면, 사용자 지정 컴포넌트를 따로 작성해야하는데 여유가 된다면 추후에 다뤄보도록 하겠다.
ExampleValidator 컴포넌트가 새 데이터셋과 이전 데이터셋 사이의 데이터셋 통계나 스키마에서 잘못된 정렬을 감지하면 메타데이터스토어에서 상태를 failed로 설정하고 파이프라인이 결국 중지된다. 이상치가 감지되지 않으면 파이프라인은 다음 단계인 데이터 전처리 단계로 이동한다.
This post was written based on what I read and studied the book below.
https://www.oreilly.com/library/view/building-machine-learning/9781492053187/
댓글