tft.make_and_track_object
Stay organized with collections
Save and categorize content based on your preferences.
Keeps track of the object created by invoking trackable_factory_callable
.
tft.make_and_track_object(
trackable_factory_callable: Callable[[], base.Trackable],
name: Optional[str] = None
) -> base.Trackable
This API is only for use when Transform APIs are run with TF2 behaviors
enabled and tft_beam.Context.force_tf_compat_v1
is set to False.
Use this API to track TF Trackable objects created in the preprocessing_fn
such as tf.hub modules, tf.data.Dataset etc. This ensures they are serialized
correctly when exporting to SavedModel.
Args |
trackable_factory_callable
|
A callable that creates and returns a Trackable
object.
|
name
|
(Optional) Provide a unique name to track this object with. If the
Trackable object created is a Keras Layer or Model this is needed for
proper tracking.
|
Example:
def preprocessing_fn(inputs):
dataset = tft.make_and_track_object(
lambda: tf.data.Dataset.from_tensor_slices([1, 2, 3]))
with tf.init_scope():
dataset_list = list(dataset.as_numpy_iterator())
return {'x_0': dataset_list[0] + inputs['x']}
raw_data = [dict(x=1), dict(x=2), dict(x=3)]
feature_spec = dict(x=tf.io.FixedLenFeature([], tf.int64))
raw_data_metadata = tft.DatasetMetadata.from_feature_spec(feature_spec)
with tft_beam.Context(temp_dir=tempfile.mkdtemp(),
force_tf_compat_v1=False):
transformed_dataset, transform_fn = (
(raw_data, raw_data_metadata)
| tft_beam.AnalyzeAndTransformDataset(preprocessing_fn))
transformed_data, transformed_metadata = transformed_dataset
transformed_data
[{'x_0': 2}, {'x_0': 3}, {'x_0': 4}]
Returns |
The object returned when trackable_factory_callable is invoked. The object
creation is lifted out to the eager context using tf.init_scope .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-11-01 UTC.
[null,null,["Last updated 2024-11-01 UTC."],[],[],null,["# tft.make_and_track_object\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/transform/blob/v1.16.0/tensorflow_transform/annotators.py#L119-L177) |\n\nKeeps track of the object created by invoking `trackable_factory_callable`. \n\n tft.make_and_track_object(\n trackable_factory_callable: Callable[[], base.Trackable],\n name: Optional[str] = None\n ) -\u003e base.Trackable\n\nThis API is only for use when Transform APIs are run with TF2 behaviors\nenabled and `tft_beam.Context.force_tf_compat_v1` is set to False.\n\nUse this API to track TF Trackable objects created in the `preprocessing_fn`\nsuch as tf.hub modules, tf.data.Dataset etc. This ensures they are serialized\ncorrectly when exporting to SavedModel.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `trackable_factory_callable` | A callable that creates and returns a Trackable object. |\n| `name` | (Optional) Provide a unique name to track this object with. If the Trackable object created is a Keras Layer or Model this is needed for proper tracking. |\n\n\u003cbr /\u003e\n\n#### Example:\n\n def preprocessing_fn(inputs):\n dataset = tft.make_and_track_object(\n lambda: tf.data.Dataset.from_tensor_slices([1, 2, 3]))\n with tf.init_scope():\n dataset_list = list(dataset.as_numpy_iterator())\n return {'x_0': dataset_list[0] + inputs['x']}\n raw_data = [dict(x=1), dict(x=2), dict(x=3)]\n feature_spec = dict(x=tf.io.FixedLenFeature([], tf.int64))\n raw_data_metadata = tft.DatasetMetadata.from_feature_spec(feature_spec)\n with tft_beam.Context(temp_dir=tempfile.mkdtemp(),\n force_tf_compat_v1=False):\n transformed_dataset, transform_fn = (\n (raw_data, raw_data_metadata)\n | tft_beam.AnalyzeAndTransformDataset(preprocessing_fn))\n transformed_data, transformed_metadata = transformed_dataset\n transformed_data\n [{'x_0': 2}, {'x_0': 3}, {'x_0': 4}]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| The object returned when trackable_factory_callable is invoked. The object creation is lifted out to the eager context using [`tf.init_scope`](https://www.tensorflow.org/api_docs/python/tf/init_scope). ||\n\n\u003cbr /\u003e"]]