TensorFlow2.0程式碼實戰專欄(五):神經網路示例

  • 2019 年 12 月 11 日
  • 筆記

作者 | Aymeric Damien

編輯 | 奇予紀

出品 | 磐創AI團隊

原項目 | https://github.com/aymericdamien/TensorFlow-Examples/

AI學習路線之TensorFlow篇

神經網路示例

使用TensorFlow v2構建一個兩層隱藏層完全連接的神經網路(多層感知器)。這個例子使用低級方法來更好地理解構建神經網路和訓練過程背後的所有機制。

神經網路概述:

MNIST數據集概述:

此示例使用手寫數字的MNIST數據集。該數據集包含60,000個用於訓練的示例和10,000個用於測試的示例。這些數字已經過尺寸標準化並位於影像中心,影像是固定大小(28×28像素),值為0到255。

在此示例中,每個影像將轉換為float32並歸一化為[0,1],並展平為784個特徵的一維數組(28 * 28)

更多資訊請查看鏈接: http://yann.lecun.com/exdb/mnist/

from __future__ import absolute_import, division, print_function    import tensorflow as tf  from tensorflow.keras import Model, layers  import numpy as np  
# MNIST 數據集參數  num_classes = 10 # 所有類別(數字 0-9)  num_features = 784 # 數據特徵數目 (影像形狀: 28*28)    # 訓練參數  learning_rate = 0.001  training_steps = 2000  batch_size = 256  display_step = 100    # 網路參數  n_hidden_1 = 128 # 第一層隱含層神經元的數目  n_hidden_2 = 256 # 第二層隱含層神經元的數目  
# 準備MNIST數據  from tensorflow.keras.datasets import mnist  (x_train, y_train), (x_test, y_test) = mnist.load_data()  # 轉化為float32  x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)  # 將每張影像展平為具有784個特徵的一維向量(28 * 28)  x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])  # 將影像值從[0,255]歸一化到[0,1]  x_train, x_test = x_train / 255., x_test / 255.  
# 使用tf.data API對數據進行隨機排序和批處理  train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))  train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)  
# 創建 TF 模型  class NeuralNet(Model):      # 設置層      def __init__(self):          super(NeuralNet, self).__init__()          # 第一層全連接隱含層          self.fc1 = layers.Dense(n_hidden_1, activation=tf.nn.relu)          # 第二層全連接隱含層          self.fc2 = layers.Dense(n_hidden_2, activation=tf.nn.relu)          # 全連接輸出層          self.out = layers.Dense(num_classes, activation=tf.nn.softmax)        # 設置前向傳播      def call(self, x, is_training=False):          x = self.fc1(x)          x = self.out(x)          if not is_training:              # tf交叉熵期望輸出沒有softmax,所以只有              #不訓練時使用softmax。              x = tf.nn.softmax(x)          return x    # 構建神經網路模型  neural_net = NeuralNet()  
# 交叉熵損失  # 注意這將會對輸使用'softmax'  def cross_entropy_loss(x, y):        # 將標籤轉化為int64以使用交叉熵函數      y = tf.cast(y, tf.int64)      #將softmax應用於輸出並計算交叉熵。      loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=x)      # 批的平均損失。      return tf.reduce_mean(loss)    # 準確率評估  def accuracy(y_pred, y_true):      # 預測類是預測向量中最高分的索引(即argmax)      correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))      return tf.reduce_mean(tf.cast(correct_prediction, tf.float32), axis=-1)    # 隨機梯度下降優化器  optimizer = tf.optimizers.SGD(learning_rate)  
# 優化過程  def run_optimization(x, y):     # 將計算封裝在GradientTape中以實現自動微分      with tf.GradientTape() as g:          # 前向傳播          pred = neural_net(x, is_training=True)          # 計算損失          loss = cross_entropy_loss(pred, y)        # 要更新的變數,即可訓練的變數      trainable_variables = neural_net.trainable_variables        # 計算梯度      gradients = g.gradient(loss, trainable_variables)        # 按gradients更新 W 和 b      optimizer.apply_gradients(zip(gradients, trainable_variables))  
# 針對給定步驟數進行訓練  for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):      # 運行優化以更新W和b值      run_optimization(batch_x, batch_y)        if step % display_step == 0:          pred = neural_net(batch_x, is_training=True)          loss = cross_entrop            ···y_loss(pred, batch_y)          acc = accuracy(pred, batch_y)          print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))  
step: 100, loss: 2.031049, accuracy: 0.535156  step: 200, loss: 1.821917, accuracy: 0.722656  step: 300, loss: 1.764789, accuracy: 0.753906  step: 400, loss: 1.677593, accuracy: 0.859375  step: 500, loss: 1.643402, accuracy: 0.867188  step: 600, loss: 1.645116, accuracy: 0.859375  step: 700, loss: 1.618012, accuracy: 0.878906  step: 800, loss: 1.618097, accuracy: 0.878906  step: 900, loss: 1.616565, accuracy: 0.875000  step: 1000, loss: 1.599962, accuracy: 0.894531  step: 1100, loss: 1.593849, accuracy: 0.910156  step: 1200, loss: 1.594491, accuracy: 0.886719  step: 1300, loss: 1.622147, accuracy: 0.859375  step: 1400, loss: 1.547483, accuracy: 0.937500  step: 1500, loss: 1.581775, accuracy: 0.898438  step: 1600, loss: 1.555893, accuracy: 0.929688  step: 1700, loss: 1.578076, accuracy: 0.898438  step: 1800, loss: 1.584776, accuracy: 0.882812  step: 1900, loss: 1.563029, accuracy: 0.921875  step: 2000, loss: 1.569637, accuracy: 0.902344  
# 在驗證集上測試模型  pred = neural_net(x_test, is_training=False)  print("Test Accuracy: %f" % accuracy(pred, y_test)  
# 可視化預測  import matplotlib.pyplot as plt  
# 從驗證集中預測5張影像  n_images = 5  test_images = x_test[:n_images]  predictions = neural_net(test_images)    # 顯示圖片和模型預測結果  for i in range(n_images):      plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')      plt.show()      print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))  

output:

Model prediction: 7

Model prediction:2

Model prediction: 1

Model prediction: 0

Model prediction: 4