<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 生產環境 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 標志加載命令行參數,加載模型和詞匯處理器,然后通過模型運行處理過的數據并進行預測。 請記住通過命令行運行此腳本,并在創建模型和詞匯表字典之前檢查是否運行了訓練腳本。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看