text_html_output_1.png
import numpy as np

import pandas as pd

import tensorflow as tf

dataset = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

def one_hot(num):
    output = np.zeros([1, 10])
    output[0, num] = 1
    return output.astype(int)

labels_encoded = np.array([np.array([int(i == l) for i in range(10)]) for l in dataset.iloc[:, 0].values])
dataset = dataset.drop('label', axis=1)
(dataset.shape, labels_encoded.shape)

train_size = 40000
validation_size = 2000

train = dataset.iloc[:train_size]
train_targets = labels_encoded[:train_size]
validation = dataset.iloc[train_size:]
validation_targets = labels_encoded[train_size:]
(train.shape, train_targets.shape, validation.shape, validation_targets.shape, test.shape)

input_size = 784
output_size = 10
hidden_layer_size = 50
tf.reset_default_graph()

inputs = tf.placeholder(tf.float32, [None, input_size])
targets = tf.placeholder(tf.float32, [None, output_size])

w_1 = tf.get_variable('w_1', [input_size, hidden_layer_size])
b_1 = tf.get_variable('b_1', [hidden_layer_size])
o_1 = tf.nn.relu(tf.matmul(inputs, w_1) + b_1)

w_2 = tf.get_variable('w_2', [hidden_layer_size, hidden_layer_size])
b_2 = tf.get_variable('b_2', [hidden_layer_size])
o_2 = tf.nn.relu(tf.matmul(o_1, w_2) + b_2)

w_3 = tf.get_variable('w_3', [hidden_layer_size, output_size])
b_3 = tf.get_variable('b_3', [output_size])
outputs = tf.matmul(o_2, w_3) + b_3

loss = tf.nn.softmax_cross_entropy_with_logits(logits=outputs, labels=targets)
mean_loss = tf.reduce_mean(loss)

optimize = tf.train.AdamOptimizer(learning_rate=0.001).minimize(mean_loss)
out_equal_target = tf.equal(tf.argmax(outputs, 1), tf.argmax(targets, 1))
accuracy = tf.reduce_mean(tf.cast(out_equal_target, tf.float32))

sess = tf.InteractiveSession()
initializer = tf.global_variables_initializer()
sess.run(initializer)

batch_size = 100
batch_number = train.shape[0] // batch_size
max_epoch = 15
prev_validation_loss = 9999999.0

for epoch_counter in range(max_epoch):
    curr_epoch_loss = 0
    start = 0
    end = start + batch_size
    for batch_counter in range(batch_number):
        input_batch = np.multiply(train.iloc[start:end].values.astype(np.float32), 1.0 / 255.0)
        target_batch = train_targets[start:end]
        start = end
        end = start + batch_size
        _, batch_loss = sess.run([optimize, mean_loss], feed_dict={inputs: input_batch, targets: target_batch})
        curr_epoch_loss += batch_loss
    curr_epoch_loss /= batch_number
    input_batch = np.multiply(validation.values.astype(np.float32), 1.0 / 255.0)
    target_batch = validation_targets
    val_loss, val_accuracy = sess.run([mean_loss, accuracy], feed_dict={inputs: input_batch, targets: target_batch})
    print('Epoch ' + str(epoch_counter + 1) + '. Training loss: ' + '{0:.3f}'.format(curr_epoch_loss) + '. Validation loss: ' + '{0:.3f}'.format(val_loss) + '. Validation accuracy: ' + '{0:.2f}'.format(val_accuracy * 100.0) + '%')
    if val_loss > prev_validation_loss:
        break
    prev_validation_loss = val_loss
print('End of Training')