Commit 9c093e5d authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Finished writing TensorflowGraph class. Now debugging tests.

parent 8ebfd64e
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -21,6 +21,33 @@ import collections
import numpy as np
from sklearn import metrics

def compute_metric(num_tasks, y_true, y_pred, metric_str, threshold=0.5):
  """Compute a performance metric for each task.

  Args:
    y_true: A list of arrays containing true values for each task.
    y_pred: A list of arrays containing predicted values for each task.
    metric_str: String description of the metric to compute. Must be in
      metrics.METRICS.
    threshold: Float threshold to apply to probabilities for positive/negative
      class assignment.

  Returns:
    A numpy array containing metric values for each task.
  """
  computed_metrics = []
  for task in xrange(num_tasks):
    yt = y_true[task]
    yp = y_pred[task]
    try:
      metric_value = compute_metric(yt, yp, metric_str,
                                    threshold=threshold)
    except (AssertionError, ValueError) as e:
      warnings.warn('Error calculating metric %s for task %d: %s'
                    % (metric_str, task, e))
      metric_value = np.nan
    computed_metrics.append(metric_value)
  return computed_metrics

def kappa_score(y_true, y_pred):
  """Calculate Cohen's kappa for classification tasks.
+0 −14
Original line number Diff line number Diff line
@@ -75,17 +75,6 @@ class Model(object):
    """
    return os.path.join(out_dir, "model_params.joblib")

  @staticmethod
  def get_task_type(model_name):
    """
    Given model type, determine if classifier or regressor.
    """
    if model_name in ["logistic", "rf_classifier", "singletask_deep_classifier",
                      "multitask_deep_classifier"]:
      return "classification"
    else:
      return "regression"

  def save(self, out_dir):
    """Dispatcher function for saving."""
    params = {"model_params" : self.model_params,
@@ -124,9 +113,6 @@ class Model(object):
    batch_size = self.model_params["batch_size"]
    for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(batch_size):
      y_pred = self.predict_on_batch(X_batch)
      print("predict()")
      print("y_pred.shape")
      print(y_pred.shape)
      y_pred = np.reshape(y_pred, np.shape(y_batch))

      # Now undo transformations on y, y_pred
+3 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ class SklearnModel(Model):
  Abstract base class for different ML models.
  """
  def __init__(self, task_types, model_params, 
               model_instance=RandomForestRegressor(),
               model_instance=None,
               initialize_raw_model=True):
    super(SklearnModel, self).__init__(
        task_types, model_params, initialize_raw_model)
@@ -31,12 +31,12 @@ class SklearnModel(Model):
  # support partial_fit, but only for some models. Might make sense to make
  # PartialSklearnModel subclass at some point to support large data models.
  # Also, use of batch_size=32 is arbitrary and kludgey
  def fit(self, numpy_dataset):
  def fit(self, dataset):
    """
    Fits SKLearn model to data.
    """
    Xs, ys = [], []
    for (X_batch, y_batch, _, _) in numpy_dataset.iterbatches(batch_size=32):
    for (X_batch, y_batch, _, _) in dataset.iterbatches(batch_size=32):
      Xs.append(X_batch)
      ys.append(y_batch)
    X = np.concatenate(Xs)
+162 −302

File changed.

Preview size limit exceeded, changes collapsed.

+14 −21
Original line number Diff line number Diff line
@@ -84,27 +84,27 @@ from deepchem.utils.evaluate import to_one_hot
class TensorflowMultiTaskClassifier(TensorflowClassifier):
  """Implements an icml model as configured in a model_config.proto."""

  def build(self, tf_graph):
  def build(self):
    """Constructs the graph architecture as specified in its config.

    This method creates the following Placeholders:
      mol_features: Molecule descriptor (e.g. fingerprint) tensor with shape
        batch_size x num_features.
    """
    assert len(tf_graph.model_params["data_shape"]) == 1
    num_features = tf_graph.model_params["data_shape"][0]
    with tf_graph.graph.as_default():
      with tf.name_scope(tf_graph.placeholder_scope):
        tf_graph.mol_features = tf.placeholder(
    assert len(self.model_params["data_shape"]) == 1
    num_features = self.model_params["data_shape"][0]
    with self.graph.as_default():
      with tf.name_scope(self.placeholder_scope):
        self.mol_features = tf.placeholder(
            tf.float32,
            shape=[tf_graph.model_params["batch_size"],
            shape=[self.model_params["batch_size"],
                   num_features],
            name='mol_features')

      layer_sizes = tf_graph.model_params["layer_sizes"]
      weight_init_stddevs = tf_graph.model_params["weight_init_stddevs"]
      bias_init_consts = tf_graph.model_params["bias_init_consts"]
      dropouts = tf_graph.model_params["dropouts"]
      layer_sizes = self.model_params["layer_sizes"]
      weight_init_stddevs = self.model_params["weight_init_stddevs"]
      bias_init_consts = self.model_params["bias_init_consts"]
      dropouts = self.model_params["dropouts"]
      lengths_set = {
          len(layer_sizes),
          len(weight_init_stddevs),
@@ -115,7 +115,7 @@ class TensorflowMultiTaskClassifier(TensorflowClassifier):
      num_layers = lengths_set.pop()
      assert num_layers > 0, 'Must have some layers defined.'

      prev_layer = tf_graph.mol_features
      prev_layer = self.mol_features
      prev_layer_size = num_features 
      for i in xrange(num_layers):
        layer = tf.nn.relu(model_ops.FullyConnectedLayer(
@@ -130,8 +130,8 @@ class TensorflowMultiTaskClassifier(TensorflowClassifier):
        prev_layer = layer
        prev_layer_size = layer_sizes[i]

      tf_graph.output = model_ops.MultitaskLogits(
          layer, tf_graph.model_params["num_classification_tasks"])
      self.output = model_ops.MultitaskLogits(
          layer, self.model_params["num_classification_tasks"])

  def construct_feed_dict(self, X_b, y_b=None, w_b=None, ids_b=None):
    """Construct a feed dictionary from minibatch data.
@@ -162,10 +162,3 @@ class TensorflowMultiTaskClassifier(TensorflowClassifier):
    orig_dict["valid"] = np.ones((self.model_params["batch_size"],), dtype=bool)
    return self._get_feed_dict(orig_dict)
  # TODO(rbharath): This explicit manipulation of scopes is ugly. Is there a
  # better design here?
  def _get_feed_dict(self, named_values):
    feed_dict = {}
    for name, value in named_values.iteritems():
      feed_dict['{}/{}:0'.format(self.placeholder_root, name)] = value
    return feed_dict
Loading