搭配 TensorFlow 2.x 使用 tf.Transform

tf.Transform0.30 版本開始,除非明確停用 TF 2.x 行為,否則預設行為是匯出 TF 2.x SavedModel。本頁面提供搭配 tf.Transform 使用的指南,以將轉換圖表匯出為 TensorFlow 2.x SavedModel。

TF 2.x 版 tf.Transform 的新功能

preprocessing_fn 中載入 Keras 模型

請使用 tft.make_and_track_object API 載入 Keras 模型,如下例所示。

def preprocessing_fn(inputs):
  keras_model = tft.make_and_track_object(lambda: tf.keras.models.load_model(...), name='_unique_name')
  ...
  return {'keras_model_output': keras_model(inputs[...])}

使用 TF 2.x tf.hub 模組

只有在追蹤 preprocessing_fn 並將其匯出為 TF 2.x SavedModel 時 (這是從 tensorflow_transform 0.30 開始的預設行為),TF 2.x hub 模組才能在 tf.Transform 中運作。請使用 tft.make_and_track_object API 載入 tf.hub 模組,如下例所示。

def preprocessing_fn(inputs):
  hub_module = tft.make_and_track_object(lambda: hub.load(...))
  ...
  return {'hub_module_output': hub_module(inputs[...])}

潛在移轉問題

如果將現有的 tf.Transform 管道從 TF 1.x 移轉至 TF 2.x,可能會遇到下列問題

RuntimeError:preprocessing_fn 中分析器的順序似乎不具決定性。

在 TF 2.x 中,使用者提供的 preprocessing_fn 會被追蹤多次。如果每次追蹤時遇到 TFT 分析器的順序都不同,就會引發這個錯誤。您可以移除叫用 TFT 分析器順序中的任何非決定性因素來修正這個問題。

transform_raw_features 的輸出不包含預期的特徵。

範例例外狀況

KeyError: \<feature key>

\<feature key> not found in features dictionary.

TFTransformOutput.transform_raw_features 忽略 drop_unused_features 參數,且行為如同設為 True。請更新這個 API 輸出字典的任何用法,以檢查您嘗試擷取的鍵是否確實存在。

tf.estimator.BaselineClassifier 看到「Table not initialized」錯誤。

範例例外狀況

tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized.

對以 Estimator 為基礎的執行器提供 Trainer 支援是盡力而為。雖然其他估算器可以運作,但我們已發現 BaselineClassifier 中的表格初始化問題。請tf.Transform 中停用 TF 2.x

已知問題/尚未支援的功能

尚不支援以 TFRecord 格式輸出詞彙表。

尚不支援將 tfrecord_gzip 作為 tft.vocabulary (和其他詞彙表 API) 中 file_format 參數的有效值。

保留舊版 tf.Transform 行為

如果您的 tf.Transform 管道不應與 TF 2.x 搭配執行,您可以透過下列其中一種方式保留舊版行為