class_docstr: Tests for :mod:`pykeen.utils`.
class_docstr_tok: ["Tests","for",":","mod",":","`","pykeen.utils","`","."]
docstr: Test _project_entity.
docstr_tok: ["Test","_project_entity","."]
code: def test_project_entity(self):
      """Test _project_entity."""
      batch_size = 2
      embedding_dim = 3
      relation_dim = 5
      num_entities = 7
  
      # random entity embeddings & projections
      e = torch.rand(1, num_entities, embedding_dim)
      e = clamp_norm(e, maxnorm=1, p=2, dim=-1)
      e_p = torch.rand(1, num_entities, embedding_dim)
  
      # random relation embeddings & projections
      r_p = torch.rand(batch_size, 1, relation_dim)
  
      # project
      e_bot = project_entity(e=e, e_p=e_p, r_p=r_p)
  
      # check shape:
      assert e_bot.shape == (batch_size, num_entities, relation_dim)
  
      # check normalization
      assert (torch.norm(e_bot, dim=-1, p=2) <= 1.0 + 1.0e-06).all()
  
      # check equivalence of re-formulation
      # e_{\bot} = M_{re} e = (r_p e_p^T + I^{d_r \times d_e}) e
      #                     = r_p (e_p^T e) + e'
      m_re = r_p.unsqueeze(dim=-1) @ e_p.unsqueeze(dim=-2)
      m_re = m_re + torch.eye(relation_dim, embedding_dim).view(
          1, 1, relation_dim, embedding_dim
      )
      assert m_re.shape == (
          batch_size,
          num_entities,
          relation_dim,
          embedding_dim,
      )
      e_vanilla = (m_re @ e.unsqueeze(dim=-1)).squeeze(dim=-1)
      e_vanilla = clamp_norm(e_vanilla, p=2, dim=-1, maxnorm=1)
      assert torch.allclose(e_vanilla, e_bot)
  
imports: import functools
  import itertools
  import operator
  import random
  import string
  import timeit
  import unittest
  from typing import Iterable, Tuple
  import numpy
  import pytest
  import torch
  from pykeen.utils import _weisfeiler_lehman_iteration, _weisfeiler_lehman_iteration_approx, calculate_broadcasted_elementwise_result_shape, clamp_norm, combine_complex, compact_mapping, compose, estimate_cost_of_sequence, flatten_dictionary, get_optimal_sequence, get_until_first_blank, iter_weisfeiler_lehman, logcumsumexp, project_entity, set_random_seed, split_complex, tensor_product, tensor_sum
label_desc: Use image node_id 4 for calling the TestUtils obj's underlying member method code with example usage: obj.test_project_entity() without return types