টিএফএক্স পাইথন ফাংশন উপাদান টিউটোরিয়াল

এই নোটবুকে TFX ইন্টারেক্টিভ কনটেক্সট এবং স্থানীয়ভাবে সাজানো TFX পাইপলাইনে পাইথন ফাংশন উপাদানগুলি কীভাবে লেখক এবং চালাতে হয় তার একটি উদাহরণ রয়েছে।

আরো প্রসঙ্গ এবং তথ্যের জন্য, দেখুন কাস্টম পাইথন ফাংশন উপাদান TFX ডকুমেন্টেশন সাইটে পাতা।

সেটআপ

আমরা প্রথমে TFX ইনস্টল করব এবং প্রয়োজনীয় মডিউল আমদানি করব। TFX এর জন্য Python 3 প্রয়োজন।

সিস্টেম পাইথন সংস্করণ পরীক্ষা করুন

import sys
sys.version
'3.7.5 (default, Feb 23 2021, 13:22:40) \n[GCC 8.4.0]'

পিপ আপগ্রেড করুন

স্থানীয়ভাবে চালানোর সময় একটি সিস্টেমে পিপ আপগ্রেড করা এড়াতে, আমরা Colab-এ চলছি কিনা তা নিশ্চিত করুন। স্থানীয় সিস্টেম অবশ্যই আলাদাভাবে আপগ্রেড করা যেতে পারে।

try:
  import colab
  !pip install --upgrade pip
except:
  pass

TFX ইনস্টল করুন

pip install -U tfx

আপনি কি রানটাইম রিস্টার্ট করেছেন?

আপনি যদি Google Colab ব্যবহার করেন, প্রথমবার উপরের সেলটি চালানোর সময়, আপনাকে অবশ্যই রানটাইমটি পুনরায় চালু করতে হবে (রানটাইম > রানটাইম পুনরায় চালু করুন ...)। Colab যেভাবে প্যাকেজগুলি লোড করে তার কারণেই এটি হয়েছে৷

প্যাকেজ আমদানি করুন

আমরা TFX আমদানি করি এবং এর সংস্করণ পরীক্ষা করি।

# Check version
from tfx import v1 as tfx
tfx.__version__
'1.4.0'

কাস্টম পাইথন ফাংশন উপাদান

এই বিভাগে, আমরা পাইথন ফাংশন থেকে উপাদান তৈরি করব। আমরা কোনো বাস্তব এমএল সমস্যা করব না — এই সাধারণ ফাংশনগুলি শুধুমাত্র পাইথন ফাংশন কম্পোনেন্ট ডেভেলপমেন্ট প্রক্রিয়াকে চিত্রিত করতে ব্যবহৃত হয়।

দেখুন পাইথন ফাংশন ভিত্তিক উপাদান নির্দেশিকা আরো ডকুমেন্টেশন জন্য।

পাইথন কাস্টম উপাদান তৈরি করুন

আমরা একটি ফাংশন লিখে শুরু করি যা কিছু ডামি ডেটা তৈরি করে। এটি তার নিজস্ব পাইথন মডিউল ফাইলে লেখা হয়।

%%writefile my_generator.py

import os
import tensorflow as tf  # Used for writing files.

from tfx import v1 as tfx

# Non-public APIs, just for showcase.
from tfx.types.experimental.simple_artifacts import Dataset

@tfx.dsl.components.component
def MyGenerator(data: tfx.dsl.components.OutputArtifact[Dataset]):
  """Create a file with dummy data in the output artifact."""
  with tf.io.gfile.GFile(os.path.join(data.uri, 'data_file.txt'), 'w') as f:
    f.write('Dummy data')

  # Set metadata and ensure that it gets passed to downstream components.
  data.set_string_custom_property('my_custom_field', 'my_custom_value')
Writing my_generator.py

এর পরে, আমরা একটি দ্বিতীয় উপাদান লিখি যা উত্পাদিত ডামি ডেটা ব্যবহার করে। আমরা শুধু ডেটার হ্যাশ গণনা করব এবং তা ফেরত দেব।

