Commit 40d0f098 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Yapf and tests

parent f64ffe36
Loading
Loading
Loading
Loading
+66 −48
Original line number Diff line number Diff line
@@ -1352,6 +1352,7 @@ class GraphPool(Layer):
      self.out_tensor = out_tensor
    return out_tensor


class GraphGather(Layer):

  def __init__(self, batch_size, activation_fn=None, **kwargs):
@@ -1399,7 +1400,6 @@ class GraphGather(Layer):
    return out_tensor



class LSTMStep(Layer):
  """ LSTM whose call is a single step in the LSTM.

@@ -1444,15 +1444,18 @@ class LSTMStep(Layer):

  def none_tensors(self):
    W, U, b, out_tensor = self.W, self.U, self.b, self.out_tensor
    h, c = self.h, self.c
    trainable_weights = self.trainable_weights
    self.W, self.U, self.b, self.out_tensor = None, None, None, None
    self.h, self.c = None, None
    self.trainable_weights = []
    return W, U, b, out_tensor, trainable_weights
    return W, U, b, h, c, out_tensor, trainable_weights

  def set_tensors(self, tensor):
    self.W, self.U, self.b, self.out_tensor, self.trainable_weights = tensor
    (self.W, self.U, self.b, self.h, self.c, self.out_tensor,
     self.trainable_weights) = tensor

  def create_tensor(self, in_layers=None, **kwargs):
  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    """Execute this layer on input tensors.

    Parameters
@@ -1489,10 +1492,13 @@ class LSTMStep(Layer):

    h = o * activation(c)

    # TODO(rbharath): This is wrong!!
    if set_tensors:
      self.h = h
      self.c = c
      self.out_tensor = h
    return h, [h, c]


def cos(x, y):
  """Computes the inner preduct (cosine distance) between two tensors.

@@ -1508,6 +1514,7 @@ def cos(x, y):
      + model_ops.epsilon())
  return model_ops.dot(x, tf.transpose(y)) / denom


class AttnLSTMEmbedding(Layer):
  """Implements AttnLSTM as in matching networks paper.

@@ -1519,12 +1526,7 @@ class AttnLSTMEmbedding(Layer):
  https://arxiv.org/abs/1511.06391
  """

  def __init__(self,
               n_test,
               n_support,
               n_feat,
               max_depth,
               **kwargs):
  def __init__(self, n_test, n_support, n_feat, max_depth, **kwargs):
    """
    Parameters
    ----------
@@ -1544,7 +1546,7 @@ class AttnLSTMEmbedding(Layer):
    self.n_support = n_support
    self.n_feat = n_feat

  def create_tensor(self, in_layers=None, **kwargs):
  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    """Execute this layer on input tensors.

    Parameters
@@ -1594,21 +1596,26 @@ class AttnLSTMEmbedding(Layer):
      y = model_ops.concatenate([q, r], axis=1)
      q, states = lstm(y, *states)

    # TODO(rbharath): This is wrong!!
    if set_tensors:
      self.out_tensor = xp
      self.xq = x + q
      self.xp = xp
    return [x + q, xp]

  def none_tensors(self):
    q_init, r_init, states_init = self.q_init, self.r_init, self.states_init
    xq, xp = self.xq, self.xp
    out_tensor = self.out_tensor
    trainable_weights = self.trainable_weights
    self.q_init, self.r_init, self.states_init = None, None, None
    self.xq, self.xp = None, None
    self.out_tensor = None
    self.trainable_weights = []
    return q_init, r_init, states_init, out_tensor, trainable_weights
    return q_init, r_init, states_init, xq, xp, out_tensor, trainable_weights

  def set_tensors(self, tensor):
    self.q_init, self.r_init, self.states_init, self.out_tensor, self.trainable_weights = tensor
    (self.q_init, self.r_init, self.states_init, self.xq, self.xp,
     self.out_tensor, self.trainable_weights) = tensor


class IterRefLSTMEmbedding(Layer):
@@ -1644,33 +1651,16 @@ class IterRefLSTMEmbedding(Layer):
    """
    super(IterRefLSTMEmbedding, self).__init__(**kwargs)

    self.init = initializations.get(init)  # Set weight initialization
    self.activation = activations.get(activation)  # Get activations
    self.max_depth = max_depth
    self.n_test = n_test
    self.n_support = n_support
    self.n_feat = n_feat

  def build(self):
    """Builds this layer.
    """
    n_feat = self.n_feat

    # Support set lstm
    self.support_lstm = LSTMStep(n_feat, 2 * n_feat)
    self.q_init = model_ops.zeros([self.n_support, n_feat])
    self.support_states_init = self.support_lstm.get_initial_states(
        [self.n_support, n_feat])

    # Test lstm
    self.test_lstm = LSTMStep(n_feat, 2 * n_feat)
    self.p_init = model_ops.zeros([self.n_test, n_feat])
    self.test_states_init = self.test_lstm.get_initial_states(
        [self.n_test, n_feat])

    self.trainable_weights = []
  #def build(self):
  #  """Builds this layer.
  #  """

  def create_tensor(self, in_layers=None, **kwargs):
  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    """Execute this layer on input tensors.

    Parameters
@@ -1687,10 +1677,26 @@ class IterRefLSTMEmbedding(Layer):
      Returns two tensors of same shape as input. Namely the output shape will
      be [(n_test, n_feat), (n_support, n_feat)]
    """
    self.build()
    n_feat = self.n_feat

    # Support set lstm
    support_lstm = LSTMStep(n_feat, 2 * n_feat)
    self.q_init = model_ops.zeros([self.n_support, n_feat])
    self.support_states_init = support_lstm.get_initial_states(
        [self.n_support, n_feat])

    # Test lstm
    test_lstm = LSTMStep(n_feat, 2 * n_feat)
    self.p_init = model_ops.zeros([self.n_test, n_feat])
    self.test_states_init = test_lstm.get_initial_states([self.n_test, n_feat])

    self.trainable_weights = []

    #self.build()
    inputs = self._get_input_tensors(in_layers)
    if len(inputs) != 2:
      raise ValueError("AttnLSTMEmbedding layer must have exactly two parents")
      raise ValueError(
          "IterRefLSTMEmbedding layer must have exactly two parents")
    x, xp = inputs

    # Get initializations
