code: def initialize_constrained_parafac(
      tensor,
      rank,
      init="svd",
      svd="truncated_svd",
      random_state=None,
      non_negative=None,
      l1_reg=None,
      l2_reg=None,
      l2_square_reg=None,
      unimodality=None,
      normalize=None,
      simplex=None,
      normalized_sparsity=None,
      soft_sparsity=None,
      smoothness=None,
      monotonicity=None,
      hard_sparsity=None,
  ):
      r"""Initialize factors used in `constrained_parafac`.
  
      Parameters
      ----------
  
      The type of initialization is set using `init`. If `init == 'random'` then
      initialize factor matrices with uniform distribution using `random_state`. If `init == 'svd'` then
      initialize the `m`th factor matrix using the `rank` left singular vectors
      of the `m`th unfolding of the input tensor. If init is a previously initialized `cp tensor`, all
      the weights are pulled in the last factor and then the weights are set to "1" for the output tensor.
      Lastly, factors are updated with proximal operator according to the selected constraint(s), so that they satisfy the
      imposed constraints (does not apply to cptensor initialization).
  
      Parameters
      ----------
      tensor : ndarray
      rank : int
      random_state : {None, int, np.random.RandomState}
      init : {'svd', 'random', cptensor}, optional
      svd : str, default is 'truncated_svd'
          function to use to compute the SVD, acceptable values in tensorly.SVD_FUNS
      non_negative : bool or dictionary
          This constraint is clipping negative values to '0'.
          If it is True, non-negative constraint is applied to all modes.
      l1_reg : float or list or dictionary, optional
          Penalizes the factor with the l1 norm using the input value as regularization parameter.
      l2_reg : float or list or dictionary, optional
          Penalizes the factor with the l2 norm using the input value as regularization parameter.
      l2_square_reg : float or list or dictionary, optional
          Penalizes the factor with the l2 square norm using the input value as regularization parameter.
      unimodality : bool or dictionary, optional
          If it is True, unimodality constraint is applied to all modes.
          Applied to each column seperately.
      normalize : bool or dictionary, optional
          This constraint divides all the values by maximum value of the input array.
          If it is True, normalize constraint is applied to all modes.
      simplex : float or list or dictionary, optional
          Projects on the simplex with the given parameter
          Applied to each column seperately.
      normalized_sparsity : float or list or dictionary, optional
          Normalizes with the norm after hard thresholding
      soft_sparsity : float or list or dictionary, optional
          Impose that the columns of factors have L1 norm bounded by a user-defined threshold.
      smoothness : float or list or dictionary, optional
          Optimizes the factors by solving a banded system
      monotonicity : bool or dictionary, optional
          Projects columns to monotonically decreasing distrbution
          Applied to each column seperately.
          If it is True, monotonicity constraint is applied to all modes.
      hard_sparsity : float or list or dictionary, optional
          Hard thresholding with the given threshold
      Returns
      -------
      factors : CPTensor
          An initial cp tensor.
      """
      n_modes = tl.ndim(tensor)
      rng = tl.check_random_state(random_state)
  
      if init == "random":
          weights, factors = random_cp(
              tl.shape(tensor),
              rank,
              normalise_factors=False,
              **tl.context(tensor),
          )
  
      elif init == "svd":
          factors = []
          for mode in range(tl.ndim(tensor)):
              U, S, _ = svd_interface(
                  unfold(tensor, mode), n_eigenvecs=rank, method=svd
              )
  
              # Put SVD initialization on the same scaling as the tensor in case normalize_factors=False
              if mode == 0:
                  idx = min(rank, tl.shape(S)[0])
                  U = tl.index_update(
                      U, tl.index[:, :idx], U[:, :idx] * S[:idx]
                  )
  
              if tensor.shape[mode] < rank:
                  random_part = tl.tensor(
                      rng.random_sample(
                          (U.shape[0], rank - tl.shape(tensor)[mode])
                      ),
                      **tl.context(tensor),
                  )
                  U = tl.concatenate([U, random_part], axis=1)
  
              factors.append(U[:, :rank])
  
      elif isinstance(init, (tuple, list, CPTensor)):
          try:
              weights, factors = CPTensor(init)
  
              if tl.all(weights == 1):
                  weights, factors = CPTensor((None, factors))
              else:
                  weights_avg = tl.prod(weights) ** (
                      1.0 / tl.shape(weights)[0]
                  )
                  for i in range(len(factors)):
                      factors[i] = factors[i] * weights_avg
              kt = CPTensor((None, factors))
              return kt
          except ValueError:
              raise ValueError(
                  "If initialization method is a mapping, then it must "
                  "be possible to convert it to a CPTensor instance"
              )
      else:
          raise ValueError(
              f'Initialization method "{init}" not recognized'
          )
  
      for i in range(n_modes):
          factors[i] = proximal_operator(
              factors[i],
              non_negative=non_negative,
              l1_reg=l1_reg,
              l2_reg=l2_reg,
              l2_square_reg=l2_square_reg,
              unimodality=unimodality,
              normalize=normalize,
              simplex=simplex,
              normalized_sparsity=normalized_sparsity,
              soft_sparsity=soft_sparsity,
              smoothness=smoothness,
              monotonicity=monotonicity,
              hard_sparsity=hard_sparsity,
              n_const=n_modes,
              order=i,
          )
      kt = CPTensor((None, factors))
      return kt
  
file_path: tensorly/tensorly/decomposition/_constrained_cp.py
imports: import numpy
  import warnings
  import tensorly
  from ..random import random_cp
  from ._base_decomposition import DecompositionMixin
  from ..base import unfold
  from ..cp_tensor import CPTensor, cp_norm, validate_cp_rank
  from ..tenalg.proximal import admm, proximal_operator, validate_constraints
  from ..tenalg.svd import svd_interface
  from ..tenalg import unfolding_dot_khatri_rao
label_desc: Use image node_id 1 for calling a global function with example usage: initialize_constrained_parafac(tensor, rank, init, svd, random_state, non_negative, l1_reg, l2_reg, l2_square_reg, unimodality, normalize, simplex, normalized_sparsity, soft_sparsity, smoothness, monotonicity, hard_sparsity) and returns: kt, kt