如何使用 Estimators 和 Tensorflow 透過 Python 評估模型?
Tensorflow 可以與評估器一起使用,透過 `classifier` 模組中的 `evaluate` 方法來評估模型。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
我們將使用 Keras Sequential API,它有助於構建一個順序模型,用於處理簡單的層堆疊,其中每一層都只有一個輸入張量和一個輸出張量。
包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路構建學習模型。
TensorFlow Text 包含與文字相關的類和操作的集合,可用於 TensorFlow 2.0。TensorFlow Text 可用於預處理序列建模。
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 基於 Jupyter Notebook 構建。
評估器是 TensorFlow 對完整模型的高階表示。它旨在實現輕鬆擴充套件和非同步訓練。
模型使用鳶尾花資料集進行訓練。
示例
eval_result = classifier.evaluate(input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest dataset accuracy is: {accuracy:0.3f}\n'.format(**eval_result))程式碼來源 -https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
輸出
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:Layer dnn 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:Starting evaluation at 2020-09-10T01:40:47Z
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpbhg2uvbr/model.ckpt-5000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Inference Time : 0.21153s
INFO:tensorflow:Finished evaluation at 2020-09-10-01:40:47
INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.96666664, average_loss = 0.42594802, global_step = 5000, loss = 0.42594802
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpbhg2uvbr/model.ckpt-5000
Test dataset accuracy is: 0.967解釋
模型訓練完成後,可以獲取一些關於效能的資訊。
沒有引數傳遞給 `evaluate` 函式。
用於評估的 `input_fn` 僅產生一個 epoch 的資料。
`eval_result` 字典包含 `average_loss`(每個樣本的平均損失)、`loss`(每個小批次的平均損失)和評估器 `global_step` 的值(它經歷的訓練迭代次數)。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP