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

Cleaned up and fixed some tests

parent a1a2b847
Loading
Loading
Loading
Loading
+0 −17
Original line number Diff line number Diff line
@@ -567,12 +567,10 @@ class DiskDataset(Dataset):
      for i in range(num_shards):
        X, y, w, ids = dataset.get_shard(shard_perm[i])
        n_samples = X.shape[0]
        #################################################################### DEBUG
        # TODO(rbharath): This happens in tests sometimes, but don't understand why?
        # Handle edge case.
        if n_samples == 0:
          continue
        #################################################################### DEBUG
        if not deterministic:
          sample_perm = np.random.permutation(n_samples)
        else:
@@ -581,11 +579,6 @@ class DiskDataset(Dataset):
          shard_batch_size = n_samples
        else:
          shard_batch_size = batch_size 
        #################################################################### DEBUG
        #print("iterbatch()")
        #print("n_samples, shard_batch_size")
        #print(n_samples, shard_batch_size)
        #################################################################### DEBUG
        interval_points = np.linspace(
            0, n_samples, np.ceil(float(n_samples)/shard_batch_size)+1, dtype=int)
        for j in range(len(interval_points)-1):
@@ -946,20 +939,10 @@ class DiskDataset(Dataset):
      shard_len = len(X)
      # Find indices which rest in this shard
      num_shard_elts = 0
      ############################################################### DEBUG
      #print("Dataset.select()")
      #print("shard_num, shard_len, indices_count, indices_count+num_shard_elts, count, count+shard_len")
      #print(shard_num, shard_len, indices_count, indices_count+num_shard_elts, count, count+shard_len)
      ############################################################### DEBUG
      while indices[indices_count+num_shard_elts] < count + shard_len:
        num_shard_elts += 1
        if indices_count + num_shard_elts >= len(indices):
          break
      ############################################################### DEBUG
      #print("after while loop")
      #print("num_shard_elts")
      #print(num_shard_elts)
      ############################################################### DEBUG
      # Need to offset indices to fit within shard_size
      shard_inds =  indices[indices_count:indices_count+num_shard_elts] - count
      X_sel = X[shard_inds]
+8 −5
Original line number Diff line number Diff line
@@ -174,6 +174,10 @@ def get_task_support(dataset, n_episodes, n_pos, n_neg, task, log_every_n=50):
    if episode % log_every_n == 0:
      print("Sampling support %d" % episode)
    # No replacement allowed for supports
    ############################################################# DEBUG
    print("len(pos_mols), n_pos, len(neg_mols), n_neg")
    print(len(pos_mols), n_pos, len(neg_mols), n_neg)
    ############################################################# DEBUG
    pos_ids = np.random.choice(len(pos_mols), (n_pos,), replace=False)
    neg_ids = np.random.choice(len(neg_mols), (n_neg,), replace=False)
    pos_inds, neg_inds = pos_mols[pos_ids], neg_mols[neg_ids]
@@ -267,6 +271,8 @@ class EpisodeGenerator(object):

      return (task, support, test)

  __next__ = next # Python 3.X compatibility


class SupportGenerator(object):
  """Generate support sets from a dataset.
@@ -274,7 +280,7 @@ class SupportGenerator(object):
  Iterates over tasks and trials. For each trial, picks one support from
  each task, and returns in a randomized order
  """
  def __init__(self, dataset, n_pos, n_neg, n_trials, replace):
  def __init__(self, dataset, n_pos, n_neg, n_trials):
    """
    Parameters
    ----------
@@ -287,8 +293,6 @@ class SupportGenerator(object):
    n_trials: int
      Number of passes over dataset to make. In total, n_tasks*n_trials
      support sets will be sampled by algorithm.
    replace: bool
      Whether to use sampling with or without replacement.
    """
      
    self.tasks = range(len(dataset.get_task_names()) )
@@ -297,7 +301,6 @@ class SupportGenerator(object):
    self.dataset = dataset
    self.n_pos = n_pos
    self.n_neg = n_neg
    self.replace = replace

    # Init the iterator
    self.perm_tasks = np.random.permutation(self.tasks)
@@ -321,7 +324,7 @@ class SupportGenerator(object):
      #support = self.supports[task][self.trial_num]
      support = get_single_task_support(
          self.dataset, n_pos=self.n_pos, n_neg=self.n_neg, task=task,
          replace=self.replace)
          replace=False)
      # Increment and update logic
      self.task_num += 1
      if self.task_num == self.n_tasks:
+4 −9
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import numpy as np
import unittest
import tensorflow as tf
import deepchem as dc

