Commit e7d390ef authored by Atreya Majumdar's avatar Atreya Majumdar
Browse files

Added Huber loss and relevant torch+TF tests

parent 9de39778
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -40,6 +40,21 @@ class L1Loss(Loss):
    import torch
    return torch.nn.L1Loss(reduction='none')

class HuberLoss(Loss):
  """Smoothened version of L1 loss.
  Less sensitive to small errors than L1, becomes linear in error for larger errors.
  When delta = 1.0 (default), this loss is equivalent to SmoothL1Loss.
  """

  def _compute_tf_loss(self, output, labels):
    import tensorflow as tf
    output, labels = _make_tf_shapes_consistent(output, labels)
    return tf.keras.losses.Huber(reduction="none")(labels, output)
    

  def _create_pytorch_loss(self):
    import torch
    return torch.nn.SmoothL1Loss(reduction='none')

class L2Loss(Loss):
  """The squared difference between the true and predicted values."""
+37 −43
Original line number Diff line number Diff line
@@ -22,13 +22,12 @@ except:
def test_gcn_regression():
  # load datasets
  featurizer = MolGraphConvFeaturizer()
  tasks, dataset, transformers, metric = get_dataset(
      'regression', featurizer=featurizer)
  tasks, dataset, transformers, metric = get_dataset('regression',
                                                     featurizer=featurizer)

  # initialize models
  n_tasks = len(tasks)
  model = GCNModel(
      mode='regression',
  model = GCNModel(mode='regression',
                   n_tasks=n_tasks,
                   number_atom_features=30,
                   batch_size=10,
@@ -44,8 +43,7 @@ def test_gcn_regression():

  tasks, all_dataset, transformers = load_delaney(featurizer=featurizer)
  train_set, _, _ = all_dataset
  model = dc.models.GCNModel(
      n_tasks=len(tasks),
  model = dc.models.GCNModel(n_tasks=len(tasks),
                             graph_conv_layers=[2],
                             residual=False,
                             predictor_hidden_feats=2)
@@ -57,13 +55,12 @@ def test_gcn_regression():
def test_gcn_classification():
  # load datasets
  featurizer = MolGraphConvFeaturizer()
  tasks, dataset, transformers, metric = get_dataset(
      'classification', featurizer=featurizer)
  tasks, dataset, transformers, metric = get_dataset('classification',
                                                     featurizer=featurizer)

  # initialize models
  n_tasks = len(tasks)
  model = GCNModel(
      mode='classification',
  model = GCNModel(mode='classification',
                   n_tasks=n_tasks,
                   number_atom_features=30,
                   batch_size=10,
@@ -80,8 +77,7 @@ def test_gcn_classification():
  tasks, all_dataset, transformers = load_bace_classification(
      featurizer=featurizer)
  train_set, _, _ = all_dataset
  model = dc.models.GCNModel(
      mode='classification',
  model = dc.models.GCNModel(mode='classification',
                             n_tasks=len(tasks),
                             graph_conv_layers=[2],
                             residual=False,
@@ -94,14 +90,13 @@ def test_gcn_classification():
def test_gcn_reload():
  # load datasets
  featurizer = MolGraphConvFeaturizer()
  tasks, dataset, transformers, metric = get_dataset(
      'classification', featurizer=featurizer)
  tasks, dataset, transformers, metric = get_dataset('classification',
                                                     featurizer=featurizer)

  # initialize models
  n_tasks = len(tasks)
  model_dir = tempfile.mkdtemp()
  model = GCNModel(
      mode='classification',
  model = GCNModel(mode='classification',
                   n_tasks=n_tasks,
                   number_atom_features=30,
                   model_dir=model_dir,
@@ -112,8 +107,7 @@ def test_gcn_reload():
  scores = model.evaluate(dataset, [metric], transformers)
  assert scores['mean-roc_auc_score'] >= 0.85

  reloaded_model = GCNModel(
      mode='classification',
  reloaded_model = GCNModel(mode='classification',
                            n_tasks=n_tasks,
                            number_atom_features=30,
                            model_dir=model_dir,
+20 −0
Original line number Diff line number Diff line
@@ -38,6 +38,26 @@ class TestLosses(unittest.TestCase):
    expected = [[0.1, 0.2], [0.6, 0.6]]
    assert np.allclose(expected, result)

  @unittest.skipIf(not has_tensorflow, 'TensorFlow is not installed')
  def test_huber_loss_tf(self):
    """Test HuberLoss."""
    loss = losses.HuberLoss()
    outputs = tf.constant([[0.1, 0.8], [0.4, 0.6]])
    labels = tf.constant([[1.0, -1.0], [-1.0, 1.0]])
    result = np.mean(loss._compute_tf_loss(outputs, labels).numpy())
    expected = 0.67125
    assert np.allclose(expected, result)
 
  @unittest.skipIf(not has_pytorch, 'PyTorch is not installed')
  def test_huber_loss_pytorch(self):
    """Test HuberLoss."""
    loss = losses.HuberLoss()
    outputs = torch.tensor([[0.1, 0.8], [0.4, 0.6]])
    labels = torch.tensor([[1.0, -1.0], [-1.0, 1.0]])
    result = np.mean(loss._create_pytorch_loss()(outputs, labels).numpy())
    expected = 0.67125
    assert np.allclose(expected, result)

  @unittest.skipIf(not has_tensorflow, 'TensorFlow is not installed')
  def test_l2_loss_tf(self):
    """Test L2Loss."""