Commit b829107f authored by VIGNESHinZONE's avatar VIGNESHinZONE
Browse files

Merge branch 'master' into pagtn

parents 6d5adc89 6ab2a69f
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -94,6 +94,31 @@ class HingeLoss(Loss):
    return loss


class SquaredHingeLoss(Loss):
  """The Squared Hinge loss function.
  
  Defined as the square of the hinge loss between y_true and y_pred. The Squared Hinge Loss is differentiable.
  """

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

  def _create_pytorch_loss(self):
    import torch

    def loss(output, labels):
      output, labels = _make_pytorch_shapes_consistent(output, labels)
      return torch.mean(
          torch.pow(
              torch.maximum(1 - torch.multiply(labels, output),
                            torch.tensor(0)), 2),
          dim=-1)

    return loss


class PoissonLoss(Loss):
  """The Poisson loss function is defined as the mean of the elements of y_pred - (y_true * log(y_pred) for an input of (y_true, y_pred).
  Poisson loss is generally used for regression tasks where the data follows the poisson
+19 −0
Original line number Diff line number Diff line
@@ -99,6 +99,25 @@ class TestLosses(unittest.TestCase):
    assert np.allclose(expected, result)

  @unittest.skipIf(not has_tensorflow, 'TensorFlow is not installed')
  def test_squared_hinge_loss_tf(self):
    """Test SquaredHingeLoss."""
    loss = losses.SquaredHingeLoss()
    outputs = tf.constant([[0.1, 0.8], [0.4, 0.6]])
    labels = tf.constant([[1.0, -1.0], [-1.0, 1.0]])
    result = loss._compute_tf_loss(outputs, labels).numpy()
    expected = [np.mean([0.8100, 3.2400]), np.mean([1.9600, 0.1600])]
    assert np.allclose(expected, result)

  @unittest.skipIf(not has_pytorch, 'PyTorch is not installed')
  def test_squared_hinge_loss_pytorch(self):
    """Test SquaredHingeLoss."""
    loss = losses.SquaredHingeLoss()
    outputs = torch.tensor([[0.1, 0.8], [0.4, 0.6]])
    labels = torch.tensor([[1.0, -1.0], [-1.0, 1.0]])
    result = loss._create_pytorch_loss()(outputs, labels).numpy()
    expected = [np.mean([0.8100, 3.2400]), np.mean([1.9600, 0.1600])]
    assert np.allclose(expected, result)

  def test_poisson_loss_tf(self):
    """Test PoissonLoss."""
    loss = losses.PoissonLoss()
+3 −0
Original line number Diff line number Diff line
@@ -196,6 +196,9 @@ Losses
.. autoclass:: deepchem.models.losses.HingeLoss
  :members:

.. autoclass:: deepchem.models.losses.SquaredHingeLoss
  :members:
  
.. autoclass:: deepchem.models.losses.PoissonLoss
  :members:

+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ dependencies:
  - pip==20.2.*
  - pip:
    - biopython
    - dgllife==0.2.*
    - dgllife==0.2.8
    - lightgbm==3.*
    - matminer
    - mordred
+1 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ dependencies:
  - pip:
    - -f https://pytorch-geometric.com/whl/torch-1.6.0.html
    - -f https://pytorch-geometric.com/whl/torch-1.6.0+cpu.html
    - dgl==0.5.*
    - dgl==0.6.*
    - torch==1.6.0
    - torchvision==0.7.0
    - torch-cluster
Loading