code: def initialize_cp(
      tensor,
      rank,
      init="svd",
      svd="truncated_svd",
      non_negative=False,
      random_state=None,
      normalize_factors=False,
      mask=None,
      svd_mask_repeats=5,
  ):
      r"""Initialize factors used in `parafac`.
  
      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.
  
      Parameters
      ----------
      tensor : ndarray
      rank : int
      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, default is False
          if True, non-negative factors are returned
  
      Returns
      -------
      factors : CPTensor
          An initial cp tensor.
  
      """
      rng = tl.check_random_state(random_state)
  
      if init == "random":
          kt = random_cp(
              tl.shape(tensor),
              rank,
              normalise_factors=False,
              random_state=rng,
              **tl.context(tensor),
          )
  
      elif init == "svd":
          factors = []
          for mode in range(tl.ndim(tensor)):
              mask_unfold = None if mask is None else unfold(mask, mode)
              U, S, _ = svd_interface(
                  unfold(tensor, mode),
                  n_eigenvecs=rank,
                  method=svd,
                  non_negative=non_negative,
                  mask=mask_unfold,
                  n_iter_mask_imputation=svd_mask_repeats,
              )
  
              # 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:
                  # TODO: this is a hack but it seems to do the job for now
                  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])
  
          kt = CPTensor((None, factors))
  
      elif isinstance(init, (tuple, list, CPTensor)):
          # TODO: Test this
          try:
              if normalize_factors is True:
                  warnings.warn(
                      "It is not recommended to initialize a tensor with normalizing. Consider normalizing the tensor before using this function"
                  )
  
              kt = CPTensor(init)
              weights, factors = kt
  
              if tl.all(weights == 1):
                  kt = 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'
          )
  
      if non_negative:
          # Make decomposition feasible by taking the absolute value of all factor matrices
          kt.factors = [tl.abs(f) for f in kt[1]]
  
      if normalize_factors:
          kt = cp_normalize(kt)
  
      return kt
  
imports: import numpy
  import warnings
  import tensorly
  from ._base_decomposition import DecompositionMixin
  from ..random import random_cp
  from ..base import unfold
  from ..cp_tensor import cp_to_tensor, CPTensor, cp_norm, cp_normalize, validate_cp_rank
  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_cp(tensor, rank, init, svd, non_negative, random_state, normalize_factors, mask, svd_mask_repeats) and returns: kt, kt