<jupyter_start><jupyter_text>keras已经集成了MNIST数据库，使用以下命令可以导入MNIST数据库<jupyter_code>mnist = keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>查看training_images 和 training_labels 类型，并从 training_images 里随机选取一张图片，显示出来。<jupyter_code>print('training_images {} '.format(training_images.shape))
print('training_labels {} '.format(training_labels.shape))
print('test_images {} '.format(test_images.shape))
print('test_labels {} '.format(test_labels.shape))

training_random = np.random.randint(60000)
plt.imshow(training_images[training_random],cmap=plt.cm.binary)
plt.xlabel(training_labels[training_random]).set_color('white')
plt.xticks([])
plt.yticks([])
plt.grid(False)<jupyter_output><empty_output><jupyter_text>从 training_images 里随机选取25张图片，并显示出来。<jupyter_code>plt.figure(figsize=(10,10))
i = 0
for x in np.random.randint(60000, size=(25)):
    image = training_images[x]
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(image, cmap=plt.cm.binary)
    plt.xlabel(training_labels[x]).set_color('white')
    i += 1
plt.show()<jupyter_output><empty_output><jupyter_text>图像归一化处理，因为每个像素是8bit，最大值为255,所以每个像素都除以255. 使得每个像素的取值范围从[0-255]变成[0-1]<jupyter_code>training_images  = training_images / 255.0
test_images = test_images / 255.0<jupyter_output><empty_output><jupyter_text>创建 输入层，隐藏层，输出层，并使用Sequential类构建成神经网络模型<jupyter_code>input_layer  = tf.keras.layers.Flatten()
hidden_layer = tf.keras.layers.Dense(128, activation=tf.nn.relu)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)
model = tf.keras.models.Sequential([input_layer, hidden_layer, output_layer])<jupyter_output><empty_output><jupyter_text>以下是softmax演示代码<jupyter_code>x = [1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0]
probability = np.exp(x) / np.sum(np.exp(x))
print(x)
print(probability)

softmax = np.zeros(len(probability))
softmax[probability.argmax()] = 1
print(softmax)
指定 损失函数loss function、优化器optimizer 和 metrics。metrics里的accuracy让神经网络模型在训练时候，同时计算每次训练数据的准确率。
model.compile(optimizer = tf.keras.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(training_images, training_labels, epochs=5, batch_size=512)<jupyter_output><empty_output><jupyter_text>画出神经网络模型在训练的时候，每次迭代时训练数据的准确率。<jupyter_code>plt.plot(history.history['accuracy'])<jupyter_output><empty_output><jupyter_text>测试数据集的准确率<jupyter_code>model.evaluate(test_images, test_labels)<jupyter_output><empty_output><jupyter_text>增加神经元，减少神经元，accuracy的变化。增加epochs次数，减少epochs次数，accuracy变化 过拟合 overfit<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras

mnist = keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

validation_images = training_images[50000:60000]
validation_labels = training_labels[50000:60000]

training_images = training_images[0:50000]
training_labels = training_labels[0:50000]

input_layer  = tf.keras.layers.Flatten()
hidden_layer = tf.keras.layers.Dense(64, activation=tf.nn.relu)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)
model = tf.keras.models.Sequential([input_layer, hidden_layer, output_layer])

model.compile(optimizer = tf.keras.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(training_images, training_labels, epochs=100, batch_size=512, validation_data=(validation_images, validation_labels))
history_dict = history.history 
loss_values = history_dict['loss'] 
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)
plt.plot(epochs, loss_values, 'g', label='Training loss') 
plt.plot(epochs, val_loss_values, 'r', label='Validation loss') 
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylim([0, 1])
plt.ylabel('Loss') 
history_dict = history.history 
acc = history_dict['accuracy'] 
val_acc = history_dict['val_accuracy']
plt.plot(epochs, acc, 'g', label='Training acc') 
plt.plot(epochs, val_acc, 'r', label='Validation acc') 
plt.title('Training and validation accuracy') 
plt.xlabel('Epochs')
plt.ylabel('Accuracy') 
plt.ylim([0.90, 1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13915258161611485, 'fraction_numerical': 0.027321139152581615, 'mean_word_length': 7.981288981288981, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '479499', 'license': 'MIT', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1816, 'n_words': 206, 'path': 'class05/class05.ipynb', 'repo_name': 'streamingnology/DeelpLearningFoundationCourse', 'size': '4319'}