# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from icecream import ic
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
#1 加载数据
fashion_mnist = keras.datasets.fashion_mnist
#2 切分训练集和测试集
(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']
#3 预处理数据
#在训练网络之前,必须对数据进行预处理。如果您检查训练集中的第一个图像,您会看到像素值处于 0 到 255 之间:
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
#将这些值缩小至 0 到 1 之间,然后将其馈送到神经网络模型。将这些值除以 255。请务必以相同的方式对训练集和测试集进行预处理:
train_images = train_images / 255.0
test_images = test_images / 255.0
#为了验证数据的格式是否正确,以及您是否已准备好构建和训练网络,让我们显示训练集中的前 25 个图像,并在每个图像下方显示类名称。
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    #不显示格子
    plt.grid(False)
    #cmap即colormaps,图谱
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    # class_name列表中的名字核xlabel一一映射
    plt.xlabel(class_names[train_labels[i]])
plt.show()
#4 构建模型
model = keras.Sequential([
    # 4.1 设置层
    #将图像格式从二维数组(28 x 28 像素)转换成一维数组(28 x 28 = 784 像素)。将该层视为图像中未堆叠的像素行并将其排列起来。
    keras.layers.Flatten(input_shape=(28, 28)),
    #展平像素后,网络会包括两个 tf.keras.layers.Dense 层的序列。它们是密集连接或全连接神经层。
    #第一个 Dense 层有 128 个节点(或神经元)
    #激活函数(activation functions)的目标是,将神经网络非线性化。激活函数是连续的(continuous),且可导的(differential)。
    #https://www.jianshu.com/p/857d5859d2cc
    keras.layers.Dense(128, activation='relu'),
    #第二个(也是最后一个)层会返回一个长度为 10 的 logits 数组。每个节点都包含一个得分,用来表示当前图像属于 10 个类中的哪一类。
    keras.layers.Dense(10)
])
#5 编译模型
model.compile(
    #优化器 - 决定模型如何根据其看到的数据和自身的损失函数进行更新。
    optimizer='adam',
    # 损失函数 - 用于测量模型在训练期间的准确率。您会希望最小化此函数,以便将模型“引导”到正确的方向上
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    #指标 - 用于监控训练和测试步骤。以下示例使用了准确率,即被正确分类的图像的比率
    metrics=['accuracy']
)

#6 训练模型
'''
训练神经网络模型需要执行以下步骤:
    将训练数据馈送给模型。在本例中,训练数据位于 train_images 和 train_labels 数组中。
    模型学习将图像和标签关联起来。
    要求模型对测试集(在本例中为 test_images 数组)进行预测。
    验证预测是否与 test_labels 数组中的标签相匹配。
'''
#开始训练,向模型馈送数据,10个大循环
model.fit(train_images, train_labels, epochs=10)
#在模型训练期间,会显示损失和准确率指标。此模型在训练数据上的准确率达到了 0.91(或 91%)左右。

# 7 评估准确率
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

print('\nTest accuracy:', test_acc)
#运行结果 Test accuracy: 0.8811
#模型在测试数据集上的准确率略低于训练数据集,训练准确率和测试准确率之间的差距代表过拟合

#8 进行预测
#模型经过训练后,使用它对一些图像进行预测。模型具有线性输出,即 logits。可以附加一个 softmax 层,将 logits 转换成更容易理解的概率。
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
#在上面,模型预测了测试集中每个图像的标签。我们来看看第一个预测结果
ic(predictions[0])
#预测结果是一个包含 10 个数字的数组。它们代表模型对 10 种不同服装中每种服装的“置信度”。我们可以看到哪个标签的置信度值最大:
ic(np.argmax(predictions[0]))
#因此,该模型非常确信这个图像是短靴,或 class_names[9]。通过检查测试标签发现这个分类是正确的
ic(test_labels[0])
#####################################################################################################################
#将其绘制成图表,看看模型对于全部 10 个类的预测。
def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array, true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array, true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')
##############################################################################################################
#9 验证预测结果
#在模型经过训练后,可以使用它对一些图像进行预测。
#第 0 个图像、预测结果和预测数组。正确的预测标签为蓝色,错误的预测标签为红色。数字表示预测标签的百分比(总计为 100)。
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()
#第 12 个图像、预测结果和预测数组。正确的预测标签为蓝色,错误的预测标签为红色。数字表示预测标签的百分比(总计为 100)。
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()
#10 使用训练好的模型,最后,使用训练好的模型对单个图像进行预测
# Grab an image from the test dataset.
img = test_images[1]
#tf.keras 模型经过了优化,可同时对一个批或一组样本进行预测。因此,即便只使用一个图像,您也需要将其添加到列表中:
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))

#现在预测这个图像的正确标签:
predictions_single = probability_model.predict(img)
print(predictions_single)

plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
#keras.Model.predict 会返回一组列表,每个列表对应一批数据中的每个图像。在批次中获取对我们(唯一)图像的预测:
ic(np.argmax(predictions_single[0]))
#该模型会按照预期预测标签。