text_plain_output_1.png
import math

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import sklearn.preprocessing as pre

import tensorflow as tf

train_data = pd.read_csv('../input/train.csv')
test_data = pd.read_csv('../input/test.csv')
images = train_data.drop('label', 1)
observations, features = images.shape
pixel_width = int(np.sqrt(features))
X = images.as_matrix()
X_train = X.reshape(observations, pixel_width, pixel_width, 1)
labels = train_data['label']
Y = labels.as_matrix()
labels = pre.LabelEncoder().fit_transform(labels)[:, None]
Y_train = pre.OneHotEncoder().fit_transform(labels).todense()
t = test_data.as_matrix()
tr, tc = t.shape
test = t.reshape(tr, pixel_width, pixel_width, 1)

def showImage(X, index):
    N, w, h, c = X.shape
    grid = np.zeros((w, h))
    for i in range(w):
        for j in range(h):
            grid[i, j] = X[index, i, j, 0]
    plt.rcParams['figure.figsize'] = [1.5, 1.5]
    plt.ion()

X = tf.placeholder('float', [None, 28, 28, 1])
Y = tf.placeholder('float', [None, 10])
lr = tf.placeholder(tf.float32)
pkeep = tf.placeholder(tf.float32)
pkeep_conv = tf.placeholder(tf.float32)
K = 24
L = 48
M = 64
N = 200
W1 = tf.Variable(tf.truncated_normal([6, 6, 1, K], stddev=0.1))
B1 = tf.Variable(tf.ones([K]) / 10)
W2 = tf.Variable(tf.truncated_normal([5, 5, K, L], stddev=0.1))
B2 = tf.Variable(tf.ones([L]) / 10)
W3 = tf.Variable(tf.truncated_normal([4, 4, L, M], stddev=0.1))
B3 = tf.Variable(tf.ones([M]) / 10)
W4 = tf.Variable(tf.truncated_normal([7 * 7 * M, N], stddev=0.1))
B4 = tf.Variable(tf.ones([N]) / 10)
W5 = tf.Variable(tf.truncated_normal([N, 10], stddev=0.1))
B5 = tf.Variable(tf.ones([10]) / 10)

def batchnorm(Ylogits, beta, convolutional=False):
    bnepsilon = 1e-05
    if convolutional:
        mean, variance = tf.nn.moments(Ylogits, [0, 1, 2])
    else:
        mean, variance = tf.nn.moments(Ylogits, [0])
    BN = tf.nn.batch_normalization(Ylogits, mean, variance, beta, None, bnepsilon)
    return BN

stride = 1
Y1 = tf.nn.conv2d(X, W1, strides=[1, stride, stride, 1], padding='SAME')
BN1 = batchnorm(Y1, B1, convolutional=True)
Y1_BN = tf.nn.relu(BN1)
stride = 2
Y2 = tf.nn.conv2d(Y1_BN, W2, strides=[1, stride, stride, 1], padding='SAME')
BN2 = batchnorm(Y2, B2, convolutional=True)
Y2_BN = tf.nn.relu(BN2)
stride = 2
Y3 = tf.nn.conv2d(Y2_BN, W3, strides=[1, stride, stride, 1], padding='SAME')
BN3 = batchnorm(Y3, B3, convolutional=True)
Y3_BN = tf.nn.relu(BN3)

YY = tf.reshape(Y3, shape=[-1, 7 * 7 * M])

Y4 = tf.matmul(YY, W4)
BN4 = batchnorm(Y4, B4)
Y4_BN = tf.nn.relu(BN4)
YY4 = tf.nn.dropout(Y4_BN, pkeep)
Ylogits = tf.matmul(YY4, W5) + B5
YHat = tf.nn.softmax(Ylogits)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits, labels=Y)
cross_entropy = tf.reduce_mean(cross_entropy)
correct_prediction = tf.equal(tf.argmax(YHat, 1), tf.argmax(Y_train, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
optimizer = tf.train.AdamOptimizer(lr).minimize(cross_entropy)
predict = tf.argmax(YHat, 1)

training_epochs = 2
batch_size = 100
max_learning_rate = 0.02
min_learning_rate = 0.0001
decay_speed = 1600.0
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    total_batch = int(observations / batch_size)
    batch_no = 1
    print('Optimization In Progress')
    for epoch in range(training_epochs):
        c = 0.0
        avg_cost = 0.0
        for i in range(total_batch):
            start = i * 100
            end = start + batch_size - 1
            learning_rate = min_learning_rate + (max_learning_rate - min_learning_rate) * math.exp(-i / decay_speed)
            batch_X = X_train[start:end]
            batch_Y = Y_train[start:end]
            _, c = sess.run([optimizer, cross_entropy], feed_dict={X: batch_X, Y: batch_Y, lr: learning_rate, pkeep: 0.75})
            avg_cost += c / total_batch
        print('epoch No {} cross entropy={}'.format(epoch + 1, avg_cost))
    print('Optimization Completed')
    print('Predictions')
    test_batch = batch_size
    predictions = np.zeros(test.shape[0])
    for i in range(0, test.shape[0] // test_batch):
        predictions[i * test_batch:(i + 1) * test_batch] = predict.eval(feed_dict={X: test[i * test_batch:(i + 1) * test_batch], pkeep: 1.0})