Unverified Commit 01430dd7 authored by peastman's avatar peastman Committed by GitHub
Browse files

Merge pull request #1229 from miaecle/DAG

Dropout for DAG
parents 8c9156eb dacf10d0
Loading
Loading
Loading
Loading
+25 −13
Original line number Diff line number Diff line
@@ -581,16 +581,18 @@ class DAGLayer(Layer):
          Number of features listed per atom.
        max_atoms: int, optional
          Maximum number of atoms in molecules.
        layer_sizes: list of int, optional(default=[1000])
          Structure of hidden layer(s)
        layer_sizes: list of int, optional(default=[100])
          List of hidden layer size(s): 
          length of this list represents the number of hidden layers, 
          and each element is the width of corresponding hidden layer.
        init: str, optional
          Weight initialization for filters.
        activation: str, optional
          Activation function applied
          Activation function applied.
        dropout: float, optional
          Dropout probability, not supported here
          Dropout probability in hidden layer(s).
        batch_size: int, optional
          number of molecules in a batch
          number of molecules in a batch.
        """
    super(DAGLayer, self).__init__(**kwargs)

@@ -683,7 +685,8 @@ class DAGLayer(Layer):
      # DAGgraph_step maps from batch_inputs to a batch of graph_features
      # of shape: (batch_size*max_atoms) * n_graph_features
      # representing the graph features of target atoms in each graph
      batch_outputs = self.DAGgraph_step(batch_inputs, self.W_list, self.b_list)
      batch_outputs = self.DAGgraph_step(batch_inputs, self.W_list, self.b_list,
                                         **kwargs)

      # index for targe atoms
      target_index = tf.stack([tf.range(n_atoms), parents[:, count, 0]], axis=1)
@@ -698,11 +701,14 @@ class DAGLayer(Layer):
      self.out_tensor = out_tensor
    return out_tensor

  def DAGgraph_step(self, batch_inputs, W_list, b_list):
  def DAGgraph_step(self, batch_inputs, W_list, b_list, **kwargs):
    outputs = batch_inputs
    for idw, W in enumerate(W_list):
      outputs = tf.nn.xw_plus_b(outputs, W, b_list[idw])
      outputs = self.activation(outputs)
      training = kwargs['training'] if 'training' in kwargs else 1.0
      if not self.dropout is None:
        outputs = tf.nn.dropout(outputs, 1.0 - self.dropout * training)
    return outputs

  def none_tensors(self):
@@ -733,19 +739,21 @@ class DAGGather(Layer):
        Parameters
        ----------
        n_graph_feat: int, optional
          Number of features for each atom
          Number of features for each atom.
        n_outputs: int, optional
          Number of features for each molecule.
        max_atoms: int, optional
          Maximum number of atoms in molecules.
        layer_sizes: list of int, optional
          Structure of hidden layer(s)
          List of hidden layer size(s): 
          length of this list represents the number of hidden layers, 
          and each element is the width of corresponding hidden layer.
        init: str, optional
          Weight initialization for filters.
        activation: str, optional
          Activation function applied
          Activation function applied.
        dropout: float, optional
          Dropout probability, not supported
          Dropout probability in the hidden layer(s).
        """
    super(DAGGather, self).__init__(**kwargs)

@@ -794,18 +802,22 @@ class DAGGather(Layer):
    # Extract atom_features
    graph_features = tf.segment_sum(atom_features, membership)
    # sum all graph outputs
    outputs = self.DAGgraph_step(graph_features, self.W_list, self.b_list)
    outputs = self.DAGgraph_step(graph_features, self.W_list, self.b_list,
                                 **kwargs)
    out_tensor = outputs
    if set_tensors:
      self.variables = self.trainable_weights
      self.out_tensor = out_tensor
    return out_tensor

  def DAGgraph_step(self, batch_inputs, W_list, b_list):
  def DAGgraph_step(self, batch_inputs, W_list, b_list, **kwargs):
    outputs = batch_inputs
    for idw, W in enumerate(W_list):
      outputs = tf.nn.xw_plus_b(outputs, W, b_list[idw])
      outputs = self.activation(outputs)
      training = kwargs['training'] if 'training' in kwargs else 1.0
      if not self.dropout is None:
        outputs = tf.nn.dropout(outputs, 1.0 - self.dropout * training)
    return outputs

  def none_tensors(self):
+23 −5
Original line number Diff line number Diff line
@@ -366,22 +366,33 @@ class DAGModel(TensorGraph):
               n_atom_feat=75,
               n_graph_feat=30,
               n_outputs=30,
               layer_sizes=[100],
               layer_sizes_gather=[100],
               dropout=None,
               mode="classification",
               **kwargs):
    """
            Parameters
            ----------
            n_tasks: int
              Number of tasks
              Number of tasks.
            max_atoms: int, optional
              Maximum number of atoms in a molecule, should be defined based on dataset
              Maximum number of atoms in a molecule, should be defined based on dataset.
            n_atom_feat: int, optional
              Number of features per atom.
            n_graph_feat: int, optional
              Number of features for atom in the graph
              Number of features for atom in the graph.
            n_outputs: int, optional
              Number of features for each molecule
            mode: str
              Number of features for each molecule.
            layer_sizes: list of int, optional
              List of hidden layer size(s) in the propagation step: 
              length of this list represents the number of hidden layers, 
              and each element is the width of corresponding hidden layer.
            layer_sizes_gather: list of int, optional
              List of hidden layer size(s) in the gather step. 
            dropout: None or float, optional
              Dropout probability, applied after each propagation step and gather step.
            mode: str, optional
              Either "classification" or "regression" for type of model.
            """
    self.n_tasks = n_tasks
@@ -389,6 +400,9 @@ class DAGModel(TensorGraph):
    self.n_atom_feat = n_atom_feat
    self.n_graph_feat = n_graph_feat
    self.n_outputs = n_outputs
    self.layer_sizes = layer_sizes
    self.layer_sizes_gather = layer_sizes_gather
    self.dropout = dropout
    self.mode = mode
    super(DAGModel, self).__init__(**kwargs)
    self.build_graph()
@@ -410,6 +424,8 @@ class DAGModel(TensorGraph):
        n_graph_feat=self.n_graph_feat,
        n_atom_feat=self.n_atom_feat,
        max_atoms=self.max_atoms,
        layer_sizes=self.layer_sizes,
        dropout=self.dropout,
        batch_size=self.batch_size,
        in_layers=[
            self.atom_features, self.parents, self.calculation_orders,
@@ -419,6 +435,8 @@ class DAGModel(TensorGraph):
        n_graph_feat=self.n_graph_feat,
        n_outputs=self.n_outputs,
        max_atoms=self.max_atoms,
        layer_sizes=self.layer_sizes_gather,
        dropout=self.dropout,
        in_layers=[dag_layer1, self.membership])

    costs = []