code: def train_one_epoch(model, dataloader, optimizer, args, epoch):
      model.train()
      tloss = 0.0
      tcnt = 0.0
      st_time = time.time()
      with tqdm(
          dataloader, desc="Train Ep " + str(epoch), mininterval=60
      ) as tq:
          for batch in tq:
              pred = model(batch)
              nll_loss = F.nll_loss(
                  pred.view(-1, pred.shape[-1]),
                  batch["tgt_text"].view(-1),
                  ignore_index=0,
              )
              loss = nll_loss
              optimizer.zero_grad()
              loss.backward()
              nn.utils.clip_grad_norm_(model.parameters(), args.clip)
              optimizer.step()
              loss = loss.item()
              if loss != loss:
                  raise ValueError("NaN appear")
              tloss += loss * len(batch["tgt_text"])
              tcnt += len(batch["tgt_text"])
              tq.set_postfix({"loss": tloss / tcnt}, refresh=False)
      print(
          "Train Ep ",
          str(epoch),
          "AVG Loss ",
          tloss / tcnt,
          "Steps ",
          tcnt,
          "Time ",
          time.time() - st_time,
          "GPU",
          torch.cuda.max_memory_cached() / 1024.0 / 1024.0 / 1024.0,
      )
      torch.save(model, args.save_model + str(epoch % 100))
  
file_path: dgl/examples/pytorch/graphwriter/train.py
imports: import os
  import sys
  import time
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from graphwriter import *
  from opts import *
  from tqdm import tqdm
  from utlis import *
  from pycocoevalcap.bleu.bleu import Bleu
  from pycocoevalcap.meteor.meteor import Meteor
  from pycocoevalcap.rouge.rouge import Rouge
label_desc: Use image node_id 1 for calling a global function with example usage: train_one_epoch(model, dataloader, optimizer, args, epoch) without return types