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

Resi-LSTM overfit test passes

parent 0d0bf039
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -804,8 +804,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
      # Measure performance on 0-th task.
      assert scores[0] > .9

  #TODO(rbharath): This test doesn't pass although it should. Debug to understand root
  #causes of these errors.
  def test_residual_lstm_singletask_classification_overfit(self):
    """Test resi-lstm multitask overfits tiny data."""
    g = tf.Graph()
@@ -819,7 +817,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
      n_neg = 4
      test_batch_size = 10
      support_batch_size = n_pos + n_neg
      n_train_trials = 30
      n_train_trials = 60
      replace = False
      
      # Load mini log-solubility dataset.
@@ -856,7 +854,7 @@ class TestOverfit(test_util.TensorFlowTestCase):

      with self.test_session() as sess:
        model = dc.models.SupportGraphClassifier(
          sess, support_model, n_tasks, test_batch_size=test_batch_size,
          sess, support_model, test_batch_size=test_batch_size,
          support_batch_size=support_batch_size, learning_rate=1e-3,
          verbosity="high")

@@ -873,8 +871,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
        # can measure model has memorized support).  Replacement is turned off to
        # ensure that support contains full training set. This checks that the
        # model has mastered memorization of provided support.
        scores = model.evaluate(dataset, range(n_tasks),
                                classification_metric, n_trials=5,
        scores = model.evaluate(dataset, classification_metric, n_trials=5,
                                n_pos=n_pos, n_neg=n_neg,
                                exclude_support=False, replace=False)

+1 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import shutil
import unittest
import numpy as np
import deepchem as dc
from sklearn.linear_model import LogisticRegression
#from deepchem.models.tests import TestAPI
#from deepchem import metrics
#from deepchem.metrics import Metric
@@ -21,7 +22,6 @@ import deepchem as dc
#from deepchem.featurizers.fingerprints import CircularFingerprint
#from deepchem.models.multitask import SingletaskToMultitask 
#from deepchem.models.sklearn_models import SklearnModel
#from sklearn.linear_model import LogisticRegression
#from deepchem.utils.evaluate import Evaluator

class TestSingletasktoMultitaskAPI(TestAPI):
+22 −21
Original line number Diff line number Diff line
@@ -543,6 +543,7 @@ class AttnLSTMEmbedding(Layer):
    
    for d in range(self.max_depth):
      # Process using attention
      # Eqn (4), appendix A.1 of Matching Networks paper
      e = cos(x+q, xp)
      a = K.softmax(e)
      r = K.dot(a, xp)
@@ -592,20 +593,19 @@ class ResiLSTMEmbedding(Layer):
    input_shape: tuple
      Tuple of ((n_test, n_feat), (n_support, n_feat))
    """
    left_input_shape, right_input_shape = input_shape  #Unpack

    n_feat = right_input_shape[1]
    _, support_input_shape = input_shape  #Unpack
    n_feat = support_input_shape[1]

    # Support set lstm
    self.right_lstm = LSTMStep(n_feat)
    self.support_lstm = LSTMStep(n_feat)
    self.q_init = K.zeros([self.n_support, n_feat])
    self.states_init = self.right_lstm.get_initial_states(
    self.support_states_init = self.support_lstm.get_initial_states(
        [self.n_support, n_feat])

    # Prediction lstm
    self.left_lstm = LSTMStep(n_feat)
    # Test lstm
    self.test_lstm = LSTMStep(n_feat)
    self.p_init = K.zeros([self.n_test, n_feat])
    self.left_states_init = self.left_lstm.get_initial_states(
    self.test_states_init = self.test_lstm.get_initial_states(
        [self.n_test, n_feat])
    
    self.trainable_weights = []
@@ -623,8 +623,6 @@ class ResiLSTMEmbedding(Layer):
    list
      Of same shape as input [(n_test, n_feat), (n_support, n_feat)]
    """
    left_input_shape, right_input_shape = input_shape  #Unpack

    return input_shape

  def call(self, argument, mask=None):
@@ -633,8 +631,8 @@ class ResiLSTMEmbedding(Layer):
    Parameters
    ----------
    argument: list
      List of two tensors (x, xp). x should be of shape (n_test, n_feat) and
      xpshould be of shape (n_support, n_feat) where n_test is the size of
      List of two tensors (X, Xp). X should be of shape (n_test, n_feat) and
      Xp should be of shape (n_support, n_feat) where n_test is the size of
      the test set, n_support that of the support set, and n_feat is the number
      of per-atom features.

@@ -649,12 +647,13 @@ class ResiLSTMEmbedding(Layer):
    # Get initializations
    p = self.p_init
    q = self.q_init        
    # Rename support
    z = xp 
    states = self.states_init
    x_states = self.left_states_init
    states = self.support_states_init
    x_states = self.test_states_init
    
    for d in range(self.max_depth):
      # Process xp using attention
      # Process support xp using attention
      e = cos(z+q, xp)
      a = K.softmax(e)
      # Get linear combination of support set
@@ -664,22 +663,24 @@ class ResiLSTMEmbedding(Layer):
      # decide
      #z = r  

      # Process using attention
      # Process test x using attention
      x_e = cos(x+p, z)
      x_a = K.softmax(x_e)
      s = K.dot(x_a, z)

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

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

      # New redifinition of support set
      # Redefine  
      z = r  
        
    return [x+p, z+q]
    #return [x+p, z+q]
    return [x+p, xp+q]

  def compute_mask(self, x, mask=None):
    if not (mask is None):
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ from deepchem.models.tf_keras_models.graph_topology import merge_dicts
from deepchem.models.tensorflow_models import model_ops
from deepchem.datasets import SupportGenerator
from deepchem.datasets import get_task_test
from deepchem.datasets import get_task_dataset
from deepchem.datasets import get_task_dataset_minus_support

class SupportGraphClassifier(Model):
+16 −19
Original line number Diff line number Diff line
@@ -11,13 +11,8 @@ __license__ = "GPL"

import numpy as np
import unittest
import deepchem as dc
from tensorflow.python.framework import test_util
from deepchem.models.tf_keras_models.graph_topology import GraphTopology
from deepchem.models.tf_keras_models.keras_layers import GraphConv
from deepchem.models.tf_keras_models.keras_layers import GraphGather
from deepchem.models.tf_keras_models.keras_layers import GraphPool
from deepchem.models.tf_keras_models.keras_layers import AttnLSTMEmbedding
from deepchem.models.tf_keras_models.keras_layers import ResiLSTMEmbedding

class TestKerasLayers(test_util.TensorFlowTestCase):
  """
@@ -36,8 +31,8 @@ class TestKerasLayers(test_util.TensorFlowTestCase):
    n_feat = 10
    nb_filter = 7
    with self.test_session() as sess:
      graph_topology = GraphTopology(n_feat)
      graph_conv_layer = GraphConv(nb_filter)
      graph_topology = dc.nn.GraphTopology(n_feat)
      graph_conv_layer = dc.nn.GraphConv(nb_filter)

      X = graph_topology.get_input_placeholders()
      out = graph_conv_layer(X)
@@ -51,8 +46,8 @@ class TestKerasLayers(test_util.TensorFlowTestCase):
    batch_size = 3
    nb_filter = 7
    with self.test_session() as sess:
      graph_topology = GraphTopology(n_feat)
      graph_gather_layer = GraphGather(batch_size)
      graph_topology = dc.nn.GraphTopology(n_feat)
      graph_gather_layer = dc.nn.GraphGather(batch_size)

      X = graph_topology.get_input_placeholders()
      out = graph_gather_layer(X)
@@ -66,8 +61,8 @@ class TestKerasLayers(test_util.TensorFlowTestCase):
    batch_size = 3
    nb_filter = 7
    with self.test_session() as sess:
      graph_topology = GraphTopology(n_feat)
      graph_pool_layer = GraphPool()
      graph_topology = dc.nn.GraphTopology(n_feat)
      graph_pool_layer = dc.nn.GraphPool()

      X = graph_topology.get_input_placeholders()
      out = graph_pool_layer(X)
@@ -80,13 +75,14 @@ class TestKerasLayers(test_util.TensorFlowTestCase):
    n_feat = 10
    nb_filter = 7
    with self.test_session() as sess:
      graph_topology_test = GraphTopology(n_feat)
      graph_topology_support = GraphTopology(n_feat)
      graph_topology_test = dc.nn.GraphTopology(n_feat)
      graph_topology_support = dc.nn.GraphTopology(n_feat)

      test = graph_topology_test.get_input_placeholders()[0]
      support = graph_topology_support.get_input_placeholders()[0]

      attn_embedding_layer = AttnLSTMEmbedding(n_test, n_support, max_depth)
      attn_embedding_layer = dc.nn.AttnLSTMEmbedding(
          n_test, n_support, max_depth)
      # Try concatenating the two lists of placeholders
      feed_dict = {test: np.zeros((n_test, n_feat)),
                   support: np.zeros((n_support, n_feat))}
@@ -102,16 +98,17 @@ class TestKerasLayers(test_util.TensorFlowTestCase):
    n_feat = 10
    nb_filter = 7
    with self.test_session() as sess:
      graph_topology_test = GraphTopology(n_feat)
      graph_topology_support = GraphTopology(n_feat)
      graph_topology_test = dc.nn.GraphTopology(n_feat)
      graph_topology_support = dc.nn.GraphTopology(n_feat)

      test = graph_topology_test.get_input_placeholders()[0]
      support = graph_topology_support.get_input_placeholders()[0]

      attn_embedding_layer = ResiLSTMEmbedding(n_test, n_support, max_depth)
      resi_embedding_layer = dc.nn.ResiLSTMEmbedding(
          n_test, n_support, max_depth)
      # Try concatenating the two lists of placeholders
      feed_dict = {test: np.zeros((n_test, n_feat)),
                   support: np.zeros((n_support, n_feat))}
      test_out, support_out = attn_embedding_layer([test, support])
      test_out, support_out = resi_embedding_layer([test, support])
      assert test_out.get_shape() == (n_test, n_feat)
      assert support_out.get_shape()[1] == (n_feat)
Loading