@@ -1715,28 +1721,40 @@ class IterRefLSTMEmbedding(Layer):

      # Generate new support attention states
      qr = model_ops.concatenate([q, r], axis=1)
      q, states = self.support_lstm(qr, *states)
      q, states = support_lstm(qr, *states)

      # Generate new test attention states
      ps = model_ops.concatenate([p, s], axis=1)
      p, x_states = self.test_lstm(ps, *x_states)
      p, x_states = test_lstm(ps, *x_states)

      # Redefine
      z = r

    if set_tensors:
      self.xp = x + p
      self.xpq = xp + q
      self.out_tensor = self.xp

    return [x + p, xp + q]

  def none_tensors(self):
    q_init, r_init, states_init = self.q_init, self.r_init, self.states_init
    p_init, q_init = self.p_init, self.q_init,
    support_states_init, test_states_init = (self.support_states_init,
                                             self.test_states_init)
    xp, xpq = self.xp, self.xpq
    out_tensor = self.out_tensor
    trainable_weights = self.trainable_weights
    self.q_init, self.r_init, self.states_init = None, None, None
    (self.p_init, self.q_init, self.support_states_init,
     self.test_states_init) = (None, None, None, None)
    self.xp, self.xpq = None, None
    self.out_tensor = None
    self.trainable_weights = []
    return q_init, r_init, states_init, out_tensor, trainable_weights
    return (p_init, q_init, support_states_init, test_states_init, xp, xpq,
            out_tensor, trainable_weights)

  def set_tensors(self, tensor):
    self.q_init, self.r_init, self.states_init, self.out_tensor, self.trainable_weights = tensor
    (self.p_init, self.q_init, self.support_states_init, self.test_states_init,
     self.xp, self.xpq, self.out_tensor, self.trainable_weights) = tensor


class BatchNorm(Layer):
+1 −26
Original line number Diff line number Diff line
@@ -389,10 +389,6 @@ class TensorGraph(Model):
      for node in order:
        with tf.name_scope(node):
          node_layer = self.layers[node]
          ############################################################ DEBUG
          #print("node_layer")
          #print(node_layer)
          ############################################################ DEBUG
          node_layer.create_tensor(training=self._training_placeholder)
          self.rnn_initial_states += node_layer.rnn_initial_states
          self.rnn_final_states += node_layer.rnn_final_states
@@ -510,28 +506,7 @@ class TensorGraph(Model):

    # Pickle itself
    pickle_name = os.path.join(self.model_dir, "model.pickle")
    ########################################################## DEBUG
    #print("self")
    #print(self)

    #def debug_pickle(instance):
    #  attribute = None

    #  for k, v in instance.__dict__.iteritems():
    #    print("checking: %s" % k)
    #    print("type(v)")
    #    print(type(v))
    #    try:
    #      pickle.dumps(v)
    #    except:
    #      attribute = k
    #      break

    #  return attribute  
    #debug_pickle(self)
    ##pickle.dumps(self)

    ########################################################## DEBUG

    with open(pickle_name, 'wb') as fout:
      try:
        pickle.dump(self, fout)
