Commit 221be57b authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Some more predict fixes

parent e5a1fe3e
Loading
Loading
Loading
Loading
+31 −14
Original line number Diff line number Diff line
@@ -54,16 +54,32 @@ class Model(object):
    raise NotImplementedError(
        "Each model is responsible for its own fit_on_batch method.")

  def predict_on_batch(self, X):
  def predict_on_batch(self, X, pad_batch=False):
    """
    Makes predictions on given batch of new data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Sklearn Model. Only used for Tensorflow models
      with rigid batch-size requirements.
    """
    raise NotImplementedError(
        "Each model is responsible for its own predict_on_batch method.")

  def predict_proba_on_batch(self, X):
  def predict_proba_on_batch(self, X, pad_batch=False):
    """
    Makes predictions of class probabilities on given batch of new data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Sklearn Model. Only used for Tensorflow models
      with rigid batch-size requirements.
    """
    raise NotImplementedError(
        "Each model is responsible for its own predict_on_batch method.")
@@ -140,15 +156,13 @@ class Model(object):
    y_preds = []
    n_tasks = self.get_num_tasks()
    for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(
        batch_size, deterministic=True, pad_batches=pad_batches):
        batch_size, deterministic=True):
      n_samples = len(X_batch)
      y_pred_batch = self.predict_on_batch(X_batch)
      ################################################################### DEBUG
      #print("X_batch.shape, y_batch.shape")
      #print(X_batch.shape, y_batch.shape)
      #print("y_pred_batch.shape")
      #print(y_pred_batch.shape)
      ################################################################### DEBUG
      ########################################################## DEBUG
      y_pred_batch = self.predict_on_batch(X_batch, pad_batch=pad_batches)
      # Discard any padded predictions
      y_pred_batch = y_pred_batch[:n_samples]
      ########################################################## DEBUG
      y_pred_batch = np.reshape(y_pred_batch, (n_samples, n_tasks))
      y_pred_batch = undo_transforms(y_pred_batch, transformers)
      y_preds.append(y_pred_batch)
@@ -358,10 +372,13 @@ class Model(object):
    y_preds = []
    n_tasks = self.get_num_tasks()
    for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(
        batch_size, deterministic=True, pad_batches=pad_batches):
      y_pred_batch = self.predict_proba_on_batch(X_batch)
      batch_size = len(y_batch)
      y_pred_batch = np.reshape(y_pred_batch, (batch_size, n_tasks, n_classes))
        batch_size, deterministic=True):
      ################################################################# DEBUG
      n_samples = len(X_batch)
      y_pred_batch = self.predict_proba_on_batch(X_batch, pad_batch=pad_batches)
      y_pred_batch = y_pred_batch[:n_samples]
      ################################################################# DEBUG
      y_pred_batch = np.reshape(y_pred_batch, (n_samples, 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)
+26 −2
Original line number Diff line number Diff line
@@ -55,7 +55,18 @@ class KerasModel(Model):
    model.load_weights(h5_filename)
    self.model_instance = model

  def predict_on_batch(self, X):
  def predict_on_batch(self, X, pad_batch=False):
    """
    Makes predictions on given batch of new data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Keras Models. Only used for Tensorflow models
      with rigid batch-size requirements.
    """
    return self.model_instance.predict_on_batch(X)

  # TODO(rbharath): The methods below aren't extensible and depend on
@@ -67,5 +78,18 @@ class KerasModel(Model):
  def get_num_tasks(self):
    return self.model_instance.n_tasks
  
  def predict_proba_on_batch(self, X, n_classes=2):
  def predict_proba_on_batch(self, X, pad_batch=False, n_classes=2):
    """
    Makes predictions of class probabilities on given batch of new data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Sklearn Model. Only used for Tensorflow models
      with rigid batch-size requirements.
    n_classes: int
      Number of classifier classes
    """
    return self.model_instance.predict_proba_on_batch(X, n_classes)
+18 −2
Original line number Diff line number Diff line
@@ -33,15 +33,31 @@ class SklearnModel(Model):
      self.model_instance.fit(X, y)
    y_pred_raw = self.model_instance.predict(X)

  def predict_on_batch(self, X):
  def predict_on_batch(self, X, pad_batch=False):
    """
    Makes predictions on batch of data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Sklearn Model. Only used for Tensorflow models
      with rigid batch-size requirements.
    """
    return self.model_instance.predict(X)

  def predict_proba_on_batch(self, X):
  def predict_proba_on_batch(self, X, pad_batch=False):
    """
    Makes per-class predictions on batch of data.

    Parameters
    ----------
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Sklearn Model. Only used for Tensorflow models
      with rigid batch-size requirements.
    """
    return self.model_instance.predict_proba(X)

+6 −3
Original line number Diff line number Diff line
@@ -277,7 +277,7 @@ class TensorflowGraphModel(object):
          self.verbosity)
    ############################################################## TIMING

  def predict_on_batch(self, X):
  def predict_on_batch(self, X, pad_batch=False):
    """Return model output for the provided input.

    Restore(checkpoint) must have previously been called on this object.
@@ -297,6 +297,8 @@ class TensorflowGraphModel(object):
      AssertionError: If model is not in evaluation mode.
      ValueError: If output and labels are not both 3D or both 2D.
    """
    if pad_batch:
      X = pad_features(self.batch_size, X)
    
    if not self._restored_model:
      self.restore()
@@ -514,7 +516,7 @@ class TensorflowClassifier(TensorflowGraphModel):
                             name='labels_%d' % task)))
      return labels

  def predict_proba_on_batch(self, X):
  def predict_proba_on_batch(self, X, pad_batch=False):
    """Return model output for the provided input.

    Restore(checkpoint) must have previously been called on this object.
@@ -532,10 +534,11 @@ class TensorflowClassifier(TensorflowGraphModel):
      AssertionError: If model is not in evaluation mode.
      ValueError: If output and labels are not both 3D or both 2D.
    """
    if pad_batch:
      X = pad_features(self.batch_size, X)
    if not self._restored_model:
      self.restore()
    with self.eval_graph.graph.as_default():

      # run eval data through the model
      n_tasks = self.n_tasks
      outputs = []