Estimator:一種可極大地簡化機器學習程式設計的高階 TensorFlow API
今天的內容要向大家介紹 Estimator (https://tensorflow.google.cn/api_docs/python/tf/estimator?hl=zh-CN) - 一種可極大地簡化機器學習程式設計的高階 TensorFlow API 。 Estimator 會封裝下列操作:
-
訓練
-
評估
-
預測
-
匯出以供使用
您可以使用我們提供的預建立的 Estimator,也可以編寫自定義 Estimator。所有 Estimator(無論是預建立的還是自定義)都是基於 tf.estimator.Estimator 類的類 (https://tensorflow.google.cn/api_docs/python/tf/estimator/Estimator?hl=zh-CN) 。
注意:TensorFlow 還包含一個已棄用的 Estimator 類 tf.contrib.learn.Estimator (https://tensorflow.google.cn/api_docs/python/tf/contrib/learn/Estimator?hl=zh-CN) ,您不應該使用此類。
Estimator 的優勢
Estimator 具有下列優勢:
-
您可以在本地主機上或分散式多伺服器環境中執行基於 Estimator 的模型,而無需更改模型。此外,您可以在 CPU、GPU 或 TPU 上執行基於 Estimator 的模型,而無需重新編碼模型。
-
Estimator 簡化了在模型開發者之間共享實現的過程。
-
您可以使用高階直觀程式碼開發先進的模型。簡言之,採用 Estimator 建立模型通常比採用低階 TensorFlow API 更簡單。
-
Estimator 本身在 tf.layers 之上構建而成 (https://tensorflow.google.cn/api_docs/python/tf/layers?hl=zh-CN) ,可以簡化自定義過程。
-
Estimator 會為您構建圖。
-
Estimator 提供安全的分散式訓練迴圈,可以控制如何以及何時:
-
構建圖
-
初始化變數
-
開始排隊
-
處理異常
-
建立檢查點檔案並從故障中恢復
-
儲存 TensorBoard 的摘要
使用 Estimator 編寫應用時,您必須將資料輸入管道從模型中分離出來。這種分離簡化了不同資料集的實驗流程。
預建立的 Estimator
藉助預建立的 Estimator,您能夠在比基本 TensorFlow API 高階很多的概念層面上進行操作。由於 Estimator 會為您處理所有 “ 管道工作 ”,因此您不必再為建立計算圖或會話而操心。也就是說,預建立的 Estimator 會為您建立和管理 Graph (https://tensorflow.google.cn/api_docs/python/tf/Graph?hl=zh-CN) 和 Session (https://tensorflow.google.cn/api_docs/python/tf/Session?hl=zh-CN) 物件。此外,藉助預建立的 Estimator,您只需稍微更改下程式碼,就可以嘗試不同的模型架構。例如, DNNClassifier 是一個預建立的 Estimator 類 (https://tensorflow.google.cn/api_docs/python/tf/estimator/DNNClassifier?hl=zh-CN) ,它根據密集的前饋神經網路訓練分類模型。
預建立的 Estimator 程式的結構
依賴預建立的 Estimator 的 TensorFlow 程式通常包含下列四個步驟:
編寫一個或多個數據集匯入函式。
例如,您可以建立一個函式來匯入訓練集,並建立另一個函式來匯入測試集。每個資料集匯入函式都必須返回兩個物件 :
-
一個字典,其中鍵是特徵名稱,值是包含相應特徵資料的張量(或 SparseTensor)
-
一個包含一個或多個標籤的張量
例如,以下程式碼展示了輸入函式的基本框架:
def input_fn(dataset):
... # manipulate dataset, extracting the feature dict and the label
return feature_dict, label
(要了解完整的詳細資訊,請參閱 匯入資料 (https://tensorflow.google.cn/guide/datasets?hl=zh-CN) 。)
定義特徵列。
每個 tf.feature_column (https://tensorflow.google.cn/api_docs/python/tf/feature_column?hl=zh-CN) 都標識了特徵名稱、特徵型別和任何輸入預處理操作。例如,以下程式碼段建立了三個儲存整數或浮點資料的特徵列。前兩個特徵列僅標識了特徵的名稱和型別。第三個特徵列還指定了一個 lambda,該程式將呼叫此 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)
例項化相關的預建立的 Estimator。
例如,下面是對名為 LinearClassifier 的預建立 Estimator 進行例項化的示例程式碼:
# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education],
)
呼叫訓練、評估或推理方法。
例如,所有 Estimator 都提供訓練模型的 train 方法。
# my_training_set is the function created in Step 1
estimator.train(input_fn=my_training_set, steps=2000)
預建立的 Estimator 的優勢
預建立的 Estimator 會編碼最佳做法,從而具有下列優勢:
-
確定計算圖不同部分的執行位置以及在單臺機器或多臺機器上實現策略的最佳做法。
-
事件(彙總)編寫和普遍有用的彙總的最佳做法。
如果您不使用預建立的 Estimator,則必須自行實現上述功能。
自定義 Estimator
每個 Estimator(無論是預建立還是自定義)的核心都是其模型函式,這是一種為訓練、評估和預測構建圖的方法。如果您使用預建立的 Estimator,則有人已經實現了模型函式。如果您使用自定義 Estimator,則必須自行編寫模型函式。 隨附文件 介紹瞭如何編寫模型函式 ( https://tensorflow.google.cn/guide/custom_estimators?hl=zh-CN) 。
推薦的工作流程
我們推薦以下工作流程:
-
假設存在合適的預建立的 Estimator,使用它構建第一個模型並使用其結果確定基準。
-
使用此預建立的 Estimator 構建和測試整體管道,包括資料的完整性和可靠性。
-
如果存在其他合適的預建立的 Estimator,則執行實驗來確定哪個預建立的 Estimator 效果最好。
-
可以通過構建自定義 Estimator 進一步改進模型。
從 Keras 模型建立 Estimator
您可以將現有的 Keras 模型轉換為 Estimator。這樣做之後,Keras 模型就可以利用 Estimator 的優勢,例如分散式訓練。呼叫
tf.keras.estimator.model_to_estimator (https://tensorflow.google.cn/api_docs/python/tf/keras/estimator/model_to_estimator?hl=zh-CN) ,如下例所示:
# 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.compat.v1.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)
請注意,Keras Estimator 的特徵列名稱和標籤來自經過編譯的對應 Keras 模型。例如,上面的 train_input_fn 的輸入鍵名稱可以從 keras_inception_v3.input_names 獲得;同樣,預測的輸出名稱可以從 keras_inception_v3.output_names 獲得。
要了解詳情,請參閱 tf.keras.estimator.model_to_estimator 的文件 (https://tensorflow.google.cn/api_docs/python/tf/keras/estimator/model_to_estimator?hl=zh-CN) 。
更多 AI 相關閱讀:
-
ofollow,noindex"> TensorFlow 不僅用於機器學習,還能模擬偏微分方程