In [1]:
import keras
keras.__version__


Using TensorFlow backend.
Out[1]:
'2.3.1'

透過二元分類訓練 IMDB 評論資料

二元分類或稱兩類分類可能是在機器學習中應用最廣泛問題。只要處理的問題只有兩個結果,就可以適用。在這個例子中,我們將根據 IMDB 評論的文本內容將電影評論分為「正面」評論和「負面」評論。

關於 IMDB Dataset 資料集

IMDB Dataset 是來自 Internet 電影數據庫 50,000 條評論文字。他們分為 25,000 條訓練數據和 25,000 條測試數據,每組皆包含包括 50% 的負面評論和 50% 的正面評論。

我們可以直接透過 Keras Datasets 函式庫載入已經整理好的資料集。這些資料集已經經過處理,會將評論依據單詞順序,排列為整數序列,其中每個整數代表字典中的特定單詞。如下:


In [2]:
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)


Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz
17465344/17464789 [==============================] - 0s 0us/step

num_words=10000 表示我們只採用前 10000 個常出現的字詞,此外在 label 中 0 表示負評 1 表示正評。


In [3]:
max([max(sequence) for sequence in train_data])


Out[3]:
9999

也可以透過字典檔,將資料組合回評論文字。


In [4]:
# word_index is a dictionary mapping words to an integer index
word_index = imdb.get_word_index()
# We reverse it, mapping integer indices to words
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
# We decode the review; note that our indices were offset by 3
# because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown".
decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[0]])
# show
decoded_review


Downloading data from https://s3.amazonaws.com/text-datasets/imdb_word_index.json
1646592/1641221 [==============================] - 0s 0us/step
Out[4]:
"? this film was just brilliant casting location scenery story direction everyone's really suited the part they played and you could just imagine being there robert ? is an amazing actor and now the same being director ? father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for ? and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also ? to the two little boy's that played the ? of norman and paul they were just brilliant children are often left out of the ? list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all"

處理資料

我們無法將代表字典檔索引位置的整數資料直接送進網路進行訓練,因此需要對資料進行轉換。由於我們只採用前 10000 常用字詞作為資料集,因此輸入資料可以轉換為 10000 維度的 one-hot-encode,例如 [3, 18] 表示一個全部都是 0 的陣列,只有 index 3, 18 是 1。我們會將這樣的資料格式作為張量進行訓練,轉換如下:


In [0]:
import numpy as np

def vectorize_sequences(sequences, dimension=10000):
    # Create an all-zero matrix of shape (len(sequences), dimension)
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.  # set specific indices of results[i] to 1s
    return results

# Our vectorized training data
x_train = vectorize_sequences(train_data)
# Our vectorized test data
x_test = vectorize_sequences(test_data)

將結果標記進行正規化


In [0]:
# Our vectorized labels
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

建立網路架構

這裡我們預計使用三層網路,全連接層僅使用兩層 16 個神經元的網路,啟動函數設定為 relu,連接最後使用一個神經元輸出(表示正評或負評),並使用 sigmoid 作為最後輸出的啟動函數。由下而上網路架構如下:


In [0]:
from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

將優化器設定為 rmsprop,損失函數使用 binary_crossentropy,將網路進行 Compile


In [0]:
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

訓練模型

先將準備訓練的 25000 筆資料集,抽出 10000 筆資料集用在訓練時期的驗證資料,好讓我們監控訓練過程的準確性變化。如下:


In [0]:
x_val = x_train[:10000]
partial_x_train = x_train[10000:]

y_val = y_train[:10000]
partial_y_train = y_train[10000:]

開始訓練模型


In [10]:
history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=100,
                    batch_size=512,
                    validation_data=(x_val, y_val))


