Registry / ai-ml / tensorflow-transform

tensorflow-transform

JSON →
library1.21.0pypypi✓ verified 89d ago

TensorFlow Transform (TFT) is a library for preprocessing data with TensorFlow. It allows users to define a preprocessing function that is applied to raw data *before* training, and then export this function as a TensorFlow graph that can be used for *inference*. This ensures consistency between training and serving. It's often used in conjunction with Apache Beam for distributed processing and is a key component of TensorFlow Extended (TFX). The current version is 1.17.0, following TensorFlow's release cadence with frequent updates.

pip install tensorflow-transform
INSTALL
IMPORT
SIG · TENSORFLOW-TRANSFO
T
tensorflow-transform
ai-mlpythonv1.21.0
Install
76.4s avg
Import
12858ms
Disk
2765MB
Pass rate
2/ 10
Env Coverage2 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.21.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 72.4s
py 3.11
✕ build_error
1/2 runs
py 3.12
✕ build_error
1/2 runs
py 3.13
✕ build_error
1/2 runs
py 3.9
✕ build_error
✓ 80.45s
2765MB installed
● package 2765MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

tensorflow_transform
✓ import tensorflow_transform as tft
tensorflow_transform.tf_utils
✓ import tensorflow_transform.tf_utils as tf_utils
tensorflow_transform.beam.impl
✓ from tensorflow_transform.beam import impl as beam_impl
DatasetMetadata
✓ from tensorflow_transform.tf_metadata import dataset_metadata
✗ from tensorflow_transform.metadata import dataset_metadata
The metadata module was moved under `tf_metadata` in later versions.

This quickstart demonstrates how to use `tensorflow-transform` to preprocess a small dataset locally. It defines a `preprocessing_fn` to scale numerical features and one-hot encode categorical features, then applies it using Apache Beam's DirectRunner.

