""" Starter code for simple logistic regression model for MNIST
with tf.data module
MNIST dataset: yann.lecun.com/exdb/mnist/
Created by Chip Huyen (chiphuyen@cs.stanford.edu)
CS20: "TensorFlow for Deep Learning Research"
cs20.stanford.edu
Lecture 03
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

import numpy as np
import tensorflow as tf
import time

import utils

# Define paramaters for the model
learning_rate = 0.01
batch_size = 128
n_epochs = 70
n_train = 240
n_test = 80

# Step 1: Read in data
mnist_folder = 'data'
utils.download_mnist(mnist_folder)
train, val, test = utils.read_mnist(mnist_folder, flatten=True)

# Step 2: Create datasets and iterator
# create training Dataset and batch it
train_data = tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(1000) # if you want to shuffle your data
train_data = train_data.batch(batch_size)

# create testing Dataset and batch it
test_data = tf.data.Dataset.from_tensor_slices(test)
test_data = test_data.batch(batch_size)

iterator = tf.data.Iterator.from_structure(train_data.output_types,train_data.output_shapes)
img, label = iterator.get_next()

train_init = iterator.make_initializer(train_data)	# initializer for train_data
test_init = iterator.make_initializer(test_data)
# create one iterator and initialize it with different datasets
iterator = tf.data.Iterator.from_structure(train_data.output_types, train_data.output_shapes)
img, label = iterator.get_next()

train_init = iterator.make_initializer(train_data)
test_init = iterator.make_initializer(test_data)

# Step 3: create weights and bias
# w is initialized to random variables with mean of 0, stddev of 0.01
# b is initialized to 0
# shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
# shape of b depends on Y
##w, b = None, None
w = tf.get_variable(name='weights', shape=(12544, 40), initializer=tf.random_normal_initializer(0, 0.01))
b = tf.get_variable(name='bias', shape=(1, 40), initializer=tf.zeros_initializer())
#L = 1000
#M = 800
#N = 200
#O = 100
# Weights initialised with small random values between -0.2 and +0.2
# When using RELUs, make sure biases are initialised with small *positive* values for example 0.1 = tf.ones([K])/10
# 784 = 28 * 28
#W1 = tf.Variable(tf.truncated_normal([12544, L], stddev=0.1))  
#B1 = tf.Variable(tf.zeros([L]))
#W2 = tf.Variable(tf.truncated_normal([L, M], stddev=0.1))
#B2 = tf.Variable(tf.zeros([M]))
#W3 = tf.Variable(tf.truncated_normal([M, N], stddev=0.1))
#B3 = tf.Variable(tf.zeros([N]))
#W4 = tf.Variable(tf.truncated_normal([N, O], stddev=0.1))
#B4 = tf.Variable(tf.zeros([O]))
#W5 = tf.Variable(tf.truncated_normal([O, 40], stddev=0.1))
#B5 = tf.Variable(tf.zeros([40]))

# Step 4: build model
# the model that returns the logits.
# this logits will be later passed through softmax layer
##logits = None
logits = tf.matmul(img, w) + b 
XX = tf.reshape(img, [-1, 12544])
Y1 = tf.nn.sigmoid(tf.matmul(XX, w) + b)
#Y2 = tf.nn.sigmoid(tf.matmul(Y1, W2) + B2)
#Y3 = tf.nn.sigmoid(tf.matmul(Y2, W3) + B3)
#Y4 = tf.nn.sigmoid(tf.matmul(Y3, W4) + B4)
#logits = tf.matmul(Y4, W5) + B5
Y = tf.nn.softmax(logits)

# Step 5: define loss function
# use cross entropy of softmax of logits as the loss function
#loss = None
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=label, name='entropy')
loss = tf.reduce_mean(entropy, name='loss')

# Step 6: define optimizer
# using Adamn Optimizer with pre-defined learning rate to minimize loss
#optimizer = None
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

# Step 7: calculate accuracy with test set
preds = tf.nn.softmax(logits)
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(label, 1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))

writer = tf.summary.FileWriter('./graphs/logreg', tf.get_default_graph())
with tf.Session() as sess:
   
	start_time = time.time()
	sess.run(tf.global_variables_initializer())

    # train the model n_epochs times
	for i in range(n_epochs): 	
		sess.run(train_init)	# drawing samples from train_data
		total_loss = 0
		n_batches = 0
		try:
			while True:
				_, l = sess.run([optimizer, loss])
				total_loss += l
				n_batches += 1
		except tf.errors.OutOfRangeError:
			pass
		print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))
	print('Total time: {0} seconds'.format(time.time() - start_time))

    # test the model
	sess.run(test_init)			# drawing samples from test_data
	total_correct_preds = 0
	try:
		while True:
			accuracy_batch = sess.run(accuracy)
			total_correct_preds += accuracy_batch
	except tf.errors.OutOfRangeError:
		pass

	print('Accuracy {0}'.format(total_correct_preds/n_test))
writer.close()