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

Updating forkMerge branch 'master' of https://github.com/deepchem/deepchem into sparse_adam

parents 43fa1a12 dd8665e9
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -41,6 +41,23 @@ class L1Loss(Loss):
    return torch.nn.L1Loss(reduction='none')


class HuberLoss(Loss):
  """Modified version of L1 Loss, also known as Smooth L1 loss.
  Less sensitive to small errors, linear for larger errors.
  Huber loss is generally better for cases where are are both large outliers as well as small, as compared to the L1 loss.
  By default, Delta = 1.0 and reduction = 'none'.
  """

  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')(output, labels)

  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."""

+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."""
+3 −0
Original line number Diff line number Diff line
@@ -184,6 +184,9 @@ Losses
.. autoclass:: deepchem.models.losses.L1Loss
  :members:

.. autoclass:: deepchem.models.losses.HuberLoss
  :members:

.. autoclass:: deepchem.models.losses.L2Loss
  :members: