Commit 61854bd3 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Tweaking

parent 3f8f9d5e
Loading
Loading
Loading
Loading
+58 −7
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import collections
import deepchem as dc
from deepchem.nn import model_ops
from deepchem.utils.save import log
from deepchem.metrics import to_one_hot, from_one_hot
from deepchem.models.tensorflow_models import TensorflowGraph
from deepchem.models.tensorflow_models import TensorflowGraphModel
from deepchem.models.tensorflow_models import TensorflowClassifier
@@ -72,8 +73,7 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
    n_classes: int
      the number of classes
    """
    super(TensorGraphMultiTaskClassifier, self).__init__(
        mode='classification', **kwargs)
    super(TensorGraphMultiTaskClassifier, self).__init__(**kwargs)
    self.n_tasks = n_tasks
    self.n_features = n_features
    self.n_classes = n_classes
@@ -150,6 +150,35 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
          feed_dict[self.task_weights[0]] = w_b
        yield feed_dict

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


  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, n_classes)
    results = self.run(dataset, transformers, outputs)
    # retval is of shape (n_samples, n_tasks)
    return np.argmax(retval, axis=2)


class TensorGraphMultiTaskRegressor(TensorGraph):

@@ -197,8 +226,7 @@ class TensorGraphMultiTaskRegressor(TensorGraph):
      len(layer_sizes).  Alternatively this may be a single value instead of a list, in which case the
      same value is used for every layer.
    """
    super(TensorGraphMultiTaskRegressor, self).__init__(
        mode='regression', **kwargs)
    super(TensorGraphMultiTaskRegressor, self).__init__(**kwargs)
    self.n_tasks = n_tasks
    self.n_features = n_features
    n_layers = len(layer_sizes)
@@ -278,6 +306,29 @@ 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.
@@ -362,7 +413,8 @@ class TensorGraphMultiTaskFitTransformRegressor(TensorGraphMultiTaskRegressor):
          feed_dict[self.task_weights[0]] = w_b
        yield feed_dict

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

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

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


class TensorflowMultiTaskClassifier(TensorflowClassifier):
+6 −7
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ class TensorGraph(Model):
               batch_size=100,
               random_seed=None,
               use_queue=True,
               mode="regression",
               graph=None,
               learning_rate=0.001,
               **kwargs):
@@ -48,10 +47,6 @@ class TensorGraph(Model):
      queue in batches of self.batch_size in a separate thread from the
      thread training the model.  You cannot use a queue when
      batches are not of consistent size
    mode: str
      "regression" or "classification".  "classification" models on
      predict will do an argmax(axis=2) to determine the class of the
      prediction.
    graph: tensorflow.Graph
      the Graph in which to create Tensorflow objects.  If None, a new Graph
      is created.
@@ -287,7 +282,11 @@ class TensorGraph(Model):
          result = sess.run(out_tensors, feed_dict=feed_dict)
          result = undo_transforms(result, transformers)
          results.append(result)
        return np.concatenate(results, axis=0)
        if len(results) == 1:
          return results[0]
        else:
          return results
        #return np.concatenate(results, axis=0)

#  def bayesian_predict_on_batch(self, X, transformers=[], n_passes=4):
#    """
@@ -361,7 +360,7 @@ class TensorGraph(Model):

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