class TestSupports(unittest.TestCase):
  """
  Test that support generation happens properly.
@@ -118,8 +119,7 @@ class TestSupports(unittest.TestCase):
    dataset = dc.data.NumpyDataset(X, y, w, ids)

    # Create support generator
    supp_gen = dc.data.SupportGenerator(
        dataset, n_pos, n_neg, n_trials, replace=True)
    supp_gen = dc.data.SupportGenerator(dataset, n_pos, n_neg, n_trials)

  def test_simple_episode_generator(self):
    """Conducts simple test that episode generator runs."""
@@ -261,8 +261,7 @@ class TestSupports(unittest.TestCase):
    dataset = dc.data.NumpyDataset(X, y, w, ids)

    # Create support generator
    supp_gen = dc.data.SupportGenerator(
        dataset, n_pos, n_neg, n_trials, replace=True)
    supp_gen = dc.data.SupportGenerator(dataset, n_pos, n_neg, n_trials)
    num_supports = 0
    
    for (task, support) in supp_gen:
@@ -291,7 +290,7 @@ class TestSupports(unittest.TestCase):
    dataset = dc.data.NumpyDataset(X, y, w, ids)

    support_generator = dc.data.SupportGenerator(dataset, 
        n_pos, n_neg, n_trials, replace=False)
        n_pos, n_neg, n_trials)

    for ind, (task, support) in enumerate(support_generator):
      task_dataset = dc.data.get_task_dataset_minus_support(
@@ -299,11 +298,7 @@ class TestSupports(unittest.TestCase):

      task_y = dataset.y[:, task]
      task_w = dataset.w[:, task]
      print("Number of y elements (including missing data)")
      print(len(task_y))
      task_y = task_y[task_w != 0]
      print("len(task_y), len(support), len(task_dataset)")
      print(len(task_y), len(support), len(task_dataset))
      assert len(task_y) == len(support) + len(task_dataset)
      print("Verifying that task_dataset doesn't overlap with support.")
      for task_id in task_dataset.ids:
+3 −3
Original line number Diff line number Diff line
@@ -718,7 +718,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
        # model has mastered memorization of provided support.
        scores = model.evaluate(dataset, classification_metric, n_trials=5,
                                n_pos=n_pos, n_neg=n_neg,
                                exclude_support=False, replace=False)
                                exclude_support=False)

      # Measure performance on 0-th task.
      assert scores[0] > .9
@@ -793,7 +793,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
        # model has mastered memorization of provided support.
        scores = model.evaluate(dataset, classification_metric, n_trials=5,
                                n_pos=n_pos, n_neg=n_neg,
                                exclude_support=False, replace=False)
                                exclude_support=False)

      # Measure performance on 0-th task.
      assert scores[0] > .9
@@ -867,7 +867,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
        # model has mastered memorization of provided support.
        scores = model.evaluate(dataset, classification_metric, n_trials=5,
                                n_pos=n_pos, n_neg=n_neg,
                                exclude_support=False, replace=False)
                                exclude_support=False)

      # Measure performance on 0-th task.
      assert scores[0] > .9
+5 −27
Original line number Diff line number Diff line
@@ -27,8 +27,7 @@ from deepchem.data import get_task_dataset_minus_support
class SupportGraphClassifier(Model):
  def __init__(self, sess, model,
               test_batch_size=10, support_batch_size=10,
               learning_rate=.001, decay_steps=20, decay_rate=1.,
               similarity="cosine", **kwargs):
               learning_rate=.001, similarity="cosine", **kwargs):
    """Builds a support-based classifier.

    See https://arxiv.org/pdf/1606.04080v1.pdf for definition of support.
@@ -43,10 +42,6 @@ class SupportGraphClassifier(Model):
      Number of positive examples in support.
    n_neg: int
      Number of negative examples in support.
    decay_steps: int, optional
      Corresponds to argument decay_steps in tf.train.exponential_decay
    decay_rate: float, optional
      Corresponds to argument decay_rate in tf.train.exponential_decay
    """
    self.sess = sess
    self.similarity = similarity
@@ -55,8 +50,6 @@ class SupportGraphClassifier(Model):
    self.support_batch_size = support_batch_size

    self.learning_rate = learning_rate
    #self.decay_steps = decay_steps
    #self.decay_rate = decay_rate
    self.epsilon = K.epsilon()

    self.add_placeholders()
@@ -377,8 +370,8 @@ class SupportGraphClassifier(Model):
    ########################################################### DEBUG
    return y_pred_batch
    
  def evaluate(self, dataset, metric, n_pos=1,
               n_neg=9, n_trials=1000, exclude_support=True):
  def evaluate(self, dataset, metric, n_pos,
               n_neg, n_trials=1000, exclude_support=True):
    """Evaluate performance on dataset according to metrics


@@ -415,15 +408,9 @@ class SupportGraphClassifier(Model):
    # Get batches
    test_tasks = range(len(dataset.get_task_names()))
    task_scores = {task: [] for task in test_tasks}
    support_generator = SupportGenerator(dataset, test_tasks,
        n_pos, n_neg, n_trials)
    support_generator = SupportGenerator(dataset, n_pos, n_neg, n_trials)
    for ind, (task, support) in enumerate(support_generator):
      print("Eval sample %d from task %s" % (ind, str(task)))
      ################################################################ DEBUG
      print("task, len(support)")
      print(task, len(support))
      ################################################################ DEBUG
      
      # TODO(rbharath): Add test for get_task_dataset_minus_support for
      # multitask case with missing data...
      if exclude_support:
@@ -441,22 +428,13 @@ class SupportGraphClassifier(Model):
      task_y = task_y[task_w != 0]
      print("Number of y elements (excluding missing data)")
      print(len(task_y))
      print("Verifying that no elements were dropped.")
      print("len(task_y), len(support), len(task_dataset)")
      print(len(task_y), len(support), len(task_dataset))
      assert len(task_y) == len(support) + len(task_dataset)
      print("Verifying that task_dataset doesn't overlap with support.")
      for task_id in task_dataset.ids:
        assert task_id not in set(support.ids)
      print("evaluate()")
      y_pred_hot = from_one_hot(y_pred)
      print("y_pred")
      print(y_pred)
      print("y_pred_hot")
      print(y_pred_hot)
      print("np.count_nonzero(y_pred_hot)")
      print(np.count_nonzero(y_pred_hot))
      print("np.count_nonzero(task_dataset.y)")
      print(np.count_nonzero(task_dataset.y))
      ################################################################ DEBUG
      task_scores[task].append(metric.compute_metric(
          task_dataset.y, y_pred, task_dataset.w))
Loading