docstr: early_stop: set true for using early stop
  tenacity: patience for early_stop
docstr_tok: ["early_stop",":","set","true","for","using","early","stop","tenacity",":","patience","for","early_stop"]
code: def train_model_local(
      self, num_epochs=300, early_stop=False, tenacity=10
  ):
      """
      early_stop: set true for using early stop
      tenacity: patience for early_stop
      """
      print("Training Local Model(Tconv)")
      optimizer = optim.Adam(params=self.seq.parameters(), lr=self.lr)
      iter_count = 0
      loss_all = []
      min_val_loss = float("inf")
      scount = 0
      val_loss = 0
      inp_test, out_target_test, _, _ = self.D.supply_test()
      while self.D.epoch < num_epochs:
          last_epoch = self.D.epoch
          inp, out_target, _, _ = self.D.next_batch()
  
          current_epoch = self.D.epoch
          inp = Variable(inp)
          out_target = Variable(out_target)
          optimizer.zero_grad()
          out = self.seq(inp)
          loss = LocalModel.loss(out, out_target)
          iter_count = iter_count + 1
          for p in self.seq.parameters():
              p.requires_grad = True
          loss.backward()
          for p in self.seq.parameters():
              p.grad.data.clamp_(max=1e5, min=-1e5)
          optimizer.step()
  
          loss_all = loss_all + [loss.item()]
  
          if current_epoch > last_epoch:
              # validate:
              inp_test = Variable(inp_test)
              out_target_test = Variable(out_target_test)
              out_test = self.seq(inp_test)
  
              val_loss = LocalModel.loss(
                  out_test, out_target_test
              ).item()
              print("Entering Epoch:{}".format(current_epoch))
              print("Train Loss:{}".format(np.mean(loss_all)))
              print("Validation Loss:{}".format(val_loss))
              if val_loss <= min_val_loss:
                  min_val_loss = val_loss
                  scount = 0
                  self.saved_seq = pickle.loads(pickle.dumps(self.seq))
              else:
                  scount += 1
                  if scount > tenacity and early_stop:
                      self.seq = self.saved_seq
  
                      break
      return val_loss
  
imports: import pickle
  import numpy
  import torch
  import torch.nn
  import torch.optim
  from torch.autograd import Variable
  from torch.nn.utils import weight_norm
  from bigdl.chronos.model.tcmf.data_loader import TCMFDataLoader
  from bigdl.chronos.model.tcmf.time import TimeCovariates
  import logging
label_desc: Use image node_id 4 for calling the LocalModel obj's underlying member method code with example usage: obj.train_model_local(num_epochs, early_stop, tenacity) and returns: val_loss