kazu.utils.sapbert¶
Classes
A dataset to be used for inferencing. |
|
Helper class to wrap useful SapBert inference functions. |
- class kazu.utils.sapbert.HFSapbertInferenceDataset[source]¶
Bases:
Dataset
[dict
[str
,Tensor
]]A dataset to be used for inferencing.
In addition to standard BERT encodings, this uses an ‘indices’ encoding that can be used to track the vector index of an embedding. This is needed in a multi GPU environment.
- __init__(encodings)[source]¶
Simple implementation of IterableDataset, producing HF tokenizer input_id.
- Parameters:
encodings (BatchEncoding) – Expected to be produced by a
transformers.PreTrainedTokenizerFast
. ‘slow’ tokenizers (transformers.PreTrainedTokenizer
) store their encodings differently and so won’t work with this class as-is.
- class kazu.utils.sapbert.SapBertHelper[source]¶
Bases:
object
Helper class to wrap useful SapBert inference functions.
Original source:
https://github.com/cambridgeltl/sapbert
Licensed under MIT
Copyright (c) Facebook, Inc. and its affiliates.
Full License
MIT License
Copyright (c) Facebook, Inc. and its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Paper:
Fangyu Liu, Ehsan Shareghi, Zaiqiao Meng, Marco Basaldella, and Nigel Collier. 2021.In Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 4228–4238.Bibtex Citation Details
@inproceedings{liu2021self, title={Self-Alignment Pretraining for Biomedical Entity Representations}, author={Liu, Fangyu and Shareghi, Ehsan and Meng, Zaiqiao and Basaldella, Marco and Collier, Nigel}, booktitle={Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies}, pages={4228--4238}, month = jun, year={2021} }
- __init__(path)[source]¶
- Parameters:
path (str) – passed to
transformers.AutoConfig
,transformers.AutoTokenizer
,transformers.AutoModel
.from_pretrained.
- get_embedding_dataloader_from_strings(texts, batch_size, num_workers, max_length=50)[source]¶
Get a dataloader with dataset
HFSapbertInferenceDataset
and DataCollatorWithPadding.This should be used to generate embeddings for strings of interest.
- Parameters:
- Returns:
- Return type:
- get_embeddings_for_strings(texts, batch_size=16)[source]¶
For a list of strings, generate embeddings.
This is a convenience function for users, as we need to carry out these steps several times in the codebase.
- get_embeddings_from_dataloader(loader)[source]¶
Get the cls token output from all data in a dataloader as a 2d tensor.
- Parameters:
loader (DataLoader[BatchEncoding])
- Returns:
2d tensor of cls output
- Return type: