#----------------
# 01_02 文本分类
#----------------

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

# TensorFlow's version : 1.12.0
print('TensorFlow\'s version : ', tf.__version__)

#----------------
# 1 下载 IMDB 数据集
#----------------

imdb = keras.datasets.imdb

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

#----------------
# 2 探索数据
#----------------

# Training entries: 25000, labels: 25000
print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels)))

print(train_data[0])

# (218, 189)
print(len(train_data[0]), len(train_data[1]))

# A dictionary mapping words to an integer index
word_index = imdb.get_word_index()

# The first indices are reserved
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2  # unknown
word_index["<UNUSED>"] = 3

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])

decode_review(train_data[0])

#----------------
# 3 准备数据
#----------------

train_data = keras.preprocessing.sequence.pad_sequences(train_data,
                                                        value=word_index["<PAD>"],
                                                        padding='post',
                                                        maxlen=256)

test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                       value=word_index["<PAD>"],
                                                       padding='post',
                                                       maxlen=256)

# (256, 256)
print((len(train_data[0]), len(train_data[1])))

print(train_data[0])

#----------------
# 4 构建模型
#----------------

# input shape is the vocabulary count used for the movie reviews (10,000 words)
vocab_size = 10000

model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))

model.summary()

model.compile(optimizer=tf.train.AdamOptimizer(),
              loss='binary_crossentropy',
              metrics=['accuracy'])

#----------------
# 5 创建验证集
#----------------

x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

#----------------
# 6 训练模型
#----------------

history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=40,
                    batch_size=512,
                    validation_data=(x_val, y_val),
                    verbose=1)

#----------------
# 7 评估模型
#----------------

results = model.evaluate(test_data, test_labels)

print(results)

#----------------
# 8 创建准确率和损失随时间变化的图
#----------------

history_dict = history.history

# dict_keys(['loss', 'val_loss', 'val_acc', 'acc'])
print(history_dict.keys())

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

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

# loss

# "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()

# acc

plt.clf()   # clear figure
acc_values = history_dict['acc']
val_acc_values = history_dict['val_acc']

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('Accuracy')
plt.legend()

plt.show()

[((1257, 1371), 'tensorflow.keras.preprocessing.sequence.pad_sequences', 'keras.preprocessing.sequence.pad_sequences', (['train_data'], {'value': "word_index['<PAD>']", 'padding': '"""post"""', 'maxlen': '(256)'}), "(train_data, value=word_index[\n    '<PAD>'], padding='post', maxlen=256)\n", (1299, 1371), False, 'from tensorflow import keras\n'), ((1548, 1661), 'tensorflow.keras.preprocessing.sequence.pad_sequences', 'keras.preprocessing.sequence.pad_sequences', (['test_data'], {'value': "word_index['<PAD>']", 'padding': '"""post"""', 'maxlen': '(256)'}), "(test_data, value=word_index[\n    '<PAD>'], padding='post', maxlen=256)\n", (1590, 1661), False, 'from tensorflow import keras\n'), ((2061, 2079), 'tensorflow.keras.Sequential', 'keras.Sequential', ([], {}), '()\n', (2077, 2079), False, 'from tensorflow import keras\n'), ((3378, 3429), 'matplotlib.pyplot.plot', 'plt.plot', (['epochs', 'loss', '"""bo"""'], {'label': '"""Training loss"""'}), "(epochs, loss, 'bo', label='Training loss')\n", (3386, 3429), True, 'import matplotlib.pyplot as plt\n'), ((3459, 3515), 'matplotlib.pyplot.plot', 'plt.plot', (['epochs', 'val_loss', '"""b"""'], {'label': '"""Validation loss"""'}), "(epochs, val_loss, 'b', label='Validation loss')\n", (3467, 3515), True, 'import matplotlib.pyplot as plt\n'), ((3516, 3557), 'matplotlib.pyplot.title', 'plt.title', (['"""Training and validation loss"""'], {}), "('Training and validation loss')\n", (3525, 3557), True, 'import matplotlib.pyplot as plt\n'), ((3558, 3578), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Epochs"""'], {}), "('Epochs')\n", (3568, 3578), True, 'import matplotlib.pyplot as plt\n'), ((3579, 3597), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Loss"""'], {}), "('Loss')\n", (3589, 3597), True, 'import matplotlib.pyplot as plt\n'), ((3598, 3610), 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), '()\n', (3608, 3610), True, 'import matplotlib.pyplot as plt\n'), ((3612, 3622), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3620, 3622), True, 'import matplotlib.pyplot as plt\n'), ((3631, 3640), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (3638, 3640), True, 'import matplotlib.pyplot as plt\n'), ((3733, 3782), 'matplotlib.pyplot.plot', 'plt.plot', (['epochs', 'acc', '"""bo"""'], {'label': '"""Training acc"""'}), "(epochs, acc, 'bo', label='Training acc')\n", (3741, 3782), True, 'import matplotlib.pyplot as plt\n'), ((3783, 3837), 'matplotlib.pyplot.plot', 'plt.plot', (['epochs', 'val_acc', '"""b"""'], {'label': '"""Validation acc"""'}), "(epochs, val_acc, 'b', label='Validation acc')\n", (3791, 3837), True, 'import matplotlib.pyplot as plt\n'), ((3838, 3883), 'matplotlib.pyplot.title', 'plt.title', (['"""Training and validation accuracy"""'], {}), "('Training and validation accuracy')\n", (3847, 3883), True, 'import matplotlib.pyplot as plt\n'), ((3884, 3904), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Epochs"""'], {}), "('Epochs')\n", (3894, 3904), True, 'import matplotlib.pyplot as plt\n'), ((3905, 3927), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""Accuracy"""'], {}), "('Accuracy')\n", (3915, 3927), True, 'import matplotlib.pyplot as plt\n'), ((3928, 3940), 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), '()\n', (3938, 3940), True, 'import matplotlib.pyplot as plt\n'), ((3942, 3952), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (3950, 3952), True, 'import matplotlib.pyplot as plt\n'), ((2090, 2128), 'tensorflow.keras.layers.Embedding', 'keras.layers.Embedding', (['vocab_size', '(16)'], {}), '(vocab_size, 16)\n', (2112, 2128), False, 'from tensorflow import keras\n'), ((2140, 2177), 'tensorflow.keras.layers.GlobalAveragePooling1D', 'keras.layers.GlobalAveragePooling1D', ([], {}), '()\n', (2175, 2177), False, 'from tensorflow import keras\n'), ((2189, 2234), 'tensorflow.keras.layers.Dense', 'keras.layers.Dense', (['(16)'], {'activation': 'tf.nn.relu'}), '(16, activation=tf.nn.relu)\n', (2207, 2234), False, 'from tensorflow import keras\n'), ((2246, 2293), 'tensorflow.keras.layers.Dense', 'keras.layers.Dense', (['(1)'], {'activation': 'tf.nn.sigmoid'}), '(1, activation=tf.nn.sigmoid)\n', (2264, 2293), False, 'from tensorflow import keras\n'), ((2337, 2361), 'tensorflow.train.AdamOptimizer', 'tf.train.AdamOptimizer', ([], {}), '()\n', (2359, 2361), True, 'import tensorflow as tf\n')]