-
Notifications
You must be signed in to change notification settings - Fork 16
Added support for masked language modeling (bidirectional models) #211
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
shehadak
wants to merge
8
commits into
main
Choose a base branch
from
ks/bidirectional-huggingface
base: main
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3116e1b
Added support for bidirectional LM models via masked processing
shehadak f0f2c68
Added base BERT huggingface subject
shehadak 3e7c3f2
Added unit tests for tasks with the bidriectional model
shehadak d375b11
Update brainscore_language/model_helpers/huggingface.py
shehadak 73fdcec
Refactored common functionality in _masked_inference and _causal_infe…
shehadak 66311de
Added unit tests for bidirectional huggingface models
shehadak 2a1d9ed
Update brainscore_language/model_helpers/huggingface.py
shehadak 08433e3
added comment explaining BERT layer assignment
shehadak 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from brainscore_language import model_registry | ||
| from brainscore_language import ArtificialSubject | ||
| from brainscore_language.model_helpers.huggingface import HuggingfaceSubject | ||
|
|
||
| # layer assignment was determined by scoring each transformer layer against three neural | ||
| # benchmarks: Pereira2018.243sentences-linear, Pereira2018.384sentences-linear, and | ||
| # Blank2014-linear, and choosing the layer with the highest average score. | ||
|
|
||
| # BERT | ||
| model_registry['bert-base-uncased'] = lambda: HuggingfaceSubject(model_id='bert-base-uncased', region_layer_mapping={ | ||
| ArtificialSubject.RecordingTarget.language_system: 'bert.encoder.layer.4'}, bidirectional=True) | ||
mschrimpf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,45 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from brainscore_language import load_model | ||
| from brainscore_language.artificial_subject import ArtificialSubject | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('model_identifier, expected_reading_times', [ | ||
| ('bert-base-uncased', [np.nan, 15.068062, 13.729589, 16.449226, | ||
| 18.178684, 18.060932, 17.804218, 26.74436]), | ||
| ]) | ||
| def test_reading_times(model_identifier, expected_reading_times): | ||
| model = load_model(model_identifier) | ||
| text = ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy'] | ||
| model.start_behavioral_task(task=ArtificialSubject.Task.reading_times) | ||
| reading_times = model.digest_text(text)['behavior'] | ||
| np.testing.assert_allclose( | ||
| reading_times, | ||
| expected_reading_times, | ||
| atol=0.0001) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('model_identifier, expected_next_words', [ | ||
| ('bert-base-uncased', ['eyes', 'into', 'fallen', 'again']), | ||
| ]) | ||
| def test_next_word(model_identifier, expected_next_words): | ||
| model = load_model(model_identifier) | ||
| text = ['The quick brown', 'fox jumps', 'over the', 'lazy dog'] | ||
| model.start_behavioral_task(task=ArtificialSubject.Task.next_word) | ||
| next_word_predictions = model.digest_text(text)['behavior'] | ||
| np.testing.assert_array_equal(next_word_predictions, expected_next_words) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('model_identifier, feature_size', [ | ||
| ('bert-base-uncased', 768), | ||
| ]) | ||
| def test_neural(model_identifier, feature_size): | ||
| model = load_model(model_identifier) | ||
| text = ['the quick brown fox', 'jumps over', 'the lazy dog'] | ||
| model.start_neural_recording(recording_target=ArtificialSubject.RecordingTarget.language_system, | ||
| recording_type=ArtificialSubject.RecordingType.fMRI) | ||
| representations = model.digest_text(text)['neural'] | ||
| assert len(representations['presentation']) == 3 | ||
| np.testing.assert_array_equal(representations['stimulus'], text) | ||
| assert len(representations['neuroid']) == feature_size |
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.