Commit bc9a9c21 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Added sequential example topology to tests.

parent 7c5bcff0
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -16,14 +16,31 @@ from deepchem.models.tf_keras_models.containers import SupportGraphContainer
from deepchem.models.tf_keras_models.graph_topology import GraphTopology

class SequentialGraphModel(object):
  """An analog of Keras Sequential model for Graph data.

  Like the Sequential class from Keras, but automatically passes topology
  placeholders from GraphTopology to each graph layer (from keras_layers) added
  to the network. Non graph layers don't get the extra placeholders. 
  """
  def __init__(self, n_atoms, n_feat, batch_size):
    """
    Parameters
    ----------
    n_atoms: int
      (Max?) Number of atoms in system.
    n_feat: int
      Number of features per atom.
    batch_size: int
      Batch size for training models.
    """
    
    #super(SequentialGraphModel, self).__init__()
    self.batch_size = batch_size
    # Create graph topology and x
    self.graph_topology = GraphTopology(n_atoms, n_feat, self.batch_size)
    self.output = self.graph_topology.get_nodes()

    self.layers = []  # Keep track of the layers
    self.output = self.graph_topology.get_atom_features_placeholder()
    # Keep track of the layers
    self.layers = []  

  def add(self, layer):
    """Adds a new layer to model."""
+1 −1
Original line number Diff line number Diff line
@@ -131,7 +131,7 @@ class GraphTopology(object):

    # Generate dicts
    deg_adj_dict = dict(zip(self.deg_adj_lists_placeholders, deg_adj_lists))
    atoms_dict = {self.nodes_placeholder : atoms,
    atoms_dict = {self.atom_features_placeholder : atoms,
                  self.deg_slice_placeholder : batch.deg_slice,
                  self.membership_placeholder : batch.membership}
    return merge_dicts([atoms_dict, deg_adj_dict])
+23 −0
Original line number Diff line number Diff line
@@ -11,9 +11,13 @@ __license__ = "GPL"

import unittest
from tensorflow.python.framework import test_util
from keras.layers import Dense, BatchNormalization
from deepchem.models.tf_keras_models.containers import GraphContainer
from deepchem.models.tf_keras_models.graph_topology import GraphTopology
from deepchem.models.tf_keras_models.graph_models import SequentialGraphModel
from deepchem.models.tf_keras_models.keras_layers import GraphConv
from deepchem.models.tf_keras_models.keras_layers import GraphPool
from deepchem.models.tf_keras_models.keras_layers import GraphGather

class TestGraphModels(test_util.TensorFlowTestCase):
  """
@@ -30,3 +34,22 @@ class TestGraphModels(test_util.TensorFlowTestCase):
    batch_size = 3
    graph_model = SequentialGraphModel(n_atoms, n_feat, batch_size)
    assert len(graph_model.layers) == 0

  def test_sample_sequential_architecture(self):
    """Tests that a representative architecture can be created."""
    n_atoms = 5
    n_feat = 10
    batch_size = 3
    graph_model = SequentialGraphModel(n_atoms, n_feat, batch_size)

    graph_model.add(GraphConv(64, activation='relu'))
    graph_model.add(BatchNormalization(epsilon=1e-5, mode=1))
    graph_model.add(GraphPool())

    # Gather Projection
    graph_model.add(Dense(128, activation='relu'))
    graph_model.add(BatchNormalization(epsilon=1e-5, mode=1))
    graph_model.add(GraphGather(batch_size, activation="tanh"))

    # There should be 8 layers in graph_model
    assert len(graph_model.layers) == 6