code: def test_tree_decoder(seed, save_every):
      """loading data"""
      data_dir = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph4nlp\ \
          pytorch\test\generation\tree_decoder\data\jobs640"
      src_vocab_file = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph4nlp \
          \pytorch\test\generation\tree_decoder\data\jobs640\vocab.q.txt"
      tgt_vocab_file = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph4nlp \
          \pytorch\test\generation\tree_decoder\data\jobs640\vocab.f.txt"
      data_file = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph4nlp\ \
          pytorch\test\generation\tree_decoder\data\jobs640\test.txt"
      checkpoint_dir = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph4nlp\ \
          pytorch\test\generation\tree_decoder\checkpoint_dir"
  
      mode = "test"
      min_freq = 2
      max_vocab_size = 10000
      batch_size = 1
      device = None
      max_dec_seq_length = 220
      max_dec_tree_depth = 100
      use_copy = True
      # use_coverage = True
  
      test_data_loader = DataLoader(
          data_dir=data_dir,
          use_copy=use_copy,
          src_vocab_file=src_vocab_file,
          tgt_vocab_file=tgt_vocab_file,
          data_file=data_file,
          mode=mode,
          min_freq=min_freq,
          max_vocab_size=max_vocab_size,
          batch_size=batch_size,
          device=device,
      )
      # print(test_data_loader.share_vocab.idx2symbol)
      # print("test samples number : ", test_data_loader.num_batch)
  
      num_batch = 25
      # model_num = save_every * num_batch - 1
      model_num = 1199
  
      if use_copy:
          # enc_emb_size = 300
          # tgt_emb_size = 300
          enc_hidden_size = 150
          dec_hidden_size = 300
      else:
          # enc_emb_size = 150
          # tgt_emb_size = 150
          enc_hidden_size = 150
          dec_hidden_size = 300
  
      max_acc = 0
      while True:
          random.seed(seed)
          np.random.seed(seed)
          torch.manual_seed(seed)
          torch.cuda.manual_seed(seed)
          torch.backends.cudnn.deterministic = True
          try:
              checkpoint = torch.load(
                  "{}/s2t".format(checkpoint_dir) + str(model_num)
              )
              print("model : s2t" + str(model_num))
          except BaseException:
              break
          encoder = checkpoint["encoder"]
          tree_decoder = checkpoint["decoder"]
  
          encoder.eval()
          tree_decoder.eval()
  
          reference_list = []
          candidate_list = []
  
          data = test_data_loader.data
          for i in range(len(data)):
              x = data[i]
              reference = torch.tensor(x[1], dtype=torch.long)
              input_word_list = x[0]
              # print(test_data_loader.tgt_vocab.get_idx_symbol_for_list(reference))
              candidate = do_generate(
                  use_copy,
                  enc_hidden_size,
                  dec_hidden_size,
                  encoder,
                  tree_decoder,
                  input_word_list,
                  test_data_loader.src_vocab,
                  test_data_loader.tgt_vocab,
                  device,
                  max_dec_seq_length,
                  max_dec_tree_depth,
              )
              candidate = [int(c) for c in candidate]
              num_left_paren = sum(
                  1
                  for c in candidate
                  if test_data_loader.tgt_vocab.idx2symbol[int(c)]
                  == "("
              )
              num_right_paren = sum(
                  1
                  for c in candidate
                  if test_data_loader.tgt_vocab.idx2symbol[int(c)]
                  == ")"
              )
              diff = num_left_paren - num_right_paren
              if diff > 0:
                  for _ in range(diff):
                      candidate.append(
                          test_data_loader.tgt_vocab.symbol2idx[")"]
                      )
              elif diff < 0:
                  candidate = candidate[:diff]
              # ref_str = convert_to_string(reference, test_data_loader.tgt_vocab)
              # cand_str = convert_to_string(candidate, test_data_loader.tgt_vocab)
              reference_list.append(reference)
              candidate_list.append(candidate)
              # print(cand_str)
  
          val_acc = compute_tree_accuracy(
              candidate_list, reference_list, test_data_loader.tgt_vocab
          )
          print("ACCURACY = {}\n".format(val_acc))
          if val_acc >= max_acc:
              max_acc = val_acc
              max_index = model_num
  
          model_num += save_every * num_batch
  
      print("max accuracy:", max_acc)
      best_model = torch.load(checkpoint_dir + "s2t" + str(max_index))
      torch.save(best_model, checkpoint_dir + "best_model")
  
imports: import random
  import time
  import warnings
  import numpy
  import torch
  import torch.nn.functional
  import torch.nn.init
  from torch import nn, optim
  from ....modules.prediction.generation.TreeBasedDecoder import StdTreeDecoder, create_mask
  from ....modules.utils.tree_utils import DataLoader, Tree, to_cuda
label_desc: Use image node_id 2 for calling a global function with example usage: test_tree_decoder(seed, save_every) without return types