class_docstr: The method class for GraphAF algorithm proposed in the paper `GraphAF: a Flow-based Autoregressive Model for Molecular Graph Generation <https://arxiv.org/abs/2001.09382>`_. This class provides interfaces for running random generation, property
  optimization, and constrained optimization with GraphAF. Please refer to the `example codes <https://github.com/divelab/DIG/tree/dig-stable/examples/ggraph/GraphAF>`_ for usage examples.
class_docstr_tok: ["The","method","class","for","GraphAF","algorithm","proposed","in","the","paper","`","GraphAF",":","a","Flow-based","Autoregressive","Model","for","Molecular","Graph","Generation","<","https",":","\/\/arxiv.org\/abs\/2001.09382",">","`","_",".","This","class","provides","interfaces","for","running","random","generation",",","property","optimization",",","and","constrained","optimization","with","GraphAF",".","Please","refer","to","the","`","example","codes","<","https",":","\/\/github.com\/divelab\/DIG\/tree\/dig-stable\/examples\/ggraph\/GraphAF",">","`","_","for","usage","examples","."]
docstr: Running fine-tuning for property optimization task.
  
  Args:
      lr (float): The learning rate for fine-tuning.
      wd (float): The weight decay factor for training.
      max_iters (int): The maximum number of training iters.
      warm_up (int): The number of linear warm-up iters.
      model_conf_dict (dict): The python dict for configuring the model hyperparameters.
      pretrain_path (str): The path to the saved pretrained model file.
      save_interval (int): Indicate the frequency to save the model parameters to .pth files,
          *e.g.*, if save_interval=20, the model parameters will be saved for every 20 training iters.
      save_dir (str): The directory to save the model parameters.
docstr_tok: ["Running","fine-tuning","for","property","optimization","task",".","Args",":","lr","(","float",")",":","The","learning","rate","for","fine-tuning",".","wd","(","float",")",":","The","weight","decay","factor","for","training",".","max_iters","(","int",")",":","The","maximum","number","of","training","iters",".","warm_up","(","int",")",":","The","number","of","linear","warm-up","iters",".","model_conf_dict","(","dict",")",":","The","python","dict","for","configuring","the","model","hyperparameters",".","pretrain_path","(","str",")",":","The","path","to","the","saved","pretrained","model","file",".","save_interval","(","int",")",":","Indicate","the","frequency","to","save","the","model","parameters","to",".pth","files",",","*","e.g",".","*",",","if","save_interval=20",",","the","model","parameters","will","be","saved","for","every","20","training","iters",".","save_dir","(","str",")",":","The","directory","to","save","the","model","parameters","."]
code: def train_prop_optim(
      self,
      lr,
      wd,
      max_iters,
      warm_up,
      model_conf_dict,
      pretrain_path,
      save_interval,
      save_dir,
  ):
      r"""
      Running fine-tuning for property optimization task.
  
      Args:
          lr (float): The learning rate for fine-tuning.
          wd (float): The weight decay factor for training.
          max_iters (int): The maximum number of training iters.
          warm_up (int): The number of linear warm-up iters.
          model_conf_dict (dict): The python dict for configuring the model hyperparameters.
          pretrain_path (str): The path to the saved pretrained model file.
          save_interval (int): Indicate the frequency to save the model parameters to .pth files,
              *e.g.*, if save_interval=20, the model parameters will be saved for every 20 training iters.
          save_dir (str): The directory to save the model parameters.
      """
  
      self.get_model("prop_opt", model_conf_dict)
      self.load_pretrain_model(pretrain_path)
      self.model.train()
      optimizer = torch.optim.Adam(
          filter(lambda p: p.requires_grad, self.model.parameters()),
          lr=lr,
          weight_decay=wd,
      )
      if not os.path.isdir(save_dir):
          os.mkdir(save_dir)
  
      print("start finetuning model(reinforce)")
      moving_baseline = None
      for cur_iter in range(max_iters):
          optimizer.zero_grad()
          (
              loss,
              per_mol_reward,
              per_mol_property_score,
              moving_baseline,
          ) = self.model.reinforce_forward_optim(
              in_baseline=moving_baseline, cur_iter=cur_iter
          )
  
          num_mol = len(per_mol_reward)
          avg_reward = sum(per_mol_reward) / num_mol
          avg_score = sum(per_mol_property_score) / num_mol
          loss.backward()
          nn.utils.clip_grad_norm_(
              filter(
                  lambda p: p.requires_grad,
                  self.model.flow_core.parameters(),
              ),
              1.0,
          )
          adjust_learning_rate(optimizer, cur_iter, lr, warm_up)
          optimizer.step()
  
          print(
              "Iter {} | reward {}, score {}, loss {}".format(
                  cur_iter, avg_reward, avg_score, loss.item()
              )
          )
  
          if cur_iter % save_interval == save_interval - 1:
              torch.save(
                  self.model.state_dict(),
                  os.path.join(
                      save_dir, "prop_opt_net_{}.pth".format(cur_iter)
                  ),
              )
  
      print("Finetuning (Reinforce) Finished!")
  
file_path: DIG/dig/ggraph/method/GraphAF/graphaf.py
imports: import os
  import torch
  import torch.nn
  from rdkit import Chem
  from dig.ggraph.method import Generator
  from .model import GraphFlowModel, GraphFlowModel_rl, GraphFlowModel_con_rl
  from .train_utils import adjust_learning_rate, DataIterator
label_desc: Use image node_id 6 for calling the GraphAF obj's underlying member method code with example usage: obj.train_prop_optim(lr, wd, max_iters, warm_up, model_conf_dict, pretrain_path, save_interval, save_dir) without return types