<jupyter_start><jupyter_text>Restore Trained Model<jupyter_code>with open('model.json', 'r') as json_file:
    loaded_model_json = json_file.read()
model = model_from_json(loaded_model_json)
model.load_weights("weights.h5")<jupyter_output>WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.<jupyter_text>Load dataKeras has a builting function for loading MNIST data while splitting it into train and test sets. x_train and x_test are arrays if train and test input images respectively. Images are represented as a 28 x 28 matrix of pixel values. y_train and y_test are train and test labels respectively.<jupyter_code>(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Save original test images for display later
orig_test = x_test

x_train, x_test = x_train / 255.0, x_test / 255.0<jupyter_output><empty_output><jupyter_text>Make PredictionsUse the trained model to make predictions on data that it was not trained on.Notice that prediction takes much less time compared with training.Keras can make predictions for a set of test data, rather than just one sample. In this case, the model is used to make predictions for all of the test data in one statement. Keras/Tensorflow will parallelize this operation as much as possible when using a GPU.The predicted output is a one-hot vector. argmax() is used to translate the output vector to a scalar value by choosing the index of the vector with the highest value.<jupyter_code>predictions = model.predict(x_test)
most_likely = predictions.argmax(1)<jupyter_output><empty_output><jupyter_text>The following snippet chooses a random test image, prints the model predicted value, expected value (label), and the original image.<jupyter_code>index = random.randrange(10000)
plt.title('Prediction: %d, label: %d' % (most_likely[index], y_test[index]))
plt.imshow(orig_test[index], cmap='gray')
plt.show()<jupyter_output><empty_output><jupyter_text>Error AnalysisThe next snippet looks for model predictions that specifically do not match the expected value. In some cases, even humans might disagree on what this image is.<jupyter_code>for i in range(10000):
    index = random.randrange(10000)
    if most_likely[index] != y_test[index]:
        break

plt.imshow(orig_test[index], cmap='gray')
plt.title('Prediction: %d, label: %d' % (most_likely[index], y_test[index]))
plt.show()

plt.bar(range(10), predictions[index], tick_label=range(10))
plt.title('Prediction values')
plt.show()<jupyter_output><empty_output><jupyter_text>The following cell calculates the error rate by comparing predicted values with expected values. This is similar to what Keras does when evaluating a model.<jupyter_code>total = 0.0
misclassified = 0.0
for i in range(10000):
    total += 1
    if most_likely[i] != y_test[i]:
        misclassified += 1
print('Error rate: %.2f %%' % (100.0*misclassified/total))<jupyter_output>Error rate: 2.19 %
{'fraction_non_alphanumeric': 0.08983587676360495, 'fraction_numerical': 0.01785200115174201, 'mean_word_length': 5.865612648221344, 'pattern_counts': {'":': 0, '<': 23, '<?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': '662118', 'license': 'MIT', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1032, 'n_words': 364, 'path': 'MNIST/MNIST_Prediction.ipynb', 'repo_name': 'cas10ph/MachineLearning', 'size': '3473'}