code: def init_quaternions(
      x: torch.FloatTensor,
  ) -> torch.FloatTensor:
      """
      Initialize quaternion.
  
      :param x: shape: (..., d, 4)
          the quaternions
  
      :raises ValueError:
          if the shape's last dimension is not 4.
  
      :return: shape: (..., d, 4)
          uniform quaternions
      """
      if x.ndim < 2 or x.shape[-1] != 4:
          raise ValueError(f"shape must be (..., 4) but is {x.shape}.")
      *shape, dim = x.shape[:-1]
      num_elements = math.prod(shape)
      # scaling factor
      s = 1.0 / math.sqrt(2 * num_elements)
      # modulus ~ Uniform[-s, s]
      modulus = 2 * s * torch.rand(num_elements, dim) - s
      # phase ~ Uniform[0, 2*pi]
      phase = 2 * math.pi * torch.rand(num_elements, dim)
      # real part
      real = (modulus * phase.cos()).unsqueeze(dim=-1)
      # purely imaginary quaternions unitary
      imag = torch.rand(num_elements, dim, 3)
      imag = functional.normalize(imag, p=2, dim=-1)
      imag = imag * (modulus * phase.sin()).unsqueeze(dim=-1)
      return torch.cat([real, imag], dim=-1)
  
imports: import functools
  import logging
  import math
  from typing import Any, Optional, Sequence
  import numpy
  import torch
  import torch.nn
  import torch.nn.init
  import torch_ppr.utils
  from class_resolver import FunctionResolver, Hint, HintOrType, OptionalKwargs
  from more_itertools import last
  from torch.nn import functional
  from .text import TextEncoder, text_encoder_resolver
  from .utils import iter_matrix_power, safe_diagonal
  from ..triples import CoreTriplesFactory, TriplesFactory
  from ..typing import Initializer, MappedTriples, OneOrSequence
  from ..utils import compose, get_edge_index, iter_weisfeiler_lehman, upgrade_to_sequence
label_desc: Use image node_id 4 for calling a global function with example usage: init_quaternions(x) and returns: torch