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

Debugging and yapf

parent 61854bd3
Loading
Loading
Loading
Loading
+7 −28
Original line number Diff line number Diff line
@@ -151,8 +151,8 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
        yield feed_dict

  def predict_proba(self, dataset, transformers=[], outputs=None):
    return self.run(dataset, transformers, outputs)

    return super(TensorGraphMultiTaskClassifier, self).predict(
        dataset, transformers, outputs)

  def predict(self, dataset, transformers=[], outputs=None):
    """
@@ -175,7 +175,8 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
    y_pred: numpy ndarray or list of numpy ndarrays
    """
    # Results is of shape (n_samples, n_tasks, n_classes)
    results = self.run(dataset, transformers, outputs)
    retval = super(TensorGraphMultiTaskClassifier, self).predict(
        dataset, transformers, outputs)
    # retval is of shape (n_samples, n_tasks)
    return np.argmax(retval, axis=2)

@@ -306,29 +307,6 @@ class TensorGraphMultiTaskRegressor(TensorGraph):
          feed_dict[self.task_weights[0]] = w_b
        yield feed_dict

  def predict(self, dataset, transformers=[], outputs=None):
    """
    Uses self to make predictions on provided Dataset object.

    Parameters
    ----------
    dataset: dc.data.Dataset
      Dataset to make prediction on
    transformers: list
      List of dc.trans.Transformers.
    outputs: object 
      If outputs is None, then will assume outputs = self.outputs[0] (single
      output). If outputs is a Layer/Tensor, then will evaluate and return as a
      single ndarray. If outputs is a list of Layers/Tensors, will return a list
      of ndarrays.

    Returns
    -------
    y_pred: numpy ndarray or list of numpy ndarrays
    """
    # Results is of shape (n_samples, n_tasks)
    return self.run(dataset, transformers, outputs)


class TensorGraphMultiTaskFitTransformRegressor(TensorGraphMultiTaskRegressor):
  """Implements a TensorGraphMultiTaskRegressor that performs on-the-fly transformation during fit/predict.
@@ -414,7 +392,7 @@ class TensorGraphMultiTaskFitTransformRegressor(TensorGraphMultiTaskRegressor):
        yield feed_dict

  #def predict_proba_on_generator(self, generator, transformers=[]):
  def run_on_generator(self, generator, transformers=[], outputs=None):
  def predict_on_generator(self, generator, transformers=[], outputs=None):

    def transform_generator():
      for feed_dict in generator:
@@ -427,7 +405,8 @@ class TensorGraphMultiTaskFitTransformRegressor(TensorGraphMultiTaskRegressor):
        yield feed_dict

    return super(TensorGraphMultiTaskFitTransformRegressor,
                 self).run_on_generator(transform_generator(), transformers, output)
                 self).predict_on_generator(transform_generator(), transformers,
                                            outputs)


class TensorflowMultiTaskClassifier(TensorflowClassifier):
+10 −3
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ class WeaveTensorGraph(TensorGraph):
               n_pair_feat=14,
               n_hidden=50,
               n_graph_feat=128,
               mode="classification",
               **kwargs):
    """
    Parameters
@@ -48,6 +49,7 @@ class WeaveTensorGraph(TensorGraph):
    self.n_pair_feat = n_pair_feat
    self.n_hidden = n_hidden
    self.n_graph_feat = n_graph_feat
    self.mode = mode
    super(WeaveTensorGraph, self).__init__(**kwargs)
    self.build_graph()

@@ -187,6 +189,7 @@ class DTNNTensorGraph(TensorGraph):
               distance_min=-1,
               distance_max=18,
               output_activation=True,
               mode="classification",
               **kwargs):
    """
    Parameters
@@ -217,6 +220,7 @@ class DTNNTensorGraph(TensorGraph):
        [distance_min + i * self.step_size for i in range(n_distance)])
    self.steps = np.expand_dims(self.steps, 0)
    self.output_activation = output_activation
    self.mode = mode
    super(DTNNTensorGraph, self).__init__(**kwargs)
    assert self.mode == "regression"
    self.build_graph()
@@ -335,6 +339,7 @@ class DAGTensorGraph(TensorGraph):
               n_atom_feat=75,
               n_graph_feat=30,
               n_outputs=30,
               mode="classification",
               **kwargs):
    """
    Parameters
@@ -349,13 +354,13 @@ class DAGTensorGraph(TensorGraph):
      Number of features for atom in the graph
    n_outputs: int, optional
      Number of features for each molecule

    """
    self.n_tasks = n_tasks
    self.max_atoms = max_atoms
    self.n_atom_feat = n_atom_feat
    self.n_graph_feat = n_graph_feat
    self.n_outputs = n_outputs
    self.mode = mode
    super(DAGTensorGraph, self).__init__(**kwargs)
    self.build_graph()

@@ -477,15 +482,17 @@ class DAGTensorGraph(TensorGraph):

class GraphConvTensorGraph(TensorGraph):

  def __init__(self, n_tasks, **kwargs):
  def __init__(self, n_tasks, mode="classification", **kwargs):
    """
    Parameters
    ----------
    n_tasks: int
      Number of tasks

    mode: str
      Either "classification" or "regression"
    """
    self.n_tasks = n_tasks
    self.mode = mode
    self.error_bars = True if 'error_bars' in kwargs and kwargs['error_bars'] else False
    kwargs['use_queue'] = False
    super(GraphConvTensorGraph, self).__init__(**kwargs)
+80 −43
Original line number Diff line number Diff line
@@ -257,6 +257,7 @@ class TensorGraph(Model):
#    return retval

#def predict_proba_on_generator(self, generator, transformers=[]):

  def predict_on_generator(self, generator, transformers=[], outputs=None):
    """
    Returns:
@@ -265,28 +266,45 @@ class TensorGraph(Model):
    if not self.built:
      self.build()
    if outputs is None:
      assert len(self.outputs) == 1
      outputs = self.outputs
    with self._get_tf("Graph").as_default():
      with tf.Session() as sess:
        saver = tf.train.Saver()
        self._initialize_weights(sess, saver)
        out_tensors = [x.out_tensor for x in self.outputs]
        results = []
        results = [[] for out in out_tensors]
        for feed_dict in generator:
          feed_dict = {
              self.layers[k.name].out_tensor: v
              for k, v in six.iteritems(feed_dict)
          }
          feed_dict[self._training_placeholder] = 0.0
          result = sess.run(out_tensors, feed_dict=feed_dict)
          result = undo_transforms(result, transformers)
          results.append(result)
        if len(results) == 1:
          return results[0]
          feed_results = sess.run(out_tensors, feed_dict=feed_dict)
          if len(feed_results) > 1:
            if len(transformers):
              raise ValueError("Does not support transformations "
                               "for multiple outputs.")
          elif len(feed_results) == 1:
            result = undo_transforms(feed_results[0], transformers)
            feed_results = [result]
          for ind, result in enumerate(feed_results):
            results[ind].append(result)

        final_results = []
        for result_list in results:
          final_results.append(np.concatenate(result_list, axis=0))
        if len(final_results) == 1:
          return final_results[0]
        else:
          return results
        #return np.concatenate(results, axis=0)
          return final_results

  def predict_proba_on_generator(self, generator, transformers=[],
                                 outputs=None):
    """
    Returns:
      y_pred: numpy ndarray of shape (n_samples, n_classes*n_tasks)
    """
    return self.predict_on_generator(generator, transformers, outputs)

#  def bayesian_predict_on_batch(self, X, transformers=[], n_passes=4):
#    """
@@ -322,20 +340,39 @@ class TensorGraph(Model):
#    generator = self.default_generator(dataset, predict=True, pad_batches=True)
#    return self.predict_on_generator(generator, transformers)

#  def predict_on_batch(self, X, sess=None, transformers=[]):
#    """Generates output predictions for the input samples,
#      processing the samples in a batched way.
#
#    # Arguments
#        x: the input data, as a Numpy array.
#        verbose: verbosity mode, 0 or 1.
#
#    # Returns
#        A Numpy array of predictions.
#    """
#    dataset = NumpyDataset(X=X, y=None)
#    generator = self.default_generator(dataset, predict=True, pad_batches=False)
#    return self.predict_on_generator(generator, transformers)
  def predict_on_batch(self, X, transformers=[]):
    """Generates predictions for input samples, processing samples in a batch.

    Parameters
    ---------- 
    X: ndarray
      the input data, as a Numpy array.
    transformers: List
      List of dc.trans.Transformers 

    Returns
    -------
    A Numpy array of predictions.
    """
    dataset = NumpyDataset(X=X, y=None)
    generator = self.default_generator(dataset, predict=True, pad_batches=False)
    return self.predict_on_generator(generator, transformers)

  def predict_proba_on_batch(self, X, transformers=[]):
    """Generates predictions for input samples, processing samples in a batch.

    Parameters
    ---------- 
    X: ndarray
      the input data, as a Numpy array.
    transformers: List
      List of dc.trans.Transformers 

    Returns
    -------
    A Numpy array of predictions.
    """
    return self.predict_on_batch(X, transformers)

#  def predict_proba_on_batch(self, X, sess=None, transformers=[]):
#    dataset = NumpyDataset(X=X, y=None)
@@ -365,26 +402,26 @@ class TensorGraph(Model):
    generator = self.default_generator(dataset, predict=True, pad_batches=False)
    return self.predict_on_generator(generator, transformers, outputs)

