code: def test_soft_dtw_loss_pytorch():
      """Tests for the class SoftDTWLossPyTorch."""
      from tslearn.metrics.soft_dtw_loss_pytorch import (
          _SoftDTWLossPyTorch,
      )
  
      b = 5
      m = 10
      n = 12
      d = 8
      batch_ts_1 = torch.zeros((b, m, d), requires_grad=True)
      batch_ts_2 = torch.ones((b, n, d), requires_grad=True)
      soft_dtw_loss_pytorch = tslearn.metrics.SoftDTWLossPyTorch(
          gamma=1.0, normalize=False, dist_func=None
      )
      loss = soft_dtw_loss_pytorch.forward(batch_ts_1, batch_ts_2)
      np.testing.assert_allclose(
          loss.detach().numpy(), 91.9806137 * torch.ones((b,))
      )
  
      loss = soft_dtw_loss_pytorch.forward(batch_ts_1, batch_ts_1)
      np.testing.assert_allclose(
          loss.detach().numpy(), -14.1957006 * torch.ones((b,))
      )
  
      soft_dtw_loss_pytorch_normalized = (
          tslearn.metrics.SoftDTWLossPyTorch(
              gamma=1.0, normalize=True, dist_func=None
          )
      )
      loss_normalized = soft_dtw_loss_pytorch_normalized.forward(
          batch_ts_1, batch_ts_1
      )
      assert torch.all(
          loss_normalized
          == torch.zeros(
              b,
          )
      )
  
      loss_normalized = soft_dtw_loss_pytorch_normalized.forward(
          batch_ts_1, batch_ts_2
      )
      np.testing.assert_allclose(
          loss_normalized.detach().numpy(),
          107.89006805 * torch.ones((b,)),
      )
  
      def euclidean_abs_dist(x, y):
          """Calculates the Euclidean squared distance between each element in x and y per timestep.
  
          Parameters
          ----------
          x : Tensor, shape=[b, m, d]
              Batch of time series.
          y : Tensor, shape=[b, n, d]
              Batch of time series.
  
          Returns
          -------
          dist : Tensor, shape=[b, m, n]
              The pairwise squared Euclidean distances.
          """
          m = x.size(1)
          n = y.size(1)
          d = x.size(2)
          x = x.unsqueeze(2).expand(-1, m, n, d)
          y = y.unsqueeze(1).expand(-1, m, n, d)
          return torch.abs(x - y).sum(3)
  
      soft_dtw_loss_pytorch = tslearn.metrics.SoftDTWLossPyTorch(
          gamma=1.0, normalize=False, dist_func=euclidean_abs_dist
      )
      loss = soft_dtw_loss_pytorch.forward(batch_ts_1, batch_ts_2)
      np.testing.assert_allclose(
          loss.detach().numpy(), 91.9806137 * torch.ones((b,))
      )
  
      def soft_dtw_loss_function(x, y, dist_func, gamma):
          d_xy = dist_func(x, y)
          return _SoftDTWLossPyTorch.apply(d_xy, gamma)
  
      loss = soft_dtw_loss_function(
          x=batch_ts_1,
          y=batch_ts_2,
          dist_func=euclidean_abs_dist,
          gamma=0.1,
      )
      loss.mean().backward()
  
      expected_grad_ts1 = np.repeat(
          np.repeat(
              np.reshape(
                  np.array(
                      [
                          -0.244445577,
                          -0.244445756,
                          -0.244445294,
                          -0.244443387,
                          -0.244441003,
                          -0.244441897,
                          -0.244444370,
                          -0.244441718,
                          -0.244441971,
                          -0.200000003,
                      ]
                  ),
                  (1, m, 1),
              ),
              repeats=b,
              axis=0,
          ),
          repeats=d,
          axis=2,
      )
      np.testing.assert_allclose(
          batch_ts_1.grad, expected_grad_ts1, rtol=5e-5
      )
  
      expected_grad_ts2 = np.repeat(
          np.repeat(
              np.reshape(
                  np.array(
                      [
                          0.2000008374,
                          0.2000008225,
                          0.2000010014,
                          0.2000009865,
                          0.1999993920,
                          0.1999970675,
                          0.1999994069,
                          0.1999980807,
                          0.1999983191,
                          0.1999950409,
                          0.2000000030,
                          0.2000000030,
                      ]
                  ),
                  (1, n, 1),
              ),
              repeats=b,
              axis=0,
          ),
          repeats=d,
          axis=2,
      )
      np.testing.assert_allclose(
          batch_ts_2.grad, expected_grad_ts2, rtol=5e-5
      )
  
imports: import platform
  import sys
  import numpy
  import pytest
  import tslearn.clustering
  import tslearn.metrics
  from scipy.spatial.distance import cdist
  from tslearn.backend.backend import Backend, cast, instantiate_backend
  from tslearn.metrics.dtw_variants import dtw_path
  from tslearn.utils import to_time_series
label_desc: Use image node_id 18 for calling a global function with example usage: test_soft_dtw_loss_pytorch() and returns: torch, _SoftDTWLossPyTorch