如何使用 Estimators 最佳化 TensorFlow 模型?
與泰坦尼克號資料集關聯的模型可以在新增特定列後進行最佳化以獲得更好的效能。新增列、訓練並評估模型後,模型將得到簡單的最佳化,從而提高效能。
閱讀更多: 什麼是 TensorFlow,以及 Keras 如何與 TensorFlow 一起建立神經網路?
我們將使用 Keras Sequential API,它有助於構建一個順序模型,用於處理簡單的層堆疊,其中每一層只有一個輸入張量和一個輸出張量。
包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。
Estimator 是 TensorFlow 中對完整模型的高階表示。它旨在易於擴充套件和非同步訓練。Estimator 使用特徵列來描述模型如何解釋原始輸入特徵。Estimator 期望一個數值輸入向量,特徵列將有助於描述模型應該如何轉換資料集中每個特徵。
示例
print("The model is optmised to make predictions on a dataset")
pred_dicts = list(linear_est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])
print("The data is plotted")
probs.plot(kind='hist', bins=20, title='predicted probabilities')程式碼來源 −https://www.tensorflow.org/tutorials/estimator/linear
輸出
The model is optmised to make predictions on a dataset
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:Layer linear/linear_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx.
If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.
To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpg17o3o7e/model.ckpt-200
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
<matplotlib.axes._subplots.AxesSubplot at 0x7f8e2c1dd358>
The data is plotted
解釋
達到了 77.6% 的準確率,這比基本特徵訓練的更好。
可以應用更多特徵和轉換,看看模型的表現是否更好。
該模型經過訓練,可以對評估集中的一名乘客進行預測。
TensorFlow 模型經過最佳化,可以一次對批次或多個示例進行預測。
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP