class_docstr: Directed Message Passing Neural Network
  
  This class implements the Directed Message Passing Neural Network (D-MPNN) [1]_.
  
  The DMPNN model has 2 phases, message-passing phase and read-out phase.
  
  - The goal of the message-passing phase is to generate 'hidden states of all the atoms in the molecule' using encoders.
  - Next in read-out phase, the features are passed into feed-forward neural network to get the task-based prediction.
  
  For additional information:
  
  - `Mapper class <https://github.com/deepchem/deepchem/blob/31676cc2497d5f2de65d648c09fc86191b594501/deepchem/models/torch_models/dmpnn.py#L10-L92>`_
  - `Encoder layer class <https://github.com/deepchem/deepchem/blob/31676cc2497d5f2de65d648c09fc86191b594501/deepchem/models/torch_models/layers.py#L1223-L1374>`_
  - `Feed-Forward class <https://github.com/deepchem/deepchem/blob/31676cc2497d5f2de65d648c09fc86191b594501/deepchem/models/torch_models/layers.py#L689-L700>`_
  
  Example
  -------
  >>> import deepchem as dc
  >>> import os
  >>> model_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  >>> input_file = os.path.join(model_dir, 'tests/assets/freesolv_sample_5.csv')
  >>> loader = dc.data.CSVLoader(tasks=['y'], feature_field='smiles', featurizer=dc.feat.DMPNNFeaturizer())
  >>> dataset = loader.create_dataset(input_file)
  >>> model = DMPNNModel()
  >>> out = model.fit(dataset, nb_epoch=1)
  
  References
  ----------
  .. [1] Analyzing Learned Molecular Representations for Property Prediction https://arxiv.org/pdf/1904.01561.pdf
class_docstr_tok: ["Directed","Message","Passing","Neural","Network","This","class","implements","the","Directed","Message","Passing","Neural","Network","(","D-MPNN",")","[","1","]","_",".","The","DMPNN","model","has","2","phases",",","message-passing","phase","and","read-out","phase",".","-","The","goal","of","the","message-passing","phase","is","to","generate","'hidden","states","of","all","the","atoms","in","the","molecule","'","using","encoders",".","-","Next","in","read-out","phase",",","the","features","are","passed","into","feed-forward","neural","network","to","get","the","task-based","prediction",".","For","additional","information",":","-","`","Mapper","class","<","https",":","\/\/github.com\/deepchem\/deepchem\/blob\/31676cc2497d5f2de65d648c09fc86191b594501\/deepchem\/models\/torch_models\/dmpnn.py","#","L10-L92",">","`","_","-","`","Encoder","layer","class","<","https",":","\/\/github.com\/deepchem\/deepchem\/blob\/31676cc2497d5f2de65d648c09fc86191b594501\/deepchem\/models\/torch_models\/layers.py","#","L1223-L1374",">","`","_","-","`","Feed-Forward","class","<","https",":","\/\/github.com\/deepchem\/deepchem\/blob\/31676cc2497d5f2de65d648c09fc86191b594501\/deepchem\/models\/torch_models\/layers.py","#","L689-L700",">","`","_","Example","--","--","--","-",">",">",">","import","deepchem","as","dc",">",">",">","import","os",">",">",">","model_dir","=","os.path.dirname","(","os.path.dirname","(","os.path.abspath","(","__file__",")",")",")",">",">",">","input_file","=","os.path.join","(","model_dir",",","'tests\/assets\/freesolv_sample_5.csv","'",")",">",">",">","loader","=","dc.data.CSVLoader","(","tasks=","[","'","y","'","]",",","feature_field='smiles","'",",","featurizer=dc.feat.DMPNNFeaturizer","(",")",")",">",">",">","dataset","=","loader.create_dataset","(","input_file",")",">",">",">","model","=","DMPNNModel","(",")",">",">",">","out","=","model.fit","(","dataset",",","nb_epoch=1",")","References","--","--","--","--","--","..","[","1","]","Analyzing","Learned","Molecular","Representations","for","Property","Prediction","https",":","\/\/arxiv.org\/pdf\/1904.01561.pdf"]
docstr: Create a generator that iterates batches for a dataset.
  
  Overrides the existing ``default_generator`` method to customize how model inputs are
  generated from the data.
  
  Here, the ``_MapperDMPNN`` helper class is used, for each molecule in a batch, to get required input parameters:
  
  - atom_features
  - f_ini_atoms_bonds
  - atom_to_incoming_bonds
  - mapping
  - global_features
  
  Then data from each molecule is converted to a ``_ModData`` object and stored as list of graphs.
  The graphs are modified such that all tensors have same size in 0th dimension. (important requirement for batching)
  
  Parameters
  ----------
  dataset: Dataset
      the data to iterate
  epochs: int
      the number of times to iterate over the full dataset
  mode: str
      allowed values are 'fit' (called during training), 'predict' (called
      during prediction), and 'uncertainty' (called during uncertainty
      prediction)
  deterministic: bool
      whether to iterate over the dataset in order, or randomly shuffle the
      data for each epoch
  pad_batches: bool
      whether to pad each batch up to this model's preferred batch size
  
  Returns
  -------
  a generator that iterates batches, each represented as a tuple of lists:
  ([inputs], [outputs], [weights])
  Here, [inputs] is list of graphs.
docstr_tok: ["Create","a","generator","that","iterates","batches","for","a","dataset",".","Overrides","the","existing","``","default_generator","``","method","to","customize","how","model","inputs","are","generated","from","the","data",".","Here",",","the","``","_MapperDMPNN","``","helper","class","is","used",",","for","each","molecule","in","a","batch",",","to","get","required","input","parameters",":","-","atom_features","-","f_ini_atoms_bonds","-","atom_to_incoming_bonds","-","mapping","-","global_features","Then","data","from","each","molecule","is","converted","to","a","``","_ModData","``","object","and","stored","as","list","of","graphs",".","The","graphs","are","modified","such","that","all","tensors","have","same","size","in","0th","dimension",".","(","important","requirement","for","batching",")","Parameters","--","--","--","--","--","dataset",":","Dataset","the","data","to","iterate","epochs",":","int","the","number","of","times","to","iterate","over","the","full","dataset","mode",":","str","allowed","values","are","'fit","'","(","called","during","training",")",",","'predict","'","(","called","during","prediction",")",",","and","'uncertainty","'","(","called","during","uncertainty","prediction",")","deterministic",":","bool","whether","to","iterate","over","the","dataset","in","order",",","or","randomly","shuffle","the","data","for","each","epoch","pad_batches",":","bool","whether","to","pad","each","batch","up","to","this","model","'s","preferred","batch","size","Returns","--","--","--","-","a","generator","that","iterates","batches",",","each","represented","as","a","tuple","of","lists",":","(","[","inputs","]",",","[","outputs","]",",","[","weights","]",")","Here",",","[","inputs","]","is","list","of","graphs","."]
code: def default_generator(
      self,
      dataset: Dataset,
      epochs: int = 1,
      mode: str = "fit",
      deterministic: bool = True,
      pad_batches: bool = False,
      **kwargs,
  ) -> Iterable[Tuple[List, List, List]]:
      """Create a generator that iterates batches for a dataset.
  
      Overrides the existing ``default_generator`` method to customize how model inputs are
      generated from the data.
  
      Here, the ``_MapperDMPNN`` helper class is used, for each molecule in a batch, to get required input parameters:
  
      - atom_features
      - f_ini_atoms_bonds
      - atom_to_incoming_bonds
      - mapping
      - global_features
  
      Then data from each molecule is converted to a ``_ModData`` object and stored as list of graphs.
      The graphs are modified such that all tensors have same size in 0th dimension. (important requirement for batching)
  
      Parameters
      ----------
      dataset: Dataset
          the data to iterate
      epochs: int
          the number of times to iterate over the full dataset
      mode: str
          allowed values are 'fit' (called during training), 'predict' (called
          during prediction), and 'uncertainty' (called during uncertainty
          prediction)
      deterministic: bool
          whether to iterate over the dataset in order, or randomly shuffle the
          data for each epoch
      pad_batches: bool
          whether to pad each batch up to this model's preferred batch size
  
      Returns
      -------
      a generator that iterates batches, each represented as a tuple of lists:
      ([inputs], [outputs], [weights])
      Here, [inputs] is list of graphs.
      """
      for epoch in range(epochs):
          for X_b, y_b, w_b, ids_b in dataset.iterbatches(
              batch_size=self.batch_size,
              deterministic=deterministic,
              pad_batches=pad_batches,
          ):
              pyg_graphs_list: List = []
  
              # maximum number of incoming bonds in the batch
              max_num_bonds: int = 1
  
              for graph in X_b:
                  # generate concatenated feature vector and mappings
                  mapper: _MapperDMPNN = _MapperDMPNN(graph)
                  pyg_graph: _ModData = self._to_pyg_graph(
                      mapper.values
                  )
                  max_num_bonds = max(
                      max_num_bonds,
                      pyg_graph["atom_to_incoming_bonds"].shape[1],
                  )
                  pyg_graphs_list.append(pyg_graph)
  
              # pad all mappings to maximum number of incoming bonds in the batch
              for graph in pyg_graphs_list:
                  required_padding: int = (
                      max_num_bonds
                      - graph["atom_to_incoming_bonds"].shape[1]
                  )
                  graph["atom_to_incoming_bonds"] = nn.functional.pad(
                      graph["atom_to_incoming_bonds"],
                      (0, required_padding, 0, 0),
                      mode="constant",
                      value=-1,
                  )
                  graph["mapping"] = nn.functional.pad(
                      graph["mapping"],
                      (0, required_padding, 0, 0),
                      mode="constant",
                      value=-1,
                  )
  
              yield (pyg_graphs_list, [y_b], [w_b])
  
imports: import numpy
  import torch
  import torch.nn
  from deepchem.models.losses import Loss, L2Loss, SparseSoftmaxCrossEntropy
  from deepchem.models.torch_models import layers
  from deepchem.models.torch_models import TorchModel
  from torch_geometric.data import Data, Batch
  from deepchem.feat import GraphData
  from deepchem.data import Dataset
  from typing import Union, List, Sequence, Optional, Iterable, Tuple
label_desc: Use image node_id 4 for calling the DMPNNModel obj's underlying member method code with example usage: obj.default_generator(dataset, epochs, mode, deterministic, pad_batches) without return types