+7 −8
Original line number Diff line number Diff line
@@ -416,13 +416,12 @@ class TestLayers(test_util.TensorFlowTestCase):
      lstm = LSTMStep(n_feat, 2 * n_feat)
      out_tensor = lstm(y, state_zero, state_one)
      sess.run(tf.global_variables_initializer())
      h_out, h_copy_out, c_out = (
        out_tensor[0].eval(), out_tensor[1][0].eval(), out_tensor[1][1].eval())
      h_out, h_copy_out, c_out = (out_tensor[0].eval(), out_tensor[1][0].eval(),
                                  out_tensor[1][1].eval())
      assert h_out.shape == (n_test, n_feat)
      assert h_copy_out.shape == (n_test, n_feat)
      assert c_out.shape == (n_test, n_feat)


  def test_attn_lstm_embedding(self):
    """Test that attention LSTM computation works properly."""
    max_depth = 5
@@ -457,8 +456,8 @@ class TestLayers(test_util.TensorFlowTestCase):
      test = tf.convert_to_tensor(test, dtype=tf.float32)
      support = tf.convert_to_tensor(support, dtype=tf.float32)

      iter_ref_embedding_layer = IterRefLSTMEmbedding(
          n_test, n_support, n_feat, max_depth)
      iter_ref_embedding_layer = IterRefLSTMEmbedding(n_test, n_support, n_feat,
                                                      max_depth)
      out_tensor = iter_ref_embedding_layer(test, support)
      sess.run(tf.global_variables_initializer())
      test_out, support_out = out_tensor[0].eval(), out_tensor[1].eval()
+22 −5
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ from deepchem.models.tensorgraph.layers import Feature, Conv1D, Dense, Flatten,
    CombineMeanStd, Repeat, GRU, L2Loss, Concat, SoftMax, Constant, Variable, Add, Multiply, InteratomicL2Distances, \
    SoftMaxCrossEntropy, ReduceMean, ToFloat, ReduceSquareDifference, Conv2D, MaxPool, ReduceSum, GraphConv, GraphPool, \
    GraphGather, BatchNorm, WeightedError, \
    LSTMStep, AttnLSTMEmbedding
    LSTMStep, AttnLSTMEmbedding, IterRefLSTMEmbedding
from deepchem.models.tensorgraph.graph_layers import Combine_AP, Separate_AP, \
    WeaveLayer, WeaveGather, DTNNEmbedding, DTNNGather, DTNNStep, \
    DTNNExtract, DAGLayer, DAGGather, MessagePassing, SetGather
@@ -452,8 +452,8 @@ def test_AttnLSTM_pickle():
  tg = TensorGraph(batch_size=n_test)
  test = Feature(shape=(None, n_feat))
  support = Feature(shape=(None, n_feat))
  out = AttnLSTMEmbedding(n_test, n_support, n_feat, max_depth,
                          in_layers=[test, support])
  out = AttnLSTMEmbedding(
      n_test, n_support, n_feat, max_depth, in_layers=[test, support])
  tg.add_output(out)
  tg.set_loss(out)
  tg.build()
@@ -474,6 +474,23 @@ def test_LSTMStep_pickle():
  tg.save()


def test_IterRefLSTM_pickle():
  """Tests that IterRefLSTM can be pickled."""
  n_feat = 10
  max_depth = 5
  n_test = 5
  n_support = 5
  tg = TensorGraph()
  test = Feature(shape=(None, n_feat))
  support = Feature(shape=(None, n_feat))
  lstm = IterRefLSTMEmbedding(
      n_test, n_support, n_feat, max_depth, in_layers=[test, support])
  tg.add_output(lstm)
  tg.set_loss(lstm)
  tg.build()
  tg.save()


def test_SetGather_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 100))
+5 −4
Original line number Diff line number Diff line
@@ -355,7 +355,8 @@ class GraphGather(Layer):
    batch_size: int
      Number of elements in batch of data.
    """
    warnings.warn("The dc.nn.GraphGather is "
    warnings.warn(
        "The dc.nn.GraphGather is "
        "deprecated. Will be removed in DeepChem 1.4. "
        "Will be replaced by dc.models.tensorgraph.layers.GraphGather",
        DeprecationWarning)