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

Test reveals padding issue for batches. Not sure how to fix yet.

parent f22cd381
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -75,14 +75,14 @@ class SequentialGraphModel(object):

class SequentialSupportGraphModel(object):
  """An analog of Keras Sequential model for test/support models."""
  def __init__(self, n_test, n_support, n_feat):
  def __init__(self, n_test, n_support, n_feat, max_atoms_per_mol=60):
    """
    Parameters
    ----------
    n_test: int
      Number of test atoms.
      Number of test molecules.
    n_support: int
      Number of support atoms.
      Number of support support.
    n_feat: int
      Number of atomic features.
    """
@@ -90,10 +90,12 @@ class SequentialSupportGraphModel(object):
    self.n_support = n_support

    # Create graph topology and x
    n_test_atoms = n_test * max_atoms_per_mol
    n_support_atoms = n_support * max_atoms_per_mol
    self.test_graph_topology = GraphTopology(
        n_test, n_feat, name='test')
        n_test_atoms, n_feat, name='test')
    self.support_graph_topology = GraphTopology(
        n_support, n_feat, name='support')
        n_support_atoms, n_feat, name='support')
    self.test = self.test_graph_topology.get_atom_features_placeholder()
    self.support = self.support_graph_topology.get_atom_features_placeholder()

@@ -148,10 +150,10 @@ class SequentialSupportGraphModel(object):
    self.layers.append(layer)
    self.test, self.support = layer([self.test, self.support])

  def get_test_outputs(self):
  def get_test_output(self):
    return self.test

  def get_support_outputs(self):
  def get_support_output(self):
    return self.support
  
  def return_outputs(self):
+19 −25
Original line number Diff line number Diff line
@@ -64,33 +64,19 @@ def get_task_support(dataset, n_pos, n_neg, task):
  mol_list = dataset.ids 
  y_task = dataset.y[:, task]


  ################################################# DEBUG
  print("get_task_support()")
  print("np.count_nonzero(y_task), len(y_task)")
  print(np.count_nonzero(y_task), len(y_task))
  ################################################# DEBUG
  # Split data into pos and neg lists.
  pos_mols = np.where(y_task == 1)[0]
  neg_mols = np.where(y_task == 0)[0]
  ################################################# DEBUG
  print("y_task")
  print(y_task)
  print("pos_mols")
  print(pos_mols)
  ################################################# DEBUG

  n_pos_avail = len(pos_mols)
  n_neg_avail = len(neg_mols)

  # Ensure that there are examples to sample
  assert n_pos_avail >= n_pos
  assert n_neg_avail >= n_neg
  # TODO(rbharath): Commenting this out since I think it's OK to duplicate
  # pos/neg samples when necessary. Delete this part once sure.
  ## Ensure that there are examples to sample
  #assert len(pos_mols) >= n_pos
  #assert len(neg_mols) >= n_neg

  # Get randomly sampled pos/neg indices (with replacement)
  pos_inds = pos_mols[np.random.choice(n_pos_avail, (n_pos))]
  neg_inds = neg_mols[np.random.choice(n_neg_avail, (n_neg))]

  pos_inds = pos_mols[np.random.choice(len(pos_mols), (n_pos))]
  neg_inds = neg_mols[np.random.choice(len(neg_mols), (n_neg))]

  # Handle one-d vs. non one-d feature matrices
  one_dimensional_features = (len(dataset.X.shape) == 1)
@@ -134,6 +120,11 @@ class SupportGenerator(object):
  # TODO(rbharath): This is generating data from one task at a time. Why not
  # have batches that mix information from multiple tasks?
  def next(self):
    """Sample next support.

    Supports are sampled from the tasks in a random order. Each support is
    drawn entirely from within one task.
    """
    if self.trial_num == self.n_trials:
      raise StopIteration
    else:
@@ -222,12 +213,12 @@ class SupportGraphClassifier(Model):
        tensor=K.placeholder(shape=[self.support_batch_size], dtype='float32',
        name="support_label_placeholder"))

  def construct_feed_dict(self, test, support, training=True):
  def construct_feed_dict(self, test, support, training=True, add_phase=False):
    """Constructs tensorflow feed from test/support sets."""
    # Generate dictionary elements for support 
    support_labels_dict = {self.support_label_placeholder: np.squeeze(support.y)}
    support_topo_dict = (
        self.model.graph_topology_support.batch_to_feed_dict(support.X))
        self.model.support_graph_topology.batch_to_feed_dict(support.X))
    support_dict = merge_dicts([support_topo_dict, support_labels_dict])
  
    # Generate dictionary elements for test
@@ -239,14 +230,17 @@ class SupportGraphClassifier(Model):
                   self.weight_placeholder: np.squeeze(test.w)}
    # Get graph information for x
    batch_topo_dict = (
        self.model.graph_topology_test.batch_to_feed_dict(test.X))
        self.model.test_graph_topology.batch_to_feed_dict(test.X))
    test_dict =  merge_dicts([batch_topo_dict, target_dict])

    test_support_dicts = merge_dicts([test_dict, support_dict])

    # Get information for keras 
    if add_phase:
      keras_dict = {K.learning_phase() : training}
      feed_dict = merge_dicts([test_support_dicts, keras_dict])
    else:
      feed_dict = test_support_dicts
    return feed_dict

  def fit(self, dataset, n_trials_per_epoch=1000, nb_epoch=10, n_pos=1,
@@ -256,7 +250,7 @@ class SupportGraphClassifier(Model):
      lr = self.learning_rate / (1 + float(epoch) / self.decay_T)

      # Create different support sets
      for (task, support) in SupportGenerator(dataset, dataset.get_task_names(),
      for (task, support) in SupportGenerator(dataset, range(self.n_tasks),
          n_pos, n_neg, n_trials_per_epoch):
        print("Sampled Support set")
        # Get batch to try it out on
+0 −20
Original line number Diff line number Diff line
@@ -59,10 +59,6 @@ class TestSupportGenerator(unittest.TestCase):
    ids = np.arange(n_samples)
    X = np.random.rand(n_samples, n_features)
    y = np.random.randint(2, size=(n_samples, n_tasks))
    ############################################## DEBUG
    print("y")
    print(y)
    ############################################## DEBUG
    w = np.ones((n_samples, n_tasks))
    dataset = NumpyDataset(X, y, w, ids)

@@ -70,27 +66,11 @@ class TestSupportGenerator(unittest.TestCase):
    supp_gen = SupportGenerator(
        dataset, np.arange(n_tasks), n_pos, n_neg, n_trials)
    num_supports = 0
    ################################################## DEBUG
    print("n_pos, n_neg, n_features")
    print(n_pos, n_neg, n_features)
    ################################################## DEBUG
    
    for (task, support) in supp_gen:
      assert support.X.shape == (n_pos + n_neg, n_features)
      num_supports += 1
      assert task == 0 # Only one task in this example
      n_supp_pos = np.count_nonzero(support.y)
      ################################################## DEBUG
      print("support.X.shape")
      print(support.X.shape)
      print("support.y")
      print(support.y)
      print("support.w")
      print(support.w)
      ################################################## DEBUG
      assert n_supp_pos == n_pos
    assert num_supports == n_trials
    ################################################## DEBUG
    print("num_supports")
    print(num_supports)
    ################################################## DEBUG