code: def run(
      args,
      model,
      device,
      optimizer,
      train_loader,
      test_loader,
      lr_scheduler,
  ):
      # using Triplet Margin Loss
      criterion = nn.TripletMarginLoss(p=2, margin=2.8)
  
      # define model training step
      def train_step(engine, batch):
          model.train()
          (
              anchor_image,
              positive_image,
              negative_image,
              anchor_label,
          ) = batch
          anchor_image = anchor_image.to(device)
          positive_image, negative_image = positive_image.to(
              device
          ), negative_image.to(device)
          anchor_label = anchor_label.to(device)
          optimizer.zero_grad()
          anchor_out, positive_out, negative_out = model(
              anchor_image, positive_image, negative_image
          )
          loss = criterion(anchor_out, positive_out, negative_out)
          loss.backward()
          optimizer.step()
          return loss
  
      # define model testing step
      def test_step(engine, batch):
          model.eval()
          with torch.no_grad():
              anchor_image, _, _, anchor_label = batch
              anchor_image = anchor_image.to(device)
              anchor_label = anchor_label.to(device)
              other_image = []
              other_label = []
              y_true = []
              for i in range(anchor_image.shape[0]):
                  index = torch.randint(
                      0, anchor_image.shape[0], (1,)
                  ).item()
                  img = anchor_image[index]
                  label = anchor_label[index]
                  other_image.append(img)
                  other_label.append(label)
                  if anchor_label[i] == other_label[i]:
                      y_true.append(1)
                  else:
                      y_true.append(0)
              other = torch.stack(other_image)
              other_label = torch.tensor(other_label)
              other, other_label = other.to(device), other_label.to(
                  device
              )
              anchor_out, other_out, _ = model(
                  anchor_image, other, other
              )
              test_loss = calculate_loss(anchor_out, other_out)
              y_pred = torch.where(test_loss < 3, 1, 0)
              y_true = torch.tensor(y_true)
              return [y_pred, y_true]
  
      # create engines for trainer and evaluator
      trainer = Engine(train_step)
      evaluator = Engine(test_step)
  
      # attach Running Average Loss metric to trainer and evaluator engines
      RunningAverage(output_transform=lambda x: x).attach(
          trainer, "loss"
      )
      Accuracy(output_transform=lambda x: x).attach(
          evaluator, "accuracy"
      )
  
      # attach progress bar to trainer with loss
      pbar1 = ProgressBar()
      pbar1.attach(trainer, metric_names=["loss"])
  
      # attach progress bar to evaluator
      pbar2 = ProgressBar()
      pbar2.attach(evaluator)
  
      # attach LR Scheduler to trainer engine
      trainer.add_event_handler(Events.ITERATION_STARTED, lr_scheduler)
  
      # event handler triggers evauator at end of every epoch
      @trainer.on(Events.EPOCH_COMPLETED(every=args.log_interval))
      def test(engine):
          state = evaluator.run(test_loader)
          print(f'Test Accuracy: {state.metrics["accuracy"]}')
  
      # run the trainer
      trainer.run(train_loader, max_epochs=args.epochs)
  
imports: import argparse
  import numpy
  import torch
  import torch.nn
  import torch.optim
  import torchvision
  from torch.optim.lr_scheduler import StepLR
  from torch.utils.data import DataLoader, Dataset
  from torchvision import datasets
  from ignite.contrib.handlers import ProgressBar
  from ignite.engine import Engine, Events
  from ignite.handlers.param_scheduler import LRScheduler
  from ignite.metrics import Accuracy, RunningAverage
  from ignite.utils import manual_seed
label_desc: Use image node_id 3 for calling a global function with example usage: run(args, model, device, optimizer, train_loader, test_loader, lr_scheduler) and returns: loss, list