def _pre_instantiation_hook( self, kwargs: MutableMapping[str, Any] ) -> MutableMapping[str, Any]: # noqa: D102 kwargs = super()._pre_instantiation_hook(kwargs=kwargs) kwargs["base"] = Embedding( num_embeddings=2 * kwargs["max_id"], shape=self.shape, ) return kwargs
def test_score_hrt_manual(self): """Manually test interaction function of TransR.""" # entity embeddings weights = torch.as_tensor(data=[[2.0, 2.0], [3.0, 3.0]], dtype=torch.float) entity_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) entity_embeddings._embeddings.weight.data.copy_(weights) self.instance.entity_embeddings = entity_embeddings # relation embeddings relation_weights = torch.as_tensor(data=[[4.0, 4], [5.0, 5.0]], dtype=torch.float) relation_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) relation_embeddings._embeddings.weight.data.copy_(relation_weights) self.instance.relation_embeddings = relation_embeddings relation_projection_weights = torch.as_tensor( data=[[5.0, 5.0, 6.0, 6.0], [7.0, 7.0, 8.0, 8.0]], dtype=torch.float) relation_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=4, ) relation_projection_embeddings._embeddings.weight.data.copy_( relation_projection_weights) self.instance.relation_projections = relation_projection_embeddings # Compute Scores batch = torch.as_tensor(data=[[0, 0, 0], [0, 0, 1]], dtype=torch.long) scores = self.instance.score_hrt(hrt_batch=batch) self.assertEqual(scores.shape[0], 2) self.assertEqual(scores.shape[1], 1) first_score = scores[0].item() # second_score = scores[1].item() self.assertAlmostEqual(first_score, -32, delta=0.01)
def test_score_hrt_manual(self): """Manually test interaction function of TransD.""" # entity embeddings weights = torch.as_tensor(data=[[2.0, 2.0], [4.0, 4.0]], dtype=torch.float) entity_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) entity_embeddings._embeddings.weight.data.copy_(weights) projection_weights = torch.as_tensor(data=[[3.0, 3.0], [2.0, 2.0]], dtype=torch.float) entity_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) entity_projection_embeddings._embeddings.weight.data.copy_( projection_weights) self.instance.entity_representations = torch.nn.ModuleList( [entity_embeddings, entity_projection_embeddings]) # relation embeddings relation_weights = torch.as_tensor(data=[[4.0], [4.0]], dtype=torch.float) relation_embeddings = Embedding( num_embeddings=2, embedding_dim=1, ) relation_embeddings._embeddings.weight.data.copy_(relation_weights) relation_projection_weights = torch.as_tensor(data=[[5.0], [3.0]], dtype=torch.float) relation_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=1, ) relation_projection_embeddings._embeddings.weight.data.copy_( relation_projection_weights) self.instance.relation_representations = torch.nn.ModuleList( [relation_embeddings, relation_projection_embeddings]) # Compute Scores batch = torch.as_tensor(data=[[0, 0, 0], [0, 0, 1]], dtype=torch.long) scores = self.instance.score_hrt(hrt_batch=batch) self.assertEqual(scores.shape[0], 2) self.assertEqual(scores.shape[1], 1) first_score = scores[0].item() self.assertAlmostEqual(first_score, -16, delta=0.01) # Use different dimension for relation embedding: relation_dim > entity_dim # relation embeddings relation_weights = torch.as_tensor(data=[[3.0, 3.0, 3.0], [3.0, 3.0, 3.0]], dtype=torch.float) relation_embeddings = Embedding( num_embeddings=2, embedding_dim=3, ) relation_embeddings._embeddings.weight.data.copy_(relation_weights) relation_projection_weights = torch.as_tensor(data=[[4.0, 4.0, 4.0], [4.0, 4.0, 4.0]], dtype=torch.float) relation_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=3, ) relation_projection_embeddings._embeddings.weight.data.copy_( relation_projection_weights) self.instance.relation_representations = torch.nn.ModuleList( [relation_embeddings, relation_projection_embeddings]) # Compute Scores batch = torch.as_tensor(data=[[0, 0, 0]], dtype=torch.long) scores = self.instance.score_hrt(hrt_batch=batch) self.assertAlmostEqual(scores.item(), -27, delta=0.01) batch = torch.as_tensor(data=[[0, 0, 0], [0, 0, 0]], dtype=torch.long) scores = self.instance.score_hrt(hrt_batch=batch) self.assertEqual(scores.shape[0], 2) self.assertEqual(scores.shape[1], 1) first_score = scores[0].item() second_score = scores[1].item() self.assertAlmostEqual(first_score, -27, delta=0.01) self.assertAlmostEqual(second_score, -27, delta=0.01) # Use different dimension for relation embedding: relation_dim < entity_dim # entity embeddings weights = torch.as_tensor(data=[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=torch.float) entity_embeddings = Embedding( num_embeddings=2, embedding_dim=3, ) entity_embeddings._embeddings.weight.data.copy_(weights) projection_weights = torch.as_tensor(data=[[2.0, 2.0, 2.0], [2.0, 2.0, 2.0]], dtype=torch.float) entity_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=3, ) entity_projection_embeddings._embeddings.weight.data.copy_( projection_weights) self.instance.entity_representations = torch.nn.ModuleList( [entity_embeddings, entity_projection_embeddings]) # relation embeddings relation_weights = torch.as_tensor(data=[[3.0, 3.0], [3.0, 3.0]], dtype=torch.float) relation_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) relation_embeddings._embeddings.weight.data.copy_(relation_weights) relation_projection_weights = torch.as_tensor(data=[[4.0, 4.0], [4.0, 4.0]], dtype=torch.float) relation_projection_embeddings = Embedding( num_embeddings=2, embedding_dim=2, ) relation_projection_embeddings._embeddings.weight.data.copy_( relation_projection_weights) self.instance.relation_representations = torch.nn.ModuleList( [relation_embeddings, relation_projection_embeddings]) # Compute Scores batch = torch.as_tensor(data=[[0, 0, 0], [0, 0, 0]], dtype=torch.long) scores = self.instance.score_hrt(hrt_batch=batch) self.assertEqual(scores.shape[0], 2) self.assertEqual(scores.shape[1], 1) first_score = scores[0].item() second_score = scores[1].item() self.assertAlmostEqual(first_score, -18, delta=0.01) self.assertAlmostEqual(second_score, -18, delta=0.01)