Train on 15000 samples, validate on 10000 samples
Epoch 1/100
15000/15000 [==============================] - 3s 191us/step - loss: 0.5370 - accuracy: 0.7950 - val_loss: 0.4182 - val_accuracy: 0.8629
Epoch 2/100
15000/15000 [==============================] - 1s 72us/step - loss: 0.3364 - accuracy: 0.8965 - val_loss: 0.3188 - val_accuracy: 0.8860
Epoch 3/100
15000/15000 [==============================] - 1s 75us/step - loss: 0.2441 - accuracy: 0.9236 - val_loss: 0.2855 - val_accuracy: 0.8909
Epoch 4/100
15000/15000 [==============================] - 1s 76us/step - loss: 0.1879 - accuracy: 0.9411 - val_loss: 0.2761 - val_accuracy: 0.8892
Epoch 5/100
15000/15000 [==============================] - 1s 75us/step - loss: 0.1532 - accuracy: 0.9527 - val_loss: 0.2893 - val_accuracy: 0.8842
Epoch 6/100
15000/15000 [==============================] - 1s 75us/step - loss: 0.1209 - accuracy: 0.9637 - val_loss: 0.2850 - val_accuracy: 0.8880
Epoch 7/100
15000/15000 [==============================] - 1s 76us/step - loss: 0.0989 - accuracy: 0.9721 - val_loss: 0.3037 - val_accuracy: 0.8830
Epoch 8/100
15000/15000 [==============================] - 1s 76us/step - loss: 0.0784 - accuracy: 0.9801 - val_loss: 0.3299 - val_accuracy: 0.8783
Epoch 9/100
15000/15000 [==============================] - 1s 76us/step - loss: 0.0633 - accuracy: 0.9843 - val_loss: 0.3941 - val_accuracy: 0.8654
Epoch 10/100
15000/15000 [==============================] - 1s 76us/step - loss: 0.0508 - accuracy: 0.9881 - val_loss: 0.3648 - val_accuracy: 0.8781
Epoch 11/100
15000/15000 [==============================] - 1s 75us/step - loss: 0.0392 - accuracy: 0.9919 - val_loss: 0.3921 - val_accuracy: 0.8774
Epoch 12/100
15000/15000 [==============================] - 1s 75us/step - loss: 0.0286 - accuracy: 0.9951 - val_loss: 0.4300 - val_accuracy: 0.8732
Epoch 13/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0230 - accuracy: 0.9963 - val_loss: 0.4500 - val_accuracy: 0.8746
Epoch 14/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0174 - accuracy: 0.9971 - val_loss: 0.4931 - val_accuracy: 0.8714
Epoch 15/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0127 - accuracy: 0.9988 - val_loss: 0.5243 - val_accuracy: 0.8701
Epoch 16/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0116 - accuracy: 0.9982 - val_loss: 0.5479 - val_accuracy: 0.8720
Epoch 17/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0053 - accuracy: 0.9998 - val_loss: 0.5866 - val_accuracy: 0.8710
Epoch 18/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0059 - accuracy: 0.9996 - val_loss: 0.6207 - val_accuracy: 0.8692
Epoch 19/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0027 - accuracy: 0.9999 - val_loss: 0.6737 - val_accuracy: 0.8704
Epoch 20/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0041 - accuracy: 0.9992 - val_loss: 0.7079 - val_accuracy: 0.8655
Epoch 21/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0013 - accuracy: 0.9999 - val_loss: 0.7494 - val_accuracy: 0.8645
Epoch 22/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0038 - accuracy: 0.9989 - val_loss: 0.7751 - val_accuracy: 0.8648
Epoch 23/100
15000/15000 [==============================] - 1s 73us/step - loss: 7.1809e-04 - accuracy: 1.0000 - val_loss: 0.8057 - val_accuracy: 0.8653
Epoch 24/100
15000/15000 [==============================] - 1s 74us/step - loss: 5.5980e-04 - accuracy: 1.0000 - val_loss: 0.8472 - val_accuracy: 0.8650
Epoch 25/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0018 - accuracy: 0.9995 - val_loss: 0.8830 - val_accuracy: 0.8646
Epoch 26/100
15000/15000 [==============================] - 1s 73us/step - loss: 2.7324e-04 - accuracy: 1.0000 - val_loss: 0.9136 - val_accuracy: 0.8642
Epoch 27/100
15000/15000 [==============================] - 1s 74us/step - loss: 2.1454e-04 - accuracy: 1.0000 - val_loss: 0.9531 - val_accuracy: 0.8642
Epoch 28/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0045 - accuracy: 0.9986 - val_loss: 0.9977 - val_accuracy: 0.8617
Epoch 29/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.4564e-04 - accuracy: 1.0000 - val_loss: 0.9969 - val_accuracy: 0.8638
Epoch 30/100
15000/15000 [==============================] - 1s 75us/step - loss: 1.0936e-04 - accuracy: 1.0000 - val_loss: 1.0151 - val_accuracy: 0.8638
Epoch 31/100
15000/15000 [==============================] - 1s 74us/step - loss: 8.6508e-05 - accuracy: 1.0000 - val_loss: 1.0579 - val_accuracy: 0.8644
Epoch 32/100
15000/15000 [==============================] - 1s 74us/step - loss: 5.6658e-05 - accuracy: 1.0000 - val_loss: 1.2194 - val_accuracy: 0.8583
Epoch 33/100
15000/15000 [==============================] - 1s 73us/step - loss: 0.0020 - accuracy: 0.9994 - val_loss: 1.1244 - val_accuracy: 0.8640
Epoch 34/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.1875e-05 - accuracy: 1.0000 - val_loss: 1.1462 - val_accuracy: 0.8635
Epoch 35/100
15000/15000 [==============================] - 1s 73us/step - loss: 2.6838e-05 - accuracy: 1.0000 - val_loss: 1.1723 - val_accuracy: 0.8634
Epoch 36/100
15000/15000 [==============================] - 1s 74us/step - loss: 2.0968e-05 - accuracy: 1.0000 - val_loss: 1.2247 - val_accuracy: 0.8606
Epoch 37/100
15000/15000 [==============================] - 1s 74us/step - loss: 0.0015 - accuracy: 0.9995 - val_loss: 1.2807 - val_accuracy: 0.8603
Epoch 38/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.3666e-05 - accuracy: 1.0000 - val_loss: 1.2815 - val_accuracy: 0.8605
Epoch 39/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.0724e-05 - accuracy: 1.0000 - val_loss: 1.2894 - val_accuracy: 0.8611
Epoch 40/100
15000/15000 [==============================] - 1s 74us/step - loss: 8.7435e-06 - accuracy: 1.0000 - val_loss: 1.3062 - val_accuracy: 0.8618
Epoch 41/100
15000/15000 [==============================] - 1s 74us/step - loss: 6.9121e-06 - accuracy: 1.0000 - val_loss: 1.3452 - val_accuracy: 0.8622
Epoch 42/100
15000/15000 [==============================] - 1s 74us/step - loss: 4.6631e-06 - accuracy: 1.0000 - val_loss: 1.4124 - val_accuracy: 0.8610
Epoch 43/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.4874e-04 - accuracy: 0.9998 - val_loss: 1.4730 - val_accuracy: 0.8600
Epoch 44/100
15000/15000 [==============================] - 1s 75us/step - loss: 2.8657e-06 - accuracy: 1.0000 - val_loss: 1.4725 - val_accuracy: 0.8609
Epoch 45/100
15000/15000 [==============================] - 1s 76us/step - loss: 2.1685e-06 - accuracy: 1.0000 - val_loss: 1.4773 - val_accuracy: 0.8610
Epoch 46/100
15000/15000 [==============================] - 1s 76us/step - loss: 1.8228e-06 - accuracy: 1.0000 - val_loss: 1.4863 - val_accuracy: 0.8606
Epoch 47/100
15000/15000 [==============================] - 1s 75us/step - loss: 1.4949e-06 - accuracy: 1.0000 - val_loss: 1.5067 - val_accuracy: 0.8610
Epoch 48/100
15000/15000 [==============================] - 1s 74us/step - loss: 1.0827e-06 - accuracy: 1.0000 - val_loss: 1.5500 - val_accuracy: 0.8606
Epoch 49/100
15000/15000 [==============================] - 1s 73us/step - loss: 7.5468e-04 - accuracy: 0.9997 - val_loss: 1.5735 - val_accuracy: 0.8603
Epoch 50/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.7357e-06 - accuracy: 1.0000 - val_loss: 1.5610 - val_accuracy: 0.8628
Epoch 51/100
15000/15000 [==============================] - 1s 74us/step - loss: 9.7477e-07 - accuracy: 1.0000 - val_loss: 1.5648 - val_accuracy: 0.8626
Epoch 52/100
15000/15000 [==============================] - 1s 73us/step - loss: 7.3837e-07 - accuracy: 1.0000 - val_loss: 1.5753 - val_accuracy: 0.8635
Epoch 53/100
15000/15000 [==============================] - 1s 74us/step - loss: 6.1472e-07 - accuracy: 1.0000 - val_loss: 1.5871 - val_accuracy: 0.8636
Epoch 54/100
15000/15000 [==============================] - 1s 73us/step - loss: 5.0420e-07 - accuracy: 1.0000 - val_loss: 1.6109 - val_accuracy: 0.8635
Epoch 55/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.7519e-07 - accuracy: 1.0000 - val_loss: 1.6538 - val_accuracy: 0.8635
Epoch 56/100
15000/15000 [==============================] - 1s 74us/step - loss: 6.6194e-04 - accuracy: 0.9999 - val_loss: 1.7132 - val_accuracy: 0.8613
Epoch 57/100
15000/15000 [==============================] - 1s 73us/step - loss: 8.9521e-07 - accuracy: 1.0000 - val_loss: 1.6799 - val_accuracy: 0.8640
Epoch 58/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.1866e-07 - accuracy: 1.0000 - val_loss: 1.6846 - val_accuracy: 0.8632
Epoch 59/100
15000/15000 [==============================] - 1s 73us/step - loss: 2.5007e-07 - accuracy: 1.0000 - val_loss: 1.6930 - val_accuracy: 0.8620
Epoch 60/100
15000/15000 [==============================] - 1s 73us/step - loss: 2.1298e-07 - accuracy: 1.0000 - val_loss: 1.7040 - val_accuracy: 0.8628
Epoch 61/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.8438e-07 - accuracy: 1.0000 - val_loss: 1.7173 - val_accuracy: 0.8625
Epoch 62/100
15000/15000 [==============================] - 1s 74us/step - loss: 1.5160e-07 - accuracy: 1.0000 - val_loss: 1.7439 - val_accuracy: 0.8625
Epoch 63/100
15000/15000 [==============================] - 1s 74us/step - loss: 1.1354e-07 - accuracy: 1.0000 - val_loss: 1.7830 - val_accuracy: 0.8630
Epoch 64/100
15000/15000 [==============================] - 1s 73us/step - loss: 7.5001e-08 - accuracy: 1.0000 - val_loss: 1.8279 - val_accuracy: 0.8635
Epoch 65/100
15000/15000 [==============================] - 1s 73us/step - loss: 5.1758e-08 - accuracy: 1.0000 - val_loss: 1.8691 - val_accuracy: 0.8621
Epoch 66/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.7454e-08 - accuracy: 1.0000 - val_loss: 1.9018 - val_accuracy: 0.8624
Epoch 67/100
15000/15000 [==============================] - 1s 73us/step - loss: 2.9243e-08 - accuracy: 1.0000 - val_loss: 1.9260 - val_accuracy: 0.8634
Epoch 68/100
15000/15000 [==============================] - 1s 72us/step - loss: 2.3442e-08 - accuracy: 1.0000 - val_loss: 1.9545 - val_accuracy: 0.8615
Epoch 69/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.9533e-08 - accuracy: 1.0000 - val_loss: 1.9731 - val_accuracy: 0.8619
Epoch 70/100
15000/15000 [==============================] - 1s 74us/step - loss: 1.6481e-08 - accuracy: 1.0000 - val_loss: 1.9927 - val_accuracy: 0.8614
Epoch 71/100
15000/15000 [==============================] - 1s 74us/step - loss: 1.4330e-08 - accuracy: 1.0000 - val_loss: 2.0064 - val_accuracy: 0.8621
Epoch 72/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.2707e-08 - accuracy: 1.0000 - val_loss: 2.0204 - val_accuracy: 0.8620
Epoch 73/100
15000/15000 [==============================] - 1s 73us/step - loss: 1.1277e-08 - accuracy: 1.0000 - val_loss: 2.0338 - val_accuracy: 0.8624
Epoch 74/100
15000/15000 [==============================] - 1s 72us/step - loss: 1.0200e-08 - accuracy: 1.0000 - val_loss: 2.0466 - val_accuracy: 0.8613
Epoch 75/100
15000/15000 [==============================] - 1s 74us/step - loss: 9.3305e-09 - accuracy: 1.0000 - val_loss: 2.0570 - val_accuracy: 0.8624
Epoch 76/100
15000/15000 [==============================] - 1s 74us/step - loss: 8.5097e-09 - accuracy: 1.0000 - val_loss: 2.0664 - val_accuracy: 0.8627
Epoch 77/100
15000/15000 [==============================] - 1s 73us/step - loss: 7.8838e-09 - accuracy: 1.0000 - val_loss: 2.0761 - val_accuracy: 0.8621
Epoch 78/100
15000/15000 [==============================] - 1s 76us/step - loss: 7.3241e-09 - accuracy: 1.0000 - val_loss: 2.0874 - val_accuracy: 0.8610
Epoch 79/100
15000/15000 [==============================] - 1s 75us/step - loss: 6.8340e-09 - accuracy: 1.0000 - val_loss: 2.0943 - val_accuracy: 0.8614
Epoch 80/100
15000/15000 [==============================] - 1s 76us/step - loss: 6.3996e-09 - accuracy: 1.0000 - val_loss: 2.1027 - val_accuracy: 0.8612
Epoch 81/100
15000/15000 [==============================] - 1s 75us/step - loss: 6.0267e-09 - accuracy: 1.0000 - val_loss: 2.1097 - val_accuracy: 0.8612
Epoch 82/100
15000/15000 [==============================] - 1s 74us/step - loss: 5.7257e-09 - accuracy: 1.0000 - val_loss: 2.1156 - val_accuracy: 0.8616
Epoch 83/100
15000/15000 [==============================] - 1s 73us/step - loss: 5.4171e-09 - accuracy: 1.0000 - val_loss: 2.1220 - val_accuracy: 0.8612
Epoch 84/100
15000/15000 [==============================] - 1s 74us/step - loss: 5.1584e-09 - accuracy: 1.0000 - val_loss: 2.1279 - val_accuracy: 0.8612
Epoch 85/100
15000/15000 [==============================] - 1s 74us/step - loss: 4.9217e-09 - accuracy: 1.0000 - val_loss: 2.1337 - val_accuracy: 0.8613
Epoch 86/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.7044e-09 - accuracy: 1.0000 - val_loss: 2.1404 - val_accuracy: 0.8610
Epoch 87/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.5153e-09 - accuracy: 1.0000 - val_loss: 2.1455 - val_accuracy: 0.8613
Epoch 88/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.3098e-09 - accuracy: 1.0000 - val_loss: 2.1510 - val_accuracy: 0.8608
Epoch 89/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.1607e-09 - accuracy: 1.0000 - val_loss: 2.1566 - val_accuracy: 0.8602
Epoch 90/100
15000/15000 [==============================] - 1s 73us/step - loss: 4.0159e-09 - accuracy: 1.0000 - val_loss: 2.1614 - val_accuracy: 0.8600
Epoch 91/100
15000/15000 [==============================] - 1s 75us/step - loss: 3.8889e-09 - accuracy: 1.0000 - val_loss: 2.1657 - val_accuracy: 0.8603
Epoch 92/100
15000/15000 [==============================] - 1s 74us/step - loss: 3.7604e-09 - accuracy: 1.0000 - val_loss: 2.1704 - val_accuracy: 0.8603
Epoch 93/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.6384e-09 - accuracy: 1.0000 - val_loss: 2.1753 - val_accuracy: 0.8597
Epoch 94/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.5410e-09 - accuracy: 1.0000 - val_loss: 2.1788 - val_accuracy: 0.8600
Epoch 95/100
15000/15000 [==============================] - 1s 74us/step - loss: 3.4376e-09 - accuracy: 1.0000 - val_loss: 2.1822 - val_accuracy: 0.8600
Epoch 96/100
15000/15000 [==============================] - 1s 74us/step - loss: 3.3416e-09 - accuracy: 1.0000 - val_loss: 2.1871 - val_accuracy: 0.8598
Epoch 97/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.2658e-09 - accuracy: 1.0000 - val_loss: 2.1921 - val_accuracy: 0.8605
Epoch 98/100
15000/15000 [==============================] - 1s 73us/step - loss: 3.1822e-09 - accuracy: 1.0000 - val_loss: 2.1943 - val_accuracy: 0.8600
Epoch 99/100
15000/15000 [==============================] - 1s 74us/step - loss: 3.0997e-09 - accuracy: 1.0000 - val_loss: 2.1975 - val_accuracy: 0.8599
Epoch 100/100
15000/15000 [==============================] - 1s 74us/step - loss: 3.0297e-09 - accuracy: 1.0000 - val_loss: 2.2011 - val_accuracy: 0.8600

訓練的過程會把相關資訊存放在 history,透過事後分析訓練過程的資訊可以幫助我們優化參數。


In [11]:
history_dict = history.history
history_dict.keys()


Out[11]:
dict_keys(['val_loss', 'val_accuracy', 'loss', 'accuracy'])

透過上面的方法可以取得訓練 History 包含的資訊,然後我們將資訊繪製成為圖表,如下:


In [12]:
#@title
import matplotlib.pyplot as plt

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)

# "bo" is for "blue dot"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()



In [13]:
plt.clf()   # clear figure
acc_values = history_dict['accuracy']
val_acc_values = history_dict['val_accuracy']

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()


由上面的數據可以看出來,以現在的網路架構,其實在第 3 次 Epoch 就已經獲得最佳的結果,之後的訓練已經造成過度擬合 (Over Fitting),因此在這個案例中將 Epoch 設定為 3 或 4 是取得最佳訓練模型的方法。