%%writefile my_consumer.py

import hashlib
import os
import tensorflow as tf

from tfx import v1 as tfx

# Non-public APIs, just for showcase.
from tfx.types.experimental.simple_artifacts import Dataset
from tfx.types.standard_artifacts import String

@tfx.dsl.components.component
def MyConsumer(data: tfx.dsl.components.InputArtifact[Dataset],
               hash: tfx.dsl.components.OutputArtifact[String],
               algorithm: tfx.dsl.components.Parameter[str] = 'sha256'):
  """Reads the contents of data and calculate."""
  with tf.io.gfile.GFile(
      os.path.join(data.uri, 'data_file.txt'), 'r') as f:
    contents = f.read()
  h = hashlib.new(algorithm)
  h.update(tf.compat.as_bytes(contents))
  hash.value = h.hexdigest()

  # Read a custom property from the input artifact and set to the output.
  custom_value = data.get_string_custom_property('my_custom_field')
  hash.set_string_custom_property('input_custom_field', custom_value)
Writing my_consumer.py

ইন্টারেক্টিভ কনটেক্সট সহ নোটবুক চালান

এখন, আমরা TFX ইন্টারেক্টিভ কনটেক্সটে আমাদের নতুন উপাদানের ব্যবহার প্রদর্শন করব।

আপনি TFX নোটবুক InteractiveContext দিয়ে কি করতে পারেন কি আরো তথ্যের জন্য, ইন-নোটবুক দেখতে TFX Keras কম্পোনেন্ট টিউটোরিয়াল

from my_generator import MyGenerator
from my_consumer import MyConsumer

ইন্টারেক্টিভ কনটেক্সট তৈরি করুন

# Here, we create an InteractiveContext using default parameters. This will
# use a temporary directory with an ephemeral ML Metadata database instance.
# To use your own pipeline root or database, the optional properties
# `pipeline_root` and `metadata_connection_config` may be passed to
# InteractiveContext. Calls to InteractiveContext are no-ops outside of the
# notebook.
from tfx.orchestration.experimental.interactive.interactive_context import InteractiveContext
context = InteractiveContext()
WARNING:absl:InteractiveContext pipeline_root argument not provided: using temporary directory /tmp/tfx-interactive-2021-12-05T10_37_04.715534-3q0k1y0m as root for pipeline outputs.
WARNING:absl:InteractiveContext metadata_connection_config not provided: using SQLite ML Metadata database at /tmp/tfx-interactive-2021-12-05T10_37_04.715534-3q0k1y0m/metadata.sqlite.

সঙ্গে ইন্টারেক্টিভ আপনার উপাদান চালান context.run()

এর পরে, আমরা আমাদের উপাদান ইন্টারেক্টিভ নোটবুক মধ্যে দিয়ে চালানো context.run() । আমাদের ভোক্তা উপাদান জেনারেটর উপাদানের আউটপুট ব্যবহার করে।

generator = MyGenerator()
context.run(generator)
WARNING: Logging before InitGoogleLogging() is written to STDERR
I1205 10:37:04.765872 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
consumer = MyConsumer(
    data=generator.outputs['data'],
    algorithm='md5')
context.run(consumer)
I1205 10:37:04.808555 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type

কার্যকর করার পরে, আমরা ডিস্কে ভোক্তা উপাদানের "হ্যাশ" আউটপুট আর্টিফ্যাক্টের বিষয়বস্তু পরিদর্শন করতে পারি।

tail -v {consumer.outputs['hash'].get()[0].uri}
==> /tmp/tfx-interactive-2021-12-05T10_37_04.715534-3q0k1y0m/MyConsumer/hash/2/value <==
0015fe7975d1a2794b59aa12635703f1

এটাই, এবং আপনি এখন আপনার নিজস্ব কাস্টম উপাদানগুলি লিখেছেন এবং কার্যকর করেছেন!

পাইপলাইনের সংজ্ঞা লিখ

