code: def train_model(epoch, model, train):
      model.train()
      args = model.args
  
      unroll_size = args.unroll_size
      batch_size = args.batch_size
      N = (len(train[0]) - 1) // unroll_size + 1
      lr = args.lr
  
      total_loss = 0.0
      criterion = nn.CrossEntropyLoss(size_average=False)
      hidden = model.init_hidden(batch_size)
      for i in range(N):
          x = train[0][i * unroll_size : (i + 1) * unroll_size]
          y = train[1][i * unroll_size : (i + 1) * unroll_size].view(-1)
          x, y = Variable(x), Variable(y)
          hidden = (
              (Variable(hidden[0].data), Variable(hidden[1].data))
              if args.lstm
              else Variable(hidden.data)
          )
  
          model.zero_grad()
          output, hidden = model(x, hidden)
          assert x.size(1) == batch_size
          loss = criterion(output, y) / x.size(1)
          loss.backward()
  
          torch.nn.utils.clip_grad_norm(
              model.parameters(), args.clip_grad
          )
          for p in model.parameters():
              if p.requires_grad:
                  if args.weight_decay > 0:
                      p.data.mul_(1.0 - args.weight_decay)
                  p.data.add_(-lr, p.grad.data)
  
          if math.isnan(loss.item()) or math.isinf(loss.item()):
              sys.exit(0)
              return
  
          total_loss += loss.item() / x.size(0)
          if i % 10 == 0:
              sys.stdout.write("\r{}".format(i))
              sys.stdout.flush()
  
      return np.exp(total_loss / N)
  
imports: import sys
  import os
  import argparse
  import time
  import random
  import math
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.autograd import Variable
  import sru
label_desc: Use image node_id 3 for calling a global function with example usage: train_model(epoch, model, train) and returns: np