Estimators
TensorFlow 高級API 的 Estimator 極大的簡化了機器學習編程。Estimators 封裝了以下操作:
- training
- evaluation
- prediction
- export for serving
Estimators 的優點
Estimators 提供以下好處:
- 你可以運行基于 Estimators 的模型在一個本地或者分布式的多服務器環境之上并不用修改你的模型。更進一步的講,你可以在CPUs, GPUs或者是 TPUs上,運行 基于 Estimators 的模型而不需要重新編寫你的模型。
- Estimators 簡化共享執行在模型開發者之間。
- 你可以使用高級直觀的代碼來開發藝術模型的一個狀態。簡短的說,用 Estimators 創建模型比用低級 TensorFlow APIs 簡單了太多太多。
- Estimators 是自創建在
tf.layers
上的,這樣簡化了自定義化。 - Estimators 為你創建
graph
,換句話說,你不用自己去創建graph
- Estimators 提供了一個安全的分布式訓練循環:
- 創建
graph
- 初始化變量
- 開始排隊
- 解決異常
- 創建
checkpoint
文件和恢復 - 為TensorBoard 保存概要
- 創建
當用 Estimators 編寫一個應用時,你必須把輸入數據管道從模型里分開。這一分離簡化了實驗室使用的不同數據集。
預制的 Estimators
預制的 Estimators 能夠使你致力于更高的概念等級。你不再需要擔心創建計算圖或者 sessions
,因為 Estimators 為你解決了所有的基礎工作。預制的 Estimators 為你創建并管理 Graph
和 Session
對象。進一步講,預制的 Estimators 允許你用不同的模型架構進行試驗通過最小的代碼修改。例如
DNNClassifier
是一個預制的 Estimator 類,它訓練分類模型通過密集的,向前傳播神經網絡。
一個預制的 Estimators 編程結構
一個預制的 Estimator 編程過程典型的由接下來的四個步驟組成:
-
寫一個或者多個數據集導入方法。例如,你可能創建一個方法來導入訓練數據集和另一個方法來導入測試數據集。每個數據集導入方法必須返還兩個對象:
- 一個 ‘keys’ 是 feature 名字和 ‘values’ 是 Tensors 的字典,其包含對應的特征數據。
- 一個 Tensor 包含一個或者多個標簽。
例如:下面的代碼為一個輸入方法展示一個基礎框架:
def input_fn(dataset):
... # manipulate dataset, extracting feature names and the label
return feature_dict, label
-
定義特征列。每個
tf.feature_column
識別一個特征名稱,它的類型,和任何輸入預處理。例如,下面的代碼創建三個特征列它們保留著整數型或者浮點型數據。前兩個特征列簡單地識別特征的名字和類型。第三個特征列也調用一個指定的 lambda 方程來縮放原數據。
# Define three numeric feature columns.
population = tf.feature_column.numeric_column('population')
crime_rate = tf.feature_column.numeric_column('crime_rate')
median_education = tf.feature_column.numeric_column('median_education',
normalizer_fn='lambda x: x - global_education_mean')
-
實例化相關的預制 Estimators。例如:這里有個
LinearClassifier
預制 Estimator 例子。
# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.Estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education],
)
-
調用 training, evaluation, 或者 inference 函數。例如:所有的 Estimators 提供一個
train
方法來訓練模型。
# my_training_set is the function created in Step 1
estimator.train(input_fn=my_training_set, steps=2000)
預制 Estimators 的好處
預制 Estimators 編碼的最優方案,提供以下幾點好處:
- 最佳實踐于決定位于計算圖的不同的部分應該運行,實施策略在一個單一的機器或者集群。
- 最佳實踐于 event(summary) 寫入和有用的概括。
如果不用,你必須執行這些特征通過你自己。
自定義 Estimators
無論是自定義還是預制的 Estimator,每個 Estimator 的中心點是它的 model function,它是一個方法為訓練,評估和預測來創建圖。當你正在使用一個預制的 Estimator,其他某個人已經執行模型方法。當依賴一個自定義 Estimator,你必須自己寫這個模型方法。 這個指南文件解釋如何寫模型方程。
推薦的工作流程
我們推薦以下工作流程:
1. 假設存在合適的預制 Estimator,請使用它構建你的第一個模型并使用其結果建立一個基線。
2. 使用此預制 Estimator 構建和測試你的整體流程管道,包括你的數據的完整性和可靠性。
3. 如果有合適的替代預制 Estimator 可用,則運行實驗以確定哪種預制估算器能夠產生最佳結果。
4. 可能通過構建您自己的自定義估算器來進一步改進您的模型。
從 Keras 模型創建 Estimators
你可以將現有的 Keras 模型轉換為 Estimators。 這樣做可以讓你的 Keras 模型訪問 Estimator 的優點,例如分布式訓練。調用 tf.keras.estimator.model_to_estimator
,如下例所示:
# Instantiate a Keras inception v3 model.
keras_inception_v3 = tf.keras.applications.inception_v3.InceptionV3(weights=None)
# Compile model with the optimizer, loss, and metrics you'd like to train with.
keras_inception_v3.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy',
metric='accuracy')
# Create an Estimator from the compiled Keras model. Note the initial model
# state of the keras model is preserved in the created Estimator.
est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3)
# Treat the derived Estimator as you would with any other Estimator.
# First, recover the input name(s) of Keras model, so we can use them as the
# feature column name(s) of the Estimator input function:
keras_inception_v3.input_names # print out: ['input_1']
# Once we have the input name(s), we can create the input function, for example,
# for input(s) in the format of numpy ndarray:
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"input_1": train_data},
y=train_labels,
num_epochs=1,
shuffle=False)
# To train, we call Estimator's train function:
est_inception_v3.train(input_fn=train_input_fn, steps=2000)
Note that the names of feature columns and labels of a keras estimator come from the corresponding compiled keras model. For example, the input key names for train_input_fn above can be obtained from keras_inception_v3.input_names, and similarly, the predicted output names can be obtained from keras_inception_v3.output_names.
For more details, please refer to the documentation for tf.keras.estimator.model_to_estimator
.
更新:一月 27, 2018