-
Notifications
You must be signed in to change notification settings - Fork 132
Freeze embeddings #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nzteb
wants to merge
5
commits into
uma-pi1:master
Choose a base branch
from
Nzteb:freeze_embeddings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Freeze embeddings #136
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c180b26
Add freeze functionality; revise embed api
Nzteb ca566fe
Make freeze configurable and allow loading ids file with object ids t…
Nzteb 82b7c45
Added tests to freeze
Nzteb 5154efe
Allow resuming frozen models
Nzteb 2317dbd
Revise freeze embeddings
Nzteb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import unittest | ||
| import os | ||
| import torch | ||
| from tests.util import create_config, empty_cache, get_cache_dir | ||
| from kge.misc import kge_base_dir | ||
| from kge.model.kge_model import KgeModel | ||
| from kge.job import TrainingJob | ||
| from kge.dataset import Dataset | ||
|
|
||
|
|
||
| class TestFreeze(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.dataset_name = "toy" | ||
| self.folder = os.path.join(get_cache_dir(), "test_freeze") | ||
| self.config = create_config(self.dataset_name) | ||
| self.config.folder = self.folder | ||
| self.config.init_folder() | ||
| self.config.set("train.max_epochs", 1) | ||
| self.dataset = Dataset.create(config=self.config) | ||
|
|
||
| def tearDown(self) -> None: | ||
| empty_cache() | ||
|
|
||
| def test_freeze(self) -> None: | ||
| """Test if frozen embeddings are correctly frozen. | ||
|
|
||
| Ensure, after calling freeze() of the LookupEmbedder, embeddings are hold | ||
| constant during training. | ||
|
|
||
| """ | ||
|
|
||
| model = KgeModel.create(config=self.config, dataset=self.dataset) | ||
|
|
||
| # freeze every other entity and relation embedding | ||
| freeze_indexes_ent = list(range(0, model.dataset.num_entities(), 2)) | ||
| freeze_indexes_rel = list(range(0, model.dataset.num_relations(), 2)) | ||
|
|
||
| entity_embedder = model.get_o_embedder() | ||
| relation_embedder = model.get_p_embedder() | ||
|
|
||
| # copy before freeze | ||
| frozen_emb_rel = ( | ||
| relation_embedder.embed(torch.tensor(freeze_indexes_rel)).clone().detach() | ||
| ) | ||
|
|
||
| frozen_emb_ent = ( | ||
| entity_embedder.embed(torch.tensor(freeze_indexes_ent)).clone().detach() | ||
| ) | ||
|
|
||
| # freeze | ||
| entity_embedder.freeze(freeze_indexes_ent) | ||
| relation_embedder.freeze(freeze_indexes_rel) | ||
|
|
||
| training_job = TrainingJob.create( | ||
| config=model.config, dataset=model.dataset, model=model | ||
| ) | ||
| training_job.run() | ||
|
|
||
| frozen_emb_rel_after = relation_embedder.embed(torch.tensor(freeze_indexes_rel)) | ||
| frozen_emb_ent_after = entity_embedder.embed(torch.tensor(freeze_indexes_ent)) | ||
|
|
||
| # Ensure the frozen embeddings have not been changed | ||
| self.assertTrue( | ||
| torch.all(torch.eq(frozen_emb_ent, frozen_emb_ent_after)), | ||
| msg="Frozen parameter changed during training", | ||
| ) | ||
|
|
||
| self.assertTrue( | ||
| torch.all(torch.eq(frozen_emb_rel, frozen_emb_rel_after)), | ||
| msg="Frozen parameter changed during training", | ||
| ) | ||
|
|
||
| def test_scores_after_freeze(self) -> None: | ||
| """Test if score calculation is correct after calling freeze() on Embeddings.""" | ||
|
|
||
| model = KgeModel.create(config=self.config, dataset=self.dataset) | ||
|
|
||
| # freeze every other entity and relation embedding | ||
| freeze_indexes_ent = list(range(0, model.dataset.num_entities(), 2)) | ||
| freeze_indexes_rel = list(range(0, model.dataset.num_relations(), 2)) | ||
|
|
||
| entity_embedder = model.get_o_embedder() | ||
| relation_embedder = model.get_p_embedder() | ||
|
|
||
| triples = self.dataset.split("train") | ||
| scores_before = model.score_spo(triples[:, 0], triples[:, 1], triples[:, 2]) | ||
|
|
||
| entity_embedder.freeze(freeze_indexes_ent) | ||
| relation_embedder.freeze(freeze_indexes_rel) | ||
|
|
||
| scores_after = model.score_spo(triples[:, 0], triples[:, 1], triples[:, 2]) | ||
|
|
||
| self.assertTrue( | ||
| torch.all(torch.eq(scores_before, scores_after)), | ||
| msg="Model score computation has changed after calling freeze." | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.