text: # -*- coding: utf-8 -*-
  #  @Time    : 2020-05-22 10:46
  #  @Author  : Shupeng
  
  import time
  
  
  def run_epoch(data_iter, model, loss_compute):
      "Standard training and logging function"
      start = time.time()
      total_tokens = 0
      total_loss = 0
      tokens = 0
  
      for i, batch in enumerate(data_iter):
          out = model.forward(batch.src, batch.tgt,
                              batch.src_mask, batch.tgt_mask)
          loss = loss_compute(out, batch.tgt_y, batch.ntokens)
          total_loss += loss
          total_tokens += batch.ntokens
          tokens += batch.ntokens
          if i % 50 == 1:
              elapsed = time.time() - start
  
              print("Epoch Step: %d Loss: %f Tokens per Sec: %f" %
                    (i, loss / batch.ntokens, tokens / elapsed))
              start = time.time()
              tokens = 0
  
      return total_loss / total_tokens
  