Commit 58bf2bf9 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Fixing some sizing bugs and conventions for models

parent 3a4304b3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -246,7 +246,7 @@ class Dataset(object):
    """
    ws = []
    for (_, _, w_b, _) in self.itershards():
      ws.append(w_b)
      ws.append(np.array(w_b))
    return np.vstack(ws)

  def _pad_batch(self, X_b, y_b, w_b, ids_b, batch_size):
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ class TestTFHyperparamOptAPI(TestAPI):
        splittype, compound_featurizers, 
        complex_featurizers, input_transformers,
        output_transformers, input_file, tasks)
    metric = Metric(metrics.matthews_corrcoef, np.mean)
    metric = Metric(metrics.matthews_corrcoef, np.mean, mode="classification")
    params_dict = {"activation": ["relu"],
                    "momentum": [.9],
                    "batch_size": [50],
+26 −18
Original line number Diff line number Diff line
@@ -127,7 +127,6 @@ class Metric(object):
    """Compute a performance metric for each task.

    Args:
      num_tasks: Number of tasks
      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: Must be a class that inherits from Metric 
@@ -135,27 +134,28 @@ class Metric(object):
    Returns:
      A numpy array containing metric values for each task.
    """
    print("y_true.shape, y_pred.shape")
    print(y_true.shape, y_pred.shape)
    assert y_true.shape[0] == y_pred.shape[0] == w.shape[0]
    num_tasks = y_true.shape[1] 
    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))
    computed_metrics = []
    for task in xrange(num_tasks):
    for task in xrange(n_tasks):
      y_task = y_true[:, task]
      if self.mode == "regression":
        y_pred_task = y_pred[:, task]
      else:
        y_pred_task = y_pred[:, task*n_classes:(task+1)*n_classes]
        y_pred_task = y_pred[:, task, :]
      w_task = w[:, task]
    
      try:
      metric_value = self.compute_singletask_metric(
          y_task, y_pred_task, w_task)
      except (AssertionError, ValueError) as e:
        warnings.warn("Error calculating metric for task %d: %s"
                      % (task, e))
        metric_value = np.nan
      computed_metrics.append(metric_value)
    log("computed_metrics: %s" % str(computed_metrics), self.verbosity)
    if num_tasks == 1:
    if n_tasks == 1:
      computed_metrics = computed_metrics[0]
    if not self.is_multitask:
      return computed_metrics
@@ -175,22 +175,30 @@ class Metric(object):
    Raises:
      NotImplementedError: If metric_str is not in METRICS.
    """
    y_true = np.squeeze(y_true[w != 0])
    y_pred = np.squeeze(y_pred[w != 0])
    print("compute_singletask_metric()")
    y_true = np.array(np.squeeze(y_true[w != 0]))
    y_pred = np.array(np.squeeze(y_pred[w != 0]))
    if len(y_true.shape) == 0:
      n_samples = 1
    else:
      n_samples = y_true.shape[0]
    # If there are no nonzero examples, metric is ill-defined.
    if not len(y_true):
    if not y_true.size:
      return np.nan
    print("y_pred")
    print(y_pred)
    y_true = np.reshape(y_true, (n_samples,))
    if self.mode == "classification":
      n_classes = y_pred.shape[-1]
      if self.name == "roc_auc_score":
        y_true = to_one_hot(y_true).astype(int)
        y_pred = np.reshape(y_pred, (n_samples, n_classes))
      else:
        y_true = y_true.astype(int)
        # Reshape to handle 1-d edge cases
        y_pred = np.reshape(y_pred, (n_samples, n_classes))
        y_pred = from_one_hot(y_pred)
      #y_pred = from_one_hot(y_pred[:, np.newaxis])
    print("y_pred")
    print(y_pred)
    else:
      y_pred = np.reshape(y_pred, (n_samples,))
      
    if self.threshold is not None:
      y_pred = np.greater(y_pred, threshold)
    try:
+8 −2
Original line number Diff line number Diff line
@@ -162,12 +162,18 @@ class Model(object):
    y_preds = []
    batch_size = self.model_params["batch_size"]
    n_classes = None
    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)
      if n_classes is None:
        n_classes = y_pred_batch.shape[1]
        n_classes = y_pred_batch.shape[-1]
      batch_size = len(y_batch)
      y_pred_batch = np.reshape(y_pred_batch, (batch_size, n_classes))
      print("y_pred_batch.shape")
      print(y_pred_batch.shape)
      print("batch_size, n_tasks, n_classes")
      print(batch_size, n_tasks, n_classes)
      y_pred_batch = np.squeeze(
          np.reshape(y_pred_batch, (batch_size, n_tasks, n_classes)))
      y_pred_batch = undo_transforms(y_pred_batch, transformers)
      y_preds.append(y_pred_batch)
    y_pred = np.vstack(y_preds)
+5 −11
Original line number Diff line number Diff line
@@ -257,20 +257,14 @@ class TensorflowGraph(object):
            #step, loss, _ = sess.run(
            #    [train_op.values()[0], self.loss, self.updates],
            #    feed_dict=feed_dict)
            output, step, loss, _ = sess.run(
                self.output + [train_op.values()[0], self.loss, self.updates],
            fetches = self.output + [train_op.values()[0], self.loss, self.updates]
            fetched_values = sess.run(
                fetches,
                feed_dict=feed_dict)
            #print("loss")
            #print(loss)
            output = fetched_values[:len(self.output)]
            step, loss = fetched_values[-3], fetched_values[-2]
            y_pred = np.squeeze(np.array(output))
            #print("y_pred")
            #print(y_pred)
            y_b = y_b.flatten()
            #print("y_b")
            #print(y_b)
            #print(".5*np.sum((y_b - y_pred)**2)/len(y_b)")
            #print(.5*np.sum((y_b - y_pred)**2)/len(y_b))
          # Save model checkpoints at end of epoch
          saver.save(sess, self._save_path, global_step=self.global_step)
          log('Ending epoch %d: loss %g' % (epoch, loss), self.verbosity)
        # Always save a final checkpoint when complete.
Loading