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.estimator import regression
from tflearn.data_utils import to_categorical
from import_data import *

# Acquiring the data
datasets_source = '/media/arcyfelix/DEA8A421A8A3F665/1 GitHub/Python3/Python3/Machine-Learning/Kaggle Competitions/datasets'
#datasets_source = '/media/arcyfelix/1E6848486848213F/1 GitHub/Python3/Python3/Machine-Learning/Kaggle Competitions/datasets'

folder = 'Digit Recognizer'
file_name = 'train.csv'
specific_dataset_source = datasets_source + '/' + folder + '/' + file_name
output_columns = ['label']

data = import_csv(specific_dataset_source, shuffle = True)
x_data, y_data = get_xy_mutual(data, output_columns, type = 'numpy')

x_data = standalization_divide(x_data, 255)
get_info(x_data, 'input')

num_samples = x_data.shape[0]
input_features = x_data.shape[1]

number_of_labels = labels_info(y_data)
y_data_as_numbers = labels_as_numbers(y_data)

split_percentage = 80
x_train, x_val = cross_validation(x_data, split_percentage)

y_train = np.array(y_data_as_numbers[0:(int(x_data.shape[0]/(100/split_percentage)))])
y_val = np.array(y_data_as_numbers[(int(x_data.shape[0]/(100/split_percentage))):x_data.shape[0]])

# Shaping data to the correct shape.
x_data = x_data.reshape([-1, 28, 28, 1])
y_data = to_categorical(y_data, nb_classes = 10)

print('Size of the intput '+ str(x_data.shape))
print('Size of the output '+ str(y_data.shape))
print('First five examples of one-hot encoded output:')
print(y_data[:5, :])

# --------------------------------------------------------------------------------------------------------

# Building convolutional network
network = input_data(shape=[None, 28, 28, 1], name='input')

network = conv_2d(network, 10, 3, activation = 'relu')

# Hidden layer 1
network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)
#network = local_response_normalization(network)

# Hidden layer 2
network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

# Hidden layer 3
network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

# Hidden layer 4
network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
                     loss='categorical_crossentropy', name='target')

# Training
# model = tflearn.DNN(network, tensorboard_verbose = 3, tensorboard_dir='./logs')

model = tflearn.DNN(network, tensorboard_verbose = 0, tensorboard_dir='./logs') # checkpoint_path = '/loga/model_vgg',
model.fit(x_data, y_data, n_epoch = 1, shuffle = True,
          show_metric = True, batch_size = 100, #snapshot_step = 100,
          snapshot_epoch = False, run_id = 'vgg_digits_adam')


'''
model.fit({'input': X}, {'target': Y}, n_epoch = 1,
           validation_set=({'input': testX}, {'target': testY}), show_metric=True, run_id='convnet')
'''


Training Step: 420  | total loss: 0.31687 | time: 35.476s
| Adam | epoch: 001 | loss: 0.31687 - acc: 0.9283 -- iter: 42000/42000
Out[1]:
"\nmodel.fit({'input': X}, {'target': Y}, n_epoch = 1,\n           validation_set=({'input': testX}, {'target': testY}), show_metric=True, run_id='convnet')\n"

In [8]:
for index in range(11,15):
    predicted_as_prob = np.array(model.predict(x_data[index].reshape([1,28,28,1])))
    print('*' * 70)
    print(type(predicted_as_prob))
    print(predicted_as_prob.shape)
    #print(predicted_as_prob)
    #print(predicted_as_prob.max())
    print(np.amax(predicted_as_prob))





#predicted_as_idx = np.where(predicted_as_prob == predicted_as_prob.max())[1]
#print(predicted_as_idx)


**********************************************************************
<class 'numpy.ndarray'>
(1, 10)
0.98489767313
**********************************************************************
<class 'numpy.ndarray'>
(1, 10)
1.0
**********************************************************************
<class 'numpy.ndarray'>
(1, 10)
0.99941265583
**********************************************************************
<class 'numpy.ndarray'>
(1, 10)
0.933474183083

In [ ]: