<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 用于 MNIST 分類的基于 TensorFlow 的 MLP 首先,加載 MNIST 數據集,并使用以下代碼定義訓練和測試特征以及目標: ```py from tensorflow.examples.tutorials.mnist import input_data mnist_home = os.path.join(datasetslib.datasets_root, 'mnist') mnist = input_data.read_data_sets(mnist_home, one_hot=True) X_train = mnist.train.images X_test = mnist.test.images Y_train = mnist.train.labels Y_test = mnist.test.labels num_outputs = 10 # 0-9 digits num_inputs = 784 # total pixels ``` 我們創建了三個輔助函數,它們將幫助我們創建一個只有一個隱藏層的簡單 MLP,然后是一個更大的 MLP,每層有多個層和多個神經元。 `mlp()`函數使用以下邏輯構建網絡層: 1. `mlp()`函數需要五個輸入: * `x`是輸入特征張量 * `num_inputs`是輸入特征的數量 * `num_outputs`是輸出目標的數量 * `num_layers`是所需隱藏層數 * `num_neurons`是包含每層神經元數量的列表 2. 將權重和偏差列表設置為空: ```py w=[] b=[] ``` 1. 為隱藏層的數量運行循環以創建權重和偏移張量并將它們附加到各自的列表: * 張量分別名為`w_<layer_num>`和`b_<layer_num>`。命名張量有助于調試和查找代碼問題。 * 使用`tf.random_normal()`以正態分布初始化張量。 * 權重張量的第一個維度是來自前一層的輸入數量。對于第一個隱藏層,第一個維度是`num_inputs`。權重張量的第二維是當前層中的神經元的數量。 * 偏差都是一維張量,其中維度等于當前層中的神經元數量。 ```py for i in range(num_layers): # weights w.append(tf.Variable(tf.random_normal( [num_inputs if i == 0 else num_neurons[i - 1], num_neurons[i]]), name="w_{0:04d}".format(i) )) # biases b.append(tf.Variable(tf.random_normal( [num_neurons[i]]), name="b_{0:04d}".format(i) )) ``` 1. 為最后一個隱藏層創建權重和偏差。在這種情況下,權重張量的維度等于最后隱藏層中的神經元數量和輸出目標的數量。偏差是一個張量,具有輸出特征數量大小的單一維度: ```py w.append(tf.Variable(tf.random_normal( [num_neurons[num_layers - 1] if num_layers > 0 else num_inputs, num_outputs]), name="w_out")) b.append(tf.Variable(tf.random_normal([num_outputs]), name="b_out")) ``` 1. 現在開始定義層。首先,將`x`視為第一個最明顯的輸入層: ```py # x is input layer layer = x ``` 1. 在循環中添加隱藏的層。每個隱藏層表示通過激活函數`tf.nn.relu()`使線性函數`tf.matmul(layer, w[i]) + b[i]`非線性化: ```py # add hidden layers for i in range(num_layers): layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i]) ``` 1. 添加輸出層。輸出層和隱藏層之間的一個區別是輸出層中沒有激活函數: ```py layer = tf.matmul(layer, w[num_layers]) + b[num_layers] ``` 1. 返回包含 MLP 網絡的`layer`對象: ```py return layer ``` 整個 MLP 函數的完整代碼如下: ```py def mlp(x, num_inputs, num_outputs, num_layers, num_neurons): w = [] b = [] for i in range(num_layers): # weights w.append(tf.Variable(tf.random_normal( [num_inputs if i == 0 else num_neurons[i - 1], num_neurons[i]]), name="w_{0:04d}".format(i) )) # biases b.append(tf.Variable(tf.random_normal( [num_neurons[i]]), name="b_{0:04d}".format(i) )) w.append(tf.Variable(tf.random_normal( [num_neurons[num_layers - 1] if num_layers > 0 else num_inputs, num_outputs]), name="w_out")) b.append(tf.Variable(tf.random_normal([num_outputs]), name="b_out")) # x is input layer layer = x # add hidden layers for i in range(num_layers): layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i]) # add output layer layer = tf.matmul(layer, w[num_layers]) + b[num_layers] return layer ``` 輔助函數`mnist_batch_func()`為 MNIST 數據集包裝 TensorFlow 的批量函數,以提供下一批圖像: ```py def mnist_batch_func(batch_size=100): X_batch, Y_batch = mnist.train.next_batch(batch_size) return [X_batch, Y_batch] ``` 此函數不言自明。 TensorFlow 為 MNIST 數據集提供此函數;但是,對于其他數據集,我們可能必須編寫自己的批量函數。 輔助函數`tensorflow_classification()`訓練并評估模型。 1. `tensorflow_classification()` 函數有幾個輸入: * * `n_epochs`是要運行的訓練循環的數量 * `n_batches`是應該運行每個循環中的訓練的隨機抽樣批次的數量 * `batch_size`是每批中的樣品數 * `batch_func`是`batch_size`并返回`X`和`Y`樣本批次的函數 * `model`是具有神經元的實際神經網絡或層 * `optimizer`是使用 TensorFlow 定義的優化函數 * `loss`是優化器優化參數的成本函數損失 * `accuracy_function`是計算準確率分數的函數 * `X_test`和`Y_test`是測試的數據集 1. 啟動 TensorFlow 會話以運行訓練循環: ```py with tf.Session() as tfs: tf.global_variables_initializer().run() ``` 1. 運行`n_epoch`循環的訓練: ```py for epoch in range(n_epochs): ``` 1. 在每個循環中,取樣本集的`n_batches`數量并訓練模型,計算每批的損失,計算每個周期的平均損失: ```py epoch_loss = 0.0 for batch in range(n_batches): X_batch, Y_batch = batch_func(batch_size) feed_dict = {x: X_batch, y: Y_batch} _, batch_loss = tfs.run([optimizer, loss], feed_dict) epoch_loss += batch_loss average_loss = epoch_loss / n_batches print("epoch: {0:04d} loss = {1:0.6f}".format( epoch, average_loss)) ``` 1. 完成所有周期循環后,計算并打印用`accuracy_function`計算的精度分數: ```py feed_dict = {x: X_test, y: Y_test} accuracy_score = tfs.run(accuracy_function, feed_dict=feed_dict) print("accuracy={0:.8f}".format(accuracy_score)) ``` `tensorflow_classification()`函數的完整代碼如下: ```py def tensorflow_classification(n_epochs, n_batches, batch_size, batch_func, model, optimizer, loss, accuracy_function, X_test, Y_test): with tf.Session() as tfs: tfs.run(tf.global_variables_initializer()) for epoch in range(n_epochs): epoch_loss = 0.0 for batch in range(n_batches): X_batch, Y_batch = batch_func(batch_size) feed_dict = {x: X_batch, y: Y_batch} _, batch_loss = tfs.run([optimizer, loss], feed_dict) epoch_loss += batch_loss average_loss = epoch_loss / n_batches print("epoch: {0:04d} loss = {1:0.6f}".format( epoch, average_loss)) feed_dict = {x: X_test, y: Y_test} accuracy_score = tfs.run(accuracy_function, feed_dict=feed_dict) print("accuracy={0:.8f}".format(accuracy_score)) ``` 現在讓我們定義輸入和輸出占位符,`x`和`y`以及其他超參數: ```py # input images x = tf.placeholder(dtype=tf.float32, name="x", shape=[None, num_inputs]) # target output y = tf.placeholder(dtype=tf.float32, name="y", shape=[None, num_outputs]) num_layers = 0 num_neurons = [] learning_rate = 0.01 n_epochs = 50 batch_size = 100 n_batches = int(mnist.train.num_examples/batch_size) ``` 參數如下所述: * `num_layers`是隱藏層數。我們首先練習沒有隱藏層,只有輸入和輸出層。 * `num_neurons`是空列表,因為沒有隱藏層。 * `learning_rate`是 0.01,隨機選擇的小數。 * `num_epochs`代表 50 次迭代,以學習將輸入連接到輸出的唯一神經元的參數。 * `batch_size`保持在 100,這也是一個選擇問題。較大的批量大小不一定提供更高的好處。您可能需要探索不同的批量大小,以找到神經網絡的最佳批量大小。 * `n_batches`:批次數大致計算為示例數除以批次中的樣本數。 現在讓我們將所有內容放在一起,使用到目前為止定義的變量定義網絡,`loss`函數,`optimizer`函數和`accuracy`函數。 ```py model = mlp(x=x, num_inputs=num_inputs, num_outputs=num_outputs, num_layers=num_layers, num_neurons=num_neurons) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=y)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) predictions_check = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1)) accuracy_function = tf.reduce_mean(tf.cast(predictions_check, tf.float32)) ``` 在這段代碼中,我們使用一個新的 tensorflow 函數來定義損失函數: ```py tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=y) ``` 當使用`softmax_cross_entropy_with_logits()`函數時,請確保輸出未縮放且尚未通過`softmax`激活函數。 此函數在內部使用`softmax`來縮放輸出。 該函數計算模型之間的 softmax 熵(估計值`y`)和`y`的實際值。當輸出屬于一個類而不是一個類時,使用熵函數。在我們的示例中,圖像只能屬于其中一個數字。 有關此熵函數的更多信息,請參閱?[https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits](https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits). 一旦定義了所有內容,運行 `tensorflow_classification` 函數來訓練和評估模型: ```py tensorflow_classification(n_epochs=n_epochs, n_batches=n_batches, batch_size=batch_size, batch_func=mnist_batch_func, model = model, optimizer = optimizer, loss = loss, accuracy_function = accuracy_function, X_test = mnist.test.images, Y_test = mnist.test.labels ) ``` 我們從運行分類得到以下輸出: ```py epoch: 0000 loss = 8.364567 epoch: 0001 loss = 4.347608 epoch: 0002 loss = 3.085622 epoch: 0003 loss = 2.468341 epoch: 0004 loss = 2.099220 epoch: 0005 loss = 1.853206 --- Epoch 06 to 45 output removed for brevity --- epoch: 0046 loss = 0.684285 epoch: 0047 loss = 0.678972 epoch: 0048 loss = 0.673685 epoch: 0049 loss = 0.668717 accuracy=0.85720009 ``` 我們看到單個神經元網絡在 50 次迭代中緩慢地將損失從 8.3 降低到 0.66,最終得到幾乎 85%的準確率。對于這個具體的例子,這是非常糟糕的準確性,因為這只是使用 TensorFlow 進行分類使用 MLP 的演示。 我們使用更多層和神經元運行相同的代碼,并獲得以下準確性: | 層數 | 每個隱藏層中的神經元數量 | 準確性 | | --- | --- | --- | | 0 | 0 | 0.857 | | 1 | 8 | 0.616 | | 2 | 256 | 0.936 | 因此,通過在每層添加兩行和 256 個神經元,我們將精度提高到 0.936。我們鼓勵您嘗試使用不同變量值的代碼來觀察它如何影響損失和準確性。
                  <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>

                              哎呀哎呀视频在线观看