import hyper_model as hp
import numpy as np
import neural_network_steps as nn
from tensorflow.python.framework import ops

"""
The following code must be implemented into main.py and is used 
during the automatic hyperparameter tuning to loop over all possible combinations
and saving the accuracies for train, validation and test data.
This is then being saved into a csv file where the accuracies can be sorted.
"""
#Setting the hyperparameters and training the model to receive the parameters
  learning_rate = 0.025
  num_neuronsl1 = 25
  num_neuronsl2 = 10
  minibatch_size = 128
  dropout_factor = 1.0

#Initializing the arrays to store the results of model training
  L_Rate = []
  NodesL1 = []
  NodesL2 = []
  Batch_Size = []
  Dropout = []
  training_accuracy = []
  validation_accuracy = []
  testing_accuracy = []
  stopping_epoch = []

#Iteration over all possible combinations of the hyperparameters
  for lr in range(len(learning_rate)):
      for n1 in range(len(num_neuronsl1)):
          for n2 in range(len(num_neuronsl2)):
              for ms in range(len(minibatch_size)):
                  for df in range(len(dropout_factor)):
                      print("L_rate = " + str(learning_rate[lr]) + ", Neurons = (" + str(num_neuronsl1[n1]) + " , " + str(num_neuronsl2[n2]) +
                      "), MiniBatch = " + str(minibatch_size[ms]) + ", Dropout = " + str(dropout_factor[df]))
                      train_acc, test_acc, valid_acc, stop_epoch = hp.model(train_set_x, train_set_y, test_set_x, test_set_y, valid_set_x, valid_set_y,
                                              learning_rate = learning_rate[lr],
                                              num_epochs = 1000,
                                              minibatch_size = minibatch_size[ms],
                                              num_neuronsl1 = num_neuronsl1[n1],
                                              num_neuronsl2 = num_neuronsl2[n2],
                                              kp = dropout_factor[df],
                                              print_cost=False)
                      L_Rate.append(learning_rate[lr])
                      NodesL1.append(num_neuronsl1[n1])
                      NodesL2.append(num_neuronsl2[n2])
                      Batch_Size.append(minibatch_size[ms])
                      Dropout.append(dropout_factor[df])
                      training_accuracy.append(train_acc)
                      validation_accuracy.append(valid_acc)
                      testing_accuracy.append(test_acc)
                      stopping_epoch.append(stop_epoch)

#Saving the results of the training into a data array
  data_array = np.transpose(np.array([L_Rate, NodesL1, NodesL2, Batch_Size, Dropout, stopping_epoch,
                                       training_accuracy, validation_accuracy, testing_accuracy]))

  data_f = pd.DataFrame(data_array,
                        columns = ['L_Rate', 'NodesL1', 'NodesL2', 'Batch_Size', 'Dropout', 'stopping_epoch',
                                   'training_accuracy', 'validation_accuracy', 'testing_accuracy' ])
  data_f.to_csv('/Users/lilmaga/Desktop/NeuralNet/HyperparameterTune.csv')
