<jupyter_start><jupyter_text>Plotting loss and accuracy curvesNotbook plots the loss and accuracy curves for the following case:1. No season gaps2. With season gaps3. Linear interpolation and season gaps4. GP and season gapsAuthor: <NAME> Date: 27th May 2020 Import all required python libraries<jupyter_code>from google.colab import drive
import os
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({'font.size': 16})<jupyter_output><empty_output><jupyter_text>Mount google drive Directory where the `python` files are stored on Google Drive. These imported files are also available in `.ipynb` format on the GitHub repo with same names (without the nb; `dr``0X_sameFileName`). nb stands for notebook.<jupyter_code>drive.mount('/content/gdrive')
os.chdir('/content/gdrive/My Drive/Colab Notebooks')

import nb_02i_preprocessing_data as dat
import nb_03_neural_network_designs as nnet<jupyter_output><empty_output><jupyter_text>Setting up the path to the data directory<jupyter_code>current_dir = os.getcwd()
data_dir = '/content/gdrive/My Drive/training_set_microlensing/'
save_image_dir = r'/content/gdrive/My Drive/Colab Notebooks/Images/'
save_model_dir = r'/content/gdrive/My Drive/Colab Notebooks/Models/'
save_data_dir = r'/content/gdrive/My Drive/Colab Notebooks/Data/'<jupyter_output><empty_output><jupyter_text>Parameters<jupyter_code>keywords = ['no_gaps', 'gaps', 'gaps_linear', 'gaps_gp']
valid_color = ['#8a7e7d', '#a9ad4c']<jupyter_output><empty_output><jupyter_text>Functions for plotting loss and accuracy curves<jupyter_code>def setLabels(ax, xlabel, ylabel, ticks, legend):
    """
    Function sets the axes labels, legent size, and ticks
    """
    ax.set_xlabel(xlabel, fontsize=20)
    ax.set_ylabel(ylabel, fontsize=20)
    if legend:
        ax.legend(fontsize=14)
    ax.grid()
    if ticks:
        ax.tick_params(which='both', labelsize=18)
    return

def plotLoss(ax, history, loss_Accuracy, model_name, linestyle, color, valid_color):
    """
    Function plots the loss curve for training run of x epochs
    @valid_color :: color code for the validation curve if multiple cases are plotted together
    """
    ax.plot(history[loss_Accuracy[0]], color=color, linestyle= linestyle, lw=2, label=model_name)
    ax.plot(history[loss_Accuracy[1]], valid_color, linestyle= linestyle, lw=2) 
    setLabels(ax, '', r'$\mathcal{L}\ \equiv$ Categorical cross entropy', ticks=True, legend=True)
    return

def plotAccuracy(ax, history, loss_Accuracy, model_name, linestyle, color, valid_color):
    """
    Function plots the accuracy curve for training run of x epochs
    @multiple_models :: boolean to check if multiple epochs are plotted together
    """
    ax.plot(history[loss_Accuracy[2]],  color=color, linestyle= linestyle, lw=2, label=model_name)
    ax.plot(history[loss_Accuracy[3]], valid_color, linestyle= linestyle, lw=2)
    setLabels(ax, 'Epoch', 'Accuracy', ticks=True, legend=True)
    return

def decideColor(input_word):
  " Function decides the color of the loss and accuracy curves"
  
  key_word_arr = ['no_gaps', 'gaps', 'gaps_linear', 'gaps_gp']
  colors_arr = ['#0786db', '#d60939', '#08c424','#5208c9']
  
  for idx, key_word in enumerate(key_word_arr):  
    if input_word == key_word:
      color = colors_arr[idx]
  return color

def plotLACurves(fig, ax, key_word, networks, model_name, valid_color):
  """
  Function plos the loss and accuracy curves
  @fig, ax :: figure and axis handles
  @key_word :: decides the type of curves that needs to be plotted (e.g. 'no_gaps')
  @networks :: array with [loss, valid_loss, acc, valid_acc] for the different networks
  @model_name :: arr with the names of the networks
  """
  color = decideColor(key_word)
  loss_Accuracy = [0, 1, 2, 3]
  
  linestyle = ['-.', ':', '-']

  # plotting the curves
  for i, num in enumerate(networks):
    plotLoss(ax[0], num, loss_Accuracy, model_name[i], linestyle[i], color, valid_color)
    plotAccuracy(ax[1], num, loss_Accuracy, model_name[i], linestyle[i], color, valid_color)

  fig.savefig(save_image_dir+'/LandA_%s.png'%key_word)    
  fig.tight_layout()
  return<jupyter_output><empty_output><jupyter_text>1. Plotting Loss and Accuracy curves for the case without season gaps<jupyter_code>cnn_no_gaps = np.load(save_data_dir+'noSeasonGaps_CNN.npy')
# [todo: generate this file again] resnet_7_no_gaps = np.load(save_data_dir+'noSeasonGaps_ResNet7.npy')
resnet_18_no_gaps = np.load(save_data_dir+'noSeasonGaps_ResNet18.npy')
no_gaps = [cnn_no_gaps, resnet_7_no_gaps, resnet_18_no_gaps]
model_name_no_gaps = ['CNN', 'ResNet-7', 'ResNet-18']

# plots the history
fig, ax = plt.subplots(2, 1, figsize=(7, 10))
plotLACurves(fig, ax, keywords[0], no_gaps, model_name_no_gaps, valid_color[0])<jupyter_output><empty_output><jupyter_text>2. Plots Loss and Accuracy curves for the fitted light curves with season gaps<jupyter_code># loading loss-accuracy files for GPR (Gaussian processes regression)
resnet_7_gp_gaps = np.load(save_data_dir+'SeasonGaps_GP_ResNet7.npy')
resnet_18_gp_gaps = np.load(save_data_dir+'SeasonGaps_GP_ResNet18.npy')

# loading loss-accuracy files for LI (Linear Interpolation)
resnet_7_li_gaps = np.load(save_data_dir+'SeasonGaps_LI_ResNet7.npy')
resnet_18_li_gaps = np.load(save_data_dir+'SeasonGaps_LI_ResNet18.npy')
gpr_gaps = [resnet_7_gp_gaps, resnet_18_gp_gaps]
model_name_gpr = ['GPR: ResNet-7', 'GPR: ResNet-18']
valid_colors = ['#8a7e7d', '#a9ad4c']

fig, ax = plt.subplots(2, 1, figsize=(7, 10))
# plots the curves for GPR
plotLACurves(fig, ax, keywords[3], gpr_gaps, model_name_gpr, valid_color[0])

li_gaps = [resnet_7_li_gaps, resnet_18_li_gaps]
model_name_li  = ['LI: ResNet-7', 'LI: ResNet-18']

# plots the curves for LI
plotLACurves(fig, ax, keywords[2], li_gaps, model_name_li, valid_color[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11656652360515021, 'fraction_numerical': 0.020944206008583693, 'mean_word_length': 5.782305005820722, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '75887', 'license': 'MIT', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2101, 'n_words': 508, 'path': '09_Plotting_combined_Loss_Accuracy_graphs.ipynb', 'repo_name': 'sophiestucki/Microlensing_with_NeuralNets', 'size': '5834'}