import tensorflow as tf import tensorflow_transform as tft from tensorflow_transform.tf_metadata import dataset_metadata from tensorflow_transform.tf_metadata import schema_utils import apache_beam as beam from apache_beam.runners.direct import direct_runner # 1. Define the schema of the raw data _RAW_DATA_FEATURE_SPEC = { 'x': tf.io.FixedLenFeature([], tf.float32), 'y': tf.io.FixedLenFeature([], tf.string), 's': tf.io.FixedLenFeature([], tf.float32, default_value=0.0) } _RAW_DATA_METADATA = dataset_metadata.DatasetMetadata( schema_utils.schema_from_feature_spec(_RAW_DATA_FEATURE_SPEC)) # 2. Define the preprocessing function def preprocessing_fn(inputs): """Preprocesses raw inputs into transformed features.""" outputs = {} outputs['x_scaled'] = tft.scale_to_z_score(inputs['x']) outputs['y_one_hot'] = tft.one_hot( tft.string_to_int(inputs['y'], vocab_filename='vocab_y'), num_buckets=3 ) # Assuming max 3 unique values for y outputs['s_identity'] = inputs['s'] # Pass through return outputs # 3. Prepare some raw data raw_data = [ {'x': 10.0, 'y': 'apple', 's': 1.0}, {'x': 20.0, 'y': 'banana', 's': 2.0}, {'x': 30.0, 'y': 'apple', 's': 3.0}, {'x': 40.0, 'y': 'orange', 's': 4.0}, {'x': 50.0, 'y': 'banana', 's': 5.0}, ] # 4. Run the transform locally using Apache Beam DirectRunner with beam.Pipeline(runner=direct_runner.DirectRunner()) as p: # Create a PCollection of raw data raw_data_pcollection = ( p | 'CreateRawData' >> beam.Create(raw_data) ) # Apply the transform: Analyze (compute stats) and Transform (apply changes) transformed_data_pcollection, transform_fn = ( (raw_data_pcollection, _RAW_DATA_METADATA) | 'AnalyzeAndTransform' >> tft.beam.AnalyzeAndTransformDataset(preprocessing_fn) ) # Collect and print the transformed data print('Transformed data:') _ = ( transformed_data_pcollection | 'PrintTransformedData' >> beam.Map(print) ) print('Preprocessing complete.')
tft --version
Debug
Known issues
breakingTensorFlow 2.x Compatibility: The `preprocessing_fn` passed to `AnalyzeAndTransformDataset` is traced into a TensorFlow graph, and for TFT versions >= 1.0, it runs in a TF2 context. Mixing TF1 `tf.compat.v1` APIs or session-based operations directly within `preprocessing_fn` can lead to errors.
fix
Ensure your `preprocessing_fn` exclusively uses TensorFlow 2.x APIs. Avoid `tf.compat.v1` and ops that require a `tf.Session`.
affects: >=1.0.0
gotchaTwo-Pass Transformation Model: TFT operates in two phases: 'Analyze' and 'Transform'. The 'Analyze' phase computes statistics (e.g., min/max for scaling, vocabulary for string-to-int) over the entire dataset. The 'Transform' phase then applies these computed statistics to individual data points. New users often misunderstand that `preprocessing_fn` is traced and executed as a graph, not a simple row-wise Python function.
fix
Design `preprocessing_fn` to be a pure TensorFlow graph definition. Use `tft` APIs for analyzers (e.g., `tft.scale_to_z_score`, `tft.string_to_int`) which handle the two-pass logic. Avoid stateful Python logic or non-TensorFlow operations that are not part of the `preprocessing_fn`'s graph construction.
affects: *
gotchaApache Beam Integration for Scale: While TFT can run locally with Beam's `DirectRunner`, its primary use case is distributed processing with other Beam runners (e.g., Dataflow, Flink, Spark). Misconfiguring Beam runners, I/O connectors, or managing large datasets can be a source of errors and performance bottlenecks.
fix
For production, familiarize yourself with Apache Beam's programming model and specific runner configurations. Test with a small subset of data on your chosen distributed runner before scaling up. Pay attention to serialization, data formats (e.g., TFRecord), and error handling in a distributed context.
affects: *
Errors
Common errors & fixes
AttributeError: module 'tensorflow.compat.v2.io.gfile' has no attribute 'Exists'
This error often occurs when TensorFlow's `tf.io.gfile` (or `tf.compat.v2.io.gfile`) is used in a context where the underlying filesystem (e.g., local, GCS) is not properly initialized or accessible, or if the `tensorflow` version is mismatched with `tensorflow-transform` requirements.
fix
Ensure `tensorflow` and `tensorflow-transform` versions are compatible. Verify file paths are correct and accessible by the running user/service. For cloud storage, ensure appropriate authentication and permissions are set for the Beam runner.
TypeError: unsupported operand type(s) for +: 'Tensor' and 'NoneType'
This typically happens inside `preprocessing_fn` if a feature is expected to be present but is missing in some input records, leading to a `None` value being passed to a TensorFlow operation that expects a `Tensor`.
fix
Use `tf.io.FixedLenFeature` with `default_value` when defining your `_RAW_DATA_FEATURE_SPEC` to handle missing values gracefully. Alternatively, use `tf.where` or `tf.cond` to handle `None` or empty tensors explicitly within your `preprocessing_fn`.
tf.errors.FailedPreconditionError: Table not initialized.
This error often occurs when using `tft.string_to_int` or other vocabulary-based transformations, and the underlying lookup table (built from the vocabulary generated during the 'Analyze' phase) is not properly initialized before the 'Transform' phase or when attempting inference.
fix
Ensure the `AnalyzeAndTransformDataset` pipeline completes successfully, generating the `transform_fn` and the associated vocabulary. When serving, load the `transform_fn` correctly and ensure all assets (including vocabularies) are present and accessible in the exported `TransformGraph`.
Upgrade
Version history
1.21.0latest on PyPI · released Jun 11, 2026
Audit
Dependencies
tensorflowrequiredCore TensorFlow library required for graph definition and execution.
apache-beamrequiredUsed for distributed data processing when running transformations at scale.
Agent activity
16 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
tensorflow-transform — pip install tensorflow-transform · libregistry