# 生產環境 TensorFlow 的一個例子
生產機器學習模型的一個好方法是將訓練和評估程序分開。在本節中,我們將說明一個評估腳本,該腳本已經擴展到包括單元測試,模型保存和加載以及評估。
## 做好準備
在本文中,我們將向您展示如何使用上述標準實現評估腳本。代碼實際上包含一個訓練腳本和一個評估腳本,但是對于這個秘籍,我們只會向您展示評估腳本。提醒一下,兩個腳本都可以在 [](https://github.com/nfmcclure/tensorflow_cookbook/) [https://github.com/nfmcclure/tensorflow_cookbook/](https://github.com/nfmcclure/tensorflow_cookbook/) 的在線 GitHub 倉庫和官方 Packt 倉庫中看到: [https ://github.com/PacktPublishing/TensorFlow-Machine-Learning-Cookbook-Second-Edition](https://github.com/PacktPublishing/TensorFlow-Machine-Learning-Cookbook-Second-Edition) 。
對于即將到來的示例,我們將實現[第 9 章](../Text/68.html),回歸神經網絡中的第一個 RNN 示例,該示例試圖預測文本消息是垃圾郵件還是火腿。我們將假設 RNN 模型與詞匯一起被訓練和保存。
## 操作步驟
1. 首先,我們首先加載必要的庫并聲明 TensorFlow 應用標志,如下所示:
```py
import os
import re
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Define App Flags
tf.flags.DEFINE_string("storage_folder", "temp", "Where to store model and data.")
tf.flags.DEFINE_float('learning_rate', 0.0005, 'Initial learning rate.')
tf.flags.DEFINE_float('dropout_prob', 0.5, 'Per to keep probability for dropout.')
tf.flags.DEFINE_integer('epochs', 20, 'Number of epochs for training.')
tf.flags.DEFINE_integer('batch_size', 250, 'Batch Size for training.')
tf.flags.DEFINE_integer('rnn_size', 15, 'RNN feature size.')
tf.flags.DEFINE_integer('embedding_size', 25, 'Word embedding size.')
tf.flags.DEFINE_integer('min_word_frequency', 20, 'Word frequency cutoff.')
tf.flags.DEFINE_boolean('run_unit_tests', False, 'If true, run tests.')
FLAGS = tf.flags.FLAGS
```
1. 接下來,我們聲明一個文本清理函數。這與訓練腳本中使用的清潔函數相同,如下所示:
```py
def clean_text(text_string):
text_string = re.sub(r'([^sw]|_|[0-9])+', '', text_string)
text_string = " ".join(text_string.split())
text_string = text_string.lower()
return text_string
```
1. 現在,我們需要加載以下詞匯處理函數:
```py
def load_vocab():
vocab_path = os.path.join(FLAGS.storage_folder, "vocab")
vocab_processor = tf.contrib.learn.preprocessing.VocabularyProcessor.restore(vocab_path)
return vocab_processor
```
1. 現在我們有了清理文本的方法,并且還有一個詞匯處理器,我們可以將這些函數組合起來為給定的文本創建數據處理管道,如下所示:
```py
def process_data(input_data, vocab_processor):
input_data = clean_text(input_data)
input_data = input_data.split()
processed_input = np.array(list(vocab_processor.transform(input_data)))
return processed_input
```
1. 接下來,我們需要一種方法來獲取要評估的數據。為此,我們將要求用戶在屏幕上鍵入文本。然后,我們將處理文本并返回以下處理過的文本:
```py
def get_input_data():
input_text = input("Please enter a text message to evaluate: ")
vocab_processor = load_vocab()
return process_data(input_text, vocab_processor)
```
> 對于此示例,我們通過要求用戶鍵入來創建評估數據。雖然許多應用將通過提供的文件或 API 請求獲取數據,但我們可以相應地更改此輸入數據函數。
1. 對于單元測試,我們需要使用以下代碼確保我們的文本清理函數正常運行:
```py
class clean_test(tf.test.TestCase):
# Make sure cleaning function behaves correctly
def clean_string_test(self):
with self.test_session():
test_input = '--Tensorflow's so Great! Dont you think so? '
test_expected = 'tensorflows so great don you think so'
test_out = clean_text(test_input)
self.assertEqual(test_expected, test_out)
```
1. 現在我們有了模型和數據,我們可以運行`main`函數。 `main`函數將獲取數據,設置圖,加載變量,輸入處理過的數據,然后打印輸出,如下面的代碼片段所示:
```py
def main(args):
# Get flags
storage_folder = FLAGS.storage_folder
# Get user input text
x_data = get_input_data()
# Load model
graph = tf.Graph()
with graph.as_default():
sess = tf.Session()
with sess.as_default():
# Load the saved meta graph and restore variables
saver = tf.train.import_meta_graph("{}.meta".format(os.path.join(storage_folder, "model.ckpt")))
saver.restore(sess, os.path.join(storage_folder, "model.ckpt"))
# Get the placeholders from the graph by name
x_data_ph = graph.get_operation_by_name("x_data_ph").outputs[0]
dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
probability_outputs = graph.get_operation_by_name("probability_outputs").outputs[0]
# Make the prediction
eval_feed_dict = {x_data_ph: x_data, dropout_keep_prob: 1.0}
probability_prediction = sess.run(tf.reduce_mean(probability_outputs, 0), eval_feed_dict)
# Print output (Or save to file or DB connection?)
print('Probability of Spam: {:.4}'.format(probability_prediction[1]))
```
1. 最后,要運行`main()`函數或單元測試,請使用以下代碼:
```py
if __name__ == "__main__":
if FLAGS.run_unit_tests:
# Perform unit tests
tf.test.main()
else:
# Run evaluation
tf.app.run()
```
## 工作原理
為了評估模型,我們能夠使用 TensorFlow 的 app 標志加載命令行參數,加載模型和詞匯處理器,然后通過模型運行處理過的數據并進行預測。
請記住通過命令行運行此腳本,并在創建模型和詞匯表字典之前檢查是否運行了訓練腳本。
- TensorFlow 入門
- 介紹
- TensorFlow 如何工作
- 聲明變量和張量
- 使用占位符和變量
- 使用矩陣
- 聲明操作符
- 實現激活函數
- 使用數據源
- 其他資源
- TensorFlow 的方式
- 介紹
- 計算圖中的操作
- 對嵌套操作分層
- 使用多個層
- 實現損失函數
- 實現反向傳播
- 使用批量和隨機訓練
- 把所有東西結合在一起
- 評估模型
- 線性回歸
- 介紹
- 使用矩陣逆方法
- 實現分解方法
- 學習 TensorFlow 線性回歸方法
- 理解線性回歸中的損失函數
- 實現 deming 回歸
- 實現套索和嶺回歸
- 實現彈性網絡回歸
- 實現邏輯回歸
- 支持向量機
- 介紹
- 使用線性 SVM
- 簡化為線性回歸
- 在 TensorFlow 中使用內核
- 實現非線性 SVM
- 實現多類 SVM
- 最近鄰方法
- 介紹
- 使用最近鄰
- 使用基于文本的距離
- 使用混合距離函數的計算
- 使用地址匹配的示例
- 使用最近鄰進行圖像識別
- 神經網絡
- 介紹
- 實現操作門
- 使用門和激活函數
- 實現單層神經網絡
- 實現不同的層
- 使用多層神經網絡
- 改進線性模型的預測
- 學習玩井字棋
- 自然語言處理
- 介紹
- 使用詞袋嵌入
- 實現 TF-IDF
- 使用 Skip-Gram 嵌入
- 使用 CBOW 嵌入
- 使用 word2vec 進行預測
- 使用 doc2vec 進行情緒分析
- 卷積神經網絡
- 介紹
- 實現簡單的 CNN
- 實現先進的 CNN
- 重新訓練現有的 CNN 模型
- 應用 StyleNet 和 NeuralStyle 項目
- 實現 DeepDream
- 循環神經網絡
- 介紹
- 為垃圾郵件預測實現 RNN
- 實現 LSTM 模型
- 堆疊多個 LSTM 層
- 創建序列到序列模型
- 訓練 Siamese RNN 相似性度量
- 將 TensorFlow 投入生產
- 介紹
- 實現單元測試
- 使用多個執行程序
- 并行化 TensorFlow
- 將 TensorFlow 投入生產
- 生產環境 TensorFlow 的一個例子
- 使用 TensorFlow 服務
- 更多 TensorFlow
- 介紹
- 可視化 TensorBoard 中的圖
- 使用遺傳算法
- 使用 k 均值聚類
- 求解常微分方程組
- 使用隨機森林
- 使用 TensorFlow 和 Keras