Estimators
TensorFlow 高級API 的 Estimator 極大的簡化了機(jī)器學(xué)習(xí)編程。Estimators 封裝了以下操作:
- training
- evaluation
- prediction
- export for serving
Estimators 的優(yōu)點(diǎn)
Estimators 提供以下好處:
- 你可以運(yùn)行基于 Estimators 的模型在一個本地或者分布式的多服務(wù)器環(huán)境之上并不用修改你的模型。更進(jìn)一步的講,你可以在CPUs, GPUs或者是 TPUs上,運(yùn)行 基于 Estimators 的模型而不需要重新編寫你的模型。
- Estimators 簡化共享執(zhí)行在模型開發(fā)者之間。
- 你可以使用高級直觀的代碼來開發(fā)藝術(shù)模型的一個狀態(tài)。簡短的說,用 Estimators 創(chuàng)建模型比用低級 TensorFlow APIs 簡單了太多太多。
- Estimators 是自創(chuàng)建在
tf.layers上的,這樣簡化了自定義化。 - Estimators 為你創(chuàng)建
graph,換句話說,你不用自己去創(chuàng)建graph - Estimators 提供了一個安全的分布式訓(xùn)練循環(huán):
- 創(chuàng)建
graph - 初始化變量
- 開始排隊(duì)
- 解決異常
- 創(chuàng)建
checkpoint文件和恢復(fù) - 為TensorBoard 保存概要
- 創(chuàng)建
當(dāng)用 Estimators 編寫一個應(yīng)用時,你必須把輸入數(shù)據(jù)管道從模型里分開。這一分離簡化了實(shí)驗(yàn)室使用的不同數(shù)據(jù)集。
預(yù)制的 Estimators
預(yù)制的 Estimators 能夠使你致力于更高的概念等級。你不再需要擔(dān)心創(chuàng)建計(jì)算圖或者 sessions,因?yàn)?Estimators 為你解決了所有的基礎(chǔ)工作。預(yù)制的 Estimators 為你創(chuàng)建并管理 Graph 和 Session 對象。進(jìn)一步講,預(yù)制的 Estimators 允許你用不同的模型架構(gòu)進(jìn)行試驗(yàn)通過最小的代碼修改。例如
DNNClassifier 是一個預(yù)制的 Estimator 類,它訓(xùn)練分類模型通過密集的,向前傳播神經(jīng)網(wǎng)絡(luò)。
一個預(yù)制的 Estimators 編程結(jié)構(gòu)
一個預(yù)制的 Estimator 編程過程典型的由接下來的四個步驟組成:
-
寫一個或者多個數(shù)據(jù)集導(dǎo)入方法。例如,你可能創(chuàng)建一個方法來導(dǎo)入訓(xùn)練數(shù)據(jù)集和另一個方法來導(dǎo)入測試數(shù)據(jù)集。每個數(shù)據(jù)集導(dǎo)入方法必須返還兩個對象:
- 一個 ‘keys’ 是 feature 名字和 ‘values’ 是 Tensors 的字典,其包含對應(yīng)的特征數(shù)據(jù)。
- 一個 Tensor 包含一個或者多個標(biāo)簽。
例如:下面的代碼為一個輸入方法展示一個基礎(chǔ)框架:
def input_fn(dataset):
... # manipulate dataset, extracting feature names and the label
return feature_dict, label
-
定義特征列。每個
tf.feature_column識別一個特征名稱,它的類型,和任何輸入預(yù)處理。例如,下面的代碼創(chuàng)建三個特征列它們保留著整數(shù)型或者浮點(diǎn)型數(shù)據(jù)。前兩個特征列簡單地識別特征的名字和類型。第三個特征列也調(diào)用一個指定的 lambda 方程來縮放原數(shù)據(jù)。
# 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')
-
實(shí)例化相關(guān)的預(yù)制 Estimators。例如:這里有個
LinearClassifier預(yù)制 Estimator 例子。
# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.Estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education],
)
-
調(diào)用 training, evaluation, 或者 inference 函數(shù)。例如:所有的 Estimators 提供一個
train方法來訓(xùn)練模型。
# my_training_set is the function created in Step 1
estimator.train(input_fn=my_training_set, steps=2000)
預(yù)制 Estimators 的好處
預(yù)制 Estimators 編碼的最優(yōu)方案,提供以下幾點(diǎn)好處:
- 最佳實(shí)踐于決定位于計(jì)算圖的不同的部分應(yīng)該運(yùn)行,實(shí)施策略在一個單一的機(jī)器或者集群。
- 最佳實(shí)踐于 event(summary) 寫入和有用的概括。
如果不用,你必須執(zhí)行這些特征通過你自己。
自定義 Estimators
無論是自定義還是預(yù)制的 Estimator,每個 Estimator 的中心點(diǎn)是它的 model function,它是一個方法為訓(xùn)練,評估和預(yù)測來創(chuàng)建圖。當(dāng)你正在使用一個預(yù)制的 Estimator,其他某個人已經(jīng)執(zhí)行模型方法。當(dāng)依賴一個自定義 Estimator,你必須自己寫這個模型方法。 這個指南文件解釋如何寫模型方程。
推薦的工作流程
我們推薦以下工作流程:
1. 假設(shè)存在合適的預(yù)制 Estimator,請使用它構(gòu)建你的第一個模型并使用其結(jié)果建立一個基線。
2. 使用此預(yù)制 Estimator 構(gòu)建和測試你的整體流程管道,包括你的數(shù)據(jù)的完整性和可靠性。
3. 如果有合適的替代預(yù)制 Estimator 可用,則運(yùn)行實(shí)驗(yàn)以確定哪種預(yù)制估算器能夠產(chǎn)生最佳結(jié)果。
4. 可能通過構(gòu)建您自己的自定義估算器來進(jìn)一步改進(jìn)您的模型。
從 Keras 模型創(chuàng)建 Estimators
你可以將現(xiàn)有的 Keras 模型轉(zhuǎn)換為 Estimators。 這樣做可以讓你的 Keras 模型訪問 Estimator 的優(yōu)點(diǎn),例如分布式訓(xùn)練。調(diào)用 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