<jupyter_start><jupyter_text>Data Loading and Preprocessing<jupyter_code># Loads the training and test data sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
first_image = X_train[0, :, :]
# To interpret the values as a 28x28 image, we need to reshape
# the numpy array, which is one dimensional.
plt.imshow(first_image, cmap=plt.cm.Greys);
num_classes = len(np.unique(y_train))
num_classes
# 60K training 28 x 28 (pixel) images
X_train.shape
# 10K test 28 x 28 (pixel) images
X_test.shape
input_dim = np.prod(X_train.shape[1:])
input_dim
# The training and test data sets are integers, ranging from 0 to 255.
# We reshape the training and test data sets to be matrices with 784 (= 28 * 28) features.
X_train = X_train.reshape(60000, input_dim).astype('float32')
X_test = X_test.reshape(10000, input_dim).astype('float32')
# Scales the training and test data to range between 0 and 1.
max_value = X_train.max()
X_train /= max_value
X_test /= max_value
# The training and test labels are integers from 0 to 9 indicating the class label
(y_train, y_test)
# We convert the class labels to binary class matrices
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)<jupyter_output><empty_output><jupyter_text>Multilayer PerceptronTechnically, we're building a perceptron with one hidden layer.<jupyter_code>model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(input_dim,)))
model.add(Dense(num_classes, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Different Ways to Summarize Model<jupyter_code>model.summary()
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
import json
json.loads(model.to_json())<jupyter_output><empty_output><jupyter_text>Train Classifier<jupyter_code># Trains the model, iterating on the training data in batches of 32 in 3 epochs.
# Using the Adam optimizer.
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=32, epochs=3, verbose=1)<jupyter_output>Epoch 1/3
60000/60000 [==============================] - 20s - loss: 0.2018 - acc: 0.9409    
Epoch 2/3
60000/60000 [==============================] - 21s - loss: 0.0815 - acc: 0.9752    
Epoch 3/3
60000/60000 [==============================] - 22s - loss: 0.0529 - acc: 0.9834<jupyter_text>Model Evaluation<jupyter_code># Test accuracy is ~97%.
model.evaluate(X_test, y_test)<jupyter_output>9472/10000 [===========================>..] - ETA: 0s<jupyter_text>Predicting a Couple of Held-Out Images<jupyter_code>first_test_image = X_test[0, :]
plt.imshow(first_test_image.reshape(28, 28), cmap=plt.cm.Greys);
second_test_image = X_test[1, :]
plt.imshow(second_test_image.reshape(28, 28), cmap=plt.cm.Greys);
model.predict_classes(X_test[[0, 1], :])<jupyter_output>2/2 [==============================] - 0s
{'fraction_non_alphanumeric': 0.16707105719237436, 'fraction_numerical': 0.05407279029462738, 'mean_word_length': 6.696, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '51080', 'license': 'MIT', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1022, 'n_words': 249, 'path': 'notebooks/03_multilayer_perceptron.ipynb', 'repo_name': 'ramhiser/Keras-Tutorials', 'size': '2885'}