#  def predict_proba(self, dataset, transformers=[], outputs=None):
#    """
#    Parameters
#    ----------
#    dataset: dc.data.Dataset
#      Dataset to make prediction on
#    transformers: list
#      List of dc.trans.Transformers.
#    outputs: object 
#      If outputs is None, then will assume outputs = self.outputs[0] (single
#      output). If outputs is a Layer/Tensor, then will evaluate and return as a
#      single ndarray. If outputs is a list of Layers/Tensors, will return a list
#      of ndarrays.
#
#    Returns
#    -------
#    y_pred: numpy ndarray or list of numpy ndarrays
#    """
#    generator = self.default_generator(dataset, predict=True, pad_batches=False)
#    return self.predict_proba_on_generator(generator, transformers, output)
  def predict_proba(self, dataset, transformers=[], outputs=None):
    """
    Parameters
    ----------
    dataset: dc.data.Dataset
      Dataset to make prediction on
    transformers: list
      List of dc.trans.Transformers.
    outputs: object 
      If outputs is None, then will assume outputs = self.outputs[0] (single
      output). If outputs is a Layer/Tensor, then will evaluate and return as a
      single ndarray. If outputs is a list of Layers/Tensors, will return a list
      of ndarrays.

    Returns
    -------
    y_pred: numpy ndarray or list of numpy ndarrays
    """
    generator = self.default_generator(dataset, predict=True, pad_batches=False)
    return self.predict_proba_on_generator(generator, transformers, outputs)

  def topsort(self):
    return nx.topological_sort(self.nxgraph)
+13 −14
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ class TestTensorGraph(unittest.TestCase):
    tg.add_output(output)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=1000)
    prediction = np.squeeze(tg.predict_proba_on_batch(X))
    prediction = np.squeeze(tg.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, y, atol=0.4)))

  @flaky
@@ -78,10 +78,10 @@ class TestTensorGraph(unittest.TestCase):
    tg.fit_generator(
        databag.iterbatches(
            epochs=1000, batch_size=tg.batch_size, pad_batches=True))
    prediction = tg.predict_proba_on_generator(databag.iterbatches())
    predictions = tg.predict_on_generator(databag.iterbatches())
    for i in range(2):
      y_real = ys[i].X
      y_pred = prediction[:, i, :]
      y_pred = predictions[i]
      assert_true(np.all(np.isclose(y_pred, y_real, atol=0.6)))

  def test_single_task_regressor(self):
@@ -98,7 +98,7 @@ class TestTensorGraph(unittest.TestCase):
    tg.add_output(dense)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=1000)
    prediction = np.squeeze(tg.predict_proba_on_batch(X))
    prediction = np.squeeze(tg.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, y, atol=3.0)))

  def test_multi_task_regressor(self):
@@ -137,10 +137,10 @@ class TestTensorGraph(unittest.TestCase):
    tg.fit_generator(
        databag.iterbatches(
            epochs=1000, batch_size=tg.batch_size, pad_batches=True))
    prediction = tg.predict_proba_on_generator(databag.iterbatches())
    predictions = tg.predict_on_generator(databag.iterbatches())
    for i in range(2):
      y_real = ys[i].X
      y_pred = prediction[:, i, :]
      y_pred = predictions[i]
      assert_true(np.all(np.isclose(y_pred, y_real, atol=1.5)))

  @flaky
@@ -160,7 +160,7 @@ class TestTensorGraph(unittest.TestCase):
    tg.add_output(output)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=1000)
    prediction = np.squeeze(tg.predict_proba_on_batch(X))
    prediction = np.squeeze(tg.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, y, atol=0.4)))

  @flaky
@@ -184,11 +184,11 @@ class TestTensorGraph(unittest.TestCase):
        initial_rate=0.1, decay_rate=0.96, decay_steps=100000)
    tg.set_optimizer(GradientDescent(learning_rate=learning_rate))
    tg.fit(dataset, nb_epoch=1000)
    prediction = np.squeeze(tg.predict_proba_on_batch(X))
    prediction = np.squeeze(tg.predict_on_batch(X))
    tg.save()

    tg1 = TensorGraph.load_from_dir(tg.model_dir)
    prediction2 = np.squeeze(tg1.predict_proba_on_batch(X))
    prediction2 = np.squeeze(tg1.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, prediction2, atol=0.01)))

  @nottest
@@ -235,11 +235,11 @@ class TestTensorGraph(unittest.TestCase):
    tg.add_output(output)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=1)
    prediction = np.squeeze(tg.predict_proba_on_batch(X))
    prediction = np.squeeze(tg.predict_on_batch(X))
    tg.save()

    tg1 = TensorGraph.load_from_dir(tg.model_dir)
    prediction2 = np.squeeze(tg1.predict_proba_on_batch(X))
    prediction2 = np.squeeze(tg1.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, prediction2, atol=0.01)))

  def test_shared_layer(self):
@@ -279,6 +279,5 @@ class TestTensorGraph(unittest.TestCase):
    tg.fit_generator(
        databag.iterbatches(
            epochs=1, batch_size=tg.batch_size, pad_batches=True))
    prediction = tg.predict_proba_on_generator(databag.iterbatches())
    assert_true(
        np.all(np.isclose(prediction[:, 0], prediction[:, 1], atol=0.01)))
    prediction = tg.predict_on_generator(databag.iterbatches())
    assert_true(np.all(np.isclose(prediction[0], prediction[1], atol=0.01)))