from __future__ import division, print_function, absolute_import

import tflearn

"""
Explanation: Simple Deep Neural Net in TFLearn
for MNIST Digit Classfication
based on https://github.com/tflearn/tflearn/blob/master/examples/images/dnn.py
End of explanation
"""


import tflearn.datasets.mnist as mnist

X, Y, testX, testY = mnist.load_data(one_hot=True)

"""
Explanation: Data Loading and Preprocessing
End of explanation
"""


input_layer = tflearn.input_data(shape=[None, 784])

dense1 = tflearn.fully_connected(input_layer, 64, activation='tanh', regularizer='L2', weight_decay=0.001)

dropout1 = tflearn.dropout(dense1, 0.8)

dense2 = tflearn.fully_connected(dropout1, 64, activation='tanh', regularizer='L2', weight_decay=0.001)

dropout2 = tflearn.dropout(dense2, 0.8)

softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')

"""
Explanation: Building Deep Neural Network
End of explanation
"""


sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)

top_k = tflearn.metrics.Top_k(3)

net = tflearn.regression(softmax, optimizer=sgd, metric=top_k, loss='categorical_crossentropy')

"""
Explanation: Regression using SGD with learning rate decay and Top-3 accuracy
End of explanation
"""


model = tflearn.DNN(net, tensorboard_verbose=0)

model.fit(X, Y, n_epoch=10, validation_set=(testX, testY), show_metric=True, run_id='dense_model')

"""
Explanation: Training
End of explanation
"""


