# 深度學習筆記-----基於TensorFlow2.2.0代碼練習（第三課&第四課）

**

## 正文

**

``````import tensorflow as tf
from tensorflow import keras
``````
``````import numpy as np
import pandas as pd #將輸入轉換成浮點類型數據
import matplotlib.pyplot as plt
``````
``````mnist = keras.datasets.fashion_mnist
``````
``````type(mnist)#看看是不是有
``````
``````module
``````
``````(X_train, y_train),(X_test,y_test) = mnist.load_data()#只需要下載數據一次就行，第一次慢
``````
``````X_train.shape#查看是否對的
``````
``````(60000, 28, 28)
``````
``````np.max(X_train)#提取訓練集中最大值
``````
``````255
``````
``````y_train#查看標籤的數值，因爲其分類爲10類
``````
``````array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
``````
``````class_names =['top','trouser','pullover','dress','coat','sandal','shirt','sneaker','bag','ankle boot']#定義10個類別的E名稱
``````
``````plt.figure()#可視化
plt.imshow(X_train[1])#【】裏面的數據可以自己輸入隨便一個畫出第幾個的圖
plt.colorbar()#加一個顏色條

``````
``````<matplotlib.colorbar.Colorbar at 0x21e0b34b5c8>
``````

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-nP4p5tlv-1590051367946)(output_9_1.png)]

``````#將數據集歸一化 即降低數據集的值
X_train = X_train/255.0
X_test = X_test/255.0
plt.figure()#可視化
plt.imshow(X_train[1])#【】裏面的數據可以自己輸入隨便一個畫出第幾個的圖
plt.colorbar()#加一個顏色條
#可以看出值被縮放到0到1之間
``````
``````<matplotlib.colorbar.Colorbar at 0x21e1387c788>
``````

``````# 建立使用TF模型
from tensorflow.python.keras.models import Sequential #導入訓練模型
from tensorflow.python.keras.layers import Flatten,Dense#導入神經網絡的第一層和第二層
#z這幾行代碼是沒有用的
``````
``````#model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),
#   tf.keras.layers.Dense(128,activation=tf.nn.relu),
#  tf.keras.layers.Dense(10,activation=tf.nn.softmax)])
``````
``````model = Sequential()
# 這裏要是不懂可以查看吳恩達老師深度學習的3.6節課
``````
``````model.summary()
``````
``````_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
flatten_2 (Flatten)          (None, 784)               0
_________________________________________________________________
dense_4 (Dense)              (None, 128)               100480
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
``````
``````#模型補充
``````

### 從這開始改動相比於第一課所實現的代碼

``````history = model.fit(X_train,y_train,epochs = 10,batch_size = 10)#進行訓練，epochs是顯示運行多少次
#batch_size是爲了讓其運行精確
``````
``````Epoch 1/10
60000/60000 [==============================] - 7s 123us/sample - loss: 0.4832 - acc: 0.8282
Epoch 2/10
60000/60000 [==============================] - 7s 114us/sample - loss: 0.3679 - acc: 0.8665
Epoch 3/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.3313 - acc: 0.8785
Epoch 4/10
60000/60000 [==============================] - 8s 128us/sample - loss: 0.3107 - acc: 0.8856
Epoch 5/10
60000/60000 [==============================] - 8s 131us/sample - loss: 0.2938 - acc: 0.8905
Epoch 6/10
60000/60000 [==============================] - 8s 131us/sample - loss: 0.2804 - acc: 0.8944
Epoch 7/10
60000/60000 [==============================] - 8s 139us/sample - loss: 0.2699 - acc: 0.8997
Epoch 8/10
60000/60000 [==============================] - 8s 127us/sample - loss: 0.2604 - acc: 0.9035
Epoch 9/10
60000/60000 [==============================] - 7s 123us/sample - loss: 0.2505 - acc: 0.9065
Epoch 10/10
60000/60000 [==============================] - 8s 126us/sample - loss: 0.2434 - acc: 0.9085
``````
``````
``````
``````test_loss, test_acc = model.evaluate(X_test,y_test)#利用測試集測試訓練下的模型的準確度
print(test_acc)
``````
``````10000/10000 [==============================] - 0s 23us/sample - loss: 0.3213 - acc: 0.8835
0.8835
``````
``````#測試的準確率低於訓練的準確率表明模型是過度擬合
``````
``````#預測模型精確度
from sklearn.metrics import accuracy_score

``````
`````` y_pred = model.predict_classes(X_test)
``````
``````accuracy_score(y_test,y_pred)
``````
``````0.8835
``````

### 第三課新的代碼

#### 繪製函數圖

``````history.history#顯示 history中儲存的值
``````
``````{'loss': [0.27756324788878556,
0.26186962779764145,
0.25036989084614714,
0.24372021847127084,
0.23414909314259422,
0.22922870261633943,
0.22273475261893327,
0.21730233209339106,
0.20946445644778428,
0.20822698713549517],
'acc': [0.89695,
0.90316665,
0.9069,
0.90935,
0.9127667,
0.91536665,
0.91735,
0.91936666,
0.921,
0.92188334]}
``````
``````#畫損失和精確度值
import matplotlib.pyplot as plt
``````
``````plt.plot(history.history['acc'])
plt.title('Model Accuracy')
plt.xlabel('Accuracy')
plt.ylabel('Epoch')
plt.legend(['Train','Test'], loc ='upper left')
plt.show()

plt.plot(history.history['loss'])
plt.title('Model Loss')
plt.xlabel('Loss')
plt.ylabel('Epoch')
plt.legend(['Train','Test'], loc ='upper left')
plt.show()

``````

#### 繪製Confusion矩陣

``````from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
``````
``````mat = confusion_matrix(y_test,y_pred)
plot_confusion_matrix(conf_mat= mat,figsize=(8,8),class_names=class_names,show_normed=True)#class_names是將標籤0到9替換成文字，這是之前定義過，show_normed是顯示預測的概率
``````

#### 改變confusion矩陣點，點的粗細，點的大小,格式等

``````import matplotlib
font = {
'family':'Times New Roman',
'weight':'normal',
'size':16

}
matplotlib.rc('font',**font)#將自己建的font傳到繪圖中
mat = confusion_matrix(y_test,y_pred)
plot_confusion_matrix(conf_mat= mat,figsize=(8,8),class_names=class_names,show_normed=True)
``````

### 第四課內容

``````matplotlib.rc_params()
plt.rc('font',family= 'Serif')
mat = confusion_matrix(y_test,y_pred)
fig,ax = plot_confusion_matrix(conf_mat= mat,figsize=(8,8),class_names=class_names,show_normed=True)
plt.tight_layout()
fig.savefig('cm.png')
``````

### 第三課添加驗證集

``````history = model.fit(X_train,y_train,epochs = 10,batch_size = 10,validation_split=0.2)#進行訓練，epochs是顯示運行多少次
#validation_split=0.2,將數據集的20%分爲驗證集
``````
``````Train on 48000 samples, validate on 12000 samples
Epoch 1/10
48000/48000 [==============================] - 6s 132us/sample - loss: 0.4986 - acc: 0.8224 - val_loss: 0.4114 - val_acc: 0.8537
Epoch 2/10
48000/48000 [==============================] - 6s 125us/sample - loss: 0.3762 - acc: 0.8637 - val_loss: 0.4444 - val_acc: 0.8454
Epoch 3/10
48000/48000 [==============================] - 6s 127us/sample - loss: 0.3386 - acc: 0.8749 - val_loss: 0.3480 - val_acc: 0.8752
Epoch 4/10
48000/48000 [==============================] - 6s 133us/sample - loss: 0.3153 - acc: 0.8832 - val_loss: 0.3765 - val_acc: 0.8663
Epoch 5/10
48000/48000 [==============================] - 6s 125us/sample - loss: 0.2963 - acc: 0.8898 - val_loss: 0.3255 - val_acc: 0.8813
Epoch 6/10
48000/48000 [==============================] - 6s 129us/sample - loss: 0.2832 - acc: 0.8956 - val_loss: 0.3500 - val_acc: 0.8773
Epoch 7/10
48000/48000 [==============================] - 6s 128us/sample - loss: 0.2719 - acc: 0.8997 - val_loss: 0.3633 - val_acc: 0.8759
Epoch 8/10
48000/48000 [==============================] - 6s 128us/sample - loss: 0.2629 - acc: 0.9024 - val_loss: 0.3426 - val_acc: 0.8817
Epoch 9/10
48000/48000 [==============================] - 6s 130us/sample - loss: 0.2527 - acc: 0.9052 - val_loss: 0.3223 - val_acc: 0.8878
Epoch 10/10
48000/48000 [==============================] - 6s 126us/sample - loss: 0.2429 - acc: 0.9097 - val_loss: 0.3394 - val_acc: 0.8821
``````

``````history.history
``````
``````{'loss': [0.49861026119586316,
0.37618109596476035,
0.33858689041961043,
0.3153435294679851,
0.2962673265104968,
0.2831872513704972,
0.27189580952241765,
0.26286001327599173,
0.25268071742318854,
0.24288962757445764],
'acc': [0.82239586,
0.8637292,
0.87485415,
0.8832292,
0.8898125,
0.89558333,
0.8996875,
0.9023542,
0.90522915,
0.9097083],
'val_loss': [0.4113784717333813,
0.4443767974122117,
0.3480171253090278,
0.37646267937573913,
0.325533945398541,
0.34996221882950823,
0.3633425140720404,
0.3425569225193855,
0.3223259873449206,
0.33936566090870957],
'val_acc': [0.85375,
0.84541667,
0.87516665,
0.8663333,
0.88125,
0.87725,
0.87591666,
0.88175,
0.88775,
0.88208336]}
``````
``````plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.xlabel('Accuracy')
plt.ylabel('Epoch')
plt.legend(['Train','Val'], loc ='upper left')
plt.show()

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.xlabel('Loss')
plt.ylabel('Epoch')
plt.legend(['Train','Val'], loc ='upper left')
plt.show()

``````

``````
``````