<jupyter_start><jupyter_text>Section 15.7 15.6.1 Loading the MNIST Dataset<jupyter_code>from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>15.6.2 Data Exploration<jupyter_code>X_train.shape
y_train.shape
X_test.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Visualizing Digits<jupyter_code># enable high-res images in notebook (not in the book's code)
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=2)
import numpy as np
index = np.random.choice(np.arange(len(X_train)), 24, replace=False)
figure, axes = plt.subplots(nrows=4, ncols=6, figsize=(16, 9))

for item in zip(axes.ravel(), X_train[index], y_train[index]):
    axes, image, target = item
    axes.imshow(image, cmap=plt.cm.gray_r)
    axes.set_xticks([])  # remove x-axis tick marks
    axes.set_yticks([])  # remove y-axis tick marks
    axes.set_title(target)
plt.tight_layout()
sns.set(font_scale=1)  # reset font scale--Not in book's code<jupyter_output><empty_output><jupyter_text>15.6.3 Data Preparation Reshaping the Image Data<jupyter_code>X_train = X_train.reshape((60000, 28, 28, 1)) 
X_train.shape
X_test = X_test.reshape((10000, 28, 28, 1))
X_test.shape<jupyter_output><empty_output><jupyter_text>Normalizing the Image Data<jupyter_code>X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255<jupyter_output><empty_output><jupyter_text>One-Hot Encoding: Converting the Labels From Integers to Categorical Data<jupyter_code>from tensorflow.keras.utils import to_categorical
y_train = to_categorical(y_train)
y_train.shape
y_train[0]
y_test = to_categorical(y_test)
y_test.shape<jupyter_output><empty_output><jupyter_text>15.6.4 Creating the Neural Network<jupyter_code>from tensorflow.keras.models import Sequential
cnn = Sequential()<jupyter_output><empty_output><jupyter_text>Adding Layers to the Network<jupyter_code>from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D<jupyter_output><empty_output><jupyter_text>Convolution  Adding a **`Conv2D`** Convolution Layer to Our Model<jupyter_code>cnn.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', 
               input_shape=(28, 28, 1)))<jupyter_output><empty_output><jupyter_text>Dimensionality of the First Convolution Layer’s Output Overfitting Adding a Pooling Layer<jupyter_code>cnn.add(MaxPooling2D(pool_size=(2, 2)))<jupyter_output><empty_output><jupyter_text>Adding Another Convolutional Layer and Pooling Layer<jupyter_code>cnn.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu'))
cnn.add(MaxPooling2D(pool_size=(2, 2)))<jupyter_output><empty_output><jupyter_text>Flattening the Results<jupyter_code>cnn.add(Flatten())<jupyter_output><empty_output><jupyter_text>Adding a **`Dense`** Layer to Reduce the Number of Features<jupyter_code>cnn.add(Dense(units=128, activation='relu'))<jupyter_output><empty_output><jupyter_text>Adding Another **`Dense`** Layer to Produce the Final Results<jupyter_code>cnn.add(Dense(units=10, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Printing the Model’s Summary<jupyter_code>cnn.summary()<jupyter_output><empty_output><jupyter_text>Visualizing a Model’s Structure<jupyter_code>from tensorflow.keras.utils import plot_model
from IPython.display import Image
plot_model(cnn, to_file='convnet.png', show_shapes=True, 
           show_layer_names=True)
Image(filename='convnet.png')<jupyter_output><empty_output><jupyter_text>Compiling the Model<jupyter_code>cnn.compile(optimizer='adam',
            loss='categorical_crossentropy',
            metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>15.6.5 Training and Evaluating the Model<jupyter_code>from tensorflow.keras.callbacks import TensorBoard

import time

tensorboard_callback = TensorBoard(log_dir=f'./logs/mnist{time.time()}',   
    histogram_freq=1, write_graph=True)
cnn.fit(X_train, y_train, epochs=10, batch_size=64, validation_split=0.1, 
        callbacks=[tensorboard_callback])<jupyter_output><empty_output><jupyter_text>Evaluating the Model<jupyter_code>loss, accuracy = cnn.evaluate(X_test, y_test)
loss
accuracy<jupyter_output><empty_output><jupyter_text>Making Predictions<jupyter_code>predictions = cnn.predict(X_test)
y_test[0]
for index, probability in enumerate(predictions[0]):
    print(f'{index}: {probability:.10%}')<jupyter_output><empty_output><jupyter_text>Locating the Incorrect Predictions<jupyter_code>images = X_test.reshape((10000, 28, 28))
incorrect_predictions = []
for i, (p, e) in enumerate(zip(predictions, y_test)):
    predicted, expected = np.argmax(p), np.argmax(e)
    
    if predicted != expected:
        incorrect_predictions.append((i, images[i], predicted, expected))
len(incorrect_predictions)<jupyter_output><empty_output><jupyter_text>Visualizing Incorrect Predictions<jupyter_code>figure, axes = plt.subplots(nrows=4, ncols=6, figsize=(16, 12))

for axes, item in zip(axes.ravel(), incorrect_predictions):
    index, image, predicted, expected = item
    axes.imshow(image, cmap=plt.cm.gray_r)
    axes.set_xticks([])  # remove x-axis tick marks
    axes.set_yticks([])  # remove y-axis tick marks
    axes.set_title(f'index: {index}\np: {predicted}; e: {expected}')
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Displaying the Probabilities for Several Incorrect Predictions<jupyter_code>def display_probabilities(prediction):
    for index, probability in enumerate(prediction):
        print(f'{index}: {probability:.10%}')
display_probabilities(predictions[583])
display_probabilities(predictions[625])
display_probabilities(predictions[1299])<jupyter_output><empty_output><jupyter_text>15.6.6 Saving and Loading a Model<jupyter_code>cnn.save('mnist_cnn.h5')
##########################################################################
# (C) Copyright 2019 by Deitel & Associates, Inc. and                    #
# Pearson Education, Inc. All Rights Reserved.                           #
#                                                                        #
# DISCLAIMER: The authors and publisher of this book have used their     #
# best efforts in preparing the book. These efforts include the          #
# development, research, and testing of the theories and programs        #
# to determine their effectiveness. The authors and publisher make       #
# no warranty of any kind, expressed or implied, with regard to these    #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or       #
# consequential damages in connection with, or arising out of, the       #
# furnishing, performance, or use of these programs.                     #
##########################################################################<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1323529411764706, 'fraction_numerical': 0.020184544405997693, 'mean_word_length': 6.248693834900731, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '384635', 'license': 'Apache-2.0', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2346, 'n_words': 460, 'path': 'examples/ch15/MNIST_CNN_TensorBoard.ipynb', 'repo_name': 'germanngc/PythonFundamentals', 'size': '6936'}