code: def soft_dtw_alignment(
      ts1, ts2, gamma=1.0, be=None, compute_with_backend=False
  ):
      r"""Compute Soft-DTW metric between two time series and return both the
      similarity measure and the alignment matrix.
  
      Soft-DTW was originally presented in [1]_ and is
      discussed in more details in our
      :ref:`user-guide page on DTW and its variants<dtw>`.
  
      Soft-DTW is computed as:
  
      .. math::
  
          \text{soft-DTW}_{\gamma}(X, Y) =
              \min_{\pi}{}^\gamma \sum_{(i, j) \in \pi} \|X_i, Y_j\|^2
  
      where :math:`\min^\gamma` is the soft-min operator of parameter
      :math:`\gamma`.
  
      In the limit case :math:`\gamma = 0`, :math:`\min^\gamma` reduces to a
      hard-min operator and soft-DTW is defined as the square of the DTW
      similarity measure.
  
      Parameters
      ----------
      ts1 : array-like, shape=(sz1, d) or (sz1,)
          A time series.
          If shape is (sz1,), the time series is assumed to be univariate.
      ts2 : array-like, shape=(sz2, d) or (sz2,)
          Another time series.
          If shape is (sz2,), the time series is assumed to be univariate.
      gamma : float (default 1.)
          Gamma parameter for Soft-DTW.
      be : Backend object or string or None
          Backend. If `be` is an instance of the class `NumPyBackend` or the string `"numpy"`,
          the NumPy backend is used.
          If `be` is an instance of the class `PyTorchBackend` or the string `"pytorch"`,
          the PyTorch backend is used.
          If `be` is `None`, the backend is determined by the input arrays.
          See our :ref:`dedicated user-guide page <backend>` for more information.
      compute_with_backend : bool, default=False
          This parameter has no influence when the NumPy backend is used.
          When a backend different from NumPy is used (cf parameter `be`):
          If `True`, the computation is done with the corresponding backend.
          If `False`, a conversion to the NumPy backend can be used to accelerate the computation.
  
      Returns
      -------
      array-like, shape=(sz1, sz2)
          Soft-alignment matrix
      float
          Similarity
  
      Examples
      --------
      >>> a, dist = soft_dtw_alignment([1, 2, 2, 3],
      ...                              [1., 2., 3., 4.],
      ...                              gamma=1.)  # doctest: +ELLIPSIS
      >>> dist
      -0.89...
      >>> a  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      array([[1.00...e+00, 1.88...e-01, 2.83...e-04, 4.19...e-11],
             [3.40...e-01, 8.17...e-01, 8.87...e-02, 3.94...e-05],
             [5.05...e-02, 7.09...e-01, 5.30...e-01, 6.98...e-03],
             [1.37...e-04, 1.31...e-01, 7.30...e-01, 1.00...e+00]])
  
      The PyTorch backend can be used to compute gradients:
  
      >>> import torch
      >>> ts1 = torch.tensor([[1.0], [2.0], [3.0]], requires_grad=True)
      >>> ts2 = torch.tensor([[3.0], [4.0], [-3.0]])
      >>> path, sim = soft_dtw_alignment(ts1, ts2, gamma=1.0, be="pytorch", compute_with_backend=True)
      >>> print(sim)
      tensor(41.1876, dtype=torch.float64, grad_fn=<AsStridedBackward0>)
      >>> sim.backward()
      >>> print(ts1.grad)
      tensor([[-4.0001],
              [-2.2852],
              [10.1643]])
  
      >>> ts1_2d = torch.tensor([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]], requires_grad=True)
      >>> ts2_2d = torch.tensor([[3.0, 3.0], [4.0, 4.0], [-3.0, -3.0]])
      >>> path, sim = soft_dtw_alignment(ts1_2d, ts2_2d, gamma=1.0, be="pytorch", compute_with_backend=True)
      >>> print(sim)
      tensor(83.2951, dtype=torch.float64, grad_fn=<AsStridedBackward0>)
      >>> sim.backward()
      >>> print(ts1_2d.grad)
      tensor([[-4.0000, -4.0000],
              [-2.0261, -2.0261],
              [10.0206, 10.0206]])
  
      See Also
      --------
      soft_dtw : Returns soft-DTW score alone
  
      References
      ----------
      .. [1] M. Cuturi, M. Blondel "Soft-DTW: a Differentiable Loss Function for
         Time-Series," ICML 2017.
      """  # noqa: E501
      be = instantiate_backend(be, ts1, ts2)
      ts1 = be.array(ts1)
      ts2 = be.array(ts2)
      if gamma == 0.0:
          path, dist = dtw_path(ts1, ts2, be=be)
          dist_sq = dist**2
          a = be.zeros((ts_size(ts1), ts_size(ts2)))
          for i, j in path:
              a[i, j] = 1.0
      else:
          sdtw = SoftDTW(
              SquaredEuclidean(
                  ts1[: ts_size(ts1)], ts2[: ts_size(ts2)], be=be
              ),
              gamma=gamma,
              be=be,
              compute_with_backend=compute_with_backend,
          )
          dist_sq = sdtw.compute()
          a = sdtw.grad()
      return a, dist_sq
  
imports: import numpy
  from joblib import Parallel, delayed
  from numba import njit
  from sklearn.utils import check_random_state
  from tslearn.backend import instantiate_backend
  from tslearn.utils import check_equal_size, to_time_series, to_time_series_dataset, ts_size
  from .dtw_variants import dtw, dtw_path
  from .soft_dtw_fast import _jacobian_product_sq_euc, _njit_jacobian_product_sq_euc, _njit_soft_dtw, _njit_soft_dtw_grad, _soft_dtw, _soft_dtw_grad
  from .utils import _cdist_generic
label_desc: Use image node_id 10 for calling a global function with example usage: soft_dtw_alignment(ts1, ts2, gamma, be, compute_with_backend) and returns: a,  dist_sq