<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Data Loading and Preprocessing
Step1:  Multilayer Perceptron
In this example, we increase the depth of the network to have two hidden layers.
We also apply dropout, which is a regularization technique that randomly drops a percentage of the nodes (neurons) in the network.
Step2:  Different Ways to Summarize Model
Step3:  Train Classifier
Step4:  Model Evaluation
Step5:  Predicting a Couple of Held-Out Images
<ASSISTANT_TASK:>
Python 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)
Explanation: Data Loading and Preprocessing
End of explanation
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(input_dim,)))
model.add(Dropout(0.3))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
Explanation: Multilayer Perceptron
In this example, we increase the depth of the network to have two hidden layers.
We also apply dropout, which is a regularization technique that randomly drops a percentage of the nodes (neurons) in the network.
End of explanation
model.summary()
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
import json
json.loads(model.to_json())
Explanation: Different Ways to Summarize Model
End of explanation
# 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)
Explanation: Train Classifier
End of explanation
# Test accuracy is ~98%.
model.evaluate(X_test, y_test)
Explanation: Model Evaluation
End of explanation
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], :])
Explanation: Predicting a Couple of Held-Out Images
End of explanation
<END_TASK>