Skip to content

Commit

Permalink
⚖️🗺️ Fix estimate diversity function (pykeen#1242)
Browse files Browse the repository at this point in the history
Closes pykeen#1241

Fixed a bug in NodePieceRepresentation's estimate_diversity function

---------

Co-authored-by: Max Berrendorf <max.berrendorf@gmail.com>
Co-authored-by: Charles Tapley Hoyt <cthoyt@gmail.com>
  • Loading branch information
3 people authored Mar 23, 2023
1 parent b253678 commit b3ef9d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/pykeen/nn/node_piece/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,11 @@ def estimate_diversity(self) -> HashDiversityInfo:
.. seealso:: https://github.com/pykeen/pykeen/pull/896
"""
# unique hashes per representation
uniques_per_representation = [
tokens.assignment.unique(dim=0).shape[0] / self.max_id for tokens in self.token_representations
]
uniques_per_representation = [tokens.assignment.unique(dim=0).shape[0] / self.max_id for tokens in self.base]

# unique hashes if we concatenate all representations together
unnormalized_uniques_total = torch.unique(
torch.cat([tokens.assignment for tokens in self.token_representations], dim=-1), dim=0
torch.cat([tokens.assignment for tokens in self.base], dim=-1), dim=0
).shape[0]
return HashDiversityInfo(
uniques_per_representation=uniques_per_representation,
Expand Down
6 changes: 6 additions & 0 deletions tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,12 @@ def _pre_instantiation_hook(self, kwargs: MutableMapping[str, Any]) -> MutableMa
kwargs.pop("max_id")
return kwargs

def test_estimate_diversity(self):
"""Test estimating diversity."""
diversity = self.instance.estimate_diversity()
assert len(diversity.uniques_per_representation) == len(self.instance.base)
assert 0.0 <= diversity.uniques_total <= 1.0


class EvaluationLoopTestCase(GenericTestCase[pykeen.evaluation.evaluation_loop.EvaluationLoop]):
"""Tests for evaluation loops."""
Expand Down

0 comments on commit b3ef9d1

Please sign in to comment.