Lesson 02 監督式學習 basic classification by tf.keras


監督式學習是利用已標記(labeled)的資料作訓練資料(train data)用來對未知資料進行分類的方法. 本例使用MNIST流行資料庫的資料進行監督式學習. MNIST流行資料庫包含七萬個灰階的低解析度衣物圖形. 其中有6萬個已標記的圖形, 我們將用其來進行訓練並用以分類最後的一萬個圖形.

Lesson 02.01: import tf.keras
from __future__ import absolute_import, division, print_function
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)
註解:
import tensorflow as tf #引入tensorflow
from tensorflow import keras #引入keras
import numpy as np  #引入數據處理函式庫
import matplotlib.pyplot as plt  #引入圖形函式庫
print(tf.__version__) # 列印出 tensorflow 版本



Lesson 02.2 Import the Fashion MNIST dataset as data
MNIST 流行資料庫包含七萬個灰階的低解析度衣物圖形. 我們將嘗試著利用kreas來分類圖形. 在這個練習中,我們將利用6萬個圖形來進行訓練,並用以分類最後的一萬個圖形.
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
註解:
fashion_mnist = keras.datasets.fashion_mnist #宣告物件一個MNIST物件
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() #下載MNIST資料
train_images與train_labels #是用來訓練的圖形與標記,  test_images與 test_labels是用來測試訓練結果的圖形與標記
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] #其中的包含9個分類項目
嘗試了解MNIST急的資料大小與型態
print(train_images.shape)
print(len(train_labels))
print(train_labels)
print(test_images.shape)
print(len(test_labels))

train_images.shape #訓練影像的維度 60000個圖, 28*28個pixels點
len(train_labels) #訓練標籤的長度.
train_labels #訓練標籤的內容. 6萬個圖已被分門別類
test_images.shape #待判定影像的維度 10000個圖, 28*28個pixels點
len(test_images.shape) #訓練標籤的長度.



我們可以利用matplotlib函式庫畫出訓練物件的圖形. plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i]]) plt.show()


Lesson 02.03 build the model
建立模型:
層(layer)是神經網路的基本組成. 大多數的深度學習都是藉由連結許多的層而組成.
在本例中, 第一層是利用tf.keras.layers.Flatten將原本是2d array (28*28 pixels 像數)的圖形轉換程1為的陣列(28 * 28 = 784 像數 pixels)
第二層則是由是keras.layers.Dense所建立的全連結層(densely-connected, or fully-connected), 第二層利用tf.nn.rele定義128個節點(神經元).
第三層則是一個具有10個節點的歸一畫指數函數層(softmax layer). 這一層將給我們一個包含10個機率的陣列(10個分數的總和為1,並對應每一個節點)每一個節點的對應於這個圖形應該是屬於那一個分類.
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])




Lesson 02.04 build the model
編譯模型:
在訓練模型前, 我們需要編譯模型的步驟. 其中包含: 1.Loss function 損失函數 — #判斷模型在訓練時的精確程度, 我們希望借由此函數的最小值來引導模型的正確走向
2.Optimizer 優化器 — #This is how the model is updated based on the data it sees and its loss function.
3.Metrics — #Used to monitor the training and testing steps. The following example uses accuracy, the fraction of the images that are correctly classified.
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])



Lesson 02.05 Training the model
訓練模型與計算準確度:
我們可利用model.fit函數訓練模型
利用model.evaluate函數來測試此模型對待測資料(test data)的準確度

model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)



Lesson 02.06 Make predictions
檢查待測資料的結果:
我們可利用model.fit函數訓練模型
利用prediction函數來存放此模型對待訓練圖形(test data)的結果

predictions = model.predict(test_images)
print(predictions[0])
在此predictions存放了10000筆待測資料預測, predictions[0]結果將回傳9個浮點數, 這九個浮點數對應於9個標記類別的個別預測機率


Lesson 02.07 summary
在此我們的監督式訓練已完成, 在此我將以上的程式做個整理. :
在練習學習的過程中, 我個人建議使用jupyter notebook 作為平台, jupyter notebook有著即見即得的優點, 可以在執行過程中直間觀看結果.
from __future__ import absolute_import, division, print_function
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
print(train_images.shape)
print(len(train_labels))
print(train_labels)
print(test_images.shape)
print(len(test_labels))
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
predictions = model.predict(test_images)
print(predictions[0])