Posted on

Tensorflow裡Estimator介紹

tf.estimator.Estimator 介紹

官方介紹頁面: https://www.tensorflow.org/guide/estimator
tf.estimator.Estimator tf.keras.Model 類似,estimator是模型級別的抽象。tf.estimator提供了一些目前仍在為 tf.keras 開發中的功能。包括:

  • 基於參數服務器的訓練
  • 完整的TFX集成

使用預製Estimator,能夠在比基礎TensorFlow API 高很多的概念層面上工作。您無需再擔心創建計算圖或會話,因為Estimator 會替您完成所有“基礎工作”。此外,使用預製Estimator,您只需改動較少代碼就能試驗不同的模型架構。例如,tf.estimator.DNNClassifier是一個預製Estimator 類,可基於密集的前饋神經網絡對分類模型進行訓練。

它提供了一種統一的方式來實現和使用各種不同類型的機器學習模型。它提供了一些預定義的模型類型,如線性迴歸、決策樹、KNN等,可以讓開發者更輕鬆的實現常見的機器學習模型。

主要功能

tf.estimator 是 TensorFlow 的一個高級 API,它提供了一種統一的方式來實現和使用各種不同類型的機器學習模型。它的主要用途是簡化建立、訓練和使用模型的流程,使開發人員能夠更輕鬆地實現常見的機器學習模型。

其主要功能如下:

  • 提供預定義的模型類型, 如線性迴歸,決策樹和KNN等
  • 提供一組工具,可以幫助您訓練,評估和預測模型
  • 提供一個統一的界面,使得用戶能夠更容易地訓練、評估和使用模型
  • 支持模型訓練和預測的分布式計算
  • 具有模型檢查点和恢復功能,可以將模型訓練過程中的狀態保存到磁盤上

優點

  • 提供了一個統一的界面,使開發人員能夠更輕鬆地實現常見的機器學習模型
  • 提供了一組工具,可以幫助您訓練,評估和預測模型
  • 支持模型訓練和預測的分布式計算
  • 具有模型檢查点和恢復功能,可以將模

在2.0裡被標註為棄用

在官網的API文件裡可以發現這個類別旁邊被註記了【棄用標籤】

在 TensorFlow 2.0 中,tf.estimator 被宣布即將棄用是因為它與 TensorFlow 的其他部分存在一些不一致性。隨著 TensorFlow 進化,其他部分,特別是 Keras API,已經成為了更簡單,更直接的機器學習模型構建選項。

Keras API 提供了一個統一的界面,可以用於創建和調用各種不同類型的模型,並且與 TensorFlow 的其他部分(如 TensorFlow 的低級計算圖 API)更加一致。

另外,Keras API 更加易於學習和使用,並且支持更多的高級功能,如自動微調。因此,TensorFlow 的開發團隊決定將重點轉移到 Keras API 上,並將 tf.estimator 標記為即將棄用。

學習 Keras API 更簡單,並且與其他 TensorFlow 特性更加一致,因此官方才會建議將重點轉向學習 Keras API。

使用Keras API來取代tf.estimator.Estimator的範例

tf.estimator.LinearRegressor 的使用範例

import tensorflow as tf

# Define the input function
def input_fn(features, labels, batch_size):
    dataset = tf.data.Dataset.from_tensor_slices((features, labels))
    dataset = dataset.shuffle(1000).batch(batch_size)
    return dataset

# Define the feature columns
feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]

# Define the LinearRegressor
regressor = tf.estimator.LinearRegressor(feature_columns=feature_columns)

# Define the training inputs
train_input_fn = lambda: input_fn(x_train, y_train, batch_size=batch_size)

# Train the model
regressor.train(input_fn=train_input_fn, steps=1000)

# Define the testing inputs
test_input_fn = lambda: input_fn(x_test, y_test, batch_size=batch_size)

# Evaluate the model
metrics = regressor.evaluate(input_fn=test_input_fn)
print("Mean squared error: ", metrics["average_loss"])

把上面的程式碼使用Keras API改寫

from tensorflow import keras

# Define the model
model = keras.Sequential()
model.add(keras.layers.Dense(1, input_shape=[1]))

# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')

# Train the model
model.fit(x_train, y_train, epochs=100)

在上面的程式碼之中,線性回歸的設定是來自於以下compile所設定的參數
model.compile(optimizer='sgd', loss='mean_squared_error')
使用 ‘sgd’ 作為優化器和 ‘mean_squared_error’ 作為損失函數是常用於實現線性迴歸模型的設定。

‘SGD’ 是隨機梯度下降的縮寫,它是一種優化算法,用於尋找最小化損失函數的權重值。’mean_squared_error’ 是一種常用的損失函數,用於計算預測值和實際值之間的差距。

在線性迴歸中, 我們嘗試找到一組權重和偏差, 使得預測值和實際值之間的差距最小, 而 mean_squared_error 是計算預測值和實際值之間差距的常用方法, 所以使用 mean_squared_error 作為損失函數是適當的.

tf.estimator.train_and_evaluate

tf.estimator.train_and_evaluate是TensorFlow Estimator API 中的一個函數,用於在訓練和評估模型時自動管理訓練循環。

它接受兩個參數:一個是估計器對象,另一個是一個字典,其中包含訓練和評估所需的所有配置信息。

在訓練期間,該函數會自動調用估計器的train() 方法,並在訓練過程中記錄訓練步驟和損失值。在評估期間,該函數會自動調用估計器的evaluate() 方法,並在評估過程中記錄評估指標。

通過使用tf.estimator.train_and_evaluate可以簡化訓練和評估模型的流程,避免手動管理訓練循環和評估過程中的各種細節。這樣可以讓開發人員專注於模型的構建和調優,而不必擔心訓練和評估的細節實現。

使用示例如下:

# Define the estimator
estimator = tf.estimator.LinearClassifier(feature_columns=feature_columns)

# Define the training and evaluation specs
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=1000)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)

# Train and evaluate the estimator
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)

可以使用Keras API 來使用tf.estimator.train_and_evaluate。
使用TensorFlow 的Keras API 和Estimator API 來構建模型時,可以使用 tf.keras.estimator.model_to_estimator() 函數將Keras 模型轉換為Estimator,然後使用 tf.estimator.train_and_evaluate() 函數訓練和評估模型。

示例如下:

import tensorflow as tf
from tensorflow import keras

# Define the Keras model
def build_model():
    model = keras.Sequential([
        keras.layers.Dense(64, activation='relu', input_shape=(784,)),
        keras.layers.Dense(64, activation='relu'),
        keras.layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer=tf.keras.optimizers.Adam(),
                  loss=tf.keras.losses.categorical_crossentropy,
                  metrics=[tf.keras.metrics.categorical_accuracy])
    return model

# Convert the Keras model to an Estimator
estimator = tf.keras.estimator.model_to_estimator(keras_model=build_model())

# Define the input function for training
def train_input_fn():
    pass  # return training data

# Define the input function for evaluation
def eval_input_fn():
    pass  # return evaluation data

# Train and evaluate the Estimator
tf.estimator.train_and_evaluate(estimator,
                                train_spec=tf.estimator.TrainSpec(train_input_fn),
                                eval_spec=tf.estimator.EvalSpec(eval_input_fn))