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

Partial fixes to tests

parent a1d40a7b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -517,7 +517,7 @@ class TestOverfit(test_util.TensorFlowTestCase):

    with g.as_default():
      graph_model = dc.nn.SequentialGraph(n_feat)
      graph_model.add(dc.nn.GraphConv(64, activation='relu'))
      graph_model.add(dc.nn.GraphConv(64, n_feat, activation='relu'))
      graph_model.add(dc.nn.BatchNormalization(epsilon=1e-5, mode=1))
      graph_model.add(dc.nn.GraphPool())
      # Gather Projection
+2 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ __author__ = "Han Altae-Tran and Bharath Ramsundar"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "GPL"

import tensorflow as tf
from deepchem.nn.layers import GraphGather
from deepchem.models.tf_new_models.graph_topology import GraphTopology

@@ -27,6 +28,7 @@ class SequentialGraph(object):
      Number of features per atom.
    """
    self.graph = tf.Graph()
    config = tf.ConfigProto(allow_soft_placement=True)
    self.session = tf.Session(graph=self.graph, config=config)
    with self.graph.as_default():
      self.graph_topology = GraphTopology(n_feat)
+4 −3
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ class TestGraphModels(test_util.TensorFlowTestCase):
    batch_size = 3
    graph_model = SequentialGraph(n_feat)

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

@@ -65,7 +65,7 @@ class TestGraphModels(test_util.TensorFlowTestCase):
      support_model = SequentialSupportGraph(n_feat)
      
      # Add layers
      support_model.add(dc.nn.GraphConv(64, activation='relu'))
      support_model.add(dc.nn.GraphConv(64, n_feat, activation='relu'))
      # Need to add batch-norm separately to test/support due to differing
      # shapes.
      support_model.add_test(dc.nn.BatchNormalization(epsilon=1e-5, mode=1))
@@ -73,7 +73,8 @@ class TestGraphModels(test_util.TensorFlowTestCase):
      support_model.add(dc.nn.GraphPool())

      # Apply an attention lstm layer
      support_model.join(dc.nn.AttnLSTMEmbedding(n_test, n_support, max_depth))
      support_model.join(dc.nn.AttnLSTMEmbedding(n_test, n_support, 64,
                                                 max_depth))

      # Gather Projection
      support_model.add(dc.nn.Dense(128, activation='relu'))
+46 −15
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ class Layer(object):
      input_dtype = kwargs.get('input_dtype', tf.float32)
      self.input_dtype = input_dtype

  def add_weight(self, shape, initializer, name=None):
  def add_weight(self, shape, initializer, regularizer=None, name=None):
    """Adds a weight variable to the layer.

    Parameters
@@ -110,8 +110,40 @@ class Layer(object):
    """
    initializer = initializations.get(initializer)
    weight = initializer(shape, name=name)
    if regularizer is not None:
      self.add_loss(regularizer(weight))

    return weight

  def add_loss(self, losses, inputs=None):
    """Adds losses to model."""
    if losses is None:
      return
    # Update self.losses
    losses = to_list(losses)
    if not hasattr(self, 'losses'):
      self.losses = []
    try:
      self.losses += losses
    except AttributeError:
      # In case self.losses isn't settable
      # (i.e. it's a getter method).
      # In that case the `losses` property is
      # auto-computed and shouldn't be set.
      pass
    # Update self._per_input_updates
    if not hasattr(self, '_per_input_losses'):
      self._per_input_losses = {}
    if inputs is not None:
      inputs_hash = object_list_uid(inputs)
    else:
      # Updates indexed by None are unconditional
      # rather than input-dependent
      inputs_hash = None
    if inputs_hash not in self._per_input_losses:
      self._per_input_losses[inputs_hash] = []
    self._per_input_losses[inputs_hash] += losses

  def call(self, x):
    """This is where the layer's logic lives.

