In [1]:
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
In [2]:
# Building convolutional network
network = input_data(shape=[None, 784], name='input')
network = fully_connected(network, 500, activation='relu' ,regularizer='L2', weight_decay=0.001)
network = fully_connected(network, 500, activation='relu',regularizer='L2', weight_decay=0.001 )
network = fully_connected(network, 500, activation='relu',regularizer='L2', weight_decay=0.001 )
network = fully_connected(network, 500, activation='relu' ,regularizer='L2', weight_decay=0.001)
network = fully_connected(network, 200, activation='relu' )
network = fully_connected(network, 100, activation='relu' )
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.0001,
loss='categorical_crossentropy', name='target')
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=20,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist_7')
sklearn logistic regression acc = %84
dev set = %97 test set = %98,714
dev_set = %99,29 test_set = %98,557
dev_set = %96,45 test set = %98,928
dev_set = %97,13 test set = %99,057
dev_set = %87,73 test set = %92,842
dev_set = %87,73 test set = %92,842
In [3]:
import pandas as pd
import numpy as np
sub_data = pd.read_csv('test.csv')
sub_data.head()
Out[3]:
In [4]:
test = sub_data.as_matrix()
In [5]:
test = test/255
In [6]:
preds = model.predict(test)
In [7]:
p = []
for i in range(len(test)):
p.append(np.argmax(preds[i]))
In [8]:
df = pd.DataFrame(p)
df.index.name='ImageId'
df.index+=1
df.index.name='ImageId'
df.columns=['Label']
df.to_csv('results.csv', header=True)
In [ ]: