docstr: Compute (for a third-order tensor)
  
  .. math::
  
      \nabla 0.5 ||\\mathcal{X} - [\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]||^2
  
  where :math:`[\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]`
  is the CP decomposition with weights
  :math:`\\mathbf{w}` and factor matrices :math:`\\mathbf{A}`, :math:`\\mathbf{B}` and :math:`\\mathbf{C}`.
  Note that this does not return the gradient
  with respect to the weights even if CP is normalized.
  
  Parameters
  ----------
  cp_tensor
      CPTensor = (weight, factors)
      factors is a list of factor matrices,
      all with the same number of columns
      i.e. for all matrix U in factor_matrices:
      U has shape ``(s_i, R)``, where R is fixed and s_i varies with i
  
  mask
      A mask to be applied to the final tensor. It should be
      broadcastable to the shape of the final tensor, that is
      ``(U[1].shape[0], ... U[-1].shape[0])``.
  
  return_loss
      Optionally return the scalar loss function along with the gradient.
  
  Returns
  -------
  cp_gradient : CPTensor = (None, factors)
      factors is a list of factor matrix gradients,
      all with the same number of columns
      i.e. for all matrix U in factor_matrices:
      U has shape ``(s_i, R)``, where R is fixed and s_i varies with i
  
  loss : float
      Scalar quantity of the loss function corresponding to cp_gradient. Only returned
      if return_loss = True.
docstr_tok: ["Compute","(","for","a","third-order","tensor",")","..","math",":",":","\\nabla","0.5","||\\\\mathcal","{","X","}","-","[","\\\\mathbf","{","w","}",";","\\\\mathbf","{","A","}",",","\\\\mathbf","{","B","}",",","\\\\mathbf","{","C","}","]","||^2","where",":","math",":","`","[","\\\\mathbf","{","w","}",";","\\\\mathbf","{","A","}",",","\\\\mathbf","{","B","}",",","\\\\mathbf","{","C","}","]","`","is","the","CP","decomposition","with","weights",":","math",":","`","\\\\mathbf","{","w","}","`","and","factor","matrices",":","math",":","`","\\\\mathbf","{","A","}","`",",",":","math",":","`","\\\\mathbf","{","B","}","`","and",":","math",":","`","\\\\mathbf","{","C","}","`",".","Note","that","this","does","not","return","the","gradient","with","respect","to","the","weights","even","if","CP","is","normalized",".","Parameters","--","--","--","--","--","cp_tensor","CPTensor","=","(","weight",",","factors",")","factors","is","a","list","of","factor","matrices",",","all","with","the","same","number","of","columns","i.e",".","for","all","matrix","U","in","factor_matrices",":","U","has","shape","``","(","s_i",",","R",")","``",",","where","R","is","fixed","and","s_i","varies","with","i","mask","A","mask","to","be","applied","to","the","final","tensor",".","It","should","be","broadcastable","to","the","shape","of","the","final","tensor",",","that","is","``","(","U","[","1","]",".shape","[","0","]",",","...","U","[","-1","]",".shape","[","0","]",")","``",".","return_loss","Optionally","return","the","scalar","loss","function","along","with","the","gradient",".","Returns","--","--","--","-","cp_gradient",":","CPTensor","=","(","None",",","factors",")","factors","is","a","list","of","factor","matrix","gradients",",","all","with","the","same","number","of","columns","i.e",".","for","all","matrix","U","in","factor_matrices",":","U","has","shape","``","(","s_i",",","R",")","``",",","where","R","is","fixed","and","s_i","varies","with","i","loss",":","float","Scalar","quantity","of","the","loss","function","corresponding","to","cp_gradient",".","Only","returned","if","return_loss","=","True","."]
code: def cp_lstsq_grad(cp_tensor, tensor, return_loss=False, mask=None):
      r"""Compute (for a third-order tensor)
  
      .. math::
  
          \nabla 0.5 ||\\mathcal{X} - [\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]||^2
  
      where :math:`[\\mathbf{w}; \\mathbf{A}, \\mathbf{B}, \\mathbf{C}]`
      is the CP decomposition with weights
      :math:`\\mathbf{w}` and factor matrices :math:`\\mathbf{A}`, :math:`\\mathbf{B}` and :math:`\\mathbf{C}`.
      Note that this does not return the gradient
      with respect to the weights even if CP is normalized.
  
      Parameters
      ----------
      cp_tensor
          CPTensor = (weight, factors)
          factors is a list of factor matrices,
          all with the same number of columns
          i.e. for all matrix U in factor_matrices:
          U has shape ``(s_i, R)``, where R is fixed and s_i varies with i
  
      mask
          A mask to be applied to the final tensor. It should be
          broadcastable to the shape of the final tensor, that is
          ``(U[1].shape[0], ... U[-1].shape[0])``.
  
      return_loss
          Optionally return the scalar loss function along with the gradient.
  
      Returns
      -------
      cp_gradient : CPTensor = (None, factors)
          factors is a list of factor matrix gradients,
          all with the same number of columns
          i.e. for all matrix U in factor_matrices:
          U has shape ``(s_i, R)``, where R is fixed and s_i varies with i
  
      loss : float
          Scalar quantity of the loss function corresponding to cp_gradient. Only returned
          if return_loss = True.
      """  # noqa: E501
      ivy.CPTensor.validate_cp_tensor(cp_tensor)
      _, factors = cp_tensor
  
      diff = tensor - ivy.CPTensor.cp_to_tensor(cp_tensor)
  
      if mask is not None:
          diff = diff * mask
  
      grad_fac = [
          -ivy.CPTensor.unfolding_dot_khatri_rao(diff, cp_tensor, ii)
          for ii in range(len(factors))
      ]
  
      if return_loss:
          return CPTensor((None, grad_fac)), 0.5 * ivy.sum(diff**2)
  
      return CPTensor((None, grad_fac))
  
imports: from .base import FactorizedTensor
  import ivy
label_desc: Use image node_id 20 for calling the CPTensor obj's underlying member method code with example usage: obj.cp_lstsq_grad(cp_tensor, tensor, return_loss, mask) and returns: CPTensor, CPTensor,  unknown