@@ -356,19 +388,17 @@ class BatchNormalization(Layer):
  momentum: momentum in the computation of the
    exponential average of the mean and standard deviation
    of the data, for feature-wise normalization.
  beta_init: name of initialization function for shift parameter
    (see [initializations](../initializations.md)), or alternatively,
    TensorFlow function to use for weights initialization.
  gamma_init: name of initialization function for scale parameter (see
    [initializations](../initializations.md)), or alternatively,
    TensorFlow function to use for weights initialization.
  gamma_regularizer: instance of [WeightRegularizer](../regularizers.md)
  beta_init: name of initialization function for shift parameter, or
    alternatively, TensorFlow function to use for weights initialization.
  gamma_init: name of initialization function for scale parameter, or
    alternatively, TensorFlow function to use for weights initialization.
  gamma_regularizer: instance of WeightRegularizer
    (eg. L1 or L2 regularization), applied to the gamma vector.
  beta_regularizer: instance of [WeightRegularizer](../regularizers.md),
  beta_regularizer: instance of WeightRegularizer,
    applied to the beta vector.

  Input shape:
  Arbitrary. Use the keyword argument `input_shape`
  Arbitrary. Use the keyword argument input_shape
  (tuple of integers, does not include the samples axis)
  when using this layer as the first layer in a model.

@@ -405,17 +435,18 @@ class BatchNormalization(Layer):
                                initializer=self.beta_init,
                                regularizer=self.beta_regularizer,
                                name='{}_beta'.format(self.name))
    # Not Trainable
    self.running_mean = self.add_weight(shape, initializer='zero',
                                        name='{}_running_mean'.format(self.name),
                                        trainable=False)
                                        name='{}_running_mean'.format(self.name))
    # Not Trainable
    self.running_std = self.add_weight(shape, initializer='one',
                                       name='{}_running_std'.format(self.name),
                                       trainable=False)
                                       name='{}_running_std'.format(self.name))


  def call(self, x):
    if self.mode == 0 or self.mode == 2:
    input_shape = model_ops.int_shape(x)
    self.build(input_shape)
    if self.mode == 0 or self.mode == 2:

      reduction_axes = list(range(len(input_shape)))
      del reduction_axes[self.axis]
+35 −22
Original line number Diff line number Diff line
@@ -201,13 +201,15 @@ class GraphConv(Layer):
  and expects that they follow the ordering provided by
  GraphTopology.get_input_placeholders().
  """
  def __init__(self, nb_filter, init='glorot_uniform', activation='linear',
               dropout=None, max_deg=10, min_deg=0, **kwargs):
  def __init__(self, nb_filter, n_atom_features, init='glorot_uniform',
               activation='linear', dropout=None, max_deg=10, min_deg=0, **kwargs):
    """
    Parameters
    ----------
    nb_filter: int
      Number of convolutional filters.
    n_atom_features: int
      Number of features listed per atom.
    init: str, optional
      Weight initialization for filters.
    activation: str, optional
@@ -230,26 +232,22 @@ class GraphConv(Layer):
    # TODO(rbharath): It's not clear where nb_affine comes from.
    # Is there a solid explanation here?
    self.nb_affine = 2*max_deg + (1-min_deg)        
    self.n_atom_features = n_atom_features

  def build(self, input_shape):
  def build(self):
    """"Construct internal trainable weights.

    This layer expects arguments of form

    [atom_features, deg_slice, membership, deg_adj_list placeholders...]

    input_shape should provide the shapes of each of these tensors.
    n_atom_features should provide the number of features per atom. 

    Parameters
    ----------
    input_shape: list
      Shapes of incoming tensors
    n_atom_features: int 
      Number of features provied per atom. 
    """
    n_atom_features = self.n_atom_features
      
    # Generate the nb_affine weights and biases
    atom_features_shape = input_shape[0]
    n_features = atom_features_shape[1]
    self.W_list = [self.init([n_features, self.nb_filter]) 
    self.W_list = [self.init([n_atom_features, self.nb_filter]) 
                   for k in range(self.nb_affine)]
    self.b_list = [model_ops.zeros(shape=[self.nb_filter,])
                   for k in range(self.nb_affine)]
@@ -288,6 +286,9 @@ class GraphConv(Layer):
    atom_features: tf.Tensor
      Of shape (n_atoms, nb_filter)
    """
    # Add trainable weights
    self.build()

    # Extract atom_features
    atom_features = x[0] 

@@ -462,7 +463,7 @@ class AttnLSTMEmbedding(Layer):
  Order Matters: Sequence to sequence for sets
  https://arxiv.org/abs/1511.06391
  """
  def __init__(self, n_test, n_support, max_depth, init='glorot_uniform',
  def __init__(self, n_test, n_support, n_feat, max_depth, init='glorot_uniform',
               activation='linear', dropout=None, **kwargs):
    """
    Parameters
@@ -471,6 +472,8 @@ class AttnLSTMEmbedding(Layer):
      Size of support set.
    n_test: int
      Size of test set.
    n_feat: int
      Number of features per atom
    max_depth: int
      Number of "processing steps" used by sequence-to-sequence for sets model.
    init: str, optional
@@ -487,6 +490,7 @@ class AttnLSTMEmbedding(Layer):
    self.max_depth = max_depth
    self.n_test = n_test
    self.n_support = n_support
    self.n_feat = n_feat

  def get_output_shape_for(self, input_shape):
    """Returns the output shape. Same as input_shape.
@@ -526,10 +530,13 @@ class AttnLSTMEmbedding(Layer):
    x, xp = x_xp

    ## Initializes trainable weights.
    ######################################################### DEBUG
    #n_feat = xp_input_shape[1]
    n_feat = xp.get_shape()[1]
    #n_feat = xp.get_shape()[1]
    n_feat = self.n_feat
    ######################################################### DEBUG

    self.lstm = LSTMStep(n_feat)
    self.lstm = LSTMStep(n_feat, self.n_test)
    self.q_init = model_ops.zeros([self.n_test, n_feat])
    self.r_init = model_ops.zeros([self.n_test, n_feat])
    self.states_init = self.lstm.get_initial_states([self.n_test, n_feat])
@@ -708,7 +715,7 @@ class LSTMStep(Layer):
  rather we generate a sequence of inputs using the intermediate outputs of the
  LSTM, and so will require step by step operation of the lstm
  """
  def __init__(self, output_dim,
  def __init__(self, output_dim, input_dim,
               init='glorot_uniform', inner_init='orthogonal',
               forget_bias_init='one', activation='tanh', 
               inner_activation='hard_sigmoid', **kwargs):
@@ -724,18 +731,22 @@ class LSTMStep(Layer):
    self.forget_bias_init = initializations.get(forget_bias_init)
    self.activation = activations.get(activation)
    self.inner_activation = activations.get(inner_activation)
    self.input_dim = input_dim

  def get_initial_states(self, input_shape):
    return [model_ops.zeros(input_shape), model_ops.zeros(input_shape)]

  def build(self, input_shape):
    x, h_tm1, c_tm1 = input_shape # Unpack
    self.input_dim = x[1]
  #def build(self, input_shape):
  def build(self):
    #################################### DEBUG
    #x, _, _ = input_shape # Unpack
    #self.input_dim = x[1]
    #################################### DEBUG

    self.W = self.init((self.input_dim, 4 * self.output_dim))
    self.U = self.inner_init((self.output_dim, 4 * self.output_dim))

    self.b = model_ops.variable(np.hstack(
    self.b = tf.Variable(np.hstack(
        (np.zeros(self.output_dim),
         np.ones(self.output_dim),
         np.zeros(self.output_dim),
@@ -747,6 +758,9 @@ class LSTMStep(Layer):
    return [(x[0], self.output_dim), h_tm1, c_tm1]

  def call(self, x_states, mask=None):
    ############################################### DEBUG
    self.build()
    ############################################### DEBUG
    x, h_tm1, c_tm1 = x_states # Unpack

    # Taken from Keras code [citation needed]
@@ -765,4 +779,3 @@ class LSTMStep(Layer):
    h = o * self.activation(c)
    
    return o, [h, c]
Loading