<commit_before>"""
handwritten_digit_recognition.py

Trains a neural network to recognize handwritten digits from the MNIST database.

Author:  <NAME>
Created: 10 - 28 - 2017
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data as mnist_input_data
from random import randint

# -------------------------------- MNIST Data ----------------------------------
# Get MNIST Data
print('Getting MNIST digit data')
mnist = mnist_input_data.read_data_sets('MNIST_data/', one_hot=True)

# --------------------------- Neural Network System ----------------------------
"""
Single Layer Neural Network: 784 -> 10

Determines the numerical digit that the given image represents.
"""
# Input z
z = tf.placeholder(tf.float32, [None, 784])

# Weights and biases of layer
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Output p
p = tf.nn.softmax(tf.matmul(z, W) + b)

# Training p
p_ = tf.placeholder(tf.float32, [None, 10])

# Error function (Least-Squares)
J = tf.reduce_mean(tf.reduce_sum(tf.square(p_ - p), reduction_indices=[1]))

# Trainer
trainer = tf.train.GradientDescentOptimizer(0.5).minimize(J)

# ----------------------------- Show Sample Helper -----------------------------
def show_sample(name, images, labels, predicts, error, rows=2, cols=2, start=0):
    """
    HELPER FUNCTION

    Shows a sample of the given MNIST data and the resulting predictions from
    the neural network. Plots images, labels, and predictions in a subplot with
    the given rows and columns. Prints the given error afterwards.

    :param name: the name of the dataset
    :param images: the images of the MNIST data
    :param labels: the labels of the MNIST data
    :param predicts: the predictions from the Nerual Network
    :param error: the error of the prediction from the Neural Network
    :param rows: the number of rows in the subplot grid (default=2)
    :param cols: the number of columns in the subplot grid (default=2)
    :param start: the start of the data to sample (default=0)
    """
    simages = np.reshape(images, (-1, 28, 28))
    slabels = np.argmax(labels, axis=1)
    spredicts = np.argmax(predicts, axis=1)
    fig = plt.figure('Sample Digits')
    for index in range(rows*cols):
        splt = plt.subplot(rows, cols, index+1)
        splt.set_title('Expected: {expected}, Predicted: {predicted}'.format(
            expected=slabels[start+index],
            predicted=spredicts[start+index]
        ))
        splt.axis('off')
        splt.imshow(simages[start+index,:,:], cmap='gray')
    plt.show()
    print('{name} error: {error}'.format(name=name, error=error))

# ----------------------------------- Session ----------------------------------
with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.global_variables_initializer())

    # Session params
    nmnist = mnist.test.images.shape[0]
    nbatch = 100
    epochs = 1000
    srows = 2
    scols = 2<commit_msg>Updated figure title<commit_after>"""
handwritten_digit_recognition.py

Trains a neural network to recognize handwritten digits from the MNIST database.

Author:  <NAME>
Created: 10 - 28 - 2017
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data as mnist_input_data
from random import randint

# -------------------------------- MNIST Data ----------------------------------
# Get MNIST Data
print('Getting MNIST digit data')
mnist = mnist_input_data.read_data_sets('MNIST_data/', one_hot=True)

# --------------------------- Neural Network System ----------------------------
"""
Single Layer Neural Network: 784 -> 10

Determines the numerical digit that the given image represents.
"""
# Input z
z = tf.placeholder(tf.float32, [None, 784])

# Weights and biases of layer
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Output p
p = tf.nn.softmax(tf.matmul(z, W) + b)

# Training p
p_ = tf.placeholder(tf.float32, [None, 10])

# Error function (Least-Squares)
J = tf.reduce_mean(tf.reduce_sum(tf.square(p_ - p), reduction_indices=[1]))

# Trainer
trainer = tf.train.GradientDescentOptimizer(0.5).minimize(J)

# ----------------------------- Show Sample Helper -----------------------------
def show_sample(name, images, labels, predicts, error, rows=2, cols=2, start=0):
    """
    HELPER FUNCTION

    Shows a sample of the given MNIST data and the resulting predictions from
    the neural network. Plots images, labels, and predictions in a subplot with
    the given rows and columns. Prints the given error afterwards.

    :param name: the name of the dataset
    :param images: the images of the MNIST data
    :param labels: the labels of the MNIST data
    :param predicts: the predictions from the Nerual Network
    :param error: the error of the prediction from the Neural Network
    :param rows: the number of rows in the subplot grid (default=2)
    :param cols: the number of columns in the subplot grid (default=2)
    :param start: the start of the data to sample (default=0)
    """
    simages = np.reshape(images, (-1, 28, 28))
    slabels = np.argmax(labels, axis=1)
    spredicts = np.argmax(predicts, axis=1)
    fig = plt.figure('{name} Sample Digits'.format(name=name))
    for index in range(rows*cols):
        splt = plt.subplot(rows, cols, index+1)
        splt.set_title('Expected: {expected}, Predicted: {predicted}'.format(
            expected=slabels[start+index],
            predicted=spredicts[start+index]
        ))
        splt.axis('off')
        splt.imshow(simages[start+index,:,:], cmap='gray')
    plt.show()
    print('{name} error: {error}'.format(name=name, error=error))

# ----------------------------------- Session ----------------------------------
with tf.Session() as sess:
    # Initialize variables
    sess.run(tf.global_variables_initializer())

    # Session params
    nmnist = mnist.test.images.shape[0]
    nbatch = 100
    epochs = 1000
    srows = 2
    scols = 2
{'fraction_non_alphanumeric': 0.182088565763384, 'fraction_numerical': 0.018506278916060808, 'mean_word_length': 2.613731343283582, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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}}
{'dir': 'git-commits-cleaned', 'id': '646493', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1817, 'n_words': 580}