docstr: Compute gradient of soft-DTW w.r.t. D by dynamic programming.
  
  Returns
  -------
  grad: array-like, shape=(m, n)
      Gradient w.r.t. D.
docstr_tok: ["Compute","gradient","of","soft-DTW","w.r.t",".","D","by","dynamic","programming",".","Returns","--","--","--","-","grad",":","array-like",",","shape=","(","m",",","n",")","Gradient","w.r.t",".","D","."]
code: def grad(self):
      """Compute gradient of soft-DTW w.r.t. D by dynamic programming.
  
      Returns
      -------
      grad: array-like, shape=(m, n)
          Gradient w.r.t. D.
      """
      if not self.computed:
          raise ValueError("Needs to call compute() first.")
  
      m, n = self.be.shape(self.D)
  
      # Add an extra row and an extra column to D.
      # Needed to deal with edge cases in the recursion.
      D = self.be.vstack((self.D, self.be.zeros(n)))
      D = self.be.hstack((D, self.be.zeros((m + 1, 1))))
  
      # Allocate memory.
      # We need +2 because we use indices starting from 1
      # and to deal with edge cases in the recursion.
      E = self.be.zeros((m + 2, n + 2), dtype=self.be.float64)
  
      if self.be.is_numpy:
          _njit_soft_dtw_grad(D, self.R_, E, gamma=self.gamma)
      elif not self.compute_with_backend:
          _njit_soft_dtw_grad(
              self.be.to_numpy(D),
              self.be.to_numpy(self.R_),
              self.be.to_numpy(E),
              gamma=self.be.to_numpy(self.gamma),
          )
          self.R_ = self.be.array(self.R_)
      else:
          _soft_dtw_grad(D, self.R_, E, gamma=self.gamma, be=self.be)
  
      return E[1:-1, 1:-1]
  
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 3 for calling the SoftDTW obj's underlying member method code with example usage: obj.grad() and returns: E