code: def train_tree_decoder(seed, save_every):
      seed = seed
      random.seed(seed)
      np.random.seed(seed)
      torch.manual_seed(seed)
      torch.cuda.manual_seed(seed)
      torch.backends.cudnn.deterministic = True
  
      """loading data"""
      data_dir = r"/home/lishucheng/Graph4AI/graph4ai/graph4nlp/pytorch/test/generation/tree_decoder/data/jobs640/"  # noqa
      src_vocab_file = r"/home/lishucheng/Graph4AI/graph4ai/graph4nlp/pytorch/test/generation/tree_decoder/data/jobs640/vocab.q.txt"  # noqa
      tgt_vocab_file = r"/home/lishucheng/Graph4AI/graph4ai/graph4nlp/pytorch/test/generation/tree_decoder/data/jobs640/vocab.f.txt"  # noqa
      data_file = r"/home/lishucheng/Graph4AI/graph4ai/graph4nlp/pytorch/test/generation/tree_decoder/data/jobs640/train.txt"  # noqa
      mode = "train"
      min_freq = 2
      max_vocab_size = 10000
      batch_size = 20
      device = None
      use_copy = True
      use_coverage = True
      train_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(train_data_loader.share_vocab.idx2symbol)
      # print(train_data_loader.share_vocab.idx2symbol)
  
      """For encoder-decoder"""
      if use_copy:
          enc_emb_size = 300
          tgt_emb_size = 300
          embeddings = nn.Embedding(
              train_data_loader.share_vocab.vocab_size,
              enc_emb_size,
              padding_idx=train_data_loader.share_vocab.get_symbol_idx(
                  train_data_loader.share_vocab.pad_token
              ),
          )
          input_size = train_data_loader.share_vocab.vocab_size
          output_size = train_data_loader.share_vocab.vocab_size
          enc_hidden_size = 150
          dec_hidden_size = 300
      else:
          embeddings = None
          input_size = train_data_loader.src_vocab.vocab_size
          output_size = train_data_loader.tgt_vocab.vocab_size
          enc_emb_size = 150
          tgt_emb_size = 150
          enc_hidden_size = 150
          dec_hidden_size = 300
  
      enc_dropout_input = 0.1
      enc_dropout_output = 0.3
      dec_dropout_input = 0.1
      # dec_dropout_input = 0
      dec_dropout_output = 0.3
      attn_dropout = 0.1
      # teacher_force_ratio = 0.3
      teacher_force_ratio = 1
      max_dec_seq_length = 220
      max_dec_tree_depth = 220
  
      encoder = SequenceEncoder(
          input_size=input_size,
          enc_emb_size=enc_emb_size,
          enc_hidden_size=enc_hidden_size,
          dec_hidden_size=dec_hidden_size,
          pad_idx=train_data_loader.src_vocab.get_symbol_idx(
              train_data_loader.src_vocab.pad_token
          ),
          dropout_input=enc_dropout_input,
          dropout_output=enc_dropout_output,
          encode_rnn_num_layer=1,
          embeddings=embeddings,
      )
  
      attention_type = "uniform"
  
      # criterion = nn.NLLLoss(size_average=False,
      # ignore_index=train_data_loader.tgt_vocab.get_symbol_idx(
      #   train_data_loader.tgt_vocab.pad_token))
      criterion = nn.NLLLoss(size_average=False)
  
      attn_unit = AttnUnit(
          dec_hidden_size, output_size, attention_type, attn_dropout
      )
  
      if not use_copy:
          tree_decoder = StdTreeDecoder(
              attn=attn_unit,
              attn_type="uniform",
              embeddings=embeddings,
              enc_hidden_size=enc_hidden_size,
              dec_emb_size=tgt_emb_size,
              dec_hidden_size=dec_hidden_size,
              output_size=output_size,
              device=device,
              criterion=criterion,
              teacher_force_ratio=teacher_force_ratio,
              use_sibling=True,
              use_attention=True,
              use_copy=use_copy,
              use_coverage=use_coverage,
              fuse_strategy="average",
              num_layers=1,
              dropout_input=dec_dropout_input,
              dropout_output=dec_dropout_output,
              rnn_type="lstm",
              max_dec_seq_length=max_dec_seq_length,
              max_dec_tree_depth=max_dec_tree_depth,
              tgt_vocab=train_data_loader.tgt_vocab,
          )
      else:
          tree_decoder = StdTreeDecoder(
              attn=attn_unit,
              attn_type="uniform",
              embeddings=embeddings,
              enc_hidden_size=enc_hidden_size,
              dec_emb_size=tgt_emb_size,
              dec_hidden_size=dec_hidden_size,
              output_size=output_size,
              device=device,
              criterion=criterion,
              teacher_force_ratio=teacher_force_ratio,
              use_sibling=True,
              use_attention=True,
              use_copy=use_copy,
              use_coverage=use_coverage,
              fuse_strategy="average",
              num_layers=1,
              dropout_input=dec_dropout_input,
              dropout_output=dec_dropout_output,
              rnn_type="lstm",
              max_dec_seq_length=max_dec_seq_length,
              max_dec_tree_depth=max_dec_tree_depth,
              tgt_vocab=train_data_loader.tgt_vocab,
          )
  
      to_cuda(encoder, device)
      to_cuda(tree_decoder, device)
  
      init_weight = 0.08
  
      print("encoder initializing...")
      for name, param in encoder.named_parameters():
          print(name, param.size())
          if param.requires_grad:
              if ("embedding.weight" in name) or (
                  "bert_embedding" in name
              ):
                  pass
              else:
                  if len(param.size()) >= 2:
                      if "rnn" in name:
                          init.orthogonal_(param)
                      else:
                          init.xavier_uniform_(param, gain=1.0)
                  else:
                      init.uniform_(param, -init_weight, init_weight)
  
      print("decoder initializing...")
      for name, param in tree_decoder.named_parameters():
          print(name, param.size())
          if param.requires_grad:
              if "rnn" in name and len(param.size()) >= 2:
                  init.orthogonal_(param)
              else:
                  init.uniform_(param, -init_weight, init_weight)
  
      max_epochs = 300
      epoch = 0
      grad_clip = 5
      optim_state = {"learningRate": 1e-3, "weight_decay": 1e-5}
  
      print("using adam")
      encoder_optimizer = optim.Adam(
          encoder.parameters(),
          lr=optim_state["learningRate"],
          weight_decay=optim_state["weight_decay"],
      )
  
      decoder_optimizer = optim.Adam(
          tree_decoder.parameters(), lr=optim_state["learningRate"]
      )
  
      print("Starting training.")
      encoder.train()
      tree_decoder.train()
  
      iterations = max_epochs * train_data_loader.num_batch
      start_time = time.time()
  
      # best_val_acc = 0
      checkpoint_dir = r"C:\Users\shuchengli\Desktop\Code\g4nlp\graph4nlp\graph\
          4nlp\pytorch\test\generation\tree_decoder\checkpoint_dir"
  
      print("Batch number per Epoch:", train_data_loader.num_batch)
      print_every = train_data_loader.num_batch
  
      loss_to_print = 0
      for i in range(iterations):
          if (i + 1) % train_data_loader.num_batch == 0:
              epoch += 1
  
          epoch = i // train_data_loader.num_batch
  
          encoder_optimizer.zero_grad()
          decoder_optimizer.zero_grad()
          (
              enc_batch,
              enc_len_batch,
              dec_tree_batch,
          ) = train_data_loader.random_batch()
  
          output_, hidden_ = encoder(enc_batch)
          encode_output_dict = {
              "graph_node_embedding": None,
              "graph_node_mask": None,
              "graph_edge_embedding": None,
              "rnn_node_embedding": output_,
              "graph_level_embedding": hidden_,
              "graph_edge_mask": None,
          }
          loss = tree_decoder(
              encode_output_dict, dec_tree_batch, enc_batch
          )
  
          loss.backward()
          torch.nn.utils.clip_grad_value_(
              encoder.parameters(), grad_clip
          )
          torch.nn.utils.clip_grad_value_(
              tree_decoder.parameters(), grad_clip
          )
          encoder_optimizer.step()
          decoder_optimizer.step()
  
          loss_to_print += loss
  
          if (i + 1) % print_every == 0:
              end_time = time.time()
              print(
                  (
                      "{}/{}, train_loss = {}, epochs = {}, time since last print = {}".format(
                          i,
                          iterations,
                          (loss_to_print / print_every),
                          epoch,
                          (end_time - start_time) / 60,
                      )
                  )
              )
              loss_to_print = 0
              start_time = time.time()
  
          if i == iterations - 1 or (
              (i + 1) % (save_every * train_data_loader.num_batch) == 0
          ):
              print("saving model...")
              checkpoint = {}
              checkpoint["encoder"] = encoder
              checkpoint["decoder"] = tree_decoder
              checkpoint["i"] = i
              checkpoint["epoch"] = epoch
              torch.save(
                  checkpoint, "{}/s2t".format(checkpoint_dir) + str(i)
              )
  
          if loss != loss:
              print("loss is NaN.  This usually indicates a bug.")
              break
  
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 1 for calling a global function with example usage: train_tree_decoder(seed, save_every) without return types