Commit 241ce853 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Debugging of multitask

parent c032ef40
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -199,7 +199,21 @@ class Dataset(object):
        yield (X_batch, y_batch, w_batch, ids_batch)

  @staticmethod
  def from_numpy(data_dir, tasks, X, y, w, ids):
  def from_numpy(data_dir, X, y, w=None, ids=None, tasks=None):
    n_samples = len(X)
    # The -1 indicates that y will be reshaped to have length -1
    y = np.reshape(y, (n_samples, -1))
    n_tasks = y.shape[1]
    if ids is None:
      ids = np.arange(n_samples)
    if w is None:
      w = np.ones_like(y)
    if tasks is None:
      tasks = np.arange(n_tasks)
    ########### DEBUG
    #print("ids.shape, X.shape, y.shape, w.shape")
    #print(ids.shape, X.shape, y.shape, w.shape)
    ########### DEBUG
    raw_data = (ids, X, y, w)
    return Dataset(data_dir=data_dir, tasks=tasks, raw_data=raw_data)
    
+6 −4
Original line number Diff line number Diff line
@@ -94,8 +94,9 @@ class TestHyperparamOptAPI(TestAPI):
    y_train = np.random.randint(2, size=(n_train, n_tasks))
    w_train = np.ones_like(y_train)
    ids_train = ["C"] * n_train
    train_dataset = Dataset.from_numpy(self.train_dir, tasks,
                                       X_train, y_train, w_train, ids_train)
    train_dataset = Dataset.from_numpy(self.train_dir,
                                       X_train, y_train, w_train, ids_train,
                                       tasks)

    # Define validation dataset
    n_valid = 10
@@ -103,8 +104,9 @@ class TestHyperparamOptAPI(TestAPI):
    y_valid = np.random.randint(2, size=(n_valid, n_tasks))
    w_valid = np.ones_like(y_valid)
    ids_valid = ["C"] * n_valid
    valid_dataset = Dataset.from_numpy(self.valid_dir, tasks,
                                       X_valid, y_valid, w_valid, ids_valid)
    valid_dataset = Dataset.from_numpy(self.valid_dir,
                                       X_valid, y_valid, w_valid, ids_valid,
                                       tasks)
    params_dict = {
        "batch_size": [32],
        "data_shape": [train_dataset.get_data_shape()],
+20 −2
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ class Metric(object):
    assert mode in ["classification", "regression"]
    self.mode = mode

  def compute_metric(self, y_true, y_pred, w, n_classes=2):
  def compute_metric(self, y_true, y_pred, w=None, n_classes=2):
    """Compute a performance metric for each task.

    Args:
@@ -134,12 +134,25 @@ class Metric(object):
    Returns:
      A numpy array containing metric values for each task.
    """
    #n_samples = len(y_true)
    #y_true = np.reshape(y_true, (n_samples, -1))
    #y_pred = np.reshape(y_pred, (n_samples, -1))
    ############### DEBUG
    #print("Metric.compute_metric()")
    #print("y_true.shape, y_pred.shape, w.shape")
    #print(y_true.shape, y_pred.shape, w.shape)
    #print("r2_score(y_true, y_pred)")
    #print(r2_score(y_true, y_pred))
    ############### DEBUG
    assert y_true.shape[0] == y_pred.shape[0] == w.shape[0]
    n_samples, n_tasks = y_true.shape[0], y_true.shape[1] 
    if self.mode == "classification":
      y_pred = np.reshape(y_pred, (n_samples, n_tasks, n_classes))
    else:
      y_pred = np.reshape(y_pred, (n_samples, n_tasks))
    #y_true = np.reshape(y_true, (n_samples, n_tasks, n_classes))
    if w is None:
      w = np.ones_like(y_true)
    computed_metrics = []
    for task in xrange(n_tasks):
      y_task = y_true[:, task]
@@ -149,6 +162,11 @@ class Metric(object):
        y_pred_task = y_pred[:, task, :]
      w_task = w[:, task]
    
      ############################## DEBUG
      print("Metric.compute_metric()")
      print("y_task.shape, y_pred_task.shape, w_task.shape")
      print(y_task.shape, y_pred_task.shape, w_task.shape)
      ############################## DEBUG
      metric_value = self.compute_singletask_metric(
          y_task, y_pred_task, w_task)
      computed_metrics.append(metric_value)
@@ -183,8 +201,8 @@ class Metric(object):
    # If there are no nonzero examples, metric is ill-defined.
    if not y_true.size:
      return np.nan
    y_true = np.reshape(y_true, (n_samples,))

    y_true = np.reshape(y_true, (n_samples,))
    if self.mode == "classification":
      n_classes = y_pred.shape[-1]
      # TODO(rbharath): This has been a major source of bugs. Is there a more
+14 −2
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@


import numpy as np

from tensorflow.python.platform import googletest

from deepchem.metrics import kappa_score 
from deepchem.metrics import Metric
from deepchem import metrics


class MetricsTest(googletest.TestCase):
@@ -35,5 +35,17 @@ class MetricsTest(googletest.TestCase):
                                    1.0 - expected_agreement)
    self.assertAlmostEquals(kappa, expected_kappa)

  def test_r2_score(self):
    """Test that R^2 metric passes basic sanity tests"""
    verbosity = "high"
    np.random.seed(123)
    n_samples = 10
    y_true = np.random.rand(n_samples,)
    y_pred = np.random.rand(n_samples,)
    regression_metric = Metric(metrics.r2_score, verbosity=verbosity)
    assert np.isclose(metrics.r2_score(y_true, y_pred),
                      regression_metric.compute_metric(y_true, y_pred))
  

if __name__ == '__main__':
  googletest.main()
+10 −0
Original line number Diff line number Diff line
@@ -155,6 +155,11 @@ class Model(object):
    n_samples, n_tasks = len(dataset), len(self.tasks)
    y_pred = y_pred[:n_samples]
    y_pred = np.reshape(y_pred, (n_samples, n_tasks))
    ############## DEBUG
    #print("Model.predict()")
    #print("y_pred.shape")
    #print(y_pred.shape)
    ############## DEBUG
    return y_pred

  def predict_proba(self, dataset, transformers=[], n_classes=2):
@@ -169,6 +174,11 @@ class Model(object):
    n_tasks = len(self.tasks)
    for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(batch_size):
      y_pred_batch = self.predict_proba_on_batch(X_batch)
      ########################## DEBUG
      #print("Model.predict_proba()")
      #print("y_pred_batch.shape")
      #print(y_pred_batch.shape)
      ########################## DEBUG
      batch_size = len(y_batch)
      y_pred_batch = np.squeeze(
          np.reshape(y_pred_batch, (batch_size, n_tasks, n_classes)))
Loading