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

Remove doubled softmax and handle keras edge case.

parent c80bd43e
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -373,11 +373,19 @@ class Model(object):
    n_tasks = self.get_num_tasks()
    for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(
        batch_size, deterministic=True):
      ################################################################# DEBUG
      n_samples = len(X_batch)
      y_pred_batch = self.predict_proba_on_batch(X_batch, pad_batch=pad_batches)
      ################################################################# DEBUG
      print("X_batch.shape")
      print(X_batch.shape)
      print("y_pred_batch.shape")
      print(y_pred_batch.shape)
      ################################################################# DEBUG
      y_pred_batch = y_pred_batch[:n_samples]
      ################################################################# DEBUG
      print("y_pred_batch.shape, (n_samples, n_tasks, n_classes)")
      print(y_pred_batch.shape, (n_samples, n_tasks, n_classes))
      ################################################################# 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)
+17 −4
Original line number Diff line number Diff line
@@ -64,10 +64,15 @@ class KerasModel(Model):
    X: np.ndarray
      Features
    pad_batch: bool, optional
      Ignored for Keras Models. Only used for Tensorflow models
      with rigid batch-size requirements.
      Used for Tensorflow models with rigid batch-size requirements.
    """
    return self.model_instance.predict_on_batch(X)
    n_samples = len(X) 
    n_tasks = self.get_num_tasks()
    if pad_batch:
      X = pad_features(self.batch_size, X)
    y_pred = self.model_instance.predict_on_batch(X)
    y_pred = np.reshape(y_pred, (n_samples, n_tasks))
    return y_pred

  # TODO(rbharath): The methods below aren't extensible and depend on
  # implementation details of fcnet. Better way to expose this information?
@@ -92,4 +97,12 @@ class KerasModel(Model):
    n_classes: int
      Number of classifier classes
    """
    return self.model_instance.predict_proba_on_batch(X, n_classes)
    n_samples = len(X) 
    n_tasks = self.get_num_tasks()
    
    if pad_batch:
      X = pad_features(self.batch_size, X)
    y_pred_proba = self.model_instance.predict_proba_on_batch(X,
        n_classes)
    y_pred_proba = np.reshape(y_pred_proba, (n_samples, n_tasks, n_classes))
    return y_pred_proba
+9 −7
Original line number Diff line number Diff line
@@ -541,7 +541,6 @@ class TensorflowClassifier(TensorflowGraphModel):
    with self.eval_graph.graph.as_default():
      # run eval data through the model
      n_tasks = self.n_tasks
      outputs = []
      with self._get_shared_session(train=False).as_default():
        feed_dict = self.construct_feed_dict(X)
        data = self._get_shared_session(train=False).run(
@@ -556,13 +555,16 @@ class TensorflowClassifier(TensorflowGraphModel):
          raise ValueError(
              'Unrecognized rank combination for output: %s ' %
              (batch_outputs.shape,))
        outputs.append(batch_outputs)

      # TODO(rbharath): This is a bug! We're actually applying softmax twice.
      # I believe this is harmless since softmax of softmax doesn't change
      # properties, but I need to check this...
      # We apply softmax to predictions to get class probabilities.
        outputs = softmax(np.squeeze(np.hstack(outputs)))
      #outputs = softmax(np.squeeze(batch_outputs))
      #outputs = softmax(batch_outputs)

      # Note that softmax is already applied in construct_grpah
      outputs = batch_outputs

    return np.copy(outputs)