class_docstr: Defines end to end MatchLSTM and Answer_Pointer network for Reading Comprehension
class_docstr_tok: ["Defines","end","to","end","MatchLSTM","and","Answer_Pointer","network","for","Reading","Comprehension"]
docstr: Function to run training/validation loop and display training loss, F1 & EM scores
  
  Args:
      session: tensorflow session
      train:   data dictionary for training/validation
      dropout: float value
      mode: 'train'/'val'
docstr_tok: ["Function","to","run","training\/validation","loop","and","display","training","loss",",","F1","&","EM","scores","Args",":","session",":","tensorflow","session","train",":","data","dictionary","for","training\/validation","dropout",":","float","value","mode",":","'train'\/'val","'"]
code: def run_loop(self, session, train, mode="train", dropout=0.6):
      """
      Function to run training/validation loop and display training loss, F1 & EM scores
  
      Args:
          session: tensorflow session
          train:   data dictionary for training/validation
          dropout: float value
          mode: 'train'/'val'
      """
  
      nbatches = int((len(train["para"]) / self.batch_size))
      f1_score = 0
      em_score = 0
      for idx in range(nbatches):
          # Train for all batches
          start_batch = self.batch_size * idx
          end_batch = self.batch_size * (idx + 1)
          if end_batch > len(train["para"]):
              break
  
          # Create feed dictionary
          feed_dict_qa = {
              self.para_ids: np.asarray(
                  train["para"][start_batch:end_batch]
              ),
              self.question_ids: np.asarray(
                  train["question"][start_batch:end_batch]
              ),
              self.para_length: np.asarray(
                  train["para_len"][start_batch:end_batch]
              ),
              self.question_length: np.asarray(
                  train["question_len"][start_batch:end_batch]
              ),
              self.labels: np.asarray(
                  train["answer"][start_batch:end_batch]
              ),
              self.para_mask: np.asarray(
                  train["para_mask"][start_batch:end_batch]
              ),
              self.ques_mask: np.asarray(
                  train["question_mask"][start_batch:end_batch]
              ),
              self.dropout: dropout,
          }
          # Training Phase
          if mode == "train":
              _, train_loss, _, logits, labels = session.run(
                  [
                      self.optimizer,
                      self.loss,
                      self.learning_rate,
                      self.logits_withsf,
                      self.labels,
                  ],
                  feed_dict=feed_dict_qa,
              )
  
              if idx % 20 == 0:
                  print(
                      "iteration = {}, train loss = {}".format(
                          idx, train_loss
                      )
                  )
                  f1_score, em_score = self.cal_f1_score(labels, logits)
                  print("F-1 and EM Scores are", f1_score, em_score)
  
              self.global_step.assign(self.global_step + 1)
  
          else:
              logits, labels = session.run(
                  [self.logits_withsf, self.labels],
                  feed_dict=feed_dict_qa,
              )
  
              f1_score_int, em_score_int = self.cal_f1_score(
                  labels, logits
              )
              f1_score += f1_score_int
              em_score += em_score_int
  
      # Validation Phase
      if mode == "val":
          print(
              "Validation F1 and EM scores are",
              f1_score / nbatches,
              em_score / nbatches,
          )
  
file_path: nlp-architect/examples/reading_comprehension/match_lstm_mrc/matchlstm_ansptr.py
imports: from __future__ import division
  from __future__ import print_function
  import re
  from collections import Counter
  import numpy
  import tensorflow
  from nlp_architect.utils.text import SpacyInstance
label_desc: Use image node_id 9 for calling the MatchLSTMAnswerPointer obj's underlying member method code with example usage: obj.run_loop(session, train, mode, dropout) without return types