class_docstr: The method class for the JTVAE algorithm proposed in the paper `Junction Tree Variational Autoencoder for Molecular Graph Generation <https://arxiv.org/abs/1802.04364>`_. This class provides interfaces for running random generation with the JTVAE algorithm. Please refer to the `example codes <https://github.com/divelab/DIG/tree/dig-stable/examples/ggraph/JTVAE>`_ for usage examples.
  
  Args:
      list_smiles (list): List of smiles in training data.
      training (boolean): If we are training (as opposed to testing).
      build_vocab (boolean): If we need to build the vocabulary (first time training with this dataset).
      device (torch.device, optional): The device where the model is deployed.
docstr: Train the Junction Tree Variational Autoencoder for the constrained optimization task.
  
  Args:
      loader (MolTreeFolder): The MolTreeFolder loader.
      batch_size (int): The batch size.
      num_epochs (int): The number of epochs.
      hidden_size (int): The hidden size.
      latent_size (int): The latent size.
      depth (int): The depth of the network.
      lr (float): The learning rate for training.
      beta (float): The KL regularization weight.
docstr_tok: ["Train","the","Junction","Tree","Variational","Autoencoder","for","the","constrained","optimization","task",".","Args",":","loader","(","MolTreeFolder",")",":","The","MolTreeFolder","loader",".","batch_size","(","int",")",":","The","batch","size",".","num_epochs","(","int",")",":","The","number","of","epochs",".","hidden_size","(","int",")",":","The","hidden","size",".","latent_size","(","int",")",":","The","latent","size",".","depth","(","int",")",":","The","depth","of","the","network",".","lr","(","float",")",":","The","learning","rate","for","training",".","beta","(","float",")",":","The","KL","regularization","weight","."]
code: def train_cons_optim(
      self,
      loader,
      batch_size,
      num_epochs,
      hidden_size,
      latent_size,
      depth,
      beta,
      lr,
  ):
      r"""
      Train the Junction Tree Variational Autoencoder for the constrained optimization task.
  
      Args:
          loader (MolTreeFolder): The MolTreeFolder loader.
          batch_size (int): The batch size.
          num_epochs (int): The number of epochs.
          hidden_size (int): The hidden size.
          latent_size (int): The latent size.
          depth (int): The depth of the network.
          lr (float): The learning rate for training.
          beta (float): The KL regularization weight.
  
      """
      for param in self.prop_vae.parameters():
          if param.dim() == 1:
              nn.init.constant(param, 0)
          else:
              nn.init.xavier_normal(param)
  
      print(
          "Model #Params: %dK"
          % (
              sum([x.nelement() for x in self.prop_vae.parameters()])
              / 1000,
          )
      )
  
      optimizer = optim.Adam(self.prop_vae.parameters(), lr=lr)
      scheduler = lr_scheduler.ExponentialLR(optimizer, 0.9)
      scheduler.step()
  
      PRINT_ITER = 20
  
      for epoch in range(num_epochs):
          word_acc, topo_acc, assm_acc, steo_acc, prop_acc = (
              0,
              0,
              0,
              0,
              0,
          )
  
          for it, batch in enumerate(tqdm(loader)):
              for mol_tree, _ in batch:
                  for node in mol_tree.nodes:
                      if node.label not in node.cands:
                          node.cands.append(node.label)
                          node.cand_mols.append(node.label_mol)
  
              self.prop_vae.zero_grad()
              (
                  loss,
                  kl_div,
                  wacc,
                  tacc,
                  sacc,
                  dacc,
                  pacc,
              ) = self.prop_vae(batch, beta)
              loss.backward()
              optimizer.step()
  
              word_acc += wacc
              topo_acc += tacc
              assm_acc += sacc
              steo_acc += dacc
              prop_acc += pacc
  
              if (it + 1) % PRINT_ITER == 0:
                  word_acc = word_acc / PRINT_ITER * 100
                  topo_acc = topo_acc / PRINT_ITER * 100
                  assm_acc = assm_acc / PRINT_ITER * 100
                  steo_acc = steo_acc / PRINT_ITER * 100
                  prop_acc /= PRINT_ITER
  
                  print(
                      "KL: %.1f, Word: %.2f, Topo: %.2f, Assm: %.2f, Steo: %.2f, Prop: %.4f"
                      % (
                          kl_div,
                          word_acc,
                          topo_acc,
                          assm_acc,
                          steo_acc,
                          prop_acc,
                      )
                  )
                  word_acc, topo_acc, assm_acc, steo_acc, prop_acc = (
                      0,
                      0,
                      0,
                      0,
                      0,
                  )
                  sys.stdout.flush()
  
              if (it + 1) % 1500 == 0:  # Fast annealing
                  scheduler.step()
                  print("learning rate: %.6f" % scheduler.get_lr()[0])
          #                     torch.save(self.prop_vae.state_dict(), opts.save_path + "/model.iter-%d-%d" % (epoch, it + 1))
  
          scheduler.step()
          print("learning rate: %.6f" % scheduler.get_lr()[0])
  
imports: import math
  import sys
  from optparse import OptionParser
  import numpy
  import rdkit
  from rdkit import RDLogger, Chem
  from rdkit.Chem import Descriptors
  from tqdm import tqdm
  import torch
  import torch.nn
  import torch.optim
  import torch.optim.lr_scheduler
  from dig.ggraph.method import Generator
  from .None import fast_jtnn
label_desc: Use image node_id 8 for calling the JTVAE obj's underlying member method code with example usage: obj.train_cons_optim(loader, batch_size, num_epochs, hidden_size, latent_size, depth, beta, lr) without return types