This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a TokenEmbedder for use with pytorch-transformers
- Loading branch information
1 parent
0e872a0
commit 6ec74aa
Showing
3 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
17 changes: 17 additions & 0 deletions
17
allennlp/modules/token_embedders/pretrained_transformer_embedder.py
This file contains 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,17 @@ | ||
from pytorch_transformers.modeling_auto import AutoModel | ||
import torch | ||
|
||
from allennlp.modules.token_embedders.token_embedder import TokenEmbedder | ||
|
||
|
||
@TokenEmbedder.register("pretrained_transformer") | ||
class PretrainedTransformerEmbedder(TokenEmbedder): | ||
""" | ||
Uses a pretrained model from ``pytorch-transformers`` as a ``TokenEmbedder``. | ||
""" | ||
def __init__(self, model_name: str) -> None: | ||
super().__init__() | ||
self.transformer_model = AutoModel.from_pretrained(model_name) | ||
|
||
def forward(self, token_ids: torch.LongTensor) -> torch.Tensor: | ||
return self.transformer_model(token_ids)[0] |
16 changes: 16 additions & 0 deletions
16
allennlp/tests/modules/token_embedders/pretrained_transformer_embedder_test.py
This file contains 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,16 @@ | ||
# pylint: disable=no-self-use,invalid-name | ||
import torch | ||
|
||
from allennlp.common import Params | ||
from allennlp.modules.token_embedders import PretrainedTransformerEmbedder | ||
from allennlp.common.testing import AllenNlpTestCase | ||
|
||
class TestPretrainedTransformerEmbedder(AllenNlpTestCase): | ||
def test_forward_runs_when_initialized_from_params(self): | ||
# This code just passes things off to pytorch-transformers, so we only have a very simple | ||
# test. | ||
params = Params({'model_name': 'bert-base-uncased'}) | ||
embedder = PretrainedTransformerEmbedder.from_params(params) | ||
tensor = torch.randint(0, 100, (1, 4)) | ||
output = embedder(tensor) | ||
assert tuple(output.size()) == (1, 4, 768) |