এর পরে, আমরা এই একই উপাদানগুলি ব্যবহার করে একটি পাইপলাইন লিখব। ব্যবহার করার সময় InteractiveContext একটি নোটবুক মধ্যে পরীক্ষার জন্য ভাল কাজ করে, একটি পাইপলাইন সংজ্ঞা আপনি উৎপাদন ব্যবহারের জন্য স্থানীয় অথবা দূরবর্তী রানার্স আপনার পাইপলাইন স্থাপন করতে দেয়।

এখানে, আমরা আপনার মেশিনে স্থানীয়ভাবে চলমান LocalDagRunner-এর ব্যবহার প্রদর্শন করব। উত্পাদন সম্পাদনের জন্য, এয়ারফ্লো বা কুবেফ্লো রানারগুলি আরও উপযুক্ত হতে পারে।

একটি পাইপলাইন নির্মাণ

import os
import tempfile
from tfx import v1 as tfx

# Select a persistent TFX root directory to store your output artifacts.
# For demonstration purposes only, we use a temporary directory.
PIPELINE_ROOT = tempfile.mkdtemp()
# Select a pipeline name so that multiple runs of the same logical pipeline
# can be grouped.
PIPELINE_NAME = "function-based-pipeline"
# We use a ML Metadata configuration that uses a local SQLite database in
# the pipeline root directory. Other backends for ML Metadata are available
# for production usage.
METADATA_CONNECTION_CONFIG = tfx.orchestration.metadata.sqlite_metadata_connection_config(
    os.path.join(PIPELINE_ROOT, 'metadata.sqlite'))

def function_based_pipeline():
  # Here, we construct our generator and consumer components in the same way.
  generator = MyGenerator()
  consumer = MyConsumer(
      data=generator.outputs['data'],
      algorithm='md5')

  return tfx.dsl.Pipeline(
      pipeline_name=PIPELINE_NAME,
      pipeline_root=PIPELINE_ROOT,
      components=[generator, consumer],
      metadata_connection_config=METADATA_CONNECTION_CONFIG)

my_pipeline = function_based_pipeline()

সঙ্গে আপনার পাইপলাইন চালান LocalDagRunner

tfx.orchestration.LocalDagRunner().run(my_pipeline)
I1205 10:37:04.983860 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:04.990442 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:04.996665 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:05.003470 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:05.013659 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:05.031374 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:05.048280 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type
I1205 10:37:05.067972 28682 rdbms_metadata_access_object.cc:686] No property is defined for the Type

আমরা এই পাইপলাইন সম্পাদন দ্বারা উত্পন্ন আউটপুট শিল্পকর্ম পরিদর্শন করতে পারেন.

find {PIPELINE_ROOT}
/tmp/tmpydmun02b
/tmp/tmpydmun02b/metadata.sqlite
/tmp/tmpydmun02b/MyConsumer
/tmp/tmpydmun02b/MyConsumer/.system
/tmp/tmpydmun02b/MyConsumer/.system/executor_execution
/tmp/tmpydmun02b/MyConsumer/.system/executor_execution/2
/tmp/tmpydmun02b/MyConsumer/hash
/tmp/tmpydmun02b/MyConsumer/hash/2
/tmp/tmpydmun02b/MyConsumer/hash/2/value
/tmp/tmpydmun02b/MyGenerator
/tmp/tmpydmun02b/MyGenerator/data
/tmp/tmpydmun02b/MyGenerator/data/1
/tmp/tmpydmun02b/MyGenerator/data/1/data_file.txt
/tmp/tmpydmun02b/MyGenerator/.system
/tmp/tmpydmun02b/MyGenerator/.system/executor_execution
/tmp/tmpydmun02b/MyGenerator/.system/executor_execution/1

আপনি এখন আপনার নিজস্ব কাস্টম উপাদান লিখেছেন এবং LocalDagRunner এ তাদের সম্পাদনা করেছেন! পরবর্তী পদক্ষেপসমূহ জন্য, অতিরিক্ত টিউটোরিয়াল এবং গাইড খুঁজে বার করো TFX ওয়েবসাইট