application_vnd.jupyter.stderr_output_1.png image_output_2.png image_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import tensorflow as tf

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

f, axarr = plt.subplots(10, 10)
for row in range(10):
    for column in range(10):
        entry = train_data[train_data['label']==column].iloc[row].drop('label').as_matrix()
        axarr[row, column].imshow(entry.reshape([28, 28]))
        axarr[row, column].get_xaxis().set_visible(False)
        axarr[row, column].get_yaxis().set_visible(False)

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

train_val_ratio = 0.7
train_data_size = len(train_data)
train_set = train_data[:int(train_data_size * train_val_ratio)]
val_set = train_data[int(train_data_size * train_val_ratio) + 1:]
init = tf.initialize_all_variables()
saver = tf.train.Saver()
sess = tf.Session()
sess.run(init)
train_eval_list = []
val_eval_list = []
for i in range(100):
    batch = train_set.sample(frac=0.1)
    batch_xs = batch.drop('label', axis=1).as_matrix() / 255.0
    batch_ys = pd.get_dummies(batch['label']).as_matrix()
    val_xs = val_set.drop('label', axis=1).as_matrix() / 255.0
    val_ys = pd.get_dummies(val_set['label']).as_matrix()
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    train_eval = sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys})
    val_eval = sess.run(accuracy, feed_dict={x: val_xs, y_: val_ys})
    train_eval_list.append(train_eval)
    val_eval_list.append(val_eval)

plt.plot(train_eval_list, label='train set')
plt.plot(val_eval_list, label='validation set')
ax.set_xlabel('Epoch')
ax.set_ylabel('Accuracy')
plt.legend()