code: def main(args):
      # Step 1: Prepare graph data and retrieve train/validation/test index ============================= #
      dataset = LegacyTUDataset(args.dataset, raw_dir=args.dataset_path)
  
      # add self loop. We add self loop for each graph here since the function "add_self_loop" does not
      # support batch graph.
      for i in range(len(dataset)):
          dataset.graph_lists[i] = dgl.remove_self_loop(
              dataset.graph_lists[i]
          )
          dataset.graph_lists[i] = dgl.add_self_loop(
              dataset.graph_lists[i]
          )
  
      # use degree as node feature
      if args.degree_as_feature:
          dataset = degree_as_feature(dataset)
          mode = "concat"
      else:
          mode = "replace"
      dataset = node_label_as_feature(dataset, mode=mode)
  
      num_training = int(len(dataset) * 0.8)
      num_val = int(len(dataset) * 0.1)
      num_test = len(dataset) - num_training - num_val
      train_set, val_set, test_set = random_split(
          dataset, [num_training, num_val, num_test]
      )
  
      train_loader = GraphDataLoader(
          train_set,
          batch_size=args.batch_size,
          shuffle=True,
          num_workers=1,
      )
      val_loader = GraphDataLoader(
          val_set, batch_size=args.batch_size, num_workers=1
      )
      test_loader = GraphDataLoader(
          test_set, batch_size=args.batch_size, num_workers=1
      )
  
      device = torch.device(args.device)
  
      # Step 2: Create model =================================================================== #
      num_feature, num_classes, _ = dataset.statistics()
      args.in_dim = int(num_feature)
      args.out_dim = int(num_classes)
      args.edge_feat_dim = 0  # No edge feature in datasets that we use.
  
      model = GraphClassifier(args).to(device)
  
      # Step 3: Create training components ===================================================== #
      optimizer = torch.optim.Adam(
          model.parameters(),
          lr=args.lr,
          amsgrad=True,
          weight_decay=args.weight_decay,
      )
  
      # Step 4: training epoches =============================================================== #
      best_test_acc = 0.0
      best_epoch = -1
      train_times = []
  
      bad_count = 0
      best_val_loss = float("inf")
      for e in range(args.epochs):
          s_time = time()
          train_loss = train(
              model, optimizer, train_loader, device, e, args.epochs
          )
          train_times.append(time() - s_time)
          _, val_loss = validate(
              model, val_loader, device, e, args.epochs
          )
          test_acc = test(model, test_loader, device)
  
          if best_val_loss > val_loss:
              best_val_loss = val_loss
              best_epoch = e
              bad_count = 0
              best_test_acc = test_acc
          else:
              bad_count += 1
  
          if bad_count > args.patience:
              break
  
          if (e + 1) % args.print_every == 0:
              log_format = "Epoch {}: loss={:.4f}, test_acc={:.4f}, best_test_acc={:.4f}"
              print(
                  log_format.format(
                      e + 1, train_loss, test_acc, best_test_acc
                  )
              )
      print(
          "Best Epoch {}, final test acc {:.4f}".format(
              best_epoch, best_test_acc
          )
      )
      return best_test_acc, sum(train_times) / len(train_times)
  
file_path: dgl/examples/pytorch/gxn/main_early_stop.py
imports: import json
  import os
  from datetime import datetime
  from time import time
  import dgl
  import torch
  import torch.nn.functional
  from data_preprocess import degree_as_feature, node_label_as_feature
  from dgl.data import LegacyTUDataset
  from dgl.dataloading import GraphDataLoader
  from networks import GraphClassifier
  from torch import Tensor
  from torch.utils.data import random_split
  from utils import get_stats, parse_args
label_desc: Use image node_id 5 for calling a global function with example usage: main(args) and returns: best_test_acc,  unknown