code: def test_tucker(monkeypatch):
      """Test for the Tucker decomposition"""
      rng = tl.check_random_state(1234)
  
      tol_norm_2 = 10e-3
      tol_max_abs = 10e-1
      tensor = tl.tensor(rng.random_sample((3, 4, 3)))
      core, factors = tucker(
          tensor, rank=None, n_iter_max=200, verbose=True
      )
      reconstructed_tensor = tucker_to_tensor((core, factors))
      norm_rec = tl.norm(reconstructed_tensor, 2)
      norm_tensor = tl.norm(tensor, 2)
      assert (norm_rec - norm_tensor) / norm_rec < tol_norm_2
  
      # Test the max abs difference between the reconstruction and the tensor
      assert tl.max(tl.abs(reconstructed_tensor - tensor)) < tol_max_abs
  
      # Test the shape of the core and factors
      ranks = [2, 3, 1]
      core, factors = tucker(
          tensor, rank=ranks, n_iter_max=100, verbose=1
      )
      for i, rank in enumerate(ranks):
          assert_equal(
              factors[i].shape,
              (tensor.shape[i], ranks[i]),
              err_msg=f"factors[i].shape = {factors[i].shape}, expected {(tensor.shape[i], ranks[i])}",
          )
          assert_equal(
              tl.shape(core)[i],
              rank,
              err_msg=f"core.shape[i] = {core.shape[i]}, expected {rank}",
          )
  
      # try fixing the core
      factors_init = [tl.copy(f) for f in factors]
      _, factors = tucker(
          tensor,
          rank=ranks,
          init=(core, factors),
          fixed_factors=[1],
          n_iter_max=100,
          verbose=1,
      )
      assert_array_equal(factors[1], factors_init[1])
  
      # Random and SVD init should converge to a similar solution
      tol_norm_2 = 10e-1
      tol_max_abs = 10e-1
  
      core_svd, factors_svd = tucker(
          tensor, rank=[3, 4, 3], n_iter_max=200, init="svd", verbose=1
      )
      core_random, factors_random = tucker(
          tensor,
          rank=[3, 4, 3],
          n_iter_max=200,
          init="random",
          random_state=1234,
      )
      rec_svd = tucker_to_tensor((core_svd, factors_svd))
      rec_random = tucker_to_tensor((core_random, factors_random))
      error = tl.norm(rec_svd - rec_random, 2)
      error /= tl.norm(rec_svd, 2)
      assert_(
          error < tol_norm_2,
          "norm 2 of difference between svd and random init too high",
      )
      assert_(
          tl.max(tl.abs(rec_svd - rec_random)) < tol_max_abs,
          "abs norm of difference between svd and random init too high",
      )
      assert_class_wrapper_correctly_passes_arguments(
          monkeypatch, tucker, Tucker, ignore_args={}, rank=3
      )
  
file_path: tensorly/tensorly/decomposition/tests/test_tucker.py
imports: import pytest
  import numpy
  import tensorly
  from .._tucker import tucker, partial_tucker, non_negative_tucker, non_negative_tucker_hals, Tucker, Tucker_NN, Tucker_NN_HALS
  from ...tucker_tensor import tucker_to_tensor
  from ...tenalg import multi_mode_dot
  from ...random import random_tucker
  from ...testing import assert_equal, assert_, assert_array_equal, assert_class_wrapper_correctly_passes_arguments
label_desc: Use image node_id 2 for calling a global function with example usage: test_tucker(monkeypatch) without return types