Enable doc in Spanish (#16518)
* Reorganize doc for multilingual support * Fix style * Style * Toc trees * Adapt templates
This commit is contained in:
170
docs/source/en/model_doc/albert.mdx
Normal file
170
docs/source/en/model_doc/albert.mdx
Normal file
@@ -0,0 +1,170 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ALBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The ALBERT model was proposed in [ALBERT: A Lite BERT for Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942) by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma,
|
||||
Radu Soricut. It presents two parameter-reduction techniques to lower memory consumption and increase the training
|
||||
speed of BERT:
|
||||
|
||||
- Splitting the embedding matrix into two smaller matrices.
|
||||
- Using repeating layers split among groups.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Increasing model size when pretraining natural language representations often results in improved performance on
|
||||
downstream tasks. However, at some point further model increases become harder due to GPU/TPU memory limitations,
|
||||
longer training times, and unexpected model degradation. To address these problems, we present two parameter-reduction
|
||||
techniques to lower memory consumption and increase the training speed of BERT. Comprehensive empirical evidence shows
|
||||
that our proposed methods lead to models that scale much better compared to the original BERT. We also use a
|
||||
self-supervised loss that focuses on modeling inter-sentence coherence, and show it consistently helps downstream tasks
|
||||
with multi-sentence inputs. As a result, our best model establishes new state-of-the-art results on the GLUE, RACE, and
|
||||
SQuAD benchmarks while having fewer parameters compared to BERT-large.*
|
||||
|
||||
Tips:
|
||||
|
||||
- ALBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather
|
||||
than the left.
|
||||
- ALBERT uses repeating layers which results in a small memory footprint, however the computational cost remains
|
||||
similar to a BERT-like architecture with the same number of hidden layers as it has to iterate through the same
|
||||
number of (repeating) layers.
|
||||
|
||||
This model was contributed by [lysandre](https://huggingface.co/lysandre). This model jax version was contributed by
|
||||
[kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/google-research/ALBERT).
|
||||
|
||||
## AlbertConfig
|
||||
|
||||
[[autodoc]] AlbertConfig
|
||||
|
||||
## AlbertTokenizer
|
||||
|
||||
[[autodoc]] AlbertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## AlbertTokenizerFast
|
||||
|
||||
[[autodoc]] AlbertTokenizerFast
|
||||
|
||||
## Albert specific outputs
|
||||
|
||||
[[autodoc]] models.albert.modeling_albert.AlbertForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.albert.modeling_tf_albert.TFAlbertForPreTrainingOutput
|
||||
|
||||
## AlbertModel
|
||||
|
||||
[[autodoc]] AlbertModel
|
||||
- forward
|
||||
|
||||
## AlbertForPreTraining
|
||||
|
||||
[[autodoc]] AlbertForPreTraining
|
||||
- forward
|
||||
|
||||
## AlbertForMaskedLM
|
||||
|
||||
[[autodoc]] AlbertForMaskedLM
|
||||
- forward
|
||||
|
||||
## AlbertForSequenceClassification
|
||||
|
||||
[[autodoc]] AlbertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## AlbertForMultipleChoice
|
||||
|
||||
[[autodoc]] AlbertForMultipleChoice
|
||||
|
||||
## AlbertForTokenClassification
|
||||
|
||||
[[autodoc]] AlbertForTokenClassification
|
||||
- forward
|
||||
|
||||
## AlbertForQuestionAnswering
|
||||
|
||||
[[autodoc]] AlbertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFAlbertModel
|
||||
|
||||
[[autodoc]] TFAlbertModel
|
||||
- call
|
||||
|
||||
## TFAlbertForPreTraining
|
||||
|
||||
[[autodoc]] TFAlbertForPreTraining
|
||||
- call
|
||||
|
||||
## TFAlbertForMaskedLM
|
||||
|
||||
[[autodoc]] TFAlbertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFAlbertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFAlbertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFAlbertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFAlbertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFAlbertForTokenClassification
|
||||
|
||||
[[autodoc]] TFAlbertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFAlbertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFAlbertForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxAlbertModel
|
||||
|
||||
[[autodoc]] FlaxAlbertModel
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForPreTraining
|
||||
|
||||
[[autodoc]] FlaxAlbertForPreTraining
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxAlbertForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxAlbertForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxAlbertForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxAlbertForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxAlbertForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxAlbertForQuestionAnswering
|
||||
- __call__
|
||||
263
docs/source/en/model_doc/auto.mdx
Normal file
263
docs/source/en/model_doc/auto.mdx
Normal file
@@ -0,0 +1,263 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Auto Classes
|
||||
|
||||
In many cases, the architecture you want to use can be guessed from the name or the path of the pretrained model you
|
||||
are supplying to the `from_pretrained()` method. AutoClasses are here to do this job for you so that you
|
||||
automatically retrieve the relevant model given the name/path to the pretrained weights/config/vocabulary.
|
||||
|
||||
Instantiating one of [`AutoConfig`], [`AutoModel`], and
|
||||
[`AutoTokenizer`] will directly create a class of the relevant architecture. For instance
|
||||
|
||||
|
||||
```python
|
||||
model = AutoModel.from_pretrained("bert-base-cased")
|
||||
```
|
||||
|
||||
will create a model that is an instance of [`BertModel`].
|
||||
|
||||
There is one class of `AutoModel` for each task, and for each backend (PyTorch, TensorFlow, or Flax).
|
||||
|
||||
## Extending the Auto Classes
|
||||
|
||||
Each of the auto classes has a method to be extended with your custom classes. For instance, if you have defined a
|
||||
custom class of model `NewModel`, make sure you have a `NewModelConfig` then you can add those to the auto
|
||||
classes like this:
|
||||
|
||||
```python
|
||||
from transformers import AutoConfig, AutoModel
|
||||
|
||||
AutoConfig.register("new-model", NewModelConfig)
|
||||
AutoModel.register(NewModelConfig, NewModel)
|
||||
```
|
||||
|
||||
You will then be able to use the auto classes like you would usually do!
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
If your `NewModelConfig` is a subclass of [`~transformer.PretrainedConfig`], make sure its
|
||||
`model_type` attribute is set to the same key you use when registering the config (here `"new-model"`).
|
||||
|
||||
Likewise, if your `NewModel` is a subclass of [`PreTrainedModel`], make sure its
|
||||
`config_class` attribute is set to the same class you use when registering the model (here
|
||||
`NewModelConfig`).
|
||||
|
||||
</Tip>
|
||||
|
||||
## AutoConfig
|
||||
|
||||
[[autodoc]] AutoConfig
|
||||
|
||||
## AutoTokenizer
|
||||
|
||||
[[autodoc]] AutoTokenizer
|
||||
|
||||
## AutoFeatureExtractor
|
||||
|
||||
[[autodoc]] AutoFeatureExtractor
|
||||
|
||||
## AutoProcessor
|
||||
|
||||
[[autodoc]] AutoProcessor
|
||||
|
||||
## AutoModel
|
||||
|
||||
[[autodoc]] AutoModel
|
||||
|
||||
## AutoModelForPreTraining
|
||||
|
||||
[[autodoc]] AutoModelForPreTraining
|
||||
|
||||
## AutoModelForCausalLM
|
||||
|
||||
[[autodoc]] AutoModelForCausalLM
|
||||
|
||||
## AutoModelForMaskedLM
|
||||
|
||||
[[autodoc]] AutoModelForMaskedLM
|
||||
|
||||
## AutoModelForSeq2SeqLM
|
||||
|
||||
[[autodoc]] AutoModelForSeq2SeqLM
|
||||
|
||||
## AutoModelForSequenceClassification
|
||||
|
||||
[[autodoc]] AutoModelForSequenceClassification
|
||||
|
||||
## AutoModelForMultipleChoice
|
||||
|
||||
[[autodoc]] AutoModelForMultipleChoice
|
||||
|
||||
## AutoModelForNextSentencePrediction
|
||||
|
||||
[[autodoc]] AutoModelForNextSentencePrediction
|
||||
|
||||
## AutoModelForTokenClassification
|
||||
|
||||
[[autodoc]] AutoModelForTokenClassification
|
||||
|
||||
## AutoModelForQuestionAnswering
|
||||
|
||||
[[autodoc]] AutoModelForQuestionAnswering
|
||||
|
||||
## AutoModelForTableQuestionAnswering
|
||||
|
||||
[[autodoc]] AutoModelForTableQuestionAnswering
|
||||
|
||||
## AutoModelForImageClassification
|
||||
|
||||
[[autodoc]] AutoModelForImageClassification
|
||||
|
||||
## AutoModelForVision2Seq
|
||||
|
||||
[[autodoc]] AutoModelForVision2Seq
|
||||
|
||||
## AutoModelForAudioClassification
|
||||
|
||||
[[autodoc]] AutoModelForAudioClassification
|
||||
|
||||
## AutoModelForAudioFrameClassification
|
||||
|
||||
[[autodoc]] AutoModelForAudioFrameClassification
|
||||
|
||||
## AutoModelForCTC
|
||||
|
||||
[[autodoc]] AutoModelForCTC
|
||||
|
||||
## AutoModelForSpeechSeq2Seq
|
||||
|
||||
[[autodoc]] AutoModelForSpeechSeq2Seq
|
||||
|
||||
## AutoModelForAudioXVector
|
||||
|
||||
[[autodoc]] AutoModelForAudioXVector
|
||||
|
||||
## AutoModelForMaskedImageModeling
|
||||
|
||||
[[autodoc]] AutoModelForMaskedImageModeling
|
||||
|
||||
## AutoModelForObjectDetection
|
||||
|
||||
[[autodoc]] AutoModelForObjectDetection
|
||||
|
||||
## AutoModelForImageSegmentation
|
||||
|
||||
[[autodoc]] AutoModelForImageSegmentation
|
||||
|
||||
## AutoModelForSemanticSegmentation
|
||||
|
||||
[[autodoc]] AutoModelForSemanticSegmentation
|
||||
|
||||
## AutoModelForInstanceSegmentation
|
||||
|
||||
[[autodoc]] AutoModelForInstanceSegmentation
|
||||
|
||||
## TFAutoModel
|
||||
|
||||
[[autodoc]] TFAutoModel
|
||||
|
||||
## TFAutoModelForPreTraining
|
||||
|
||||
[[autodoc]] TFAutoModelForPreTraining
|
||||
|
||||
## TFAutoModelForCausalLM
|
||||
|
||||
[[autodoc]] TFAutoModelForCausalLM
|
||||
|
||||
## TFAutoModelForImageClassification
|
||||
|
||||
[[autodoc]] TFAutoModelForImageClassification
|
||||
|
||||
## TFAutoModelForMaskedLM
|
||||
|
||||
[[autodoc]] TFAutoModelForMaskedLM
|
||||
|
||||
## TFAutoModelForSeq2SeqLM
|
||||
|
||||
[[autodoc]] TFAutoModelForSeq2SeqLM
|
||||
|
||||
## TFAutoModelForSequenceClassification
|
||||
|
||||
[[autodoc]] TFAutoModelForSequenceClassification
|
||||
|
||||
## TFAutoModelForMultipleChoice
|
||||
|
||||
[[autodoc]] TFAutoModelForMultipleChoice
|
||||
|
||||
## TFAutoModelForTableQuestionAnswering
|
||||
|
||||
[[autodoc]] TFAutoModelForTableQuestionAnswering
|
||||
|
||||
## TFAutoModelForTokenClassification
|
||||
|
||||
[[autodoc]] TFAutoModelForTokenClassification
|
||||
|
||||
## TFAutoModelForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFAutoModelForQuestionAnswering
|
||||
|
||||
## TFAutoModelForVision2Seq
|
||||
|
||||
[[autodoc]] TFAutoModelForVision2Seq
|
||||
|
||||
## TFAutoModelForSpeechSeq2Seq
|
||||
|
||||
[[autodoc]] TFAutoModelForSpeechSeq2Seq
|
||||
|
||||
## FlaxAutoModel
|
||||
|
||||
[[autodoc]] FlaxAutoModel
|
||||
|
||||
## FlaxAutoModelForCausalLM
|
||||
|
||||
[[autodoc]] FlaxAutoModelForCausalLM
|
||||
|
||||
## FlaxAutoModelForPreTraining
|
||||
|
||||
[[autodoc]] FlaxAutoModelForPreTraining
|
||||
|
||||
## FlaxAutoModelForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxAutoModelForMaskedLM
|
||||
|
||||
## FlaxAutoModelForSeq2SeqLM
|
||||
|
||||
[[autodoc]] FlaxAutoModelForSeq2SeqLM
|
||||
|
||||
## FlaxAutoModelForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxAutoModelForSequenceClassification
|
||||
|
||||
## FlaxAutoModelForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxAutoModelForQuestionAnswering
|
||||
|
||||
## FlaxAutoModelForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxAutoModelForTokenClassification
|
||||
|
||||
## FlaxAutoModelForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxAutoModelForMultipleChoice
|
||||
|
||||
## FlaxAutoModelForNextSentencePrediction
|
||||
|
||||
[[autodoc]] FlaxAutoModelForNextSentencePrediction
|
||||
|
||||
## FlaxAutoModelForImageClassification
|
||||
|
||||
[[autodoc]] FlaxAutoModelForImageClassification
|
||||
|
||||
## FlaxAutoModelForVision2Seq
|
||||
|
||||
[[autodoc]] FlaxAutoModelForVision2Seq
|
||||
159
docs/source/en/model_doc/bart.mdx
Normal file
159
docs/source/en/model_doc/bart.mdx
Normal file
@@ -0,0 +1,159 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BART
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
|
||||
@patrickvonplaten
|
||||
|
||||
## Overview
|
||||
|
||||
The Bart model was proposed in [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation,
|
||||
Translation, and Comprehension](https://arxiv.org/abs/1910.13461) by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan
|
||||
Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer on 29 Oct, 2019.
|
||||
|
||||
According to the abstract,
|
||||
|
||||
- Bart uses a standard seq2seq/machine translation architecture with a bidirectional encoder (like BERT) and a
|
||||
left-to-right decoder (like GPT).
|
||||
- The pretraining task involves randomly shuffling the order of the original sentences and a novel in-filling scheme,
|
||||
where spans of text are replaced with a single mask token.
|
||||
- BART is particularly effective when fine tuned for text generation but also works well for comprehension tasks. It
|
||||
matches the performance of RoBERTa with comparable training resources on GLUE and SQuAD, achieves new
|
||||
state-of-the-art results on a range of abstractive dialogue, question answering, and summarization tasks, with gains
|
||||
of up to 6 ROUGE.
|
||||
|
||||
This model was contributed by [sshleifer](https://huggingface.co/sshleifer). The Authors' code can be found [here](https://github.com/pytorch/fairseq/tree/master/examples/bart).
|
||||
|
||||
|
||||
### Examples
|
||||
|
||||
- Examples and scripts for fine-tuning BART and other models for sequence to sequence tasks can be found in
|
||||
[examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md).
|
||||
- An example of how to train [`BartForConditionalGeneration`] with a Hugging Face `datasets`
|
||||
object can be found in this [forum discussion](https://discuss.huggingface.co/t/train-bart-for-conditional-generation-e-g-summarization/1904).
|
||||
- [Distilled checkpoints](https://huggingface.co/models?search=distilbart) are described in this [paper](https://arxiv.org/abs/2010.13002).
|
||||
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- Bart doesn't use `token_type_ids` for sequence classification. Use [`BartTokenizer`] or
|
||||
[`~BartTokenizer.encode`] to get the proper splitting.
|
||||
- The forward pass of [`BartModel`] will create the `decoder_input_ids` if they are not passed.
|
||||
This is different than some other modeling APIs. A typical use case of this feature is mask filling.
|
||||
- Model predictions are intended to be identical to the original implementation when
|
||||
`forced_bos_token_id=0`. This only works, however, if the string you pass to
|
||||
[`fairseq.encode`] starts with a space.
|
||||
- [`~generation_utils.GenerationMixin.generate`] should be used for conditional generation tasks like
|
||||
summarization, see the example in that docstrings.
|
||||
- Models that load the *facebook/bart-large-cnn* weights will not have a `mask_token_id`, or be able to perform
|
||||
mask-filling tasks.
|
||||
|
||||
## Mask Filling
|
||||
|
||||
The `facebook/bart-base` and `facebook/bart-large` checkpoints can be used to fill multi-token masks.
|
||||
|
||||
```python
|
||||
from transformers import BartForConditionalGeneration, BartTokenizer
|
||||
|
||||
model = BartForConditionalGeneration.from_pretrained("facebook/bart-large", forced_bos_token_id=0)
|
||||
tok = BartTokenizer.from_pretrained("facebook/bart-large")
|
||||
example_english_phrase = "UN Chief Says There Is No <mask> in Syria"
|
||||
batch = tok(example_english_phrase, return_tensors="pt")
|
||||
generated_ids = model.generate(batch["input_ids"])
|
||||
assert tok.batch_decode(generated_ids, skip_special_tokens=True) == [
|
||||
"UN Chief Says There Is No Plan to Stop Chemical Weapons in Syria"
|
||||
]
|
||||
```
|
||||
|
||||
## BartConfig
|
||||
|
||||
[[autodoc]] BartConfig
|
||||
- all
|
||||
|
||||
## BartTokenizer
|
||||
|
||||
[[autodoc]] BartTokenizer
|
||||
- all
|
||||
|
||||
## BartTokenizerFast
|
||||
|
||||
[[autodoc]] BartTokenizerFast
|
||||
- all
|
||||
|
||||
## BartModel
|
||||
|
||||
[[autodoc]] BartModel
|
||||
- forward
|
||||
|
||||
## BartForConditionalGeneration
|
||||
|
||||
[[autodoc]] BartForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## BartForSequenceClassification
|
||||
|
||||
[[autodoc]] BartForSequenceClassification
|
||||
- forward
|
||||
|
||||
## BartForQuestionAnswering
|
||||
|
||||
[[autodoc]] BartForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## BartForCausalLM
|
||||
|
||||
[[autodoc]] BartForCausalLM
|
||||
- forward
|
||||
|
||||
## TFBartModel
|
||||
|
||||
[[autodoc]] TFBartModel
|
||||
- call
|
||||
|
||||
## TFBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFBartForConditionalGeneration
|
||||
- call
|
||||
|
||||
## FlaxBartModel
|
||||
|
||||
[[autodoc]] FlaxBartModel
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxBartForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBartForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxBartForSequenceClassification
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBartForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxBartForQuestionAnswering
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBartForCausalLM
|
||||
|
||||
[[autodoc]] FlaxBartForCausalLM
|
||||
- __call__
|
||||
50
docs/source/en/model_doc/barthez.mdx
Normal file
50
docs/source/en/model_doc/barthez.mdx
Normal file
@@ -0,0 +1,50 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BARThez
|
||||
|
||||
## Overview
|
||||
|
||||
The BARThez model was proposed in [BARThez: a Skilled Pretrained French Sequence-to-Sequence Model](https://arxiv.org/abs/2010.12321) by Moussa Kamal Eddine, Antoine J.-P. Tixier, Michalis Vazirgiannis on 23 Oct,
|
||||
2020.
|
||||
|
||||
The abstract of the paper:
|
||||
|
||||
|
||||
*Inductive transfer learning, enabled by self-supervised learning, have taken the entire Natural Language Processing
|
||||
(NLP) field by storm, with models such as BERT and BART setting new state of the art on countless natural language
|
||||
understanding tasks. While there are some notable exceptions, most of the available models and research have been
|
||||
conducted for the English language. In this work, we introduce BARThez, the first BART model for the French language
|
||||
(to the best of our knowledge). BARThez was pretrained on a very large monolingual French corpus from past research
|
||||
that we adapted to suit BART's perturbation schemes. Unlike already existing BERT-based French language models such as
|
||||
CamemBERT and FlauBERT, BARThez is particularly well-suited for generative tasks, since not only its encoder but also
|
||||
its decoder is pretrained. In addition to discriminative tasks from the FLUE benchmark, we evaluate BARThez on a novel
|
||||
summarization dataset, OrangeSum, that we release with this paper. We also continue the pretraining of an already
|
||||
pretrained multilingual BART on BARThez's corpus, and we show that the resulting model, which we call mBARTHez,
|
||||
provides a significant boost over vanilla BARThez, and is on par with or outperforms CamemBERT and FlauBERT.*
|
||||
|
||||
This model was contributed by [moussakam](https://huggingface.co/moussakam). The Authors' code can be found [here](https://github.com/moussaKam/BARThez).
|
||||
|
||||
|
||||
### Examples
|
||||
|
||||
- BARThez can be fine-tuned on sequence-to-sequence tasks in a similar way as BART, check:
|
||||
[examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md).
|
||||
|
||||
|
||||
## BarthezTokenizer
|
||||
|
||||
[[autodoc]] BarthezTokenizer
|
||||
|
||||
## BarthezTokenizerFast
|
||||
|
||||
[[autodoc]] BarthezTokenizerFast
|
||||
82
docs/source/en/model_doc/bartpho.mdx
Normal file
82
docs/source/en/model_doc/bartpho.mdx
Normal file
@@ -0,0 +1,82 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BARTpho
|
||||
|
||||
## Overview
|
||||
|
||||
The BARTpho model was proposed in [BARTpho: Pre-trained Sequence-to-Sequence Models for Vietnamese](https://arxiv.org/abs/2109.09701) by Nguyen Luong Tran, Duong Minh Le and Dat Quoc Nguyen.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present BARTpho with two versions -- BARTpho_word and BARTpho_syllable -- the first public large-scale monolingual
|
||||
sequence-to-sequence models pre-trained for Vietnamese. Our BARTpho uses the "large" architecture and pre-training
|
||||
scheme of the sequence-to-sequence denoising model BART, thus especially suitable for generative NLP tasks. Experiments
|
||||
on a downstream task of Vietnamese text summarization show that in both automatic and human evaluations, our BARTpho
|
||||
outperforms the strong baseline mBART and improves the state-of-the-art. We release BARTpho to facilitate future
|
||||
research and applications of generative Vietnamese NLP tasks.*
|
||||
|
||||
Example of use:
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import AutoModel, AutoTokenizer
|
||||
|
||||
>>> bartpho = AutoModel.from_pretrained("vinai/bartpho-syllable")
|
||||
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bartpho-syllable")
|
||||
|
||||
>>> line = "Chúng tôi là những nghiên cứu viên."
|
||||
|
||||
>>> input_ids = tokenizer(line, return_tensors="pt")
|
||||
|
||||
>>> with torch.no_grad():
|
||||
... features = bartpho(**input_ids) # Models outputs are now tuples
|
||||
|
||||
>>> # With TensorFlow 2.0+:
|
||||
>>> from transformers import TFAutoModel
|
||||
|
||||
>>> bartpho = TFAutoModel.from_pretrained("vinai/bartpho-syllable")
|
||||
>>> input_ids = tokenizer(line, return_tensors="tf")
|
||||
>>> features = bartpho(**input_ids)
|
||||
```
|
||||
|
||||
Tips:
|
||||
|
||||
- Following mBART, BARTpho uses the "large" architecture of BART with an additional layer-normalization layer on top of
|
||||
both the encoder and decoder. Thus, usage examples in the [documentation of BART](bart), when adapting to use
|
||||
with BARTpho, should be adjusted by replacing the BART-specialized classes with the mBART-specialized counterparts.
|
||||
For example:
|
||||
|
||||
```python
|
||||
>>> from transformers import MBartForConditionalGeneration
|
||||
|
||||
>>> bartpho = MBartForConditionalGeneration.from_pretrained("vinai/bartpho-syllable")
|
||||
>>> TXT = "Chúng tôi là <mask> nghiên cứu viên."
|
||||
>>> input_ids = tokenizer([TXT], return_tensors="pt")["input_ids"]
|
||||
>>> logits = bartpho(input_ids).logits
|
||||
>>> masked_index = (input_ids[0] == tokenizer.mask_token_id).nonzero().item()
|
||||
>>> probs = logits[0, masked_index].softmax(dim=0)
|
||||
>>> values, predictions = probs.topk(5)
|
||||
>>> print(tokenizer.decode(predictions).split())
|
||||
```
|
||||
|
||||
- This implementation is only for tokenization: "monolingual_vocab_file" consists of Vietnamese-specialized types
|
||||
extracted from the pre-trained SentencePiece model "vocab_file" that is available from the multilingual XLM-RoBERTa.
|
||||
Other languages, if employing this pre-trained multilingual SentencePiece model "vocab_file" for subword
|
||||
segmentation, can reuse BartphoTokenizer with their own language-specialized "monolingual_vocab_file".
|
||||
|
||||
This model was contributed by [dqnguyen](https://huggingface.co/dqnguyen). The original code can be found [here](https://github.com/VinAIResearch/BARTpho).
|
||||
|
||||
## BartphoTokenizer
|
||||
|
||||
[[autodoc]] BartphoTokenizer
|
||||
114
docs/source/en/model_doc/beit.mdx
Normal file
114
docs/source/en/model_doc/beit.mdx
Normal file
@@ -0,0 +1,114 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BEiT
|
||||
|
||||
## Overview
|
||||
|
||||
The BEiT model was proposed in [BEiT: BERT Pre-Training of Image Transformers](https://arxiv.org/abs/2106.08254) by
|
||||
Hangbo Bao, Li Dong and Furu Wei. Inspired by BERT, BEiT is the first paper that makes self-supervised pre-training of
|
||||
Vision Transformers (ViTs) outperform supervised pre-training. Rather than pre-training the model to predict the class
|
||||
of an image (as done in the [original ViT paper](https://arxiv.org/abs/2010.11929)), BEiT models are pre-trained to
|
||||
predict visual tokens from the codebook of OpenAI's [DALL-E model](https://arxiv.org/abs/2102.12092) given masked
|
||||
patches.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We introduce a self-supervised vision representation model BEiT, which stands for Bidirectional Encoder representation
|
||||
from Image Transformers. Following BERT developed in the natural language processing area, we propose a masked image
|
||||
modeling task to pretrain vision Transformers. Specifically, each image has two views in our pre-training, i.e, image
|
||||
patches (such as 16x16 pixels), and visual tokens (i.e., discrete tokens). We first "tokenize" the original image into
|
||||
visual tokens. Then we randomly mask some image patches and fed them into the backbone Transformer. The pre-training
|
||||
objective is to recover the original visual tokens based on the corrupted image patches. After pre-training BEiT, we
|
||||
directly fine-tune the model parameters on downstream tasks by appending task layers upon the pretrained encoder.
|
||||
Experimental results on image classification and semantic segmentation show that our model achieves competitive results
|
||||
with previous pre-training methods. For example, base-size BEiT achieves 83.2% top-1 accuracy on ImageNet-1K,
|
||||
significantly outperforming from-scratch DeiT training (81.8%) with the same setup. Moreover, large-size BEiT obtains
|
||||
86.3% only using ImageNet-1K, even outperforming ViT-L with supervised pre-training on ImageNet-22K (85.2%).*
|
||||
|
||||
Tips:
|
||||
|
||||
- BEiT models are regular Vision Transformers, but pre-trained in a self-supervised way rather than supervised. They
|
||||
outperform both the [original model (ViT)](vit) as well as [Data-efficient Image Transformers (DeiT)](deit) when fine-tuned on ImageNet-1K and CIFAR-100. You can check out demo notebooks regarding inference as well as
|
||||
fine-tuning on custom data [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/VisionTransformer) (you can just replace
|
||||
[`ViTFeatureExtractor`] by [`BeitFeatureExtractor`] and
|
||||
[`ViTForImageClassification`] by [`BeitForImageClassification`]).
|
||||
- There's also a demo notebook available which showcases how to combine DALL-E's image tokenizer with BEiT for
|
||||
performing masked image modeling. You can find it [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/BEiT).
|
||||
- As the BEiT models expect each image to be of the same size (resolution), one can use
|
||||
[`BeitFeatureExtractor`] to resize (or rescale) and normalize images for the model.
|
||||
- Both the patch resolution and image resolution used during pre-training or fine-tuning are reflected in the name of
|
||||
each checkpoint. For example, `microsoft/beit-base-patch16-224` refers to a base-sized architecture with patch
|
||||
resolution of 16x16 and fine-tuning resolution of 224x224. All checkpoints can be found on the [hub](https://huggingface.co/models?search=microsoft/beit).
|
||||
- The available checkpoints are either (1) pre-trained on [ImageNet-22k](http://www.image-net.org/) (a collection of
|
||||
14 million images and 22k classes) only, (2) also fine-tuned on ImageNet-22k or (3) also fine-tuned on [ImageNet-1k](http://www.image-net.org/challenges/LSVRC/2012/) (also referred to as ILSVRC 2012, a collection of 1.3 million
|
||||
images and 1,000 classes).
|
||||
- BEiT uses relative position embeddings, inspired by the T5 model. During pre-training, the authors shared the
|
||||
relative position bias among the several self-attention layers. During fine-tuning, each layer's relative position
|
||||
bias is initialized with the shared relative position bias obtained after pre-training. Note that, if one wants to
|
||||
pre-train a model from scratch, one needs to either set the `use_relative_position_bias` or the
|
||||
`use_relative_position_bias` attribute of [`BeitConfig`] to `True` in order to add
|
||||
position embeddings.
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The JAX/FLAX version of this model was
|
||||
contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/microsoft/unilm/tree/master/beit).
|
||||
|
||||
|
||||
## BEiT specific outputs
|
||||
|
||||
[[autodoc]] models.beit.modeling_beit.BeitModelOutputWithPooling
|
||||
|
||||
[[autodoc]] models.beit.modeling_flax_beit.FlaxBeitModelOutputWithPooling
|
||||
|
||||
## BeitConfig
|
||||
|
||||
[[autodoc]] BeitConfig
|
||||
|
||||
## BeitFeatureExtractor
|
||||
|
||||
[[autodoc]] BeitFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## BeitModel
|
||||
|
||||
[[autodoc]] BeitModel
|
||||
- forward
|
||||
|
||||
## BeitForMaskedImageModeling
|
||||
|
||||
[[autodoc]] BeitForMaskedImageModeling
|
||||
- forward
|
||||
|
||||
## BeitForImageClassification
|
||||
|
||||
[[autodoc]] BeitForImageClassification
|
||||
- forward
|
||||
|
||||
## BeitForSemanticSegmentation
|
||||
|
||||
[[autodoc]] BeitForSemanticSegmentation
|
||||
- forward
|
||||
|
||||
## FlaxBeitModel
|
||||
|
||||
[[autodoc]] FlaxBeitModel
|
||||
- __call__
|
||||
|
||||
## FlaxBeitForMaskedImageModeling
|
||||
|
||||
[[autodoc]] FlaxBeitForMaskedImageModeling
|
||||
- __call__
|
||||
|
||||
## FlaxBeitForImageClassification
|
||||
|
||||
[[autodoc]] FlaxBeitForImageClassification
|
||||
- __call__
|
||||
104
docs/source/en/model_doc/bert-generation.mdx
Normal file
104
docs/source/en/model_doc/bert-generation.mdx
Normal file
@@ -0,0 +1,104 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BertGeneration
|
||||
|
||||
## Overview
|
||||
|
||||
The BertGeneration model is a BERT model that can be leveraged for sequence-to-sequence tasks using
|
||||
[`EncoderDecoderModel`] as proposed in [Leveraging Pre-trained Checkpoints for Sequence Generation
|
||||
Tasks](https://arxiv.org/abs/1907.12461) by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Unsupervised pretraining of large neural models has recently revolutionized Natural Language Processing. By
|
||||
warm-starting from the publicly released checkpoints, NLP practitioners have pushed the state-of-the-art on multiple
|
||||
benchmarks while saving significant amounts of compute time. So far the focus has been mainly on the Natural Language
|
||||
Understanding tasks. In this paper, we demonstrate the efficacy of pre-trained checkpoints for Sequence Generation. We
|
||||
developed a Transformer-based sequence-to-sequence model that is compatible with publicly available pre-trained BERT,
|
||||
GPT-2 and RoBERTa checkpoints and conducted an extensive empirical study on the utility of initializing our model, both
|
||||
encoder and decoder, with these checkpoints. Our models result in new state-of-the-art results on Machine Translation,
|
||||
Text Summarization, Sentence Splitting, and Sentence Fusion.*
|
||||
|
||||
Usage:
|
||||
|
||||
- The model can be used in combination with the [`EncoderDecoderModel`] to leverage two pretrained
|
||||
BERT checkpoints for subsequent fine-tuning.
|
||||
|
||||
```python
|
||||
>>> # leverage checkpoints for Bert2Bert model...
|
||||
>>> # use BERT's cls token as BOS token and sep token as EOS token
|
||||
>>> encoder = BertGenerationEncoder.from_pretrained("bert-large-uncased", bos_token_id=101, eos_token_id=102)
|
||||
>>> # add cross attention layers and use BERT's cls token as BOS token and sep token as EOS token
|
||||
>>> decoder = BertGenerationDecoder.from_pretrained(
|
||||
... "bert-large-uncased", add_cross_attention=True, is_decoder=True, bos_token_id=101, eos_token_id=102
|
||||
... )
|
||||
>>> bert2bert = EncoderDecoderModel(encoder=encoder, decoder=decoder)
|
||||
|
||||
>>> # create tokenizer...
|
||||
>>> tokenizer = BertTokenizer.from_pretrained("bert-large-uncased")
|
||||
|
||||
>>> input_ids = tokenizer(
|
||||
... "This is a long article to summarize", add_special_tokens=False, return_tensors="pt"
|
||||
>>> ).input_ids
|
||||
>>> labels = tokenizer("This is a short summary", return_tensors="pt").input_ids
|
||||
|
||||
>>> # train...
|
||||
>>> loss = bert2bert(input_ids=input_ids, decoder_input_ids=labels, labels=labels).loss
|
||||
>>> loss.backward()
|
||||
```
|
||||
|
||||
- Pretrained [`EncoderDecoderModel`] are also directly available in the model hub, e.g.,
|
||||
|
||||
|
||||
```python
|
||||
>>> # instantiate sentence fusion model
|
||||
>>> sentence_fuser = EncoderDecoderModel.from_pretrained("google/roberta2roberta_L-24_discofuse")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("google/roberta2roberta_L-24_discofuse")
|
||||
|
||||
>>> input_ids = tokenizer(
|
||||
... "This is the first sentence. This is the second sentence.", add_special_tokens=False, return_tensors="pt"
|
||||
>>> ).input_ids
|
||||
|
||||
>>> outputs = sentence_fuser.generate(input_ids)
|
||||
|
||||
>>> print(tokenizer.decode(outputs[0]))
|
||||
```
|
||||
|
||||
Tips:
|
||||
|
||||
- [`BertGenerationEncoder`] and [`BertGenerationDecoder`] should be used in
|
||||
combination with [`EncoderDecoder`].
|
||||
- For summarization, sentence splitting, sentence fusion and translation, no special tokens are required for the input.
|
||||
Therefore, no EOS token should be added to the end of the input.
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The original code can be
|
||||
found [here](https://tfhub.dev/s?module-type=text-generation&subtype=module,placeholder).
|
||||
|
||||
## BertGenerationConfig
|
||||
|
||||
[[autodoc]] BertGenerationConfig
|
||||
|
||||
## BertGenerationTokenizer
|
||||
|
||||
[[autodoc]] BertGenerationTokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## BertGenerationEncoder
|
||||
|
||||
[[autodoc]] BertGenerationEncoder
|
||||
- forward
|
||||
|
||||
## BertGenerationDecoder
|
||||
|
||||
[[autodoc]] BertGenerationDecoder
|
||||
- forward
|
||||
74
docs/source/en/model_doc/bert-japanese.mdx
Normal file
74
docs/source/en/model_doc/bert-japanese.mdx
Normal file
@@ -0,0 +1,74 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BertJapanese
|
||||
|
||||
## Overview
|
||||
|
||||
The BERT models trained on Japanese text.
|
||||
|
||||
There are models with two different tokenization methods:
|
||||
|
||||
- Tokenize with MeCab and WordPiece. This requires some extra dependencies, [fugashi](https://github.com/polm/fugashi) which is a wrapper around [MeCab](https://taku910.github.io/mecab/).
|
||||
- Tokenize into characters.
|
||||
|
||||
To use *MecabTokenizer*, you should `pip install transformers["ja"]` (or `pip install -e .["ja"]` if you install
|
||||
from source) to install dependencies.
|
||||
|
||||
See [details on cl-tohoku repository](https://github.com/cl-tohoku/bert-japanese).
|
||||
|
||||
Example of using a model with MeCab and WordPiece tokenization:
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import AutoModel, AutoTokenizer
|
||||
|
||||
>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese")
|
||||
|
||||
>>> ## Input Japanese Text
|
||||
>>> line = "吾輩は猫である。"
|
||||
|
||||
>>> inputs = tokenizer(line, return_tensors="pt")
|
||||
|
||||
>>> print(tokenizer.decode(inputs["input_ids"][0]))
|
||||
[CLS] 吾輩 は 猫 で ある 。 [SEP]
|
||||
|
||||
>>> outputs = bertjapanese(**inputs)
|
||||
```
|
||||
|
||||
Example of using a model with Character tokenization:
|
||||
|
||||
```python
|
||||
>>> bertjapanese = AutoModel.from_pretrained("cl-tohoku/bert-base-japanese-char")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-char")
|
||||
|
||||
>>> ## Input Japanese Text
|
||||
>>> line = "吾輩は猫である。"
|
||||
|
||||
>>> inputs = tokenizer(line, return_tensors="pt")
|
||||
|
||||
>>> print(tokenizer.decode(inputs["input_ids"][0]))
|
||||
[CLS] 吾 輩 は 猫 で あ る 。 [SEP]
|
||||
|
||||
>>> outputs = bertjapanese(**inputs)
|
||||
```
|
||||
|
||||
Tips:
|
||||
|
||||
- This implementation is the same as BERT, except for tokenization method. Refer to the [documentation of BERT](bert) for more usage examples.
|
||||
|
||||
This model was contributed by [cl-tohoku](https://huggingface.co/cl-tohoku).
|
||||
|
||||
## BertJapaneseTokenizer
|
||||
|
||||
[[autodoc]] BertJapaneseTokenizer
|
||||
197
docs/source/en/model_doc/bert.mdx
Normal file
197
docs/source/en/model_doc/bert.mdx
Normal file
@@ -0,0 +1,197 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BERT
|
||||
|
||||
## Overview
|
||||
|
||||
The BERT model was proposed in [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805) by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova. It's a
|
||||
bidirectional transformer pretrained using a combination of masked language modeling objective and next sentence
|
||||
prediction on a large corpus comprising the Toronto Book Corpus and Wikipedia.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We introduce a new language representation model called BERT, which stands for Bidirectional Encoder Representations
|
||||
from Transformers. Unlike recent language representation models, BERT is designed to pre-train deep bidirectional
|
||||
representations from unlabeled text by jointly conditioning on both left and right context in all layers. As a result,
|
||||
the pre-trained BERT model can be fine-tuned with just one additional output layer to create state-of-the-art models
|
||||
for a wide range of tasks, such as question answering and language inference, without substantial task-specific
|
||||
architecture modifications.*
|
||||
|
||||
*BERT is conceptually simple and empirically powerful. It obtains new state-of-the-art results on eleven natural
|
||||
language processing tasks, including pushing the GLUE score to 80.5% (7.7% point absolute improvement), MultiNLI
|
||||
accuracy to 86.7% (4.6% absolute improvement), SQuAD v1.1 question answering Test F1 to 93.2 (1.5 point absolute
|
||||
improvement) and SQuAD v2.0 Test F1 to 83.1 (5.1 point absolute improvement).*
|
||||
|
||||
Tips:
|
||||
|
||||
- BERT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than
|
||||
the left.
|
||||
- BERT was trained with the masked language modeling (MLM) and next sentence prediction (NSP) objectives. It is
|
||||
efficient at predicting masked tokens and at NLU in general, but is not optimal for text generation.
|
||||
|
||||
This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/google-research/bert).
|
||||
|
||||
## BertConfig
|
||||
|
||||
[[autodoc]] BertConfig
|
||||
- all
|
||||
|
||||
## BertTokenizer
|
||||
|
||||
[[autodoc]] BertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## BertTokenizerFast
|
||||
|
||||
[[autodoc]] BertTokenizerFast
|
||||
|
||||
## Bert specific outputs
|
||||
|
||||
[[autodoc]] models.bert.modeling_bert.BertForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.bert.modeling_tf_bert.TFBertForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.bert.modeling_flax_bert.FlaxBertForPreTrainingOutput
|
||||
|
||||
## BertModel
|
||||
|
||||
[[autodoc]] BertModel
|
||||
- forward
|
||||
|
||||
## BertForPreTraining
|
||||
|
||||
[[autodoc]] BertForPreTraining
|
||||
- forward
|
||||
|
||||
## BertLMHeadModel
|
||||
|
||||
[[autodoc]] BertLMHeadModel
|
||||
- forward
|
||||
|
||||
## BertForMaskedLM
|
||||
|
||||
[[autodoc]] BertForMaskedLM
|
||||
- forward
|
||||
|
||||
## BertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] BertForNextSentencePrediction
|
||||
- forward
|
||||
|
||||
## BertForSequenceClassification
|
||||
|
||||
[[autodoc]] BertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## BertForMultipleChoice
|
||||
|
||||
[[autodoc]] BertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## BertForTokenClassification
|
||||
|
||||
[[autodoc]] BertForTokenClassification
|
||||
- forward
|
||||
|
||||
## BertForQuestionAnswering
|
||||
|
||||
[[autodoc]] BertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFBertModel
|
||||
|
||||
[[autodoc]] TFBertModel
|
||||
- call
|
||||
|
||||
## TFBertForPreTraining
|
||||
|
||||
[[autodoc]] TFBertForPreTraining
|
||||
- call
|
||||
|
||||
## TFBertModelLMHeadModel
|
||||
|
||||
[[autodoc]] TFBertLMHeadModel
|
||||
- call
|
||||
|
||||
## TFBertForMaskedLM
|
||||
|
||||
[[autodoc]] TFBertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] TFBertForNextSentencePrediction
|
||||
- call
|
||||
|
||||
## TFBertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFBertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFBertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFBertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFBertForTokenClassification
|
||||
|
||||
[[autodoc]] TFBertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFBertForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxBertModel
|
||||
|
||||
[[autodoc]] FlaxBertModel
|
||||
- __call__
|
||||
|
||||
## FlaxBertForPreTraining
|
||||
|
||||
[[autodoc]] FlaxBertForPreTraining
|
||||
- __call__
|
||||
|
||||
## FlaxBertForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxBertForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] FlaxBertForNextSentencePrediction
|
||||
- __call__
|
||||
|
||||
## FlaxBertForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxBertForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxBertForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxBertForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxBertForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxBertForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxBertForQuestionAnswering
|
||||
- __call__
|
||||
58
docs/source/en/model_doc/bertweet.mdx
Normal file
58
docs/source/en/model_doc/bertweet.mdx
Normal file
@@ -0,0 +1,58 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BERTweet
|
||||
|
||||
## Overview
|
||||
|
||||
The BERTweet model was proposed in [BERTweet: A pre-trained language model for English Tweets](https://www.aclweb.org/anthology/2020.emnlp-demos.2.pdf) by Dat Quoc Nguyen, Thanh Vu, Anh Tuan Nguyen.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present BERTweet, the first public large-scale pre-trained language model for English Tweets. Our BERTweet, having
|
||||
the same architecture as BERT-base (Devlin et al., 2019), is trained using the RoBERTa pre-training procedure (Liu et
|
||||
al., 2019). Experiments show that BERTweet outperforms strong baselines RoBERTa-base and XLM-R-base (Conneau et al.,
|
||||
2020), producing better performance results than the previous state-of-the-art models on three Tweet NLP tasks:
|
||||
Part-of-speech tagging, Named-entity recognition and text classification.*
|
||||
|
||||
Example of use:
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import AutoModel, AutoTokenizer
|
||||
|
||||
>>> bertweet = AutoModel.from_pretrained("vinai/bertweet-base")
|
||||
|
||||
>>> # For transformers v4.x+:
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base", use_fast=False)
|
||||
|
||||
>>> # For transformers v3.x:
|
||||
>>> # tokenizer = AutoTokenizer.from_pretrained("vinai/bertweet-base")
|
||||
|
||||
>>> # INPUT TWEET IS ALREADY NORMALIZED!
|
||||
>>> line = "SC has first two presumptive cases of coronavirus , DHEC confirms HTTPURL via @USER :cry:"
|
||||
|
||||
>>> input_ids = torch.tensor([tokenizer.encode(line)])
|
||||
|
||||
>>> with torch.no_grad():
|
||||
... features = bertweet(input_ids) # Models outputs are now tuples
|
||||
|
||||
>>> # With TensorFlow 2.0+:
|
||||
>>> # from transformers import TFAutoModel
|
||||
>>> # bertweet = TFAutoModel.from_pretrained("vinai/bertweet-base")
|
||||
```
|
||||
|
||||
This model was contributed by [dqnguyen](https://huggingface.co/dqnguyen). The original code can be found [here](https://github.com/VinAIResearch/BERTweet).
|
||||
|
||||
## BertweetTokenizer
|
||||
|
||||
[[autodoc]] BertweetTokenizer
|
||||
146
docs/source/en/model_doc/big_bird.mdx
Normal file
146
docs/source/en/model_doc/big_bird.mdx
Normal file
@@ -0,0 +1,146 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BigBird
|
||||
|
||||
## Overview
|
||||
|
||||
The BigBird model was proposed in [Big Bird: Transformers for Longer Sequences](https://arxiv.org/abs/2007.14062) by
|
||||
Zaheer, Manzil and Guruganesh, Guru and Dubey, Kumar Avinava and Ainslie, Joshua and Alberti, Chris and Ontanon,
|
||||
Santiago and Pham, Philip and Ravula, Anirudh and Wang, Qifan and Yang, Li and others. BigBird, is a sparse-attention
|
||||
based transformer which extends Transformer based models, such as BERT to much longer sequences. In addition to sparse
|
||||
attention, BigBird also applies global attention as well as random attention to the input sequence. Theoretically, it
|
||||
has been shown that applying sparse, global, and random attention approximates full attention, while being
|
||||
computationally much more efficient for longer sequences. As a consequence of the capability to handle longer context,
|
||||
BigBird has shown improved performance on various long document NLP tasks, such as question answering and
|
||||
summarization, compared to BERT or RoBERTa.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformers-based models, such as BERT, have been one of the most successful deep learning models for NLP.
|
||||
Unfortunately, one of their core limitations is the quadratic dependency (mainly in terms of memory) on the sequence
|
||||
length due to their full attention mechanism. To remedy this, we propose, BigBird, a sparse attention mechanism that
|
||||
reduces this quadratic dependency to linear. We show that BigBird is a universal approximator of sequence functions and
|
||||
is Turing complete, thereby preserving these properties of the quadratic, full attention model. Along the way, our
|
||||
theoretical analysis reveals some of the benefits of having O(1) global tokens (such as CLS), that attend to the entire
|
||||
sequence as part of the sparse attention mechanism. The proposed sparse attention can handle sequences of length up to
|
||||
8x of what was previously possible using similar hardware. As a consequence of the capability to handle longer context,
|
||||
BigBird drastically improves performance on various NLP tasks such as question answering and summarization. We also
|
||||
propose novel applications to genomics data.*
|
||||
|
||||
Tips:
|
||||
|
||||
- For an in-detail explanation on how BigBird's attention works, see [this blog post](https://huggingface.co/blog/big-bird).
|
||||
- BigBird comes with 2 implementations: **original_full** & **block_sparse**. For the sequence length < 1024, using
|
||||
**original_full** is advised as there is no benefit in using **block_sparse** attention.
|
||||
- The code currently uses window size of 3 blocks and 2 global blocks.
|
||||
- Sequence length must be divisible by block size.
|
||||
- Current implementation supports only **ITC**.
|
||||
- Current implementation doesn't support **num_random_blocks = 0**
|
||||
|
||||
This model was contributed by [vasudevgupta](https://huggingface.co/vasudevgupta). The original code can be found
|
||||
[here](https://github.com/google-research/bigbird).
|
||||
|
||||
## BigBirdConfig
|
||||
|
||||
[[autodoc]] BigBirdConfig
|
||||
|
||||
## BigBirdTokenizer
|
||||
|
||||
[[autodoc]] BigBirdTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## BigBirdTokenizerFast
|
||||
|
||||
[[autodoc]] BigBirdTokenizerFast
|
||||
|
||||
## BigBird specific outputs
|
||||
|
||||
[[autodoc]] models.big_bird.modeling_big_bird.BigBirdForPreTrainingOutput
|
||||
|
||||
## BigBirdModel
|
||||
|
||||
[[autodoc]] BigBirdModel
|
||||
- forward
|
||||
|
||||
## BigBirdForPreTraining
|
||||
|
||||
[[autodoc]] BigBirdForPreTraining
|
||||
- forward
|
||||
|
||||
## BigBirdForCausalLM
|
||||
|
||||
[[autodoc]] BigBirdForCausalLM
|
||||
- forward
|
||||
|
||||
## BigBirdForMaskedLM
|
||||
|
||||
[[autodoc]] BigBirdForMaskedLM
|
||||
- forward
|
||||
|
||||
## BigBirdForSequenceClassification
|
||||
|
||||
[[autodoc]] BigBirdForSequenceClassification
|
||||
- forward
|
||||
|
||||
## BigBirdForMultipleChoice
|
||||
|
||||
[[autodoc]] BigBirdForMultipleChoice
|
||||
- forward
|
||||
|
||||
## BigBirdForTokenClassification
|
||||
|
||||
[[autodoc]] BigBirdForTokenClassification
|
||||
- forward
|
||||
|
||||
## BigBirdForQuestionAnswering
|
||||
|
||||
[[autodoc]] BigBirdForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## FlaxBigBirdModel
|
||||
|
||||
[[autodoc]] FlaxBigBirdModel
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForPreTraining
|
||||
|
||||
[[autodoc]] FlaxBigBirdForPreTraining
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxBigBirdForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxBigBirdForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxBigBirdForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxBigBirdForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxBigBirdForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxBigBirdForQuestionAnswering
|
||||
- __call__
|
||||
81
docs/source/en/model_doc/bigbird_pegasus.mdx
Normal file
81
docs/source/en/model_doc/bigbird_pegasus.mdx
Normal file
@@ -0,0 +1,81 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BigBirdPegasus
|
||||
|
||||
## Overview
|
||||
|
||||
The BigBird model was proposed in [Big Bird: Transformers for Longer Sequences](https://arxiv.org/abs/2007.14062) by
|
||||
Zaheer, Manzil and Guruganesh, Guru and Dubey, Kumar Avinava and Ainslie, Joshua and Alberti, Chris and Ontanon,
|
||||
Santiago and Pham, Philip and Ravula, Anirudh and Wang, Qifan and Yang, Li and others. BigBird, is a sparse-attention
|
||||
based transformer which extends Transformer based models, such as BERT to much longer sequences. In addition to sparse
|
||||
attention, BigBird also applies global attention as well as random attention to the input sequence. Theoretically, it
|
||||
has been shown that applying sparse, global, and random attention approximates full attention, while being
|
||||
computationally much more efficient for longer sequences. As a consequence of the capability to handle longer context,
|
||||
BigBird has shown improved performance on various long document NLP tasks, such as question answering and
|
||||
summarization, compared to BERT or RoBERTa.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformers-based models, such as BERT, have been one of the most successful deep learning models for NLP.
|
||||
Unfortunately, one of their core limitations is the quadratic dependency (mainly in terms of memory) on the sequence
|
||||
length due to their full attention mechanism. To remedy this, we propose, BigBird, a sparse attention mechanism that
|
||||
reduces this quadratic dependency to linear. We show that BigBird is a universal approximator of sequence functions and
|
||||
is Turing complete, thereby preserving these properties of the quadratic, full attention model. Along the way, our
|
||||
theoretical analysis reveals some of the benefits of having O(1) global tokens (such as CLS), that attend to the entire
|
||||
sequence as part of the sparse attention mechanism. The proposed sparse attention can handle sequences of length up to
|
||||
8x of what was previously possible using similar hardware. As a consequence of the capability to handle longer context,
|
||||
BigBird drastically improves performance on various NLP tasks such as question answering and summarization. We also
|
||||
propose novel applications to genomics data.*
|
||||
|
||||
Tips:
|
||||
|
||||
- For an in-detail explanation on how BigBird's attention works, see [this blog post](https://huggingface.co/blog/big-bird).
|
||||
- BigBird comes with 2 implementations: **original_full** & **block_sparse**. For the sequence length < 1024, using
|
||||
**original_full** is advised as there is no benefit in using **block_sparse** attention.
|
||||
- The code currently uses window size of 3 blocks and 2 global blocks.
|
||||
- Sequence length must be divisible by block size.
|
||||
- Current implementation supports only **ITC**.
|
||||
- Current implementation doesn't support **num_random_blocks = 0**.
|
||||
- BigBirdPegasus uses the [PegasusTokenizer](https://github.com/huggingface/transformers/blob/main/src/transformers/models/pegasus/tokenization_pegasus.py).
|
||||
|
||||
The original code can be found [here](https://github.com/google-research/bigbird).
|
||||
|
||||
## BigBirdPegasusConfig
|
||||
|
||||
[[autodoc]] BigBirdPegasusConfig
|
||||
- all
|
||||
|
||||
## BigBirdPegasusModel
|
||||
|
||||
[[autodoc]] BigBirdPegasusModel
|
||||
- forward
|
||||
|
||||
## BigBirdPegasusForConditionalGeneration
|
||||
|
||||
[[autodoc]] BigBirdPegasusForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## BigBirdPegasusForSequenceClassification
|
||||
|
||||
[[autodoc]] BigBirdPegasusForSequenceClassification
|
||||
- forward
|
||||
|
||||
## BigBirdPegasusForQuestionAnswering
|
||||
|
||||
[[autodoc]] BigBirdPegasusForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## BigBirdPegasusForCausalLM
|
||||
|
||||
[[autodoc]] BigBirdPegasusForCausalLM
|
||||
- forward
|
||||
95
docs/source/en/model_doc/blenderbot-small.mdx
Normal file
95
docs/source/en/model_doc/blenderbot-small.mdx
Normal file
@@ -0,0 +1,95 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Blenderbot Small
|
||||
|
||||
Note that [`BlenderbotSmallModel`] and
|
||||
[`BlenderbotSmallForConditionalGeneration`] are only used in combination with the checkpoint
|
||||
[facebook/blenderbot-90M](https://huggingface.co/facebook/blenderbot-90M). Larger Blenderbot checkpoints should
|
||||
instead be used with [`BlenderbotModel`] and
|
||||
[`BlenderbotForConditionalGeneration`]
|
||||
|
||||
## Overview
|
||||
|
||||
The Blender chatbot model was proposed in [Recipes for building an open-domain chatbot](https://arxiv.org/pdf/2004.13637.pdf) Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu,
|
||||
Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston on 30 Apr 2020.
|
||||
|
||||
The abstract of the paper is the following:
|
||||
|
||||
*Building open-domain chatbots is a challenging area for machine learning research. While prior work has shown that
|
||||
scaling neural models in the number of parameters and the size of the data they are trained on gives improved results,
|
||||
we show that other ingredients are important for a high-performing chatbot. Good conversation requires a number of
|
||||
skills that an expert conversationalist blends in a seamless way: providing engaging talking points and listening to
|
||||
their partners, and displaying knowledge, empathy and personality appropriately, while maintaining a consistent
|
||||
persona. We show that large scale models can learn these skills when given appropriate training data and choice of
|
||||
generation strategy. We build variants of these recipes with 90M, 2.7B and 9.4B parameter models, and make our models
|
||||
and code publicly available. Human evaluations show our best models are superior to existing approaches in multi-turn
|
||||
dialogue in terms of engagingness and humanness measurements. We then discuss the limitations of this work by analyzing
|
||||
failure cases of our models.*
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The authors' code can be
|
||||
found [here](https://github.com/facebookresearch/ParlAI) .
|
||||
|
||||
## BlenderbotSmallConfig
|
||||
|
||||
[[autodoc]] BlenderbotSmallConfig
|
||||
|
||||
## BlenderbotSmallTokenizer
|
||||
|
||||
[[autodoc]] BlenderbotSmallTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## BlenderbotSmallTokenizerFast
|
||||
|
||||
[[autodoc]] BlenderbotSmallTokenizerFast
|
||||
|
||||
## BlenderbotSmallModel
|
||||
|
||||
[[autodoc]] BlenderbotSmallModel
|
||||
- forward
|
||||
|
||||
## BlenderbotSmallForConditionalGeneration
|
||||
|
||||
[[autodoc]] BlenderbotSmallForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## BlenderbotSmallForCausalLM
|
||||
|
||||
[[autodoc]] BlenderbotSmallForCausalLM
|
||||
- forward
|
||||
|
||||
## TFBlenderbotSmallModel
|
||||
|
||||
[[autodoc]] TFBlenderbotSmallModel
|
||||
- call
|
||||
|
||||
## TFBlenderbotSmallForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFBlenderbotSmallForConditionalGeneration
|
||||
- call
|
||||
|
||||
## FlaxBlenderbotSmallModel
|
||||
|
||||
[[autodoc]] FlaxBlenderbotSmallModel
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBlenderbotForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxBlenderbotSmallForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
119
docs/source/en/model_doc/blenderbot.mdx
Normal file
119
docs/source/en/model_doc/blenderbot.mdx
Normal file
@@ -0,0 +1,119 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Blenderbot
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) .
|
||||
|
||||
## Overview
|
||||
|
||||
The Blender chatbot model was proposed in [Recipes for building an open-domain chatbot](https://arxiv.org/pdf/2004.13637.pdf) Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu,
|
||||
Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston on 30 Apr 2020.
|
||||
|
||||
The abstract of the paper is the following:
|
||||
|
||||
*Building open-domain chatbots is a challenging area for machine learning research. While prior work has shown that
|
||||
scaling neural models in the number of parameters and the size of the data they are trained on gives improved results,
|
||||
we show that other ingredients are important for a high-performing chatbot. Good conversation requires a number of
|
||||
skills that an expert conversationalist blends in a seamless way: providing engaging talking points and listening to
|
||||
their partners, and displaying knowledge, empathy and personality appropriately, while maintaining a consistent
|
||||
persona. We show that large scale models can learn these skills when given appropriate training data and choice of
|
||||
generation strategy. We build variants of these recipes with 90M, 2.7B and 9.4B parameter models, and make our models
|
||||
and code publicly available. Human evaluations show our best models are superior to existing approaches in multi-turn
|
||||
dialogue in terms of engagingness and humanness measurements. We then discuss the limitations of this work by analyzing
|
||||
failure cases of our models.*
|
||||
|
||||
This model was contributed by [sshleifer](https://huggingface.co/sshleifer). The authors' code can be found [here](https://github.com/facebookresearch/ParlAI) .
|
||||
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- Blenderbot uses a standard [seq2seq model transformer](https://arxiv.org/pdf/1706.03762.pdf) based architecture.
|
||||
- Available checkpoints can be found in the [model hub](https://huggingface.co/models?search=blenderbot).
|
||||
- This is the *default* Blenderbot model class. However, some smaller checkpoints, such as
|
||||
`facebook/blenderbot_small_90M`, have a different architecture and consequently should be used with
|
||||
[BlenderbotSmall](blenderbot-small).
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
Here is an example of model usage:
|
||||
|
||||
```python
|
||||
>>> from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
||||
|
||||
>>> mname = "facebook/blenderbot-400M-distill"
|
||||
>>> model = BlenderbotForConditionalGeneration.from_pretrained(mname)
|
||||
>>> tokenizer = BlenderbotTokenizer.from_pretrained(mname)
|
||||
>>> UTTERANCE = "My friends are cool but they eat too many carbs."
|
||||
>>> inputs = tokenizer([UTTERANCE], return_tensors="pt")
|
||||
>>> reply_ids = model.generate(**inputs)
|
||||
>>> print(tokenizer.batch_decode(reply_ids))
|
||||
["<s> That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?</s>"]
|
||||
```
|
||||
|
||||
## BlenderbotConfig
|
||||
|
||||
[[autodoc]] BlenderbotConfig
|
||||
|
||||
## BlenderbotTokenizer
|
||||
|
||||
[[autodoc]] BlenderbotTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## BlenderbotTokenizerFast
|
||||
|
||||
[[autodoc]] BlenderbotTokenizerFast
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## BlenderbotModel
|
||||
|
||||
See `transformers.BartModel` for arguments to *forward* and *generate*
|
||||
|
||||
[[autodoc]] BlenderbotModel
|
||||
- forward
|
||||
|
||||
## BlenderbotForConditionalGeneration
|
||||
|
||||
See [`~transformers.BartForConditionalGeneration`] for arguments to *forward* and *generate*
|
||||
|
||||
[[autodoc]] BlenderbotForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## BlenderbotForCausalLM
|
||||
|
||||
[[autodoc]] BlenderbotForCausalLM
|
||||
- forward
|
||||
|
||||
## TFBlenderbotModel
|
||||
|
||||
[[autodoc]] TFBlenderbotModel
|
||||
- call
|
||||
|
||||
## TFBlenderbotForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFBlenderbotForConditionalGeneration
|
||||
- call
|
||||
|
||||
## FlaxBlenderbotModel
|
||||
|
||||
[[autodoc]] FlaxBlenderbotModel
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxBlenderbotForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxBlenderbotForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
42
docs/source/en/model_doc/bort.mdx
Normal file
42
docs/source/en/model_doc/bort.mdx
Normal file
@@ -0,0 +1,42 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# BORT
|
||||
|
||||
## Overview
|
||||
|
||||
The BORT model was proposed in [Optimal Subarchitecture Extraction for BERT](https://arxiv.org/abs/2010.10499) by
|
||||
Adrian de Wynter and Daniel J. Perry. It is an optimal subset of architectural parameters for the BERT, which the
|
||||
authors refer to as "Bort".
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We extract an optimal subset of architectural parameters for the BERT architecture from Devlin et al. (2018) by
|
||||
applying recent breakthroughs in algorithms for neural architecture search. This optimal subset, which we refer to as
|
||||
"Bort", is demonstrably smaller, having an effective (that is, not counting the embedding layer) size of 5.5% the
|
||||
original BERT-large architecture, and 16% of the net size. Bort is also able to be pretrained in 288 GPU hours, which
|
||||
is 1.2% of the time required to pretrain the highest-performing BERT parametric architectural variant, RoBERTa-large
|
||||
(Liu et al., 2019), and about 33% of that of the world-record, in GPU hours, required to train BERT-large on the same
|
||||
hardware. It is also 7.9x faster on a CPU, as well as being better performing than other compressed variants of the
|
||||
architecture, and some of the non-compressed variants: it obtains performance improvements of between 0.3% and 31%,
|
||||
absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks.*
|
||||
|
||||
Tips:
|
||||
|
||||
- BORT's model architecture is based on BERT, so one can refer to [BERT's documentation page](bert) for the
|
||||
model's API as well as usage examples.
|
||||
- BORT uses the RoBERTa tokenizer instead of the BERT tokenizer, so one can refer to [RoBERTa's documentation page](roberta) for the tokenizer's API as well as usage examples.
|
||||
- BORT requires a specific fine-tuning algorithm, called [Agora](https://adewynter.github.io/notes/bort_algorithms_and_applications.html#fine-tuning-with-algebraic-topology) ,
|
||||
that is sadly not open-sourced yet. It would be very useful for the community, if someone tries to implement the
|
||||
algorithm to make BORT fine-tuning work.
|
||||
|
||||
This model was contributed by [stefan-it](https://huggingface.co/stefan-it). The original code can be found [here](https://github.com/alexa/bort/).
|
||||
86
docs/source/en/model_doc/byt5.mdx
Normal file
86
docs/source/en/model_doc/byt5.mdx
Normal file
@@ -0,0 +1,86 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ByT5
|
||||
|
||||
## Overview
|
||||
|
||||
The ByT5 model was presented in [ByT5: Towards a token-free future with pre-trained byte-to-byte models](https://arxiv.org/abs/2105.13626) by Linting Xue, Aditya Barua, Noah Constant, Rami Al-Rfou, Sharan Narang, Mihir
|
||||
Kale, Adam Roberts, Colin Raffel.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Most widely-used pre-trained language models operate on sequences of tokens corresponding to word or subword units.
|
||||
Encoding text as a sequence of tokens requires a tokenizer, which is typically created as an independent artifact from
|
||||
the model. Token-free models that instead operate directly on raw text (bytes or characters) have many benefits: they
|
||||
can process text in any language out of the box, they are more robust to noise, and they minimize technical debt by
|
||||
removing complex and error-prone text preprocessing pipelines. Since byte or character sequences are longer than token
|
||||
sequences, past work on token-free models has often introduced new model architectures designed to amortize the cost of
|
||||
operating directly on raw text. In this paper, we show that a standard Transformer architecture can be used with
|
||||
minimal modifications to process byte sequences. We carefully characterize the trade-offs in terms of parameter count,
|
||||
training FLOPs, and inference speed, and show that byte-level models are competitive with their token-level
|
||||
counterparts. We also demonstrate that byte-level models are significantly more robust to noise and perform better on
|
||||
tasks that are sensitive to spelling and pronunciation. As part of our contribution, we release a new set of
|
||||
pre-trained byte-level Transformer models based on the T5 architecture, as well as all code and data used in our
|
||||
experiments.*
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The original code can be
|
||||
found [here](https://github.com/google-research/byt5).
|
||||
|
||||
ByT5's architecture is based on the T5v1.1 model, so one can refer to [T5v1.1's documentation page](t5v1.1). They
|
||||
only differ in how inputs should be prepared for the model, see the code examples below.
|
||||
|
||||
Since ByT5 was pre-trained unsupervisedly, there's no real advantage to using a task prefix during single-task
|
||||
fine-tuning. If you are doing multi-task fine-tuning, you should use a prefix.
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
ByT5 works on raw UTF-8 bytes, so it can be used without a tokenizer:
|
||||
|
||||
```python
|
||||
from transformers import T5ForConditionalGeneration
|
||||
import torch
|
||||
|
||||
model = T5ForConditionalGeneration.from_pretrained("google/byt5-small")
|
||||
|
||||
input_ids = torch.tensor([list("Life is like a box of chocolates.".encode("utf-8"))]) + 3 # add 3 for special tokens
|
||||
labels = (
|
||||
torch.tensor([list("La vie est comme une boîte de chocolat.".encode("utf-8"))]) + 3
|
||||
) # add 3 for special tokens
|
||||
|
||||
loss = model(input_ids, labels=labels).loss # forward pass
|
||||
```
|
||||
|
||||
For batched inference and training it is however recommended to make use of the tokenizer:
|
||||
|
||||
```python
|
||||
from transformers import T5ForConditionalGeneration, AutoTokenizer
|
||||
|
||||
model = T5ForConditionalGeneration.from_pretrained("google/byt5-small")
|
||||
tokenizer = AutoTokenizer.from_pretrained("google/byt5-small")
|
||||
|
||||
model_inputs = tokenizer(
|
||||
["Life is like a box of chocolates.", "Today is Monday."], padding="longest", return_tensors="pt"
|
||||
)
|
||||
labels = tokenizer(
|
||||
["La vie est comme une boîte de chocolat.", "Aujourd'hui c'est lundi."], padding="longest", return_tensors="pt"
|
||||
).input_ids
|
||||
|
||||
loss = model(**model_inputs, labels=labels).loss # forward pass
|
||||
```
|
||||
|
||||
## ByT5Tokenizer
|
||||
|
||||
[[autodoc]] ByT5Tokenizer
|
||||
|
||||
See [`ByT5Tokenizer`] for all details.
|
||||
110
docs/source/en/model_doc/camembert.mdx
Normal file
110
docs/source/en/model_doc/camembert.mdx
Normal file
@@ -0,0 +1,110 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# CamemBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The CamemBERT model was proposed in [CamemBERT: a Tasty French Language Model](https://arxiv.org/abs/1911.03894) by
|
||||
Louis Martin, Benjamin Muller, Pedro Javier Ortiz Suárez, Yoann Dupont, Laurent Romary, Éric Villemonte de la
|
||||
Clergerie, Djamé Seddah, and Benoît Sagot. It is based on Facebook's RoBERTa model released in 2019. It is a model
|
||||
trained on 138GB of French text.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pretrained language models are now ubiquitous in Natural Language Processing. Despite their success, most available
|
||||
models have either been trained on English data or on the concatenation of data in multiple languages. This makes
|
||||
practical use of such models --in all languages except English-- very limited. Aiming to address this issue for French,
|
||||
we release CamemBERT, a French version of the Bi-directional Encoders for Transformers (BERT). We measure the
|
||||
performance of CamemBERT compared to multilingual models in multiple downstream tasks, namely part-of-speech tagging,
|
||||
dependency parsing, named-entity recognition, and natural language inference. CamemBERT improves the state of the art
|
||||
for most of the tasks considered. We release the pretrained model for CamemBERT hoping to foster research and
|
||||
downstream applications for French NLP.*
|
||||
|
||||
Tips:
|
||||
|
||||
- This implementation is the same as RoBERTa. Refer to the [documentation of RoBERTa](roberta) for usage examples
|
||||
as well as the information relative to the inputs and outputs.
|
||||
|
||||
This model was contributed by [camembert](https://huggingface.co/camembert). The original code can be found [here](https://camembert-model.fr/).
|
||||
|
||||
## CamembertConfig
|
||||
|
||||
[[autodoc]] CamembertConfig
|
||||
|
||||
## CamembertTokenizer
|
||||
|
||||
[[autodoc]] CamembertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## CamembertTokenizerFast
|
||||
|
||||
[[autodoc]] CamembertTokenizerFast
|
||||
|
||||
## CamembertModel
|
||||
|
||||
[[autodoc]] CamembertModel
|
||||
|
||||
## CamembertForCausalLM
|
||||
|
||||
[[autodoc]] CamembertForCausalLM
|
||||
|
||||
## CamembertForMaskedLM
|
||||
|
||||
[[autodoc]] CamembertForMaskedLM
|
||||
|
||||
## CamembertForSequenceClassification
|
||||
|
||||
[[autodoc]] CamembertForSequenceClassification
|
||||
|
||||
## CamembertForMultipleChoice
|
||||
|
||||
[[autodoc]] CamembertForMultipleChoice
|
||||
|
||||
## CamembertForTokenClassification
|
||||
|
||||
[[autodoc]] CamembertForTokenClassification
|
||||
|
||||
## CamembertForQuestionAnswering
|
||||
|
||||
[[autodoc]] CamembertForQuestionAnswering
|
||||
|
||||
## TFCamembertModel
|
||||
|
||||
[[autodoc]] TFCamembertModel
|
||||
|
||||
## TFCamembertForCasualLM
|
||||
|
||||
[[autodoc]] TFCamembertForCausalLM
|
||||
|
||||
## TFCamembertForMaskedLM
|
||||
|
||||
[[autodoc]] TFCamembertForMaskedLM
|
||||
|
||||
## TFCamembertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFCamembertForSequenceClassification
|
||||
|
||||
## TFCamembertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFCamembertForMultipleChoice
|
||||
|
||||
## TFCamembertForTokenClassification
|
||||
|
||||
[[autodoc]] TFCamembertForTokenClassification
|
||||
|
||||
## TFCamembertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFCamembertForQuestionAnswering
|
||||
133
docs/source/en/model_doc/canine.mdx
Normal file
133
docs/source/en/model_doc/canine.mdx
Normal file
@@ -0,0 +1,133 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# CANINE
|
||||
|
||||
## Overview
|
||||
|
||||
The CANINE model was proposed in [CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language
|
||||
Representation](https://arxiv.org/abs/2103.06874) by Jonathan H. Clark, Dan Garrette, Iulia Turc, John Wieting. It's
|
||||
among the first papers that trains a Transformer without using an explicit tokenization step (such as Byte Pair
|
||||
Encoding (BPE), WordPiece or SentencePiece). Instead, the model is trained directly at a Unicode character-level.
|
||||
Training at a character-level inevitably comes with a longer sequence length, which CANINE solves with an efficient
|
||||
downsampling strategy, before applying a deep Transformer encoder.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pipelined NLP systems have largely been superseded by end-to-end neural modeling, yet nearly all commonly-used models
|
||||
still require an explicit tokenization step. While recent tokenization approaches based on data-derived subword
|
||||
lexicons are less brittle than manually engineered tokenizers, these techniques are not equally suited to all
|
||||
languages, and the use of any fixed vocabulary may limit a model's ability to adapt. In this paper, we present CANINE,
|
||||
a neural encoder that operates directly on character sequences, without explicit tokenization or vocabulary, and a
|
||||
pre-training strategy that operates either directly on characters or optionally uses subwords as a soft inductive bias.
|
||||
To use its finer-grained input effectively and efficiently, CANINE combines downsampling, which reduces the input
|
||||
sequence length, with a deep transformer stack, which encodes context. CANINE outperforms a comparable mBERT model by
|
||||
2.8 F1 on TyDi QA, a challenging multilingual benchmark, despite having 28% fewer model parameters.*
|
||||
|
||||
Tips:
|
||||
|
||||
- CANINE uses no less than 3 Transformer encoders internally: 2 "shallow" encoders (which only consist of a single
|
||||
layer) and 1 "deep" encoder (which is a regular BERT encoder). First, a "shallow" encoder is used to contextualize
|
||||
the character embeddings, using local attention. Next, after downsampling, a "deep" encoder is applied. Finally,
|
||||
after upsampling, a "shallow" encoder is used to create the final character embeddings. Details regarding up- and
|
||||
downsampling can be found in the paper.
|
||||
- CANINE uses a max sequence length of 2048 characters by default. One can use [`CanineTokenizer`]
|
||||
to prepare text for the model.
|
||||
- Classification can be done by placing a linear layer on top of the final hidden state of the special [CLS] token
|
||||
(which has a predefined Unicode code point). For token classification tasks however, the downsampled sequence of
|
||||
tokens needs to be upsampled again to match the length of the original character sequence (which is 2048). The
|
||||
details for this can be found in the paper.
|
||||
- Models:
|
||||
|
||||
- [google/canine-c](https://huggingface.co/google/canine-c): Pre-trained with autoregressive character loss,
|
||||
12-layer, 768-hidden, 12-heads, 121M parameters (size ~500 MB).
|
||||
- [google/canine-s](https://huggingface.co/google/canine-s): Pre-trained with subword loss, 12-layer,
|
||||
768-hidden, 12-heads, 121M parameters (size ~500 MB).
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/google-research/language/tree/master/language/canine).
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
CANINE works on raw characters, so it can be used without a tokenizer:
|
||||
|
||||
```python
|
||||
>>> from transformers import CanineModel
|
||||
>>> import torch
|
||||
|
||||
>>> model = CanineModel.from_pretrained("google/canine-c") # model pre-trained with autoregressive character loss
|
||||
|
||||
>>> text = "hello world"
|
||||
>>> # use Python's built-in ord() function to turn each character into its unicode code point id
|
||||
>>> input_ids = torch.tensor([[ord(char) for char in text]])
|
||||
|
||||
>>> outputs = model(input_ids) # forward pass
|
||||
>>> pooled_output = outputs.pooler_output
|
||||
>>> sequence_output = outputs.last_hidden_state
|
||||
```
|
||||
|
||||
For batched inference and training, it is however recommended to make use of the tokenizer (to pad/truncate all
|
||||
sequences to the same length):
|
||||
|
||||
```python
|
||||
>>> from transformers import CanineTokenizer, CanineModel
|
||||
|
||||
>>> model = CanineModel.from_pretrained("google/canine-c")
|
||||
>>> tokenizer = CanineTokenizer.from_pretrained("google/canine-c")
|
||||
|
||||
>>> inputs = ["Life is like a box of chocolates.", "You never know what you gonna get."]
|
||||
>>> encoding = tokenizer(inputs, padding="longest", truncation=True, return_tensors="pt")
|
||||
|
||||
>>> outputs = model(**encoding) # forward pass
|
||||
>>> pooled_output = outputs.pooler_output
|
||||
>>> sequence_output = outputs.last_hidden_state
|
||||
```
|
||||
|
||||
## CANINE specific outputs
|
||||
|
||||
[[autodoc]] models.canine.modeling_canine.CanineModelOutputWithPooling
|
||||
|
||||
## CanineConfig
|
||||
|
||||
[[autodoc]] CanineConfig
|
||||
|
||||
## CanineTokenizer
|
||||
|
||||
[[autodoc]] CanineTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
|
||||
## CanineModel
|
||||
|
||||
[[autodoc]] CanineModel
|
||||
- forward
|
||||
|
||||
## CanineForSequenceClassification
|
||||
|
||||
[[autodoc]] CanineForSequenceClassification
|
||||
- forward
|
||||
|
||||
## CanineForMultipleChoice
|
||||
|
||||
[[autodoc]] CanineForMultipleChoice
|
||||
- forward
|
||||
|
||||
## CanineForTokenClassification
|
||||
|
||||
[[autodoc]] CanineForTokenClassification
|
||||
- forward
|
||||
|
||||
## CanineForQuestionAnswering
|
||||
|
||||
[[autodoc]] CanineForQuestionAnswering
|
||||
- forward
|
||||
160
docs/source/en/model_doc/clip.mdx
Normal file
160
docs/source/en/model_doc/clip.mdx
Normal file
@@ -0,0 +1,160 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# CLIP
|
||||
|
||||
## Overview
|
||||
|
||||
The CLIP model was proposed in [Learning Transferable Visual Models From Natural Language Supervision](https://arxiv.org/abs/2103.00020) by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh,
|
||||
Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever. CLIP
|
||||
(Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs. It can be
|
||||
instructed in natural language to predict the most relevant text snippet, given an image, without directly optimizing
|
||||
for the task, similarly to the zero-shot capabilities of GPT-2 and 3.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*State-of-the-art computer vision systems are trained to predict a fixed set of predetermined object categories. This
|
||||
restricted form of supervision limits their generality and usability since additional labeled data is needed to specify
|
||||
any other visual concept. Learning directly from raw text about images is a promising alternative which leverages a
|
||||
much broader source of supervision. We demonstrate that the simple pre-training task of predicting which caption goes
|
||||
with which image is an efficient and scalable way to learn SOTA image representations from scratch on a dataset of 400
|
||||
million (image, text) pairs collected from the internet. After pre-training, natural language is used to reference
|
||||
learned visual concepts (or describe new ones) enabling zero-shot transfer of the model to downstream tasks. We study
|
||||
the performance of this approach by benchmarking on over 30 different existing computer vision datasets, spanning tasks
|
||||
such as OCR, action recognition in videos, geo-localization, and many types of fine-grained object classification. The
|
||||
model transfers non-trivially to most tasks and is often competitive with a fully supervised baseline without the need
|
||||
for any dataset specific training. For instance, we match the accuracy of the original ResNet-50 on ImageNet zero-shot
|
||||
without needing to use any of the 1.28 million training examples it was trained on. We release our code and pre-trained
|
||||
model weights at this https URL.*
|
||||
|
||||
## Usage
|
||||
|
||||
CLIP is a multi-modal vision and language model. It can be used for image-text similarity and for zero-shot image
|
||||
classification. CLIP uses a ViT like transformer to get visual features and a causal language model to get the text
|
||||
features. Both the text and visual features are then projected to a latent space with identical dimension. The dot
|
||||
product between the projected image and text features is then used as a similar score.
|
||||
|
||||
To feed images to the Transformer encoder, each image is split into a sequence of fixed-size non-overlapping patches,
|
||||
which are then linearly embedded. A [CLS] token is added to serve as representation of an entire image. The authors
|
||||
also add absolute position embeddings, and feed the resulting sequence of vectors to a standard Transformer encoder.
|
||||
The [`CLIPFeatureExtractor`] can be used to resize (or rescale) and normalize images for the model.
|
||||
|
||||
The [`CLIPTokenizer`] is used to encode the text. The [`CLIPProcessor`] wraps
|
||||
[`CLIPFeatureExtractor`] and [`CLIPTokenizer`] into a single instance to both
|
||||
encode the text and prepare the images. The following example shows how to get the image-text similarity scores using
|
||||
[`CLIPProcessor`] and [`CLIPModel`].
|
||||
|
||||
|
||||
```python
|
||||
>>> from PIL import Image
|
||||
>>> import requests
|
||||
|
||||
>>> from transformers import CLIPProcessor, CLIPModel
|
||||
|
||||
>>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
||||
>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
||||
|
||||
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
||||
>>> image = Image.open(requests.get(url, stream=True).raw)
|
||||
|
||||
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
|
||||
|
||||
>>> outputs = model(**inputs)
|
||||
>>> logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
||||
>>> probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities
|
||||
```
|
||||
|
||||
This model was contributed by [valhalla](https://huggingface.co/valhalla). The original code can be found [here](https://github.com/openai/CLIP).
|
||||
|
||||
## CLIPConfig
|
||||
|
||||
[[autodoc]] CLIPConfig
|
||||
- from_text_vision_configs
|
||||
|
||||
## CLIPTextConfig
|
||||
|
||||
[[autodoc]] CLIPTextConfig
|
||||
|
||||
## CLIPVisionConfig
|
||||
|
||||
[[autodoc]] CLIPVisionConfig
|
||||
|
||||
## CLIPTokenizer
|
||||
|
||||
[[autodoc]] CLIPTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## CLIPTokenizerFast
|
||||
|
||||
[[autodoc]] CLIPTokenizerFast
|
||||
|
||||
## CLIPFeatureExtractor
|
||||
|
||||
[[autodoc]] CLIPFeatureExtractor
|
||||
|
||||
## CLIPProcessor
|
||||
|
||||
[[autodoc]] CLIPProcessor
|
||||
|
||||
## CLIPModel
|
||||
|
||||
[[autodoc]] CLIPModel
|
||||
- forward
|
||||
- get_text_features
|
||||
- get_image_features
|
||||
|
||||
## CLIPTextModel
|
||||
|
||||
[[autodoc]] CLIPTextModel
|
||||
- forward
|
||||
|
||||
## CLIPVisionModel
|
||||
|
||||
[[autodoc]] CLIPVisionModel
|
||||
- forward
|
||||
|
||||
## TFCLIPModel
|
||||
|
||||
[[autodoc]] TFCLIPModel
|
||||
- call
|
||||
- get_text_features
|
||||
- get_image_features
|
||||
|
||||
## TFCLIPTextModel
|
||||
|
||||
[[autodoc]] TFCLIPTextModel
|
||||
- call
|
||||
|
||||
## TFCLIPVisionModel
|
||||
|
||||
[[autodoc]] TFCLIPVisionModel
|
||||
- call
|
||||
|
||||
## FlaxCLIPModel
|
||||
|
||||
[[autodoc]] FlaxCLIPModel
|
||||
- __call__
|
||||
- get_text_features
|
||||
- get_image_features
|
||||
|
||||
## FlaxCLIPTextModel
|
||||
|
||||
[[autodoc]] FlaxCLIPTextModel
|
||||
- __call__
|
||||
|
||||
## FlaxCLIPVisionModel
|
||||
|
||||
[[autodoc]] FlaxCLIPVisionModel
|
||||
- __call__
|
||||
113
docs/source/en/model_doc/convbert.mdx
Normal file
113
docs/source/en/model_doc/convbert.mdx
Normal file
@@ -0,0 +1,113 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ConvBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The ConvBERT model was proposed in [ConvBERT: Improving BERT with Span-based Dynamic Convolution](https://arxiv.org/abs/2008.02496) by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng
|
||||
Yan.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pre-trained language models like BERT and its variants have recently achieved impressive performance in various
|
||||
natural language understanding tasks. However, BERT heavily relies on the global self-attention block and thus suffers
|
||||
large memory footprint and computation cost. Although all its attention heads query on the whole input sequence for
|
||||
generating the attention map from a global perspective, we observe some heads only need to learn local dependencies,
|
||||
which means the existence of computation redundancy. We therefore propose a novel span-based dynamic convolution to
|
||||
replace these self-attention heads to directly model local dependencies. The novel convolution heads, together with the
|
||||
rest self-attention heads, form a new mixed attention block that is more efficient at both global and local context
|
||||
learning. We equip BERT with this mixed attention design and build a ConvBERT model. Experiments have shown that
|
||||
ConvBERT significantly outperforms BERT and its variants in various downstream tasks, with lower training cost and
|
||||
fewer model parameters. Remarkably, ConvBERTbase model achieves 86.4 GLUE score, 0.7 higher than ELECTRAbase, while
|
||||
using less than 1/4 training cost. Code and pre-trained models will be released.*
|
||||
|
||||
ConvBERT training tips are similar to those of BERT.
|
||||
|
||||
This model was contributed by [abhishek](https://huggingface.co/abhishek). The original implementation can be found
|
||||
here: https://github.com/yitu-opensource/ConvBert
|
||||
|
||||
## ConvBertConfig
|
||||
|
||||
[[autodoc]] ConvBertConfig
|
||||
|
||||
## ConvBertTokenizer
|
||||
|
||||
[[autodoc]] ConvBertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## ConvBertTokenizerFast
|
||||
|
||||
[[autodoc]] ConvBertTokenizerFast
|
||||
|
||||
## ConvBertModel
|
||||
|
||||
[[autodoc]] ConvBertModel
|
||||
- forward
|
||||
|
||||
## ConvBertForMaskedLM
|
||||
|
||||
[[autodoc]] ConvBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## ConvBertForSequenceClassification
|
||||
|
||||
[[autodoc]] ConvBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## ConvBertForMultipleChoice
|
||||
|
||||
[[autodoc]] ConvBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## ConvBertForTokenClassification
|
||||
|
||||
[[autodoc]] ConvBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## ConvBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] ConvBertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFConvBertModel
|
||||
|
||||
[[autodoc]] TFConvBertModel
|
||||
- call
|
||||
|
||||
## TFConvBertForMaskedLM
|
||||
|
||||
[[autodoc]] TFConvBertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFConvBertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFConvBertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFConvBertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFConvBertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFConvBertForTokenClassification
|
||||
|
||||
[[autodoc]] TFConvBertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFConvBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFConvBertForQuestionAnswering
|
||||
- call
|
||||
74
docs/source/en/model_doc/convnext.mdx
Normal file
74
docs/source/en/model_doc/convnext.mdx
Normal file
@@ -0,0 +1,74 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ConvNeXT
|
||||
|
||||
## Overview
|
||||
|
||||
The ConvNeXT model was proposed in [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) by Zhuang Liu, Hanzi Mao, Chao-Yuan Wu, Christoph Feichtenhofer, Trevor Darrell, Saining Xie.
|
||||
ConvNeXT is a pure convolutional model (ConvNet), inspired by the design of Vision Transformers, that claims to outperform them.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*The "Roaring 20s" of visual recognition began with the introduction of Vision Transformers (ViTs), which quickly superseded ConvNets as the state-of-the-art image classification model.
|
||||
A vanilla ViT, on the other hand, faces difficulties when applied to general computer vision tasks such as object detection and semantic segmentation. It is the hierarchical Transformers
|
||||
(e.g., Swin Transformers) that reintroduced several ConvNet priors, making Transformers practically viable as a generic vision backbone and demonstrating remarkable performance on a wide
|
||||
variety of vision tasks. However, the effectiveness of such hybrid approaches is still largely credited to the intrinsic superiority of Transformers, rather than the inherent inductive
|
||||
biases of convolutions. In this work, we reexamine the design spaces and test the limits of what a pure ConvNet can achieve. We gradually "modernize" a standard ResNet toward the design
|
||||
of a vision Transformer, and discover several key components that contribute to the performance difference along the way. The outcome of this exploration is a family of pure ConvNet models
|
||||
dubbed ConvNeXt. Constructed entirely from standard ConvNet modules, ConvNeXts compete favorably with Transformers in terms of accuracy and scalability, achieving 87.8% ImageNet top-1 accuracy
|
||||
and outperforming Swin Transformers on COCO detection and ADE20K segmentation, while maintaining the simplicity and efficiency of standard ConvNets.*
|
||||
|
||||
Tips:
|
||||
|
||||
- See the code examples below each model regarding usage.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/convnext_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> ConvNeXT architecture. Taken from the <a href="https://arxiv.org/abs/2201.03545">original paper</a>.</small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). TensorFlow version of the model was contributed by [ariG23498](https://github.com/ariG23498),
|
||||
[gante](https://github.com/gante), and [sayakpaul](https://github.com/sayakpaul) (equal contribution). The original code can be found [here](https://github.com/facebookresearch/ConvNeXt).
|
||||
|
||||
## ConvNextConfig
|
||||
|
||||
[[autodoc]] ConvNextConfig
|
||||
|
||||
|
||||
## ConvNextFeatureExtractor
|
||||
|
||||
[[autodoc]] ConvNextFeatureExtractor
|
||||
|
||||
|
||||
## ConvNextModel
|
||||
|
||||
[[autodoc]] ConvNextModel
|
||||
- forward
|
||||
|
||||
|
||||
## ConvNextForImageClassification
|
||||
|
||||
[[autodoc]] ConvNextForImageClassification
|
||||
- forward
|
||||
|
||||
|
||||
## TFConvNextModel
|
||||
|
||||
[[autodoc]] TFConvNextModel
|
||||
- call
|
||||
|
||||
|
||||
## TFConvNextForImageClassification
|
||||
|
||||
[[autodoc]] TFConvNextForImageClassification
|
||||
- call
|
||||
40
docs/source/en/model_doc/cpm.mdx
Normal file
40
docs/source/en/model_doc/cpm.mdx
Normal file
@@ -0,0 +1,40 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# CPM
|
||||
|
||||
## Overview
|
||||
|
||||
The CPM model was proposed in [CPM: A Large-scale Generative Chinese Pre-trained Language Model](https://arxiv.org/abs/2012.00413) by Zhengyan Zhang, Xu Han, Hao Zhou, Pei Ke, Yuxian Gu, Deming Ye, Yujia Qin,
|
||||
Yusheng Su, Haozhe Ji, Jian Guan, Fanchao Qi, Xiaozhi Wang, Yanan Zheng, Guoyang Zeng, Huanqi Cao, Shengqi Chen,
|
||||
Daixuan Li, Zhenbo Sun, Zhiyuan Liu, Minlie Huang, Wentao Han, Jie Tang, Juanzi Li, Xiaoyan Zhu, Maosong Sun.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pre-trained Language Models (PLMs) have proven to be beneficial for various downstream NLP tasks. Recently, GPT-3,
|
||||
with 175 billion parameters and 570GB training data, drew a lot of attention due to the capacity of few-shot (even
|
||||
zero-shot) learning. However, applying GPT-3 to address Chinese NLP tasks is still challenging, as the training corpus
|
||||
of GPT-3 is primarily English, and the parameters are not publicly available. In this technical report, we release the
|
||||
Chinese Pre-trained Language Model (CPM) with generative pre-training on large-scale Chinese training data. To the best
|
||||
of our knowledge, CPM, with 2.6 billion parameters and 100GB Chinese training data, is the largest Chinese pre-trained
|
||||
language model, which could facilitate several downstream Chinese NLP tasks, such as conversation, essay generation,
|
||||
cloze test, and language understanding. Extensive experiments demonstrate that CPM achieves strong performance on many
|
||||
NLP tasks in the settings of few-shot (even zero-shot) learning.*
|
||||
|
||||
This model was contributed by [canwenxu](https://huggingface.co/canwenxu). The original implementation can be found
|
||||
here: https://github.com/TsinghuaAI/CPM-Generate
|
||||
|
||||
Note: We only have a tokenizer here, since the model architecture is the same as GPT-2.
|
||||
|
||||
## CpmTokenizer
|
||||
|
||||
[[autodoc]] CpmTokenizer
|
||||
87
docs/source/en/model_doc/ctrl.mdx
Normal file
87
docs/source/en/model_doc/ctrl.mdx
Normal file
@@ -0,0 +1,87 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# CTRL
|
||||
|
||||
## Overview
|
||||
|
||||
CTRL model was proposed in [CTRL: A Conditional Transformer Language Model for Controllable Generation](https://arxiv.org/abs/1909.05858) by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and
|
||||
Richard Socher. It's a causal (unidirectional) transformer pre-trained using language modeling on a very large corpus
|
||||
of ~140 GB of text data with the first token reserved as a control code (such as Links, Books, Wikipedia etc.).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Large-scale language models show promising text generation capabilities, but users cannot easily control particular
|
||||
aspects of the generated text. We release CTRL, a 1.63 billion-parameter conditional transformer language model,
|
||||
trained to condition on control codes that govern style, content, and task-specific behavior. Control codes were
|
||||
derived from structure that naturally co-occurs with raw text, preserving the advantages of unsupervised learning while
|
||||
providing more explicit control over text generation. These codes also allow CTRL to predict which parts of the
|
||||
training data are most likely given a sequence. This provides a potential method for analyzing large amounts of data
|
||||
via model-based source attribution.*
|
||||
|
||||
Tips:
|
||||
|
||||
- CTRL makes use of control codes to generate text: it requires generations to be started by certain words, sentences
|
||||
or links to generate coherent text. Refer to the [original implementation](https://github.com/salesforce/ctrl) for
|
||||
more information.
|
||||
- CTRL is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than
|
||||
the left.
|
||||
- CTRL was trained with a causal language modeling (CLM) objective and is therefore powerful at predicting the next
|
||||
token in a sequence. Leveraging this feature allows CTRL to generate syntactically coherent text as it can be
|
||||
observed in the *run_generation.py* example script.
|
||||
- The PyTorch models can take the `past_key_values` as input, which is the previously computed key/value attention pairs.
|
||||
TensorFlow models accepts `past` as input. Using the `past_key_values` value prevents the model from re-computing
|
||||
pre-computed values in the context of text generation. See the [`forward`](model_doc/ctrl#transformers.CTRLModel.forward)
|
||||
method for more information on the usage of this argument.
|
||||
|
||||
This model was contributed by [keskarnitishr](https://huggingface.co/keskarnitishr). The original code can be found
|
||||
[here](https://github.com/salesforce/ctrl).
|
||||
|
||||
|
||||
## CTRLConfig
|
||||
|
||||
[[autodoc]] CTRLConfig
|
||||
|
||||
## CTRLTokenizer
|
||||
|
||||
[[autodoc]] CTRLTokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## CTRLModel
|
||||
|
||||
[[autodoc]] CTRLModel
|
||||
- forward
|
||||
|
||||
## CTRLLMHeadModel
|
||||
|
||||
[[autodoc]] CTRLLMHeadModel
|
||||
- forward
|
||||
|
||||
## CTRLForSequenceClassification
|
||||
|
||||
[[autodoc]] CTRLForSequenceClassification
|
||||
- forward
|
||||
|
||||
## TFCTRLModel
|
||||
|
||||
[[autodoc]] TFCTRLModel
|
||||
- call
|
||||
|
||||
## TFCTRLLMHeadModel
|
||||
|
||||
[[autodoc]] TFCTRLLMHeadModel
|
||||
- call
|
||||
|
||||
## TFCTRLForSequenceClassification
|
||||
|
||||
[[autodoc]] TFCTRLForSequenceClassification
|
||||
- call
|
||||
110
docs/source/en/model_doc/data2vec.mdx
Normal file
110
docs/source/en/model_doc/data2vec.mdx
Normal file
@@ -0,0 +1,110 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Data2Vec
|
||||
|
||||
## Overview
|
||||
|
||||
The Data2Vec model was proposed in [data2vec: A General Framework for Self-supervised Learning in Speech, Vision and Language](https://arxiv.org/pdf/2202.03555) by Alexei Baevski, Wei-Ning Hsu, Qiantong Xu, Arun Babu, Jiatao Gu and Michael Auli.
|
||||
Data2Vec proposes a unified framework for self-supervised learning across different data modalities - text, audio and images.
|
||||
Importantly, predicted targets for pre-training are contextualized latent representations of the inputs, rather than modality-specific, context-independent targets.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*While the general idea of self-supervised learning is identical across modalities, the actual algorithms and
|
||||
objectives differ widely because they were developed with a single modality in mind. To get us closer to general
|
||||
self-supervised learning, we present data2vec, a framework that uses the same learning method for either speech,
|
||||
NLP or computer vision. The core idea is to predict latent representations of the full input data based on a
|
||||
masked view of the input in a selfdistillation setup using a standard Transformer architecture.
|
||||
Instead of predicting modality-specific targets such as words, visual tokens or units of human speech which
|
||||
are local in nature, data2vec predicts contextualized latent representations that contain information from
|
||||
the entire input. Experiments on the major benchmarks of speech recognition, image classification, and
|
||||
natural language understanding demonstrate a new state of the art or competitive performance to predominant approaches.
|
||||
Models and code are available at www.github.com/pytorch/fairseq/tree/master/examples/data2vec.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Both Data2VecAudio and Data2VecText have been trained using the same self-supervised learning method.
|
||||
In the case of Data2VecAudio, preprocessing is identical to [`RobertaModel`], including tokenization.
|
||||
|
||||
This model was contributed by [edugp](https://huggingface.co/edugp).
|
||||
The original code can be found [here](https://github.com/pytorch/fairseq/tree/main/examples/data2vec).
|
||||
|
||||
|
||||
## Data2VecTextConfig
|
||||
|
||||
[[autodoc]] Data2VecTextConfig
|
||||
|
||||
## Data2VecAudioConfig
|
||||
|
||||
[[autodoc]] Data2VecAudioConfig
|
||||
|
||||
## Data2VecAudioModel
|
||||
|
||||
[[autodoc]] Data2VecAudioModel
|
||||
- forward
|
||||
|
||||
|
||||
## Data2VecAudioForAudioFrameClassification
|
||||
|
||||
[[autodoc]] Data2VecAudioForAudioFrameClassification
|
||||
- forward
|
||||
|
||||
## Data2VecAudioForCTC
|
||||
|
||||
[[autodoc]] Data2VecAudioForCTC
|
||||
- forward
|
||||
|
||||
## Data2VecAudioForSequenceClassification
|
||||
|
||||
[[autodoc]] Data2VecAudioForSequenceClassification
|
||||
- forward
|
||||
|
||||
## Data2VecAudioForXVector
|
||||
|
||||
[[autodoc]] Data2VecAudioForXVector
|
||||
- forward
|
||||
|
||||
## Data2VecTextModel
|
||||
|
||||
[[autodoc]] Data2VecTextModel
|
||||
- forward
|
||||
|
||||
## Data2VecTextForCausalLM
|
||||
|
||||
[[autodoc]] Data2VecTextForCausalLM
|
||||
- forward
|
||||
|
||||
## Data2VecTextForMaskedLM
|
||||
|
||||
[[autodoc]] Data2VecTextForMaskedLM
|
||||
- forward
|
||||
|
||||
## Data2VecTextForSequenceClassification
|
||||
|
||||
[[autodoc]] Data2VecTextForSequenceClassification
|
||||
- forward
|
||||
|
||||
## Data2VecTextForMultipleChoice
|
||||
|
||||
[[autodoc]] Data2VecTextForMultipleChoice
|
||||
- forward
|
||||
|
||||
## Data2VecTextForTokenClassification
|
||||
|
||||
[[autodoc]] Data2VecTextForTokenClassification
|
||||
- forward
|
||||
|
||||
## Data2VecTextForQuestionAnswering
|
||||
|
||||
[[autodoc]] Data2VecTextForQuestionAnswering
|
||||
- forward
|
||||
132
docs/source/en/model_doc/deberta-v2.mdx
Normal file
132
docs/source/en/model_doc/deberta-v2.mdx
Normal file
@@ -0,0 +1,132 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DeBERTa-v2
|
||||
|
||||
## Overview
|
||||
|
||||
The DeBERTa model was proposed in [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen It is based on Google's
|
||||
BERT model released in 2018 and Facebook's RoBERTa model released in 2019.
|
||||
|
||||
It builds on RoBERTa with disentangled attention and enhanced mask decoder training with half of the data used in
|
||||
RoBERTa.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recent progress in pre-trained neural language models has significantly improved the performance of many natural
|
||||
language processing (NLP) tasks. In this paper we propose a new model architecture DeBERTa (Decoding-enhanced BERT with
|
||||
disentangled attention) that improves the BERT and RoBERTa models using two novel techniques. The first is the
|
||||
disentangled attention mechanism, where each word is represented using two vectors that encode its content and
|
||||
position, respectively, and the attention weights among words are computed using disentangled matrices on their
|
||||
contents and relative positions. Second, an enhanced mask decoder is used to replace the output softmax layer to
|
||||
predict the masked tokens for model pretraining. We show that these two techniques significantly improve the efficiency
|
||||
of model pretraining and performance of downstream tasks. Compared to RoBERTa-Large, a DeBERTa model trained on half of
|
||||
the training data performs consistently better on a wide range of NLP tasks, achieving improvements on MNLI by +0.9%
|
||||
(90.2% vs. 91.1%), on SQuAD v2.0 by +2.3% (88.4% vs. 90.7%) and RACE by +3.6% (83.2% vs. 86.8%). The DeBERTa code and
|
||||
pre-trained models will be made publicly available at https://github.com/microsoft/DeBERTa.*
|
||||
|
||||
|
||||
The following information is visible directly on the [original implementation
|
||||
repository](https://github.com/microsoft/DeBERTa). DeBERTa v2 is the second version of the DeBERTa model. It includes
|
||||
the 1.5B model used for the SuperGLUE single-model submission and achieving 89.9, versus human baseline 89.8. You can
|
||||
find more details about this submission in the authors'
|
||||
[blog](https://www.microsoft.com/en-us/research/blog/microsoft-deberta-surpasses-human-performance-on-the-superglue-benchmark/)
|
||||
|
||||
New in v2:
|
||||
|
||||
- **Vocabulary** In v2 the tokenizer is changed to use a new vocabulary of size 128K built from the training data.
|
||||
Instead of a GPT2-based tokenizer, the tokenizer is now
|
||||
[sentencepiece-based](https://github.com/google/sentencepiece) tokenizer.
|
||||
- **nGiE(nGram Induced Input Encoding)** The DeBERTa-v2 model uses an additional convolution layer aside with the first
|
||||
transformer layer to better learn the local dependency of input tokens.
|
||||
- **Sharing position projection matrix with content projection matrix in attention layer** Based on previous
|
||||
experiments, this can save parameters without affecting the performance.
|
||||
- **Apply bucket to encode relative positions** The DeBERTa-v2 model uses log bucket to encode relative positions
|
||||
similar to T5.
|
||||
- **900M model & 1.5B model** Two additional model sizes are available: 900M and 1.5B, which significantly improves the
|
||||
performance of downstream tasks.
|
||||
|
||||
This model was contributed by [DeBERTa](https://huggingface.co/DeBERTa). This model TF 2.0 implementation was
|
||||
contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/microsoft/DeBERTa).
|
||||
|
||||
|
||||
## DebertaV2Config
|
||||
|
||||
[[autodoc]] DebertaV2Config
|
||||
|
||||
## DebertaV2Tokenizer
|
||||
|
||||
[[autodoc]] DebertaV2Tokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## DebertaV2Model
|
||||
|
||||
[[autodoc]] DebertaV2Model
|
||||
- forward
|
||||
|
||||
## DebertaV2PreTrainedModel
|
||||
|
||||
[[autodoc]] DebertaV2PreTrainedModel
|
||||
- forward
|
||||
|
||||
## DebertaV2ForMaskedLM
|
||||
|
||||
[[autodoc]] DebertaV2ForMaskedLM
|
||||
- forward
|
||||
|
||||
## DebertaV2ForSequenceClassification
|
||||
|
||||
[[autodoc]] DebertaV2ForSequenceClassification
|
||||
- forward
|
||||
|
||||
## DebertaV2ForTokenClassification
|
||||
|
||||
[[autodoc]] DebertaV2ForTokenClassification
|
||||
- forward
|
||||
|
||||
## DebertaV2ForQuestionAnswering
|
||||
|
||||
[[autodoc]] DebertaV2ForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFDebertaV2Model
|
||||
|
||||
[[autodoc]] TFDebertaV2Model
|
||||
- call
|
||||
|
||||
## TFDebertaV2PreTrainedModel
|
||||
|
||||
[[autodoc]] TFDebertaV2PreTrainedModel
|
||||
- call
|
||||
|
||||
## TFDebertaV2ForMaskedLM
|
||||
|
||||
[[autodoc]] TFDebertaV2ForMaskedLM
|
||||
- call
|
||||
|
||||
## TFDebertaV2ForSequenceClassification
|
||||
|
||||
[[autodoc]] TFDebertaV2ForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFDebertaV2ForTokenClassification
|
||||
|
||||
[[autodoc]] TFDebertaV2ForTokenClassification
|
||||
- call
|
||||
|
||||
## TFDebertaV2ForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFDebertaV2ForQuestionAnswering
|
||||
- call
|
||||
117
docs/source/en/model_doc/deberta.mdx
Normal file
117
docs/source/en/model_doc/deberta.mdx
Normal file
@@ -0,0 +1,117 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DeBERTa
|
||||
|
||||
## Overview
|
||||
|
||||
The DeBERTa model was proposed in [DeBERTa: Decoding-enhanced BERT with Disentangled Attention](https://arxiv.org/abs/2006.03654) by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen It is based on Google's
|
||||
BERT model released in 2018 and Facebook's RoBERTa model released in 2019.
|
||||
|
||||
It builds on RoBERTa with disentangled attention and enhanced mask decoder training with half of the data used in
|
||||
RoBERTa.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recent progress in pre-trained neural language models has significantly improved the performance of many natural
|
||||
language processing (NLP) tasks. In this paper we propose a new model architecture DeBERTa (Decoding-enhanced BERT with
|
||||
disentangled attention) that improves the BERT and RoBERTa models using two novel techniques. The first is the
|
||||
disentangled attention mechanism, where each word is represented using two vectors that encode its content and
|
||||
position, respectively, and the attention weights among words are computed using disentangled matrices on their
|
||||
contents and relative positions. Second, an enhanced mask decoder is used to replace the output softmax layer to
|
||||
predict the masked tokens for model pretraining. We show that these two techniques significantly improve the efficiency
|
||||
of model pretraining and performance of downstream tasks. Compared to RoBERTa-Large, a DeBERTa model trained on half of
|
||||
the training data performs consistently better on a wide range of NLP tasks, achieving improvements on MNLI by +0.9%
|
||||
(90.2% vs. 91.1%), on SQuAD v2.0 by +2.3% (88.4% vs. 90.7%) and RACE by +3.6% (83.2% vs. 86.8%). The DeBERTa code and
|
||||
pre-trained models will be made publicly available at https://github.com/microsoft/DeBERTa.*
|
||||
|
||||
|
||||
This model was contributed by [DeBERTa](https://huggingface.co/DeBERTa). This model TF 2.0 implementation was
|
||||
contributed by [kamalkraj](https://huggingface.co/kamalkraj) . The original code can be found [here](https://github.com/microsoft/DeBERTa).
|
||||
|
||||
|
||||
## DebertaConfig
|
||||
|
||||
[[autodoc]] DebertaConfig
|
||||
|
||||
## DebertaTokenizer
|
||||
|
||||
[[autodoc]] DebertaTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## DebertaTokenizerFast
|
||||
|
||||
[[autodoc]] DebertaTokenizerFast
|
||||
- build_inputs_with_special_tokens
|
||||
- create_token_type_ids_from_sequences
|
||||
|
||||
## DebertaModel
|
||||
|
||||
[[autodoc]] DebertaModel
|
||||
- forward
|
||||
|
||||
## DebertaPreTrainedModel
|
||||
|
||||
[[autodoc]] DebertaPreTrainedModel
|
||||
|
||||
## DebertaForMaskedLM
|
||||
|
||||
[[autodoc]] DebertaForMaskedLM
|
||||
- forward
|
||||
|
||||
## DebertaForSequenceClassification
|
||||
|
||||
[[autodoc]] DebertaForSequenceClassification
|
||||
- forward
|
||||
|
||||
## DebertaForTokenClassification
|
||||
|
||||
[[autodoc]] DebertaForTokenClassification
|
||||
- forward
|
||||
|
||||
## DebertaForQuestionAnswering
|
||||
|
||||
[[autodoc]] DebertaForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFDebertaModel
|
||||
|
||||
[[autodoc]] TFDebertaModel
|
||||
- call
|
||||
|
||||
## TFDebertaPreTrainedModel
|
||||
|
||||
[[autodoc]] TFDebertaPreTrainedModel
|
||||
- call
|
||||
|
||||
## TFDebertaForMaskedLM
|
||||
|
||||
[[autodoc]] TFDebertaForMaskedLM
|
||||
- call
|
||||
|
||||
## TFDebertaForSequenceClassification
|
||||
|
||||
[[autodoc]] TFDebertaForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFDebertaForTokenClassification
|
||||
|
||||
[[autodoc]] TFDebertaForTokenClassification
|
||||
- call
|
||||
|
||||
## TFDebertaForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFDebertaForQuestionAnswering
|
||||
- call
|
||||
51
docs/source/en/model_doc/decision_transformer.mdx
Normal file
51
docs/source/en/model_doc/decision_transformer.mdx
Normal file
@@ -0,0 +1,51 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Decision Transformer
|
||||
|
||||
## Overview
|
||||
|
||||
The Decision Transformer model was proposed in [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://arxiv.org/abs/2106.01345)
|
||||
by Lili Chen, Kevin Lu, Aravind Rajeswaran, Kimin Lee, Aditya Grover, Michael Laskin, Pieter Abbeel, Aravind Srinivas, Igor Mordatch.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We introduce a framework that abstracts Reinforcement Learning (RL) as a sequence modeling problem.
|
||||
This allows us to draw upon the simplicity and scalability of the Transformer architecture, and associated advances
|
||||
in language modeling such as GPT-x and BERT. In particular, we present Decision Transformer, an architecture that
|
||||
casts the problem of RL as conditional sequence modeling. Unlike prior approaches to RL that fit value functions or
|
||||
compute policy gradients, Decision Transformer simply outputs the optimal actions by leveraging a causally masked
|
||||
Transformer. By conditioning an autoregressive model on the desired return (reward), past states, and actions, our
|
||||
Decision Transformer model can generate future actions that achieve the desired return. Despite its simplicity,
|
||||
Decision Transformer matches or exceeds the performance of state-of-the-art model-free offline RL baselines on
|
||||
Atari, OpenAI Gym, and Key-to-Door tasks.*
|
||||
|
||||
Tips:
|
||||
|
||||
This version of the model is for tasks where the state is a vector, image-based states will come soon.
|
||||
|
||||
This model was contributed by [edbeeching](https://huggingface.co/edbeeching). The original code can be found [here](https://github.com/kzl/decision-transformer).
|
||||
|
||||
## DecisionTransformerConfig
|
||||
|
||||
[[autodoc]] DecisionTransformerConfig
|
||||
|
||||
|
||||
## DecisionTransformerGPT2Model
|
||||
|
||||
[[autodoc]] DecisionTransformerGPT2Model
|
||||
- forward
|
||||
|
||||
## DecisionTransformerModel
|
||||
|
||||
[[autodoc]] DecisionTransformerModel
|
||||
- forward
|
||||
102
docs/source/en/model_doc/deit.mdx
Normal file
102
docs/source/en/model_doc/deit.mdx
Normal file
@@ -0,0 +1,102 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DeiT
|
||||
|
||||
<Tip>
|
||||
|
||||
This is a recently introduced model so the API hasn't been tested extensively. There may be some bugs or slight
|
||||
breaking changes to fix it in the future. If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title).
|
||||
|
||||
</Tip>
|
||||
|
||||
## Overview
|
||||
|
||||
The DeiT model was proposed in [Training data-efficient image transformers & distillation through attention](https://arxiv.org/abs/2012.12877) by Hugo Touvron, Matthieu Cord, Matthijs Douze, Francisco Massa, Alexandre
|
||||
Sablayrolles, Hervé Jégou. The [Vision Transformer (ViT)](vit) introduced in [Dosovitskiy et al., 2020](https://arxiv.org/abs/2010.11929) has shown that one can match or even outperform existing convolutional neural
|
||||
networks using a Transformer encoder (BERT-like). However, the ViT models introduced in that paper required training on
|
||||
expensive infrastructure for multiple weeks, using external data. DeiT (data-efficient image transformers) are more
|
||||
efficiently trained transformers for image classification, requiring far less data and far less computing resources
|
||||
compared to the original ViT models.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recently, neural networks purely based on attention were shown to address image understanding tasks such as image
|
||||
classification. However, these visual transformers are pre-trained with hundreds of millions of images using an
|
||||
expensive infrastructure, thereby limiting their adoption. In this work, we produce a competitive convolution-free
|
||||
transformer by training on Imagenet only. We train them on a single computer in less than 3 days. Our reference vision
|
||||
transformer (86M parameters) achieves top-1 accuracy of 83.1% (single-crop evaluation) on ImageNet with no external
|
||||
data. More importantly, we introduce a teacher-student strategy specific to transformers. It relies on a distillation
|
||||
token ensuring that the student learns from the teacher through attention. We show the interest of this token-based
|
||||
distillation, especially when using a convnet as a teacher. This leads us to report results competitive with convnets
|
||||
for both Imagenet (where we obtain up to 85.2% accuracy) and when transferring to other tasks. We share our code and
|
||||
models.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Compared to ViT, DeiT models use a so-called distillation token to effectively learn from a teacher (which, in the
|
||||
DeiT paper, is a ResNet like-model). The distillation token is learned through backpropagation, by interacting with
|
||||
the class ([CLS]) and patch tokens through the self-attention layers.
|
||||
- There are 2 ways to fine-tune distilled models, either (1) in a classic way, by only placing a prediction head on top
|
||||
of the final hidden state of the class token and not using the distillation signal, or (2) by placing both a
|
||||
prediction head on top of the class token and on top of the distillation token. In that case, the [CLS] prediction
|
||||
head is trained using regular cross-entropy between the prediction of the head and the ground-truth label, while the
|
||||
distillation prediction head is trained using hard distillation (cross-entropy between the prediction of the
|
||||
distillation head and the label predicted by the teacher). At inference time, one takes the average prediction
|
||||
between both heads as final prediction. (2) is also called "fine-tuning with distillation", because one relies on a
|
||||
teacher that has already been fine-tuned on the downstream dataset. In terms of models, (1) corresponds to
|
||||
[`DeiTForImageClassification`] and (2) corresponds to
|
||||
[`DeiTForImageClassificationWithTeacher`].
|
||||
- Note that the authors also did try soft distillation for (2) (in which case the distillation prediction head is
|
||||
trained using KL divergence to match the softmax output of the teacher), but hard distillation gave the best results.
|
||||
- All released checkpoints were pre-trained and fine-tuned on ImageNet-1k only. No external data was used. This is in
|
||||
contrast with the original ViT model, which used external data like the JFT-300M dataset/Imagenet-21k for
|
||||
pre-training.
|
||||
- The authors of DeiT also released more efficiently trained ViT models, which you can directly plug into
|
||||
[`ViTModel`] or [`ViTForImageClassification`]. Techniques like data
|
||||
augmentation, optimization, and regularization were used in order to simulate training on a much larger dataset
|
||||
(while only using ImageNet-1k for pre-training). There are 4 variants available (in 3 different sizes):
|
||||
*facebook/deit-tiny-patch16-224*, *facebook/deit-small-patch16-224*, *facebook/deit-base-patch16-224* and
|
||||
*facebook/deit-base-patch16-384*. Note that one should use [`DeiTFeatureExtractor`] in order to
|
||||
prepare images for the model.
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr).
|
||||
|
||||
|
||||
## DeiTConfig
|
||||
|
||||
[[autodoc]] DeiTConfig
|
||||
|
||||
## DeiTFeatureExtractor
|
||||
|
||||
[[autodoc]] DeiTFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## DeiTModel
|
||||
|
||||
[[autodoc]] DeiTModel
|
||||
- forward
|
||||
|
||||
## DeiTForMaskedImageModeling
|
||||
|
||||
[[autodoc]] DeiTForMaskedImageModeling
|
||||
- forward
|
||||
|
||||
## DeiTForImageClassification
|
||||
|
||||
[[autodoc]] DeiTForImageClassification
|
||||
- forward
|
||||
|
||||
## DeiTForImageClassificationWithTeacher
|
||||
|
||||
[[autodoc]] DeiTForImageClassificationWithTeacher
|
||||
- forward
|
||||
169
docs/source/en/model_doc/detr.mdx
Normal file
169
docs/source/en/model_doc/detr.mdx
Normal file
@@ -0,0 +1,169 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DETR
|
||||
|
||||
## Overview
|
||||
|
||||
The DETR model was proposed in [End-to-End Object Detection with Transformers](https://arxiv.org/abs/2005.12872) by
|
||||
Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov and Sergey Zagoruyko. DETR
|
||||
consists of a convolutional backbone followed by an encoder-decoder Transformer which can be trained end-to-end for
|
||||
object detection. It greatly simplifies a lot of the complexity of models like Faster-R-CNN and Mask-R-CNN, which use
|
||||
things like region proposals, non-maximum suppression procedure and anchor generation. Moreover, DETR can also be
|
||||
naturally extended to perform panoptic segmentation, by simply adding a mask head on top of the decoder outputs.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present a new method that views object detection as a direct set prediction problem. Our approach streamlines the
|
||||
detection pipeline, effectively removing the need for many hand-designed components like a non-maximum suppression
|
||||
procedure or anchor generation that explicitly encode our prior knowledge about the task. The main ingredients of the
|
||||
new framework, called DEtection TRansformer or DETR, are a set-based global loss that forces unique predictions via
|
||||
bipartite matching, and a transformer encoder-decoder architecture. Given a fixed small set of learned object queries,
|
||||
DETR reasons about the relations of the objects and the global image context to directly output the final set of
|
||||
predictions in parallel. The new model is conceptually simple and does not require a specialized library, unlike many
|
||||
other modern detectors. DETR demonstrates accuracy and run-time performance on par with the well-established and
|
||||
highly-optimized Faster RCNN baseline on the challenging COCO object detection dataset. Moreover, DETR can be easily
|
||||
generalized to produce panoptic segmentation in a unified manner. We show that it significantly outperforms competitive
|
||||
baselines.*
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/facebookresearch/detr).
|
||||
|
||||
The quickest way to get started with DETR is by checking the [example notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR) (which showcase both inference and
|
||||
fine-tuning on custom data).
|
||||
|
||||
Here's a TLDR explaining how [`~transformers.DetrForObjectDetection`] works:
|
||||
|
||||
First, an image is sent through a pre-trained convolutional backbone (in the paper, the authors use
|
||||
ResNet-50/ResNet-101). Let's assume we also add a batch dimension. This means that the input to the backbone is a
|
||||
tensor of shape `(batch_size, 3, height, width)`, assuming the image has 3 color channels (RGB). The CNN backbone
|
||||
outputs a new lower-resolution feature map, typically of shape `(batch_size, 2048, height/32, width/32)`. This is
|
||||
then projected to match the hidden dimension of the Transformer of DETR, which is `256` by default, using a
|
||||
`nn.Conv2D` layer. So now, we have a tensor of shape `(batch_size, 256, height/32, width/32).` Next, the
|
||||
feature map is flattened and transposed to obtain a tensor of shape `(batch_size, seq_len, d_model)` =
|
||||
`(batch_size, width/32*height/32, 256)`. So a difference with NLP models is that the sequence length is actually
|
||||
longer than usual, but with a smaller `d_model` (which in NLP is typically 768 or higher).
|
||||
|
||||
Next, this is sent through the encoder, outputting `encoder_hidden_states` of the same shape (you can consider
|
||||
these as image features). Next, so-called **object queries** are sent through the decoder. This is a tensor of shape
|
||||
`(batch_size, num_queries, d_model)`, with `num_queries` typically set to 100 and initialized with zeros.
|
||||
These input embeddings are learnt positional encodings that the authors refer to as object queries, and similarly to
|
||||
the encoder, they are added to the input of each attention layer. Each object query will look for a particular object
|
||||
in the image. The decoder updates these embeddings through multiple self-attention and encoder-decoder attention layers
|
||||
to output `decoder_hidden_states` of the same shape: `(batch_size, num_queries, d_model)`. Next, two heads
|
||||
are added on top for object detection: a linear layer for classifying each object query into one of the objects or "no
|
||||
object", and a MLP to predict bounding boxes for each query.
|
||||
|
||||
The model is trained using a **bipartite matching loss**: so what we actually do is compare the predicted classes +
|
||||
bounding boxes of each of the N = 100 object queries to the ground truth annotations, padded up to the same length N
|
||||
(so if an image only contains 4 objects, 96 annotations will just have a "no object" as class and "no bounding box" as
|
||||
bounding box). The [Hungarian matching algorithm](https://en.wikipedia.org/wiki/Hungarian_algorithm) is used to find
|
||||
an optimal one-to-one mapping of each of the N queries to each of the N annotations. Next, standard cross-entropy (for
|
||||
the classes) and a linear combination of the L1 and [generalized IoU loss](https://giou.stanford.edu/) (for the
|
||||
bounding boxes) are used to optimize the parameters of the model.
|
||||
|
||||
DETR can be naturally extended to perform panoptic segmentation (which unifies semantic segmentation and instance
|
||||
segmentation). [`~transformers.DetrForSegmentation`] adds a segmentation mask head on top of
|
||||
[`~transformers.DetrForObjectDetection`]. The mask head can be trained either jointly, or in a two steps process,
|
||||
where one first trains a [`~transformers.DetrForObjectDetection`] model to detect bounding boxes around both
|
||||
"things" (instances) and "stuff" (background things like trees, roads, sky), then freeze all the weights and train only
|
||||
the mask head for 25 epochs. Experimentally, these two approaches give similar results. Note that predicting boxes is
|
||||
required for the training to be possible, since the Hungarian matching is computed using distances between boxes.
|
||||
|
||||
Tips:
|
||||
|
||||
- DETR uses so-called **object queries** to detect objects in an image. The number of queries determines the maximum
|
||||
number of objects that can be detected in a single image, and is set to 100 by default (see parameter
|
||||
`num_queries` of [`~transformers.DetrConfig`]). Note that it's good to have some slack (in COCO, the
|
||||
authors used 100, while the maximum number of objects in a COCO image is ~70).
|
||||
- The decoder of DETR updates the query embeddings in parallel. This is different from language models like GPT-2,
|
||||
which use autoregressive decoding instead of parallel. Hence, no causal attention mask is used.
|
||||
- DETR adds position embeddings to the hidden states at each self-attention and cross-attention layer before projecting
|
||||
to queries and keys. For the position embeddings of the image, one can choose between fixed sinusoidal or learned
|
||||
absolute position embeddings. By default, the parameter `position_embedding_type` of
|
||||
[`~transformers.DetrConfig`] is set to `"sine"`.
|
||||
- During training, the authors of DETR did find it helpful to use auxiliary losses in the decoder, especially to help
|
||||
the model output the correct number of objects of each class. If you set the parameter `auxiliary_loss` of
|
||||
[`~transformers.DetrConfig`] to `True`, then prediction feedforward neural networks and Hungarian losses
|
||||
are added after each decoder layer (with the FFNs sharing parameters).
|
||||
- If you want to train the model in a distributed environment across multiple nodes, then one should update the
|
||||
_num_boxes_ variable in the _DetrLoss_ class of _modeling_detr.py_. When training on multiple nodes, this should be
|
||||
set to the average number of target boxes across all nodes, as can be seen in the original implementation [here](https://github.com/facebookresearch/detr/blob/a54b77800eb8e64e3ad0d8237789fcbf2f8350c5/models/detr.py#L227-L232).
|
||||
- [`~transformers.DetrForObjectDetection`] and [`~transformers.DetrForSegmentation`] can be initialized with
|
||||
any convolutional backbone available in the [timm library](https://github.com/rwightman/pytorch-image-models).
|
||||
Initializing with a MobileNet backbone for example can be done by setting the `backbone` attribute of
|
||||
[`~transformers.DetrConfig`] to `"tf_mobilenetv3_small_075"`, and then initializing the model with that
|
||||
config.
|
||||
- DETR resizes the input images such that the shortest side is at least a certain amount of pixels while the longest is
|
||||
at most 1333 pixels. At training time, scale augmentation is used such that the shortest side is randomly set to at
|
||||
least 480 and at most 800 pixels. At inference time, the shortest side is set to 800. One can use
|
||||
[`~transformers.DetrFeatureExtractor`] to prepare images (and optional annotations in COCO format) for the
|
||||
model. Due to this resizing, images in a batch can have different sizes. DETR solves this by padding images up to the
|
||||
largest size in a batch, and by creating a pixel mask that indicates which pixels are real/which are padding.
|
||||
Alternatively, one can also define a custom `collate_fn` in order to batch images together, using
|
||||
[`~transformers.DetrFeatureExtractor.pad_and_create_pixel_mask`].
|
||||
- The size of the images will determine the amount of memory being used, and will thus determine the `batch_size`.
|
||||
It is advised to use a batch size of 2 per GPU. See [this Github thread](https://github.com/facebookresearch/detr/issues/150) for more info.
|
||||
|
||||
As a summary, consider the following table:
|
||||
|
||||
| Task | Object detection | Instance segmentation | Panoptic segmentation |
|
||||
|------|------------------|-----------------------|-----------------------|
|
||||
| **Description** | Predicting bounding boxes and class labels around objects in an image | Predicting masks around objects (i.e. instances) in an image | Predicting masks around both objects (i.e. instances) as well as "stuff" (i.e. background things like trees and roads) in an image |
|
||||
| **Model** | [`~transformers.DetrForObjectDetection`] | [`~transformers.DetrForSegmentation`] | [`~transformers.DetrForSegmentation`] |
|
||||
| **Example dataset** | COCO detection | COCO detection, COCO panoptic | COCO panoptic | |
|
||||
| **Format of annotations to provide to** [`~transformers.DetrFeatureExtractor`] | {'image_id': `int`, 'annotations': `List[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `List[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} and masks_path (path to directory containing PNG files of the masks) |
|
||||
| **Postprocessing** (i.e. converting the output of the model to COCO API) | [`~transformers.DetrFeatureExtractor.post_process`] | [`~transformers.DetrFeatureExtractor.post_process_segmentation`] | [`~transformers.DetrFeatureExtractor.post_process_segmentation`], [`~transformers.DetrFeatureExtractor.post_process_panoptic`] |
|
||||
| **evaluators** | `CocoEvaluator` with `iou_types="bbox"` | `CocoEvaluator` with `iou_types="bbox"` or `"segm"` | `CocoEvaluator` with `iou_tupes="bbox"` or `"segm"`, `PanopticEvaluator` |
|
||||
|
||||
In short, one should prepare the data either in COCO detection or COCO panoptic format, then use
|
||||
[`~transformers.DetrFeatureExtractor`] to create `pixel_values`, `pixel_mask` and optional
|
||||
`labels`, which can then be used to train (or fine-tune) a model. For evaluation, one should first convert the
|
||||
outputs of the model using one of the postprocessing methods of [`~transformers.DetrFeatureExtractor`]. These can
|
||||
be be provided to either `CocoEvaluator` or `PanopticEvaluator`, which allow you to calculate metrics like
|
||||
mean Average Precision (mAP) and Panoptic Quality (PQ). The latter objects are implemented in the [original repository](https://github.com/facebookresearch/detr). See the [example notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/DETR) for more info regarding evaluation.
|
||||
|
||||
|
||||
## DETR specific outputs
|
||||
|
||||
[[autodoc]] models.detr.modeling_detr.DetrModelOutput
|
||||
|
||||
[[autodoc]] models.detr.modeling_detr.DetrObjectDetectionOutput
|
||||
|
||||
[[autodoc]] models.detr.modeling_detr.DetrSegmentationOutput
|
||||
|
||||
## DetrConfig
|
||||
|
||||
[[autodoc]] DetrConfig
|
||||
|
||||
## DetrFeatureExtractor
|
||||
|
||||
[[autodoc]] DetrFeatureExtractor
|
||||
- __call__
|
||||
- pad_and_create_pixel_mask
|
||||
- post_process
|
||||
- post_process_segmentation
|
||||
- post_process_panoptic
|
||||
|
||||
## DetrModel
|
||||
|
||||
[[autodoc]] DetrModel
|
||||
- forward
|
||||
|
||||
## DetrForObjectDetection
|
||||
|
||||
[[autodoc]] DetrForObjectDetection
|
||||
- forward
|
||||
|
||||
## DetrForSegmentation
|
||||
|
||||
[[autodoc]] DetrForSegmentation
|
||||
- forward
|
||||
49
docs/source/en/model_doc/dialogpt.mdx
Normal file
49
docs/source/en/model_doc/dialogpt.mdx
Normal file
@@ -0,0 +1,49 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DialoGPT
|
||||
|
||||
## Overview
|
||||
|
||||
DialoGPT was proposed in [DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation](https://arxiv.org/abs/1911.00536) by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao,
|
||||
Jianfeng Gao, Jingjing Liu, Bill Dolan. It's a GPT2 Model trained on 147M conversation-like exchanges extracted from
|
||||
Reddit.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present a large, tunable neural conversational response generation model, DialoGPT (dialogue generative pre-trained
|
||||
transformer). Trained on 147M conversation-like exchanges extracted from Reddit comment chains over a period spanning
|
||||
from 2005 through 2017, DialoGPT extends the Hugging Face PyTorch transformer to attain a performance close to human
|
||||
both in terms of automatic and human evaluation in single-turn dialogue settings. We show that conversational systems
|
||||
that leverage DialoGPT generate more relevant, contentful and context-consistent responses than strong baseline
|
||||
systems. The pre-trained model and training pipeline are publicly released to facilitate research into neural response
|
||||
generation and the development of more intelligent open-domain dialogue systems.*
|
||||
|
||||
Tips:
|
||||
|
||||
- DialoGPT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather
|
||||
than the left.
|
||||
- DialoGPT was trained with a causal language modeling (CLM) objective on conversational data and is therefore powerful
|
||||
at response generation in open-domain dialogue systems.
|
||||
- DialoGPT enables the user to create a chat bot in just 10 lines of code as shown on [DialoGPT's model card](https://huggingface.co/microsoft/DialoGPT-medium).
|
||||
|
||||
Training:
|
||||
|
||||
In order to train or fine-tune DialoGPT, one can use causal language modeling training. To cite the official paper: *We
|
||||
follow the OpenAI GPT-2 to model a multiturn dialogue session as a long text and frame the generation task as language
|
||||
modeling. We first concatenate all dialog turns within a dialogue session into a long text x_1,..., x_N (N is the
|
||||
sequence length), ended by the end-of-text token.* For more information please confer to the original paper.
|
||||
|
||||
|
||||
DialoGPT's architecture is based on the GPT2 model, so one can refer to [GPT2's documentation page](gpt2).
|
||||
|
||||
The original code can be found [here](https://github.com/microsoft/DialoGPT).
|
||||
149
docs/source/en/model_doc/distilbert.mdx
Normal file
149
docs/source/en/model_doc/distilbert.mdx
Normal file
@@ -0,0 +1,149 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DistilBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The DistilBERT model was proposed in the blog post [Smaller, faster, cheaper, lighter: Introducing DistilBERT, a
|
||||
distilled version of BERT](https://medium.com/huggingface/distilbert-8cf3380435b5), and the paper [DistilBERT, a
|
||||
distilled version of BERT: smaller, faster, cheaper and lighter](https://arxiv.org/abs/1910.01108). DistilBERT is a
|
||||
small, fast, cheap and light Transformer model trained by distilling BERT base. It has 40% less parameters than
|
||||
*bert-base-uncased*, runs 60% faster while preserving over 95% of BERT's performances as measured on the GLUE language
|
||||
understanding benchmark.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*As Transfer Learning from large-scale pre-trained models becomes more prevalent in Natural Language Processing (NLP),
|
||||
operating these large models in on-the-edge and/or under constrained computational training or inference budgets
|
||||
remains challenging. In this work, we propose a method to pre-train a smaller general-purpose language representation
|
||||
model, called DistilBERT, which can then be fine-tuned with good performances on a wide range of tasks like its larger
|
||||
counterparts. While most prior work investigated the use of distillation for building task-specific models, we leverage
|
||||
knowledge distillation during the pretraining phase and show that it is possible to reduce the size of a BERT model by
|
||||
40%, while retaining 97% of its language understanding capabilities and being 60% faster. To leverage the inductive
|
||||
biases learned by larger models during pretraining, we introduce a triple loss combining language modeling,
|
||||
distillation and cosine-distance losses. Our smaller, faster and lighter model is cheaper to pre-train and we
|
||||
demonstrate its capabilities for on-device computations in a proof-of-concept experiment and a comparative on-device
|
||||
study.*
|
||||
|
||||
Tips:
|
||||
|
||||
- DistilBERT doesn't have `token_type_ids`, you don't need to indicate which token belongs to which segment. Just
|
||||
separate your segments with the separation token `tokenizer.sep_token` (or `[SEP]`).
|
||||
- DistilBERT doesn't have options to select the input positions (`position_ids` input). This could be added if
|
||||
necessary though, just let us know if you need this option.
|
||||
|
||||
This model was contributed by [victorsanh](https://huggingface.co/victorsanh). This model jax version was
|
||||
contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation).
|
||||
|
||||
|
||||
## DistilBertConfig
|
||||
|
||||
[[autodoc]] DistilBertConfig
|
||||
|
||||
## DistilBertTokenizer
|
||||
|
||||
[[autodoc]] DistilBertTokenizer
|
||||
|
||||
## DistilBertTokenizerFast
|
||||
|
||||
[[autodoc]] DistilBertTokenizerFast
|
||||
|
||||
## DistilBertModel
|
||||
|
||||
[[autodoc]] DistilBertModel
|
||||
- forward
|
||||
|
||||
## DistilBertForMaskedLM
|
||||
|
||||
[[autodoc]] DistilBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## DistilBertForSequenceClassification
|
||||
|
||||
[[autodoc]] DistilBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## DistilBertForMultipleChoice
|
||||
|
||||
[[autodoc]] DistilBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## DistilBertForTokenClassification
|
||||
|
||||
[[autodoc]] DistilBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## DistilBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] DistilBertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFDistilBertModel
|
||||
|
||||
[[autodoc]] TFDistilBertModel
|
||||
- call
|
||||
|
||||
## TFDistilBertForMaskedLM
|
||||
|
||||
[[autodoc]] TFDistilBertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFDistilBertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFDistilBertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFDistilBertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFDistilBertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFDistilBertForTokenClassification
|
||||
|
||||
[[autodoc]] TFDistilBertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFDistilBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFDistilBertForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxDistilBertModel
|
||||
|
||||
[[autodoc]] FlaxDistilBertModel
|
||||
- __call__
|
||||
|
||||
## FlaxDistilBertForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxDistilBertForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxDistilBertForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxDistilBertForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxDistilBertForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxDistilBertForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxDistilBertForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxDistilBertForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxDistilBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxDistilBertForQuestionAnswering
|
||||
- __call__
|
||||
67
docs/source/en/model_doc/dit.mdx
Normal file
67
docs/source/en/model_doc/dit.mdx
Normal file
@@ -0,0 +1,67 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DiT
|
||||
|
||||
## Overview
|
||||
|
||||
DiT was proposed in [DiT: Self-supervised Pre-training for Document Image Transformer](https://arxiv.org/abs/2203.02378) by Junlong Li, Yiheng Xu, Tengchao Lv, Lei Cui, Cha Zhang, Furu Wei.
|
||||
DiT applies the self-supervised objective of [BEiT](beit) (BERT pre-training of Image Transformers) to 42 million document images, allowing for state-of-the-art results on tasks including:
|
||||
|
||||
- document image classification: the [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/) dataset (a collection of
|
||||
400,000 images belonging to one of 16 classes).
|
||||
- document layout analysis: the [PubLayNet](https://github.com/ibm-aur-nlp/PubLayNet) dataset (a collection of more
|
||||
than 360,000 document images constructed by automatically parsing PubMed XML files).
|
||||
- table detection: the [ICDAR 2019 cTDaR](https://github.com/cndplab-founder/ICDAR2019_cTDaR) dataset (a collection of
|
||||
600 training images and 240 testing images).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Image Transformer has recently achieved significant progress for natural image understanding, either using supervised (ViT, DeiT, etc.) or self-supervised (BEiT, MAE, etc.) pre-training techniques. In this paper, we propose DiT, a self-supervised pre-trained Document Image Transformer model using large-scale unlabeled text images for Document AI tasks, which is essential since no supervised counterparts ever exist due to the lack of human labeled document images. We leverage DiT as the backbone network in a variety of vision-based Document AI tasks, including document image classification, document layout analysis, as well as table detection. Experiment results have illustrated that the self-supervised pre-trained DiT model achieves new state-of-the-art results on these downstream tasks, e.g. document image classification (91.11 → 92.69), document layout analysis (91.0 → 94.9) and table detection (94.23 → 96.55). *
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/dit_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> Summary of the approach. Taken from the [original paper](https://arxiv.org/abs/2203.02378). </small>
|
||||
|
||||
One can directly use the weights of DiT with the AutoModel API:
|
||||
|
||||
```python
|
||||
from transformers import AutoModel
|
||||
|
||||
model = AutoModel.from_pretrained("microsoft/dit-base")
|
||||
```
|
||||
|
||||
This will load the model pre-trained on masked image modeling. Note that this won't include the language modeling head on top, used to predict visual tokens.
|
||||
|
||||
To include the head, you can load the weights into a `BeitForMaskedImageModeling` model, like so:
|
||||
|
||||
```python
|
||||
from transformers import BeitForMaskedImageModeling
|
||||
|
||||
model = BeitForMaskedImageModeling.from_pretrained("microsoft/dit-base")
|
||||
```
|
||||
|
||||
You can also load a fine-tuned model from the [hub](https://huggingface.co/models?other=dit), like so:
|
||||
|
||||
```python
|
||||
from transformers import AutoModelForImageClassification
|
||||
|
||||
model = AutoModelForImageClassification.from_pretrained("microsoft/dit-base-finetuned-rvlcdip")
|
||||
```
|
||||
|
||||
This particular checkpoint was fine-tuned on [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/), an important benchmark for document image classification.
|
||||
A notebook that illustrates inference for document image classification can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/DiT/Inference_with_DiT_(Document_Image_Transformer)_for_document_image_classification.ipynb).
|
||||
|
||||
As DiT's architecture is equivalent to that of BEiT, one can refer to [BEiT's documentation page](beit) for all tips, code examples and notebooks.
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/microsoft/unilm/tree/master/dit).
|
||||
98
docs/source/en/model_doc/dpr.mdx
Normal file
98
docs/source/en/model_doc/dpr.mdx
Normal file
@@ -0,0 +1,98 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DPR
|
||||
|
||||
## Overview
|
||||
|
||||
Dense Passage Retrieval (DPR) is a set of tools and models for state-of-the-art open-domain Q&A research. It was
|
||||
introduced in [Dense Passage Retrieval for Open-Domain Question Answering](https://arxiv.org/abs/2004.04906) by
|
||||
Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, Wen-tau Yih.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Open-domain question answering relies on efficient passage retrieval to select candidate contexts, where traditional
|
||||
sparse vector space models, such as TF-IDF or BM25, are the de facto method. In this work, we show that retrieval can
|
||||
be practically implemented using dense representations alone, where embeddings are learned from a small number of
|
||||
questions and passages by a simple dual-encoder framework. When evaluated on a wide range of open-domain QA datasets,
|
||||
our dense retriever outperforms a strong Lucene-BM25 system largely by 9%-19% absolute in terms of top-20 passage
|
||||
retrieval accuracy, and helps our end-to-end QA system establish new state-of-the-art on multiple open-domain QA
|
||||
benchmarks.*
|
||||
|
||||
This model was contributed by [lhoestq](https://huggingface.co/lhoestq). The original code can be found [here](https://github.com/facebookresearch/DPR).
|
||||
|
||||
|
||||
## DPRConfig
|
||||
|
||||
[[autodoc]] DPRConfig
|
||||
|
||||
## DPRContextEncoderTokenizer
|
||||
|
||||
[[autodoc]] DPRContextEncoderTokenizer
|
||||
|
||||
## DPRContextEncoderTokenizerFast
|
||||
|
||||
[[autodoc]] DPRContextEncoderTokenizerFast
|
||||
|
||||
## DPRQuestionEncoderTokenizer
|
||||
|
||||
[[autodoc]] DPRQuestionEncoderTokenizer
|
||||
|
||||
## DPRQuestionEncoderTokenizerFast
|
||||
|
||||
[[autodoc]] DPRQuestionEncoderTokenizerFast
|
||||
|
||||
## DPRReaderTokenizer
|
||||
|
||||
[[autodoc]] DPRReaderTokenizer
|
||||
|
||||
## DPRReaderTokenizerFast
|
||||
|
||||
[[autodoc]] DPRReaderTokenizerFast
|
||||
|
||||
## DPR specific outputs
|
||||
|
||||
[[autodoc]] models.dpr.modeling_dpr.DPRContextEncoderOutput
|
||||
|
||||
[[autodoc]] models.dpr.modeling_dpr.DPRQuestionEncoderOutput
|
||||
|
||||
[[autodoc]] models.dpr.modeling_dpr.DPRReaderOutput
|
||||
|
||||
## DPRContextEncoder
|
||||
|
||||
[[autodoc]] DPRContextEncoder
|
||||
- forward
|
||||
|
||||
## DPRQuestionEncoder
|
||||
|
||||
[[autodoc]] DPRQuestionEncoder
|
||||
- forward
|
||||
|
||||
## DPRReader
|
||||
|
||||
[[autodoc]] DPRReader
|
||||
- forward
|
||||
|
||||
## TFDPRContextEncoder
|
||||
|
||||
[[autodoc]] TFDPRContextEncoder
|
||||
- call
|
||||
|
||||
## TFDPRQuestionEncoder
|
||||
|
||||
[[autodoc]] TFDPRQuestionEncoder
|
||||
- call
|
||||
|
||||
## TFDPRReader
|
||||
|
||||
[[autodoc]] TFDPRReader
|
||||
- call
|
||||
57
docs/source/en/model_doc/dpt.mdx
Normal file
57
docs/source/en/model_doc/dpt.mdx
Normal file
@@ -0,0 +1,57 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# DPT
|
||||
|
||||
## Overview
|
||||
|
||||
The DPT model was proposed in [Vision Transformers for Dense Prediction](https://arxiv.org/abs/2103.13413) by René Ranftl, Alexey Bochkovskiy, Vladlen Koltun.
|
||||
DPT is a model that leverages the [Vision Transformer (ViT)](vit) as backbone for dense prediction tasks like semantic segmentation and depth estimation.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We introduce dense vision transformers, an architecture that leverages vision transformers in place of convolutional networks as a backbone for dense prediction tasks. We assemble tokens from various stages of the vision transformer into image-like representations at various resolutions and progressively combine them into full-resolution predictions using a convolutional decoder. The transformer backbone processes representations at a constant and relatively high resolution and has a global receptive field at every stage. These properties allow the dense vision transformer to provide finer-grained and more globally coherent predictions when compared to fully-convolutional networks. Our experiments show that this architecture yields substantial improvements on dense prediction tasks, especially when a large amount of training data is available. For monocular depth estimation, we observe an improvement of up to 28% in relative performance when compared to a state-of-the-art fully-convolutional network. When applied to semantic segmentation, dense vision transformers set a new state of the art on ADE20K with 49.02% mIoU. We further show that the architecture can be fine-tuned on smaller datasets such as NYUv2, KITTI, and Pascal Context where it also sets the new state of the art.*
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/dpt_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> DPT architecture. Taken from the <a href="https://arxiv.org/abs/2103.13413" target="_blank">original paper</a>. </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/isl-org/DPT).
|
||||
|
||||
## DPTConfig
|
||||
|
||||
[[autodoc]] DPTConfig
|
||||
|
||||
|
||||
## DPTFeatureExtractor
|
||||
|
||||
[[autodoc]] DPTFeatureExtractor
|
||||
- __call__
|
||||
|
||||
|
||||
## DPTModel
|
||||
|
||||
[[autodoc]] DPTModel
|
||||
- forward
|
||||
|
||||
|
||||
## DPTForDepthEstimation
|
||||
|
||||
[[autodoc]] DPTForDepthEstimation
|
||||
- forward
|
||||
|
||||
|
||||
## DPTForSemanticSegmentation
|
||||
|
||||
[[autodoc]] DPTForSemanticSegmentation
|
||||
- forward
|
||||
184
docs/source/en/model_doc/electra.mdx
Normal file
184
docs/source/en/model_doc/electra.mdx
Normal file
@@ -0,0 +1,184 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ELECTRA
|
||||
|
||||
## Overview
|
||||
|
||||
The ELECTRA model was proposed in the paper [ELECTRA: Pre-training Text Encoders as Discriminators Rather Than
|
||||
Generators](https://openreview.net/pdf?id=r1xMH1BtvB). ELECTRA is a new pretraining approach which trains two
|
||||
transformer models: the generator and the discriminator. The generator's role is to replace tokens in a sequence, and
|
||||
is therefore trained as a masked language model. The discriminator, which is the model we're interested in, tries to
|
||||
identify which tokens were replaced by the generator in the sequence.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Masked language modeling (MLM) pretraining methods such as BERT corrupt the input by replacing some tokens with [MASK]
|
||||
and then train a model to reconstruct the original tokens. While they produce good results when transferred to
|
||||
downstream NLP tasks, they generally require large amounts of compute to be effective. As an alternative, we propose a
|
||||
more sample-efficient pretraining task called replaced token detection. Instead of masking the input, our approach
|
||||
corrupts it by replacing some tokens with plausible alternatives sampled from a small generator network. Then, instead
|
||||
of training a model that predicts the original identities of the corrupted tokens, we train a discriminative model that
|
||||
predicts whether each token in the corrupted input was replaced by a generator sample or not. Thorough experiments
|
||||
demonstrate this new pretraining task is more efficient than MLM because the task is defined over all input tokens
|
||||
rather than just the small subset that was masked out. As a result, the contextual representations learned by our
|
||||
approach substantially outperform the ones learned by BERT given the same model size, data, and compute. The gains are
|
||||
particularly strong for small models; for example, we train a model on one GPU for 4 days that outperforms GPT (trained
|
||||
using 30x more compute) on the GLUE natural language understanding benchmark. Our approach also works well at scale,
|
||||
where it performs comparably to RoBERTa and XLNet while using less than 1/4 of their compute and outperforms them when
|
||||
using the same amount of compute.*
|
||||
|
||||
Tips:
|
||||
|
||||
- ELECTRA is the pretraining approach, therefore there is nearly no changes done to the underlying model: BERT. The
|
||||
only change is the separation of the embedding size and the hidden size: the embedding size is generally smaller,
|
||||
while the hidden size is larger. An additional projection layer (linear) is used to project the embeddings from their
|
||||
embedding size to the hidden size. In the case where the embedding size is the same as the hidden size, no projection
|
||||
layer is used.
|
||||
- The ELECTRA checkpoints saved using [Google Research's implementation](https://github.com/google-research/electra)
|
||||
contain both the generator and discriminator. The conversion script requires the user to name which model to export
|
||||
into the correct architecture. Once converted to the HuggingFace format, these checkpoints may be loaded into all
|
||||
available ELECTRA models, however. This means that the discriminator may be loaded in the
|
||||
[`ElectraForMaskedLM`] model, and the generator may be loaded in the
|
||||
[`ElectraForPreTraining`] model (the classification head will be randomly initialized as it
|
||||
doesn't exist in the generator).
|
||||
|
||||
This model was contributed by [lysandre](https://huggingface.co/lysandre). The original code can be found [here](https://github.com/google-research/electra).
|
||||
|
||||
|
||||
## ElectraConfig
|
||||
|
||||
[[autodoc]] ElectraConfig
|
||||
|
||||
## ElectraTokenizer
|
||||
|
||||
[[autodoc]] ElectraTokenizer
|
||||
|
||||
## ElectraTokenizerFast
|
||||
|
||||
[[autodoc]] ElectraTokenizerFast
|
||||
|
||||
## Electra specific outputs
|
||||
|
||||
[[autodoc]] models.electra.modeling_electra.ElectraForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.electra.modeling_tf_electra.TFElectraForPreTrainingOutput
|
||||
|
||||
## ElectraModel
|
||||
|
||||
[[autodoc]] ElectraModel
|
||||
- forward
|
||||
|
||||
## ElectraForPreTraining
|
||||
|
||||
[[autodoc]] ElectraForPreTraining
|
||||
- forward
|
||||
|
||||
## ElectraForCausalLM
|
||||
|
||||
[[autodoc]] ElectraForCausalLM
|
||||
- forward
|
||||
|
||||
## ElectraForMaskedLM
|
||||
|
||||
[[autodoc]] ElectraForMaskedLM
|
||||
- forward
|
||||
|
||||
## ElectraForSequenceClassification
|
||||
|
||||
[[autodoc]] ElectraForSequenceClassification
|
||||
- forward
|
||||
|
||||
## ElectraForMultipleChoice
|
||||
|
||||
[[autodoc]] ElectraForMultipleChoice
|
||||
- forward
|
||||
|
||||
## ElectraForTokenClassification
|
||||
|
||||
[[autodoc]] ElectraForTokenClassification
|
||||
- forward
|
||||
|
||||
## ElectraForQuestionAnswering
|
||||
|
||||
[[autodoc]] ElectraForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFElectraModel
|
||||
|
||||
[[autodoc]] TFElectraModel
|
||||
- call
|
||||
|
||||
## TFElectraForPreTraining
|
||||
|
||||
[[autodoc]] TFElectraForPreTraining
|
||||
- call
|
||||
|
||||
## TFElectraForMaskedLM
|
||||
|
||||
[[autodoc]] TFElectraForMaskedLM
|
||||
- call
|
||||
|
||||
## TFElectraForSequenceClassification
|
||||
|
||||
[[autodoc]] TFElectraForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFElectraForMultipleChoice
|
||||
|
||||
[[autodoc]] TFElectraForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFElectraForTokenClassification
|
||||
|
||||
[[autodoc]] TFElectraForTokenClassification
|
||||
- call
|
||||
|
||||
## TFElectraForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFElectraForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxElectraModel
|
||||
|
||||
[[autodoc]] FlaxElectraModel
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForPreTraining
|
||||
|
||||
[[autodoc]] FlaxElectraForPreTraining
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxElectraForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxElectraForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxElectraForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxElectraForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxElectraForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxElectraForQuestionAnswering
|
||||
- __call__
|
||||
68
docs/source/en/model_doc/encoder-decoder.mdx
Normal file
68
docs/source/en/model_doc/encoder-decoder.mdx
Normal file
@@ -0,0 +1,68 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Encoder Decoder Models
|
||||
|
||||
The [`EncoderDecoderModel`] can be used to initialize a sequence-to-sequence model with any
|
||||
pretrained autoencoding model as the encoder and any pretrained autoregressive model as the decoder.
|
||||
|
||||
The effectiveness of initializing sequence-to-sequence models with pretrained checkpoints for sequence generation tasks
|
||||
was shown in [Leveraging Pre-trained Checkpoints for Sequence Generation Tasks](https://arxiv.org/abs/1907.12461) by
|
||||
Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
|
||||
|
||||
After such an [`EncoderDecoderModel`] has been trained/fine-tuned, it can be saved/loaded just like
|
||||
any other models (see the examples for more information).
|
||||
|
||||
An application of this architecture could be to leverage two pretrained [`BertModel`] as the encoder
|
||||
and decoder for a summarization model as was shown in: [Text Summarization with Pretrained Encoders](https://arxiv.org/abs/1908.08345) by Yang Liu and Mirella Lapata.
|
||||
|
||||
The [`~TFEncoderDecoderModel.from_pretrained`] currently doesn't support initializing the model from a
|
||||
pytorch checkpoint. Passing `from_pt=True` to this method will throw an exception. If there are only pytorch
|
||||
checkpoints for a particular encoder-decoder model, a workaround is:
|
||||
|
||||
```python
|
||||
>>> # a workaround to load from pytorch checkpoint
|
||||
>>> _model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert-cnn_dailymail-fp16")
|
||||
>>> _model.encoder.save_pretrained("./encoder")
|
||||
>>> _model.decoder.save_pretrained("./decoder")
|
||||
>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained(
|
||||
... "./encoder", "./decoder", encoder_from_pt=True, decoder_from_pt=True
|
||||
... )
|
||||
>>> # This is only for copying some specific attributes of this particular model.
|
||||
>>> model.config = _model.config
|
||||
```
|
||||
|
||||
This model was contributed by [thomwolf](https://github.com/thomwolf). This model's TensorFlow and Flax versions
|
||||
were contributed by [ydshieh](https://github.com/ydshieh).
|
||||
|
||||
|
||||
## EncoderDecoderConfig
|
||||
|
||||
[[autodoc]] EncoderDecoderConfig
|
||||
|
||||
## EncoderDecoderModel
|
||||
|
||||
[[autodoc]] EncoderDecoderModel
|
||||
- forward
|
||||
- from_encoder_decoder_pretrained
|
||||
|
||||
## TFEncoderDecoderModel
|
||||
|
||||
[[autodoc]] TFEncoderDecoderModel
|
||||
- call
|
||||
- from_encoder_decoder_pretrained
|
||||
|
||||
## FlaxEncoderDecoderModel
|
||||
|
||||
[[autodoc]] FlaxEncoderDecoderModel
|
||||
- __call__
|
||||
- from_encoder_decoder_pretrained
|
||||
109
docs/source/en/model_doc/flaubert.mdx
Normal file
109
docs/source/en/model_doc/flaubert.mdx
Normal file
@@ -0,0 +1,109 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# FlauBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The FlauBERT model was proposed in the paper [FlauBERT: Unsupervised Language Model Pre-training for French](https://arxiv.org/abs/1912.05372) by Hang Le et al. It's a transformer model pretrained using a masked language
|
||||
modeling (MLM) objective (like BERT).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Language models have become a key step to achieve state-of-the art results in many different Natural Language
|
||||
Processing (NLP) tasks. Leveraging the huge amount of unlabeled texts nowadays available, they provide an efficient way
|
||||
to pre-train continuous word representations that can be fine-tuned for a downstream task, along with their
|
||||
contextualization at the sentence level. This has been widely demonstrated for English using contextualized
|
||||
representations (Dai and Le, 2015; Peters et al., 2018; Howard and Ruder, 2018; Radford et al., 2018; Devlin et al.,
|
||||
2019; Yang et al., 2019b). In this paper, we introduce and share FlauBERT, a model learned on a very large and
|
||||
heterogeneous French corpus. Models of different sizes are trained using the new CNRS (French National Centre for
|
||||
Scientific Research) Jean Zay supercomputer. We apply our French language models to diverse NLP tasks (text
|
||||
classification, paraphrasing, natural language inference, parsing, word sense disambiguation) and show that most of the
|
||||
time they outperform other pretraining approaches. Different versions of FlauBERT as well as a unified evaluation
|
||||
protocol for the downstream tasks, called FLUE (French Language Understanding Evaluation), are shared to the research
|
||||
community for further reproducible experiments in French NLP.*
|
||||
|
||||
This model was contributed by [formiel](https://huggingface.co/formiel). The original code can be found [here](https://github.com/getalp/Flaubert).
|
||||
|
||||
|
||||
## FlaubertConfig
|
||||
|
||||
[[autodoc]] FlaubertConfig
|
||||
|
||||
## FlaubertTokenizer
|
||||
|
||||
[[autodoc]] FlaubertTokenizer
|
||||
|
||||
## FlaubertModel
|
||||
|
||||
[[autodoc]] FlaubertModel
|
||||
- forward
|
||||
|
||||
## FlaubertWithLMHeadModel
|
||||
|
||||
[[autodoc]] FlaubertWithLMHeadModel
|
||||
- forward
|
||||
|
||||
## FlaubertForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaubertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## FlaubertForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaubertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## FlaubertForTokenClassification
|
||||
|
||||
[[autodoc]] FlaubertForTokenClassification
|
||||
- forward
|
||||
|
||||
## FlaubertForQuestionAnsweringSimple
|
||||
|
||||
[[autodoc]] FlaubertForQuestionAnsweringSimple
|
||||
- forward
|
||||
|
||||
## FlaubertForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaubertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFFlaubertModel
|
||||
|
||||
[[autodoc]] TFFlaubertModel
|
||||
- call
|
||||
|
||||
## TFFlaubertWithLMHeadModel
|
||||
|
||||
[[autodoc]] TFFlaubertWithLMHeadModel
|
||||
- call
|
||||
|
||||
## TFFlaubertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFFlaubertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFFlaubertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFFlaubertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFFlaubertForTokenClassification
|
||||
|
||||
[[autodoc]] TFFlaubertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFFlaubertForQuestionAnsweringSimple
|
||||
|
||||
[[autodoc]] TFFlaubertForQuestionAnsweringSimple
|
||||
- call
|
||||
98
docs/source/en/model_doc/fnet.mdx
Normal file
98
docs/source/en/model_doc/fnet.mdx
Normal file
@@ -0,0 +1,98 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# FNet
|
||||
|
||||
## Overview
|
||||
|
||||
The FNet model was proposed in [FNet: Mixing Tokens with Fourier Transforms](https://arxiv.org/abs/2105.03824) by
|
||||
James Lee-Thorp, Joshua Ainslie, Ilya Eckstein, Santiago Ontanon. The model replaces the self-attention layer in a BERT
|
||||
model with a fourier transform which returns only the real parts of the transform. The model is significantly faster
|
||||
than the BERT model because it has fewer parameters and is more memory efficient. The model achieves about 92-97%
|
||||
accuracy of BERT counterparts on GLUE benchmark, and trains much faster than the BERT model. The abstract from the
|
||||
paper is the following:
|
||||
|
||||
*We show that Transformer encoder architectures can be sped up, with limited accuracy costs, by replacing the
|
||||
self-attention sublayers with simple linear transformations that "mix" input tokens. These linear mixers, along with
|
||||
standard nonlinearities in feed-forward layers, prove competent at modeling semantic relationships in several text
|
||||
classification tasks. Most surprisingly, we find that replacing the self-attention sublayer in a Transformer encoder
|
||||
with a standard, unparameterized Fourier Transform achieves 92-97% of the accuracy of BERT counterparts on the GLUE
|
||||
benchmark, but trains 80% faster on GPUs and 70% faster on TPUs at standard 512 input lengths. At longer input lengths,
|
||||
our FNet model is significantly faster: when compared to the "efficient" Transformers on the Long Range Arena
|
||||
benchmark, FNet matches the accuracy of the most accurate models, while outpacing the fastest models across all
|
||||
sequence lengths on GPUs (and across relatively shorter lengths on TPUs). Finally, FNet has a light memory footprint
|
||||
and is particularly efficient at smaller model sizes; for a fixed speed and accuracy budget, small FNet models
|
||||
outperform Transformer counterparts.*
|
||||
|
||||
Tips on usage:
|
||||
|
||||
- The model was trained without an attention mask as it is based on Fourier Transform. The model was trained with
|
||||
maximum sequence length 512 which includes pad tokens. Hence, it is highly recommended to use the same maximum
|
||||
sequence length for fine-tuning and inference.
|
||||
|
||||
This model was contributed by [gchhablani](https://huggingface.co/gchhablani). The original code can be found [here](https://github.com/google-research/google-research/tree/master/f_net).
|
||||
|
||||
## FNetConfig
|
||||
|
||||
[[autodoc]] FNetConfig
|
||||
|
||||
## FNetTokenizer
|
||||
|
||||
[[autodoc]] FNetTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## FNetTokenizerFast
|
||||
|
||||
[[autodoc]] FNetTokenizerFast
|
||||
|
||||
## FNetModel
|
||||
|
||||
[[autodoc]] FNetModel
|
||||
- forward
|
||||
|
||||
## FNetForPreTraining
|
||||
|
||||
[[autodoc]] FNetForPreTraining
|
||||
- forward
|
||||
|
||||
## FNetForMaskedLM
|
||||
|
||||
[[autodoc]] FNetForMaskedLM
|
||||
- forward
|
||||
|
||||
## FNetForNextSentencePrediction
|
||||
|
||||
[[autodoc]] FNetForNextSentencePrediction
|
||||
- forward
|
||||
|
||||
## FNetForSequenceClassification
|
||||
|
||||
[[autodoc]] FNetForSequenceClassification
|
||||
- forward
|
||||
|
||||
## FNetForMultipleChoice
|
||||
|
||||
[[autodoc]] FNetForMultipleChoice
|
||||
- forward
|
||||
|
||||
## FNetForTokenClassification
|
||||
|
||||
[[autodoc]] FNetForTokenClassification
|
||||
- forward
|
||||
|
||||
## FNetForQuestionAnswering
|
||||
|
||||
[[autodoc]] FNetForQuestionAnswering
|
||||
- forward
|
||||
63
docs/source/en/model_doc/fsmt.mdx
Normal file
63
docs/source/en/model_doc/fsmt.mdx
Normal file
@@ -0,0 +1,63 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# FSMT
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
|
||||
@stas00.
|
||||
|
||||
## Overview
|
||||
|
||||
FSMT (FairSeq MachineTranslation) models were introduced in [Facebook FAIR's WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616) by Nathan Ng, Kyra Yee, Alexei Baevski, Myle Ott, Michael Auli, Sergey Edunov.
|
||||
|
||||
The abstract of the paper is the following:
|
||||
|
||||
*This paper describes Facebook FAIR's submission to the WMT19 shared news translation task. We participate in two
|
||||
language pairs and four language directions, English <-> German and English <-> Russian. Following our submission from
|
||||
last year, our baseline systems are large BPE-based transformer models trained with the Fairseq sequence modeling
|
||||
toolkit which rely on sampled back-translations. This year we experiment with different bitext data filtering schemes,
|
||||
as well as with adding filtered back-translated data. We also ensemble and fine-tune our models on domain-specific
|
||||
data, then decode using noisy channel model reranking. Our submissions are ranked first in all four directions of the
|
||||
human evaluation campaign. On En->De, our system significantly outperforms other systems as well as human translations.
|
||||
This system improves upon our WMT'18 submission by 4.5 BLEU points.*
|
||||
|
||||
This model was contributed by [stas](https://huggingface.co/stas). The original code can be found
|
||||
[here](https://github.com/pytorch/fairseq/tree/master/examples/wmt19).
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- FSMT uses source and target vocabulary pairs that aren't combined into one. It doesn't share embeddings tokens
|
||||
either. Its tokenizer is very similar to [`XLMTokenizer`] and the main model is derived from
|
||||
[`BartModel`].
|
||||
|
||||
|
||||
## FSMTConfig
|
||||
|
||||
[[autodoc]] FSMTConfig
|
||||
|
||||
## FSMTTokenizer
|
||||
|
||||
[[autodoc]] FSMTTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## FSMTModel
|
||||
|
||||
[[autodoc]] FSMTModel
|
||||
- forward
|
||||
|
||||
## FSMTForConditionalGeneration
|
||||
|
||||
[[autodoc]] FSMTForConditionalGeneration
|
||||
- forward
|
||||
153
docs/source/en/model_doc/funnel.mdx
Normal file
153
docs/source/en/model_doc/funnel.mdx
Normal file
@@ -0,0 +1,153 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Funnel Transformer
|
||||
|
||||
## Overview
|
||||
|
||||
The Funnel Transformer model was proposed in the paper [Funnel-Transformer: Filtering out Sequential Redundancy for
|
||||
Efficient Language Processing](https://arxiv.org/abs/2006.03236). It is a bidirectional transformer model, like
|
||||
BERT, but with a pooling operation after each block of layers, a bit like in traditional convolutional neural networks
|
||||
(CNN) in computer vision.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*With the success of language pretraining, it is highly desirable to develop more efficient architectures of good
|
||||
scalability that can exploit the abundant unlabeled data at a lower cost. To improve the efficiency, we examine the
|
||||
much-overlooked redundancy in maintaining a full-length token-level presentation, especially for tasks that only
|
||||
require a single-vector presentation of the sequence. With this intuition, we propose Funnel-Transformer which
|
||||
gradually compresses the sequence of hidden states to a shorter one and hence reduces the computation cost. More
|
||||
importantly, by re-investing the saved FLOPs from length reduction in constructing a deeper or wider model, we further
|
||||
improve the model capacity. In addition, to perform token-level predictions as required by common pretraining
|
||||
objectives, Funnel-Transformer is able to recover a deep representation for each token from the reduced hidden sequence
|
||||
via a decoder. Empirically, with comparable or fewer FLOPs, Funnel-Transformer outperforms the standard Transformer on
|
||||
a wide variety of sequence-level prediction tasks, including text classification, language understanding, and reading
|
||||
comprehension.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Since Funnel Transformer uses pooling, the sequence length of the hidden states changes after each block of layers.
|
||||
The base model therefore has a final sequence length that is a quarter of the original one. This model can be used
|
||||
directly for tasks that just require a sentence summary (like sequence classification or multiple choice). For other
|
||||
tasks, the full model is used; this full model has a decoder that upsamples the final hidden states to the same
|
||||
sequence length as the input.
|
||||
- The Funnel Transformer checkpoints are all available with a full version and a base version. The first ones should be
|
||||
used for [`FunnelModel`], [`FunnelForPreTraining`],
|
||||
[`FunnelForMaskedLM`], [`FunnelForTokenClassification`] and
|
||||
class:*~transformers.FunnelForQuestionAnswering*. The second ones should be used for
|
||||
[`FunnelBaseModel`], [`FunnelForSequenceClassification`] and
|
||||
[`FunnelForMultipleChoice`].
|
||||
|
||||
This model was contributed by [sgugger](https://huggingface.co/sgugger). The original code can be found [here](https://github.com/laiguokun/Funnel-Transformer).
|
||||
|
||||
|
||||
## FunnelConfig
|
||||
|
||||
[[autodoc]] FunnelConfig
|
||||
|
||||
## FunnelTokenizer
|
||||
|
||||
[[autodoc]] FunnelTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## FunnelTokenizerFast
|
||||
|
||||
[[autodoc]] FunnelTokenizerFast
|
||||
|
||||
## Funnel specific outputs
|
||||
|
||||
[[autodoc]] models.funnel.modeling_funnel.FunnelForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.funnel.modeling_tf_funnel.TFFunnelForPreTrainingOutput
|
||||
|
||||
## FunnelBaseModel
|
||||
|
||||
[[autodoc]] FunnelBaseModel
|
||||
- forward
|
||||
|
||||
## FunnelModel
|
||||
|
||||
[[autodoc]] FunnelModel
|
||||
- forward
|
||||
|
||||
## FunnelModelForPreTraining
|
||||
|
||||
[[autodoc]] FunnelForPreTraining
|
||||
- forward
|
||||
|
||||
## FunnelForMaskedLM
|
||||
|
||||
[[autodoc]] FunnelForMaskedLM
|
||||
- forward
|
||||
|
||||
## FunnelForSequenceClassification
|
||||
|
||||
[[autodoc]] FunnelForSequenceClassification
|
||||
- forward
|
||||
|
||||
## FunnelForMultipleChoice
|
||||
|
||||
[[autodoc]] FunnelForMultipleChoice
|
||||
- forward
|
||||
|
||||
## FunnelForTokenClassification
|
||||
|
||||
[[autodoc]] FunnelForTokenClassification
|
||||
- forward
|
||||
|
||||
## FunnelForQuestionAnswering
|
||||
|
||||
[[autodoc]] FunnelForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFFunnelBaseModel
|
||||
|
||||
[[autodoc]] TFFunnelBaseModel
|
||||
- call
|
||||
|
||||
## TFFunnelModel
|
||||
|
||||
[[autodoc]] TFFunnelModel
|
||||
- call
|
||||
|
||||
## TFFunnelModelForPreTraining
|
||||
|
||||
[[autodoc]] TFFunnelForPreTraining
|
||||
- call
|
||||
|
||||
## TFFunnelForMaskedLM
|
||||
|
||||
[[autodoc]] TFFunnelForMaskedLM
|
||||
- call
|
||||
|
||||
## TFFunnelForSequenceClassification
|
||||
|
||||
[[autodoc]] TFFunnelForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFFunnelForMultipleChoice
|
||||
|
||||
[[autodoc]] TFFunnelForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFFunnelForTokenClassification
|
||||
|
||||
[[autodoc]] TFFunnelForTokenClassification
|
||||
- call
|
||||
|
||||
## TFFunnelForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFFunnelForQuestionAnswering
|
||||
- call
|
||||
61
docs/source/en/model_doc/glpn.mdx
Normal file
61
docs/source/en/model_doc/glpn.mdx
Normal file
@@ -0,0 +1,61 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# GLPN
|
||||
|
||||
<Tip>
|
||||
|
||||
This is a recently introduced model so the API hasn't been tested extensively. There may be some bugs or slight
|
||||
breaking changes to fix it in the future. If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title).
|
||||
|
||||
</Tip>
|
||||
|
||||
## Overview
|
||||
|
||||
The GLPN model was proposed in [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://arxiv.org/abs/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim.
|
||||
GLPN combines [SegFormer](segformer)'s hierarchical mix-Transformer with a lightweight decoder for monocular depth estimation. The proposed decoder shows better performance than the previously proposed decoders, with considerably
|
||||
less computational complexity.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Depth estimation from a single image is an important task that can be applied to various fields in computer vision, and has grown rapidly with the development of convolutional neural networks. In this paper, we propose a novel structure and training strategy for monocular depth estimation to further improve the prediction accuracy of the network. We deploy a hierarchical transformer encoder to capture and convey the global context, and design a lightweight yet powerful decoder to generate an estimated depth map while considering local connectivity. By constructing connected paths between multi-scale local features and the global decoding stream with our proposed selective feature fusion module, the network can integrate both representations and recover fine details. In addition, the proposed decoder shows better performance than the previously proposed decoders, with considerably less computational complexity. Furthermore, we improve the depth-specific augmentation method by utilizing an important observation in depth estimation to enhance the model. Our network achieves state-of-the-art performance over the challenging depth dataset NYU Depth V2. Extensive experiments have been conducted to validate and show the effectiveness of the proposed approach. Finally, our model shows better generalisation ability and robustness than other comparative models.*
|
||||
|
||||
Tips:
|
||||
|
||||
- A notebook illustrating inference with [`GLPNForDepthEstimation`] can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/GLPN/GLPN_inference_(depth_estimation).ipynb).
|
||||
- One can use [`GLPNFeatureExtractor`] to prepare images for the model.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/glpn_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> Summary of the approach. Taken from the <a href="https://arxiv.org/abs/2201.07436" target="_blank">original paper</a>. </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/vinvino02/GLPDepth).
|
||||
|
||||
## GLPNConfig
|
||||
|
||||
[[autodoc]] GLPNConfig
|
||||
|
||||
## GLPNFeatureExtractor
|
||||
|
||||
[[autodoc]] GLPNFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## GLPNModel
|
||||
|
||||
[[autodoc]] GLPNModel
|
||||
- forward
|
||||
|
||||
## GLPNForDepthEstimation
|
||||
|
||||
[[autodoc]] GLPNForDepthEstimation
|
||||
- forward
|
||||
131
docs/source/en/model_doc/gpt2.mdx
Normal file
131
docs/source/en/model_doc/gpt2.mdx
Normal file
@@ -0,0 +1,131 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# OpenAI GPT2
|
||||
|
||||
## Overview
|
||||
|
||||
OpenAI GPT-2 model was proposed in [Language Models are Unsupervised Multitask Learners](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf) by Alec
|
||||
Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever. It's a causal (unidirectional)
|
||||
transformer pretrained using language modeling on a very large corpus of ~40 GB of text data.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*GPT-2 is a large transformer-based language model with 1.5 billion parameters, trained on a dataset[1] of 8 million
|
||||
web pages. GPT-2 is trained with a simple objective: predict the next word, given all of the previous words within some
|
||||
text. The diversity of the dataset causes this simple goal to contain naturally occurring demonstrations of many tasks
|
||||
across diverse domains. GPT-2 is a direct scale-up of GPT, with more than 10X the parameters and trained on more than
|
||||
10X the amount of data.*
|
||||
|
||||
Tips:
|
||||
|
||||
- GPT-2 is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than
|
||||
the left.
|
||||
- GPT-2 was trained with a causal language modeling (CLM) objective and is therefore powerful at predicting the next
|
||||
token in a sequence. Leveraging this feature allows GPT-2 to generate syntactically coherent text as it can be
|
||||
observed in the *run_generation.py* example script.
|
||||
- The model can take the *past_key_values* (for PyTorch) or *past* (for TF) as input, which is the previously computed
|
||||
key/value attention pairs. Using this (*past_key_values* or *past*) value prevents the model from re-computing
|
||||
pre-computed values in the context of text generation. For PyTorch, see *past_key_values* argument of the
|
||||
[`GPT2Model.forward`] method, or for TF the *past* argument of the
|
||||
[`TFGPT2Model.call`] method for more information on its usage.
|
||||
- Enabling the *scale_attn_by_inverse_layer_idx* and *reorder_and_upcast_attn* flags will apply the training stability
|
||||
improvements from [Mistral](https://github.com/stanford-crfm/mistral/) (for PyTorch only).
|
||||
|
||||
[Write With Transformer](https://transformer.huggingface.co/doc/gpt2-large) is a webapp created and hosted by
|
||||
Hugging Face showcasing the generative capabilities of several models. GPT-2 is one of them and is available in five
|
||||
different sizes: small, medium, large, xl and a distilled version of the small checkpoint: *distilgpt-2*.
|
||||
|
||||
This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://openai.com/blog/better-language-models/).
|
||||
|
||||
|
||||
## GPT2Config
|
||||
|
||||
[[autodoc]] GPT2Config
|
||||
|
||||
## GPT2Tokenizer
|
||||
|
||||
[[autodoc]] GPT2Tokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## GPT2TokenizerFast
|
||||
|
||||
[[autodoc]] GPT2TokenizerFast
|
||||
|
||||
## GPT2 specific outputs
|
||||
|
||||
[[autodoc]] models.gpt2.modeling_gpt2.GPT2DoubleHeadsModelOutput
|
||||
|
||||
[[autodoc]] models.gpt2.modeling_tf_gpt2.TFGPT2DoubleHeadsModelOutput
|
||||
|
||||
## GPT2Model
|
||||
|
||||
[[autodoc]] GPT2Model
|
||||
- forward
|
||||
- parallelize
|
||||
- deparallelize
|
||||
|
||||
## GPT2LMHeadModel
|
||||
|
||||
[[autodoc]] GPT2LMHeadModel
|
||||
- forward
|
||||
- parallelize
|
||||
- deparallelize
|
||||
|
||||
## GPT2DoubleHeadsModel
|
||||
|
||||
[[autodoc]] GPT2DoubleHeadsModel
|
||||
- forward
|
||||
|
||||
## GPT2ForSequenceClassification
|
||||
|
||||
[[autodoc]] GPT2ForSequenceClassification
|
||||
- forward
|
||||
|
||||
## GPT2ForTokenClassification
|
||||
|
||||
[[autodoc]] GPT2ForTokenClassification
|
||||
- forward
|
||||
|
||||
## TFGPT2Model
|
||||
|
||||
[[autodoc]] TFGPT2Model
|
||||
- call
|
||||
|
||||
## TFGPT2LMHeadModel
|
||||
|
||||
[[autodoc]] TFGPT2LMHeadModel
|
||||
- call
|
||||
|
||||
## TFGPT2DoubleHeadsModel
|
||||
|
||||
[[autodoc]] TFGPT2DoubleHeadsModel
|
||||
- call
|
||||
|
||||
## TFGPT2ForSequenceClassification
|
||||
|
||||
[[autodoc]] TFGPT2ForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFSequenceClassifierOutputWithPast
|
||||
|
||||
[[autodoc]] modeling_tf_outputs.TFSequenceClassifierOutputWithPast
|
||||
|
||||
## FlaxGPT2Model
|
||||
|
||||
[[autodoc]] FlaxGPT2Model
|
||||
- __call__
|
||||
|
||||
## FlaxGPT2LMHeadModel
|
||||
|
||||
[[autodoc]] FlaxGPT2LMHeadModel
|
||||
- __call__
|
||||
80
docs/source/en/model_doc/gpt_neo.mdx
Normal file
80
docs/source/en/model_doc/gpt_neo.mdx
Normal file
@@ -0,0 +1,80 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# GPT Neo
|
||||
|
||||
## Overview
|
||||
|
||||
The GPTNeo model was released in the [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) repository by Sid
|
||||
Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy. It is a GPT2 like causal language model trained on the
|
||||
[Pile](https://pile.eleuther.ai/) dataset.
|
||||
|
||||
The architecture is similar to GPT2 except that GPT Neo uses local attention in every other layer with a window size of
|
||||
256 tokens.
|
||||
|
||||
This model was contributed by [valhalla](https://huggingface.co/valhalla).
|
||||
|
||||
### Generation
|
||||
|
||||
The `generate()` method can be used to generate text using GPT Neo model.
|
||||
|
||||
```python
|
||||
>>> from transformers import GPTNeoForCausalLM, GPT2Tokenizer
|
||||
|
||||
>>> model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B")
|
||||
>>> tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
|
||||
|
||||
>>> prompt = (
|
||||
... "In a shocking finding, scientists discovered a herd of unicorns living in a remote, "
|
||||
... "previously unexplored valley, in the Andes Mountains. Even more surprising to the "
|
||||
... "researchers was the fact that the unicorns spoke perfect English."
|
||||
... )
|
||||
|
||||
>>> input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
||||
|
||||
>>> gen_tokens = model.generate(
|
||||
... input_ids,
|
||||
... do_sample=True,
|
||||
... temperature=0.9,
|
||||
... max_length=100,
|
||||
... )
|
||||
>>> gen_text = tokenizer.batch_decode(gen_tokens)[0]
|
||||
```
|
||||
|
||||
## GPTNeoConfig
|
||||
|
||||
[[autodoc]] GPTNeoConfig
|
||||
|
||||
## GPTNeoModel
|
||||
|
||||
[[autodoc]] GPTNeoModel
|
||||
- forward
|
||||
|
||||
## GPTNeoForCausalLM
|
||||
|
||||
[[autodoc]] GPTNeoForCausalLM
|
||||
- forward
|
||||
|
||||
## GPTNeoForSequenceClassification
|
||||
|
||||
[[autodoc]] GPTNeoForSequenceClassification
|
||||
- forward
|
||||
|
||||
## FlaxGPTNeoModel
|
||||
|
||||
[[autodoc]] FlaxGPTNeoModel
|
||||
- __call__
|
||||
|
||||
## FlaxGPTNeoForCausalLM
|
||||
|
||||
[[autodoc]] FlaxGPTNeoForCausalLM
|
||||
- __call__
|
||||
161
docs/source/en/model_doc/gptj.mdx
Normal file
161
docs/source/en/model_doc/gptj.mdx
Normal file
@@ -0,0 +1,161 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# GPT-J
|
||||
|
||||
## Overview
|
||||
|
||||
The GPT-J model was released in the [kingoflolz/mesh-transformer-jax](https://github.com/kingoflolz/mesh-transformer-jax) repository by Ben Wang and Aran Komatsuzaki. It is a GPT-2-like
|
||||
causal language model trained on [the Pile](https://pile.eleuther.ai/) dataset.
|
||||
|
||||
This model was contributed by [Stella Biderman](https://huggingface.co/stellaathena).
|
||||
|
||||
Tips:
|
||||
|
||||
- To load [GPT-J](https://huggingface.co/EleutherAI/gpt-j-6B) in float32 one would need at least 2x model size CPU
|
||||
RAM: 1x for initial weights and another 1x to load the checkpoint. So for GPT-J it would take at least 48GB of CPU
|
||||
RAM to just load the model. To reduce the CPU RAM usage there are a few options. The `torch_dtype` argument can be
|
||||
used to initialize the model in half-precision. And the `low_cpu_mem_usage` argument can be used to keep the RAM
|
||||
usage to 1x. There is also a [fp16 branch](https://huggingface.co/EleutherAI/gpt-j-6B/tree/float16) which stores
|
||||
the fp16 weights, which could be used to further minimize the RAM usage. Combining all this it should take roughly
|
||||
12.1GB of CPU RAM to load the model.
|
||||
|
||||
```python
|
||||
>>> from transformers import GPTJForCausalLM
|
||||
>>> import torch
|
||||
|
||||
>>> model = GPTJForCausalLM.from_pretrained(
|
||||
... "EleutherAI/gpt-j-6B", revision="float16", torch_dtype=torch.float16, low_cpu_mem_usage=True
|
||||
... )
|
||||
```
|
||||
|
||||
- The model should fit on 16GB GPU for inference. For training/fine-tuning it would take much more GPU RAM. Adam
|
||||
optimizer for example makes four copies of the model: model, gradients, average and squared average of the gradients.
|
||||
So it would need at least 4x model size GPU memory, even with mixed precision as gradient updates are in fp32. This
|
||||
is not including the activations and data batches, which would again require some more GPU RAM. So one should explore
|
||||
solutions such as DeepSpeed, to train/fine-tune the model. Another option is to use the original codebase to
|
||||
train/fine-tune the model on TPU and then convert the model to Transformers format for inference. Instructions for
|
||||
that could be found [here](https://github.com/kingoflolz/mesh-transformer-jax/blob/master/howto_finetune.md)
|
||||
|
||||
- Although the embedding matrix has a size of 50400, only 50257 entries are used by the GPT-2 tokenizer. These extra
|
||||
tokens are added for the sake of efficiency on TPUs. To avoid the mis-match between embedding matrix size and vocab
|
||||
size, the tokenizer for [GPT-J](https://huggingface.co/EleutherAI/gpt-j-6B) contains 143 extra tokens
|
||||
`<|extratoken_1|>... <|extratoken_143|>`, so the `vocab_size` of tokenizer also becomes 50400.
|
||||
|
||||
### Generation
|
||||
|
||||
The [`~generation_utils.GenerationMixin.generate`] method can be used to generate text using GPT-J
|
||||
model.
|
||||
|
||||
```python
|
||||
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||
|
||||
>>> model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-j-6B")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
|
||||
|
||||
>>> prompt = (
|
||||
... "In a shocking finding, scientists discovered a herd of unicorns living in a remote, "
|
||||
... "previously unexplored valley, in the Andes Mountains. Even more surprising to the "
|
||||
... "researchers was the fact that the unicorns spoke perfect English."
|
||||
... )
|
||||
|
||||
>>> input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
||||
|
||||
>>> gen_tokens = model.generate(
|
||||
... input_ids,
|
||||
... do_sample=True,
|
||||
... temperature=0.9,
|
||||
... max_length=100,
|
||||
... )
|
||||
>>> gen_text = tokenizer.batch_decode(gen_tokens)[0]
|
||||
```
|
||||
|
||||
...or in float16 precision:
|
||||
|
||||
```python
|
||||
>>> from transformers import GPTJForCausalLM, AutoTokenizer
|
||||
>>> import torch
|
||||
|
||||
>>> model = GPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B", torch_dtype=torch.float16)
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
|
||||
|
||||
>>> prompt = (
|
||||
... "In a shocking finding, scientists discovered a herd of unicorns living in a remote, "
|
||||
... "previously unexplored valley, in the Andes Mountains. Even more surprising to the "
|
||||
... "researchers was the fact that the unicorns spoke perfect English."
|
||||
... )
|
||||
|
||||
>>> input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
||||
|
||||
>>> gen_tokens = model.generate(
|
||||
... input_ids,
|
||||
... do_sample=True,
|
||||
... temperature=0.9,
|
||||
... max_length=100,
|
||||
... )
|
||||
>>> gen_text = tokenizer.batch_decode(gen_tokens)[0]
|
||||
```
|
||||
|
||||
## GPTJConfig
|
||||
|
||||
[[autodoc]] GPTJConfig
|
||||
- all
|
||||
|
||||
## GPTJModel
|
||||
|
||||
[[autodoc]] GPTJModel
|
||||
- forward
|
||||
|
||||
## GPTJForCausalLM
|
||||
|
||||
[[autodoc]] GPTJForCausalLM
|
||||
- forward
|
||||
|
||||
## GPTJForSequenceClassification
|
||||
|
||||
[[autodoc]] GPTJForSequenceClassification
|
||||
- forward
|
||||
|
||||
## GPTJForQuestionAnswering
|
||||
|
||||
[[autodoc]] GPTJForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFGPTJModel
|
||||
|
||||
[[autodoc]] TFGPTJModel
|
||||
- call
|
||||
|
||||
## TFGPTJForCausalLM
|
||||
|
||||
[[autodoc]] TFGPTJForCausalLM
|
||||
- call
|
||||
|
||||
## TFGPTJForSequenceClassification
|
||||
|
||||
[[autodoc]] TFGPTJForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFGPTJForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFGPTJForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxGPTJModel
|
||||
|
||||
[[autodoc]] FlaxGPTJModel
|
||||
- __call__
|
||||
|
||||
## FlaxGPTJForCausalLM
|
||||
|
||||
[[autodoc]] FlaxGPTJForCausalLM
|
||||
- __call__
|
||||
65
docs/source/en/model_doc/herbert.mdx
Normal file
65
docs/source/en/model_doc/herbert.mdx
Normal file
@@ -0,0 +1,65 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# HerBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The HerBERT model was proposed in [KLEJ: Comprehensive Benchmark for Polish Language Understanding](https://www.aclweb.org/anthology/2020.acl-main.111.pdf) by Piotr Rybak, Robert Mroczkowski, Janusz Tracz, and
|
||||
Ireneusz Gawlik. It is a BERT-based Language Model trained on Polish Corpora using only MLM objective with dynamic
|
||||
masking of whole words.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*In recent years, a series of Transformer-based models unlocked major improvements in general natural language
|
||||
understanding (NLU) tasks. Such a fast pace of research would not be possible without general NLU benchmarks, which
|
||||
allow for a fair comparison of the proposed methods. However, such benchmarks are available only for a handful of
|
||||
languages. To alleviate this issue, we introduce a comprehensive multi-task benchmark for the Polish language
|
||||
understanding, accompanied by an online leaderboard. It consists of a diverse set of tasks, adopted from existing
|
||||
datasets for named entity recognition, question-answering, textual entailment, and others. We also introduce a new
|
||||
sentiment analysis task for the e-commerce domain, named Allegro Reviews (AR). To ensure a common evaluation scheme and
|
||||
promote models that generalize to different NLU tasks, the benchmark includes datasets from varying domains and
|
||||
applications. Additionally, we release HerBERT, a Transformer-based model trained specifically for the Polish language,
|
||||
which has the best average performance and obtains the best results for three out of nine tasks. Finally, we provide an
|
||||
extensive evaluation, including several standard baselines and recently proposed, multilingual Transformer-based
|
||||
models.*
|
||||
|
||||
Examples of use:
|
||||
|
||||
```python
|
||||
>>> from transformers import HerbertTokenizer, RobertaModel
|
||||
|
||||
>>> tokenizer = HerbertTokenizer.from_pretrained("allegro/herbert-klej-cased-tokenizer-v1")
|
||||
>>> model = RobertaModel.from_pretrained("allegro/herbert-klej-cased-v1")
|
||||
|
||||
>>> encoded_input = tokenizer.encode("Kto ma lepszą sztukę, ma lepszy rząd – to jasne.", return_tensors="pt")
|
||||
>>> outputs = model(encoded_input)
|
||||
|
||||
>>> # HerBERT can also be loaded using AutoTokenizer and AutoModel:
|
||||
>>> import torch
|
||||
>>> from transformers import AutoModel, AutoTokenizer
|
||||
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("allegro/herbert-klej-cased-tokenizer-v1")
|
||||
>>> model = AutoModel.from_pretrained("allegro/herbert-klej-cased-v1")
|
||||
```
|
||||
|
||||
This model was contributed by [rmroczkowski](https://huggingface.co/rmroczkowski). The original code can be found
|
||||
[here](https://github.com/allegro/HerBERT).
|
||||
|
||||
|
||||
## HerbertTokenizer
|
||||
|
||||
[[autodoc]] HerbertTokenizer
|
||||
|
||||
## HerbertTokenizerFast
|
||||
|
||||
[[autodoc]] HerbertTokenizerFast
|
||||
71
docs/source/en/model_doc/hubert.mdx
Normal file
71
docs/source/en/model_doc/hubert.mdx
Normal file
@@ -0,0 +1,71 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Hubert
|
||||
|
||||
## Overview
|
||||
|
||||
Hubert was proposed in [HuBERT: Self-Supervised Speech Representation Learning by Masked Prediction of Hidden Units](https://arxiv.org/abs/2106.07447) by Wei-Ning Hsu, Benjamin Bolte, Yao-Hung Hubert Tsai, Kushal Lakhotia, Ruslan
|
||||
Salakhutdinov, Abdelrahman Mohamed.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Self-supervised approaches for speech representation learning are challenged by three unique problems: (1) there are
|
||||
multiple sound units in each input utterance, (2) there is no lexicon of input sound units during the pre-training
|
||||
phase, and (3) sound units have variable lengths with no explicit segmentation. To deal with these three problems, we
|
||||
propose the Hidden-Unit BERT (HuBERT) approach for self-supervised speech representation learning, which utilizes an
|
||||
offline clustering step to provide aligned target labels for a BERT-like prediction loss. A key ingredient of our
|
||||
approach is applying the prediction loss over the masked regions only, which forces the model to learn a combined
|
||||
acoustic and language model over the continuous inputs. HuBERT relies primarily on the consistency of the unsupervised
|
||||
clustering step rather than the intrinsic quality of the assigned cluster labels. Starting with a simple k-means
|
||||
teacher of 100 clusters, and using two iterations of clustering, the HuBERT model either matches or improves upon the
|
||||
state-of-the-art wav2vec 2.0 performance on the Librispeech (960h) and Libri-light (60,000h) benchmarks with 10min, 1h,
|
||||
10h, 100h, and 960h fine-tuning subsets. Using a 1B parameter model, HuBERT shows up to 19% and 13% relative WER
|
||||
reduction on the more challenging dev-other and test-other evaluation subsets.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Hubert is a speech model that accepts a float array corresponding to the raw waveform of the speech signal.
|
||||
- Hubert model was fine-tuned using connectionist temporal classification (CTC) so the model output has to be decoded
|
||||
using [`Wav2Vec2CTCTokenizer`].
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten).
|
||||
|
||||
|
||||
## HubertConfig
|
||||
|
||||
[[autodoc]] HubertConfig
|
||||
|
||||
## HubertModel
|
||||
|
||||
[[autodoc]] HubertModel
|
||||
- forward
|
||||
|
||||
## HubertForCTC
|
||||
|
||||
[[autodoc]] HubertForCTC
|
||||
- forward
|
||||
|
||||
## HubertForSequenceClassification
|
||||
|
||||
[[autodoc]] HubertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## TFHubertModel
|
||||
|
||||
[[autodoc]] TFHubertModel
|
||||
- call
|
||||
|
||||
## TFHubertForCTC
|
||||
|
||||
[[autodoc]] TFHubertForCTC
|
||||
- call
|
||||
72
docs/source/en/model_doc/ibert.mdx
Normal file
72
docs/source/en/model_doc/ibert.mdx
Normal file
@@ -0,0 +1,72 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# I-BERT
|
||||
|
||||
## Overview
|
||||
|
||||
The I-BERT model was proposed in [I-BERT: Integer-only BERT Quantization](https://arxiv.org/abs/2101.01321) by
|
||||
Sehoon Kim, Amir Gholami, Zhewei Yao, Michael W. Mahoney and Kurt Keutzer. It's a quantized version of RoBERTa running
|
||||
inference up to four times faster.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformer based models, like BERT and RoBERTa, have achieved state-of-the-art results in many Natural Language
|
||||
Processing tasks. However, their memory footprint, inference latency, and power consumption are prohibitive for
|
||||
efficient inference at the edge, and even at the data center. While quantization can be a viable solution for this,
|
||||
previous work on quantizing Transformer based models use floating-point arithmetic during inference, which cannot
|
||||
efficiently utilize integer-only logical units such as the recent Turing Tensor Cores, or traditional integer-only ARM
|
||||
processors. In this work, we propose I-BERT, a novel quantization scheme for Transformer based models that quantizes
|
||||
the entire inference with integer-only arithmetic. Based on lightweight integer-only approximation methods for
|
||||
nonlinear operations, e.g., GELU, Softmax, and Layer Normalization, I-BERT performs an end-to-end integer-only BERT
|
||||
inference without any floating point calculation. We evaluate our approach on GLUE downstream tasks using
|
||||
RoBERTa-Base/Large. We show that for both cases, I-BERT achieves similar (and slightly higher) accuracy as compared to
|
||||
the full-precision baseline. Furthermore, our preliminary implementation of I-BERT shows a speedup of 2.4 - 4.0x for
|
||||
INT8 inference on a T4 GPU system as compared to FP32 inference. The framework has been developed in PyTorch and has
|
||||
been open-sourced.*
|
||||
|
||||
This model was contributed by [kssteven](https://huggingface.co/kssteven). The original code can be found [here](https://github.com/kssteven418/I-BERT).
|
||||
|
||||
|
||||
## IBertConfig
|
||||
|
||||
[[autodoc]] IBertConfig
|
||||
|
||||
## IBertModel
|
||||
|
||||
[[autodoc]] IBertModel
|
||||
- forward
|
||||
|
||||
## IBertForMaskedLM
|
||||
|
||||
[[autodoc]] IBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## IBertForSequenceClassification
|
||||
|
||||
[[autodoc]] IBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## IBertForMultipleChoice
|
||||
|
||||
[[autodoc]] IBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## IBertForTokenClassification
|
||||
|
||||
[[autodoc]] IBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## IBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] IBertForQuestionAnswering
|
||||
- forward
|
||||
100
docs/source/en/model_doc/imagegpt.mdx
Normal file
100
docs/source/en/model_doc/imagegpt.mdx
Normal file
@@ -0,0 +1,100 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License. -->
|
||||
|
||||
# ImageGPT
|
||||
|
||||
## Overview
|
||||
|
||||
The ImageGPT model was proposed in [Generative Pretraining from Pixels](https://openai.com/blog/image-gpt) by Mark
|
||||
Chen, Alec Radford, Rewon Child, Jeffrey Wu, Heewoo Jun, David Luan, Ilya Sutskever. ImageGPT (iGPT) is a GPT-2-like
|
||||
model trained to predict the next pixel value, allowing for both unconditional and conditional image generation.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Inspired by progress in unsupervised representation learning for natural language, we examine whether similar models
|
||||
can learn useful representations for images. We train a sequence Transformer to auto-regressively predict pixels,
|
||||
without incorporating knowledge of the 2D input structure. Despite training on low-resolution ImageNet without labels,
|
||||
we find that a GPT-2 scale model learns strong image representations as measured by linear probing, fine-tuning, and
|
||||
low-data classification. On CIFAR-10, we achieve 96.3% accuracy with a linear probe, outperforming a supervised Wide
|
||||
ResNet, and 99.0% accuracy with full fine-tuning, matching the top supervised pre-trained models. We are also
|
||||
competitive with self-supervised benchmarks on ImageNet when substituting pixels for a VQVAE encoding, achieving 69.0%
|
||||
top-1 accuracy on a linear probe of our features.*
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/imagegpt_architecture.png"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> Summary of the approach. Taken from the [original paper](https://cdn.openai.com/papers/Generative_Pretraining_from_Pixels_V2.pdf). </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr), based on [this issue](https://github.com/openai/image-gpt/issues/7). The original code can be found
|
||||
[here](https://github.com/openai/image-gpt).
|
||||
|
||||
Tips:
|
||||
|
||||
- Demo notebooks for ImageGPT can be found
|
||||
[here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/ImageGPT).
|
||||
- ImageGPT is almost exactly the same as [GPT-2](gpt2), with the exception that a different activation
|
||||
function is used (namely "quick gelu"), and the layer normalization layers don't mean center the inputs. ImageGPT
|
||||
also doesn't have tied input- and output embeddings.
|
||||
- As the time- and memory requirements of the attention mechanism of Transformers scales quadratically in the sequence
|
||||
length, the authors pre-trained ImageGPT on smaller input resolutions, such as 32x32 and 64x64. However, feeding a
|
||||
sequence of 32x32x3=3072 tokens from 0..255 into a Transformer is still prohibitively large. Therefore, the authors
|
||||
applied k-means clustering to the (R,G,B) pixel values with k=512. This way, we only have a 32*32 = 1024-long
|
||||
sequence, but now of integers in the range 0..511. So we are shrinking the sequence length at the cost of a bigger
|
||||
embedding matrix. In other words, the vocabulary size of ImageGPT is 512, + 1 for a special "start of sentence" (SOS)
|
||||
token, used at the beginning of every sequence. One can use [`ImageGPTFeatureExtractor`] to prepare
|
||||
images for the model.
|
||||
- Despite being pre-trained entirely unsupervised (i.e. without the use of any labels), ImageGPT produces fairly
|
||||
performant image features useful for downstream tasks, such as image classification. The authors showed that the
|
||||
features in the middle of the network are the most performant, and can be used as-is to train a linear model (such as
|
||||
a sklearn logistic regression model for example). This is also referred to as "linear probing". Features can be
|
||||
easily obtained by first forwarding the image through the model, then specifying `output_hidden_states=True`, and
|
||||
then average-pool the hidden states at whatever layer you like.
|
||||
- Alternatively, one can further fine-tune the entire model on a downstream dataset, similar to BERT. For this, you can
|
||||
use [`ImageGPTForImageClassification`].
|
||||
- ImageGPT comes in different sizes: there's ImageGPT-small, ImageGPT-medium and ImageGPT-large. The authors did also
|
||||
train an XL variant, which they didn't release. The differences in size are summarized in the following table:
|
||||
|
||||
| **Model variant** | **Depths** | **Hidden sizes** | **Decoder hidden size** | **Params (M)** | **ImageNet-1k Top 1** |
|
||||
|---|---|---|---|---|---|
|
||||
| MiT-b0 | [2, 2, 2, 2] | [32, 64, 160, 256] | 256 | 3.7 | 70.5 |
|
||||
| MiT-b1 | [2, 2, 2, 2] | [64, 128, 320, 512] | 256 | 14.0 | 78.7 |
|
||||
| MiT-b2 | [3, 4, 6, 3] | [64, 128, 320, 512] | 768 | 25.4 | 81.6 |
|
||||
| MiT-b3 | [3, 4, 18, 3] | [64, 128, 320, 512] | 768 | 45.2 | 83.1 |
|
||||
| MiT-b4 | [3, 8, 27, 3] | [64, 128, 320, 512] | 768 | 62.6 | 83.6 |
|
||||
| MiT-b5 | [3, 6, 40, 3] | [64, 128, 320, 512] | 768 | 82.0 | 83.8 |
|
||||
|
||||
## ImageGPTConfig
|
||||
|
||||
[[autodoc]] ImageGPTConfig
|
||||
|
||||
## ImageGPTFeatureExtractor
|
||||
|
||||
[[autodoc]] ImageGPTFeatureExtractor
|
||||
|
||||
- __call__
|
||||
|
||||
## ImageGPTModel
|
||||
|
||||
[[autodoc]] ImageGPTModel
|
||||
|
||||
- forward
|
||||
|
||||
## ImageGPTForCausalImageModeling
|
||||
|
||||
[[autodoc]] ImageGPTForCausalImageModeling
|
||||
|
||||
- forward
|
||||
|
||||
## ImageGPTForImageClassification
|
||||
|
||||
[[autodoc]] ImageGPTForImageClassification
|
||||
|
||||
- forward
|
||||
124
docs/source/en/model_doc/layoutlm.mdx
Normal file
124
docs/source/en/model_doc/layoutlm.mdx
Normal file
@@ -0,0 +1,124 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LayoutLM
|
||||
|
||||
<a id='Overview'></a>
|
||||
|
||||
## Overview
|
||||
|
||||
The LayoutLM model was proposed in the paper [LayoutLM: Pre-training of Text and Layout for Document Image
|
||||
Understanding](https://arxiv.org/abs/1912.13318) by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, and
|
||||
Ming Zhou. It's a simple but effective pretraining method of text and layout for document image understanding and
|
||||
information extraction tasks, such as form understanding and receipt understanding. It obtains state-of-the-art results
|
||||
on several downstream tasks:
|
||||
|
||||
- form understanding: the [FUNSD](https://guillaumejaume.github.io/FUNSD/) dataset (a collection of 199 annotated
|
||||
forms comprising more than 30,000 words).
|
||||
- receipt understanding: the [SROIE](https://rrc.cvc.uab.es/?ch=13) dataset (a collection of 626 receipts for
|
||||
training and 347 receipts for testing).
|
||||
- document image classification: the [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/) dataset (a collection of
|
||||
400,000 images belonging to one of 16 classes).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pre-training techniques have been verified successfully in a variety of NLP tasks in recent years. Despite the
|
||||
widespread use of pretraining models for NLP applications, they almost exclusively focus on text-level manipulation,
|
||||
while neglecting layout and style information that is vital for document image understanding. In this paper, we propose
|
||||
the LayoutLM to jointly model interactions between text and layout information across scanned document images, which is
|
||||
beneficial for a great number of real-world document image understanding tasks such as information extraction from
|
||||
scanned documents. Furthermore, we also leverage image features to incorporate words' visual information into LayoutLM.
|
||||
To the best of our knowledge, this is the first time that text and layout are jointly learned in a single framework for
|
||||
document-level pretraining. It achieves new state-of-the-art results in several downstream tasks, including form
|
||||
understanding (from 70.72 to 79.27), receipt understanding (from 94.02 to 95.24) and document image classification
|
||||
(from 93.07 to 94.42).*
|
||||
|
||||
Tips:
|
||||
|
||||
- In addition to *input_ids*, [`~transformers.LayoutLMModel.forward`] also expects the input `bbox`, which are
|
||||
the bounding boxes (i.e. 2D-positions) of the input tokens. These can be obtained using an external OCR engine such
|
||||
as Google's [Tesseract](https://github.com/tesseract-ocr/tesseract) (there's a [Python wrapper](https://pypi.org/project/pytesseract/) available). Each bounding box should be in (x0, y0, x1, y1) format, where
|
||||
(x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1, y1) represents the
|
||||
position of the lower right corner. Note that one first needs to normalize the bounding boxes to be on a 0-1000
|
||||
scale. To normalize, you can use the following function:
|
||||
|
||||
```python
|
||||
def normalize_bbox(bbox, width, height):
|
||||
return [
|
||||
int(1000 * (bbox[0] / width)),
|
||||
int(1000 * (bbox[1] / height)),
|
||||
int(1000 * (bbox[2] / width)),
|
||||
int(1000 * (bbox[3] / height)),
|
||||
]
|
||||
```
|
||||
|
||||
Here, `width` and `height` correspond to the width and height of the original document in which the token
|
||||
occurs. Those can be obtained using the Python Image Library (PIL) library for example, as follows:
|
||||
|
||||
```python
|
||||
from PIL import Image
|
||||
|
||||
image = Image.open("name_of_your_document - can be a png file, pdf, etc.")
|
||||
|
||||
width, height = image.size
|
||||
```
|
||||
|
||||
- For a demo which shows how to fine-tune [`LayoutLMForTokenClassification`] on the [FUNSD dataset](https://guillaumejaume.github.io/FUNSD/) (a collection of annotated forms), see [this notebook](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/LayoutLM/Fine_tuning_LayoutLMForTokenClassification_on_FUNSD.ipynb).
|
||||
It includes an inference part, which shows how to use Google's Tesseract on a new document.
|
||||
|
||||
This model was contributed by [liminghao1630](https://huggingface.co/liminghao1630). The original code can be found
|
||||
[here](https://github.com/microsoft/unilm/tree/master/layoutlm).
|
||||
|
||||
|
||||
## LayoutLMConfig
|
||||
|
||||
[[autodoc]] LayoutLMConfig
|
||||
|
||||
## LayoutLMTokenizer
|
||||
|
||||
[[autodoc]] LayoutLMTokenizer
|
||||
|
||||
## LayoutLMTokenizerFast
|
||||
|
||||
[[autodoc]] LayoutLMTokenizerFast
|
||||
|
||||
## LayoutLMModel
|
||||
|
||||
[[autodoc]] LayoutLMModel
|
||||
|
||||
## LayoutLMForMaskedLM
|
||||
|
||||
[[autodoc]] LayoutLMForMaskedLM
|
||||
|
||||
## LayoutLMForSequenceClassification
|
||||
|
||||
[[autodoc]] LayoutLMForSequenceClassification
|
||||
|
||||
## LayoutLMForTokenClassification
|
||||
|
||||
[[autodoc]] LayoutLMForTokenClassification
|
||||
|
||||
## TFLayoutLMModel
|
||||
|
||||
[[autodoc]] TFLayoutLMModel
|
||||
|
||||
## TFLayoutLMForMaskedLM
|
||||
|
||||
[[autodoc]] TFLayoutLMForMaskedLM
|
||||
|
||||
## TFLayoutLMForSequenceClassification
|
||||
|
||||
[[autodoc]] TFLayoutLMForSequenceClassification
|
||||
|
||||
## TFLayoutLMForTokenClassification
|
||||
|
||||
[[autodoc]] TFLayoutLMForTokenClassification
|
||||
301
docs/source/en/model_doc/layoutlmv2.mdx
Normal file
301
docs/source/en/model_doc/layoutlmv2.mdx
Normal file
@@ -0,0 +1,301 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LayoutLMV2
|
||||
|
||||
## Overview
|
||||
|
||||
The LayoutLMV2 model was proposed in [LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding](https://arxiv.org/abs/2012.14740) by Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu,
|
||||
Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou. LayoutLMV2 improves [LayoutLM](layoutlm) to obtain
|
||||
state-of-the-art results across several document image understanding benchmarks:
|
||||
|
||||
- information extraction from scanned documents: the [FUNSD](https://guillaumejaume.github.io/FUNSD/) dataset (a
|
||||
collection of 199 annotated forms comprising more than 30,000 words), the [CORD](https://github.com/clovaai/cord)
|
||||
dataset (a collection of 800 receipts for training, 100 for validation and 100 for testing), the [SROIE](https://rrc.cvc.uab.es/?ch=13) dataset (a collection of 626 receipts for training and 347 receipts for testing)
|
||||
and the [Kleister-NDA](https://github.com/applicaai/kleister-nda) dataset (a collection of non-disclosure
|
||||
agreements from the EDGAR database, including 254 documents for training, 83 documents for validation, and 203
|
||||
documents for testing).
|
||||
- document image classification: the [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/) dataset (a collection of
|
||||
400,000 images belonging to one of 16 classes).
|
||||
- document visual question answering: the [DocVQA](https://arxiv.org/abs/2007.00398) dataset (a collection of 50,000
|
||||
questions defined on 12,000+ document images).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Pre-training of text and layout has proved effective in a variety of visually-rich document understanding tasks due to
|
||||
its effective model architecture and the advantage of large-scale unlabeled scanned/digital-born documents. In this
|
||||
paper, we present LayoutLMv2 by pre-training text, layout and image in a multi-modal framework, where new model
|
||||
architectures and pre-training tasks are leveraged. Specifically, LayoutLMv2 not only uses the existing masked
|
||||
visual-language modeling task but also the new text-image alignment and text-image matching tasks in the pre-training
|
||||
stage, where cross-modality interaction is better learned. Meanwhile, it also integrates a spatial-aware self-attention
|
||||
mechanism into the Transformer architecture, so that the model can fully understand the relative positional
|
||||
relationship among different text blocks. Experiment results show that LayoutLMv2 outperforms strong baselines and
|
||||
achieves new state-of-the-art results on a wide variety of downstream visually-rich document understanding tasks,
|
||||
including FUNSD (0.7895 -> 0.8420), CORD (0.9493 -> 0.9601), SROIE (0.9524 -> 0.9781), Kleister-NDA (0.834 -> 0.852),
|
||||
RVL-CDIP (0.9443 -> 0.9564), and DocVQA (0.7295 -> 0.8672). The pre-trained LayoutLMv2 model is publicly available at
|
||||
this https URL.*
|
||||
|
||||
Tips:
|
||||
|
||||
- The main difference between LayoutLMv1 and LayoutLMv2 is that the latter incorporates visual embeddings during
|
||||
pre-training (while LayoutLMv1 only adds visual embeddings during fine-tuning).
|
||||
- LayoutLMv2 adds both a relative 1D attention bias as well as a spatial 2D attention bias to the attention scores in
|
||||
the self-attention layers. Details can be found on page 5 of the [paper](https://arxiv.org/abs/2012.14740).
|
||||
- Demo notebooks on how to use the LayoutLMv2 model on RVL-CDIP, FUNSD, DocVQA, CORD can be found [here](https://github.com/NielsRogge/Transformers-Tutorials).
|
||||
- LayoutLMv2 uses Facebook AI's [Detectron2](https://github.com/facebookresearch/detectron2/) package for its visual
|
||||
backbone. See [this link](https://detectron2.readthedocs.io/en/latest/tutorials/install.html) for installation
|
||||
instructions.
|
||||
- In addition to `input_ids`, [`~LayoutLMv2Model.forward`] expects 2 additional inputs, namely
|
||||
`image` and `bbox`. The `image` input corresponds to the original document image in which the text
|
||||
tokens occur. The model expects each document image to be of size 224x224. This means that if you have a batch of
|
||||
document images, `image` should be a tensor of shape (batch_size, 3, 224, 224). This can be either a
|
||||
`torch.Tensor` or a `Detectron2.structures.ImageList`. You don't need to normalize the channels, as this is
|
||||
done by the model. Important to note is that the visual backbone expects BGR channels instead of RGB, as all models
|
||||
in Detectron2 are pre-trained using the BGR format. The `bbox` input are the bounding boxes (i.e. 2D-positions)
|
||||
of the input text tokens. This is identical to [`LayoutLMModel`]. These can be obtained using an
|
||||
external OCR engine such as Google's [Tesseract](https://github.com/tesseract-ocr/tesseract) (there's a [Python
|
||||
wrapper](https://pypi.org/project/pytesseract/) available). Each bounding box should be in (x0, y0, x1, y1)
|
||||
format, where (x0, y0) corresponds to the position of the upper left corner in the bounding box, and (x1, y1)
|
||||
represents the position of the lower right corner. Note that one first needs to normalize the bounding boxes to be on
|
||||
a 0-1000 scale. To normalize, you can use the following function:
|
||||
|
||||
```python
|
||||
def normalize_bbox(bbox, width, height):
|
||||
return [
|
||||
int(1000 * (bbox[0] / width)),
|
||||
int(1000 * (bbox[1] / height)),
|
||||
int(1000 * (bbox[2] / width)),
|
||||
int(1000 * (bbox[3] / height)),
|
||||
]
|
||||
```
|
||||
|
||||
Here, `width` and `height` correspond to the width and height of the original document in which the token
|
||||
occurs (before resizing the image). Those can be obtained using the Python Image Library (PIL) library for example, as
|
||||
follows:
|
||||
|
||||
```python
|
||||
from PIL import Image
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
)
|
||||
|
||||
width, height = image.size
|
||||
```
|
||||
|
||||
However, this model includes a brand new [`~transformers.LayoutLMv2Processor`] which can be used to directly
|
||||
prepare data for the model (including applying OCR under the hood). More information can be found in the "Usage"
|
||||
section below.
|
||||
|
||||
- Internally, [`~transformers.LayoutLMv2Model`] will send the `image` input through its visual backbone to
|
||||
obtain a lower-resolution feature map, whose shape is equal to the `image_feature_pool_shape` attribute of
|
||||
[`~transformers.LayoutLMv2Config`]. This feature map is then flattened to obtain a sequence of image tokens. As
|
||||
the size of the feature map is 7x7 by default, one obtains 49 image tokens. These are then concatenated with the text
|
||||
tokens, and send through the Transformer encoder. This means that the last hidden states of the model will have a
|
||||
length of 512 + 49 = 561, if you pad the text tokens up to the max length. More generally, the last hidden states
|
||||
will have a shape of `seq_length` + `image_feature_pool_shape[0]` *
|
||||
`config.image_feature_pool_shape[1]`.
|
||||
- When calling [`~transformers.LayoutLMv2Model.from_pretrained`], a warning will be printed with a long list of
|
||||
parameter names that are not initialized. This is not a problem, as these parameters are batch normalization
|
||||
statistics, which are going to have values when fine-tuning on a custom dataset.
|
||||
- If you want to train the model in a distributed environment, make sure to call [`synchronize_batch_norm`] on the
|
||||
model in order to properly synchronize the batch normalization layers of the visual backbone.
|
||||
|
||||
In addition, there's LayoutXLM, which is a multilingual version of LayoutLMv2. More information can be found on
|
||||
[LayoutXLM's documentation page](layoutxlm).
|
||||
|
||||
## Usage: LayoutLMv2Processor
|
||||
|
||||
The easiest way to prepare data for the model is to use [`LayoutLMv2Processor`], which internally
|
||||
combines a feature extractor ([`LayoutLMv2FeatureExtractor`]) and a tokenizer
|
||||
([`LayoutLMv2Tokenizer`] or [`LayoutLMv2TokenizerFast`]). The feature extractor
|
||||
handles the image modality, while the tokenizer handles the text modality. A processor combines both, which is ideal
|
||||
for a multi-modal model like LayoutLMv2. Note that you can still use both separately, if you only want to handle one
|
||||
modality.
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2FeatureExtractor, LayoutLMv2TokenizerFast, LayoutLMv2Processor
|
||||
|
||||
feature_extractor = LayoutLMv2FeatureExtractor() # apply_ocr is set to True by default
|
||||
tokenizer = LayoutLMv2TokenizerFast.from_pretrained("microsoft/layoutlmv2-base-uncased")
|
||||
processor = LayoutLMv2Processor(feature_extractor, tokenizer)
|
||||
```
|
||||
|
||||
In short, one can provide a document image (and possibly additional data) to [`LayoutLMv2Processor`],
|
||||
and it will create the inputs expected by the model. Internally, the processor first uses
|
||||
[`LayoutLMv2FeatureExtractor`] to apply OCR on the image to get a list of words and normalized
|
||||
bounding boxes, as well to resize the image to a given size in order to get the `image` input. The words and
|
||||
normalized bounding boxes are then provided to [`LayoutLMv2Tokenizer`] or
|
||||
[`LayoutLMv2TokenizerFast`], which converts them to token-level `input_ids`,
|
||||
`attention_mask`, `token_type_ids`, `bbox`. Optionally, one can provide word labels to the processor,
|
||||
which are turned into token-level `labels`.
|
||||
|
||||
[`LayoutLMv2Processor`] uses [PyTesseract](https://pypi.org/project/pytesseract/), a Python
|
||||
wrapper around Google's Tesseract OCR engine, under the hood. Note that you can still use your own OCR engine of
|
||||
choice, and provide the words and normalized boxes yourself. This requires initializing
|
||||
[`LayoutLMv2FeatureExtractor`] with `apply_ocr` set to `False`.
|
||||
|
||||
In total, there are 5 use cases that are supported by the processor. Below, we list them all. Note that each of these
|
||||
use cases work for both batched and non-batched inputs (we illustrate them for non-batched inputs).
|
||||
|
||||
**Use case 1: document image classification (training, inference) + token classification (inference), apply_ocr =
|
||||
True**
|
||||
|
||||
This is the simplest case, in which the processor (actually the feature extractor) will perform OCR on the image to get
|
||||
the words and normalized bounding boxes.
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Processor
|
||||
from PIL import Image
|
||||
|
||||
processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased")
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
).convert("RGB")
|
||||
encoding = processor(
|
||||
image, return_tensors="pt"
|
||||
) # you can also add all tokenizer parameters here such as padding, truncation
|
||||
print(encoding.keys())
|
||||
# dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'image'])
|
||||
```
|
||||
|
||||
**Use case 2: document image classification (training, inference) + token classification (inference), apply_ocr=False**
|
||||
|
||||
In case one wants to do OCR themselves, one can initialize the feature extractor with `apply_ocr` set to
|
||||
`False`. In that case, one should provide the words and corresponding (normalized) bounding boxes themselves to
|
||||
the processor.
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Processor
|
||||
from PIL import Image
|
||||
|
||||
processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr")
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
).convert("RGB")
|
||||
words = ["hello", "world"]
|
||||
boxes = [[1, 2, 3, 4], [5, 6, 7, 8]] # make sure to normalize your bounding boxes
|
||||
encoding = processor(image, words, boxes=boxes, return_tensors="pt")
|
||||
print(encoding.keys())
|
||||
# dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'image'])
|
||||
```
|
||||
|
||||
**Use case 3: token classification (training), apply_ocr=False**
|
||||
|
||||
For token classification tasks (such as FUNSD, CORD, SROIE, Kleister-NDA), one can also provide the corresponding word
|
||||
labels in order to train a model. The processor will then convert these into token-level `labels`. By default, it
|
||||
will only label the first wordpiece of a word, and label the remaining wordpieces with -100, which is the
|
||||
`ignore_index` of PyTorch's CrossEntropyLoss. In case you want all wordpieces of a word to be labeled, you can
|
||||
initialize the tokenizer with `only_label_first_subword` set to `False`.
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Processor
|
||||
from PIL import Image
|
||||
|
||||
processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr")
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
).convert("RGB")
|
||||
words = ["hello", "world"]
|
||||
boxes = [[1, 2, 3, 4], [5, 6, 7, 8]] # make sure to normalize your bounding boxes
|
||||
word_labels = [1, 2]
|
||||
encoding = processor(image, words, boxes=boxes, word_labels=word_labels, return_tensors="pt")
|
||||
print(encoding.keys())
|
||||
# dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'labels', 'image'])
|
||||
```
|
||||
|
||||
**Use case 4: visual question answering (inference), apply_ocr=True**
|
||||
|
||||
For visual question answering tasks (such as DocVQA), you can provide a question to the processor. By default, the
|
||||
processor will apply OCR on the image, and create [CLS] question tokens [SEP] word tokens [SEP].
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Processor
|
||||
from PIL import Image
|
||||
|
||||
processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased")
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
).convert("RGB")
|
||||
question = "What's his name?"
|
||||
encoding = processor(image, question, return_tensors="pt")
|
||||
print(encoding.keys())
|
||||
# dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'image'])
|
||||
```
|
||||
|
||||
**Use case 5: visual question answering (inference), apply_ocr=False**
|
||||
|
||||
For visual question answering tasks (such as DocVQA), you can provide a question to the processor. If you want to
|
||||
perform OCR yourself, you can provide your own words and (normalized) bounding boxes to the processor.
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Processor
|
||||
from PIL import Image
|
||||
|
||||
processor = LayoutLMv2Processor.from_pretrained("microsoft/layoutlmv2-base-uncased", revision="no_ocr")
|
||||
|
||||
image = Image.open(
|
||||
"name_of_your_document - can be a png, jpg, etc. of your documents (PDFs must be converted to images)."
|
||||
).convert("RGB")
|
||||
question = "What's his name?"
|
||||
words = ["hello", "world"]
|
||||
boxes = [[1, 2, 3, 4], [5, 6, 7, 8]] # make sure to normalize your bounding boxes
|
||||
encoding = processor(image, question, words, boxes=boxes, return_tensors="pt")
|
||||
print(encoding.keys())
|
||||
# dict_keys(['input_ids', 'token_type_ids', 'attention_mask', 'bbox', 'image'])
|
||||
```
|
||||
|
||||
## LayoutLMv2Config
|
||||
|
||||
[[autodoc]] LayoutLMv2Config
|
||||
|
||||
## LayoutLMv2FeatureExtractor
|
||||
|
||||
[[autodoc]] LayoutLMv2FeatureExtractor
|
||||
- __call__
|
||||
|
||||
## LayoutLMv2Tokenizer
|
||||
|
||||
[[autodoc]] LayoutLMv2Tokenizer
|
||||
- __call__
|
||||
- save_vocabulary
|
||||
|
||||
## LayoutLMv2TokenizerFast
|
||||
|
||||
[[autodoc]] LayoutLMv2TokenizerFast
|
||||
- __call__
|
||||
|
||||
## LayoutLMv2Processor
|
||||
|
||||
[[autodoc]] LayoutLMv2Processor
|
||||
- __call__
|
||||
|
||||
## LayoutLMv2Model
|
||||
|
||||
[[autodoc]] LayoutLMv2Model
|
||||
- forward
|
||||
|
||||
## LayoutLMv2ForSequenceClassification
|
||||
|
||||
[[autodoc]] LayoutLMv2ForSequenceClassification
|
||||
|
||||
## LayoutLMv2ForTokenClassification
|
||||
|
||||
[[autodoc]] LayoutLMv2ForTokenClassification
|
||||
|
||||
## LayoutLMv2ForQuestionAnswering
|
||||
|
||||
[[autodoc]] LayoutLMv2ForQuestionAnswering
|
||||
77
docs/source/en/model_doc/layoutxlm.mdx
Normal file
77
docs/source/en/model_doc/layoutxlm.mdx
Normal file
@@ -0,0 +1,77 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LayoutXLM
|
||||
|
||||
## Overview
|
||||
|
||||
LayoutXLM was proposed in [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://arxiv.org/abs/2104.08836) by Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha
|
||||
Zhang, Furu Wei. It's a multilingual extension of the [LayoutLMv2 model](https://arxiv.org/abs/2012.14740) trained
|
||||
on 53 languages.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Multimodal pre-training with text, layout, and image has achieved SOTA performance for visually-rich document
|
||||
understanding tasks recently, which demonstrates the great potential for joint learning across different modalities. In
|
||||
this paper, we present LayoutXLM, a multimodal pre-trained model for multilingual document understanding, which aims to
|
||||
bridge the language barriers for visually-rich document understanding. To accurately evaluate LayoutXLM, we also
|
||||
introduce a multilingual form understanding benchmark dataset named XFUN, which includes form understanding samples in
|
||||
7 languages (Chinese, Japanese, Spanish, French, Italian, German, Portuguese), and key-value pairs are manually labeled
|
||||
for each language. Experiment results show that the LayoutXLM model has significantly outperformed the existing SOTA
|
||||
cross-lingual pre-trained models on the XFUN dataset.*
|
||||
|
||||
One can directly plug in the weights of LayoutXLM into a LayoutLMv2 model, like so:
|
||||
|
||||
```python
|
||||
from transformers import LayoutLMv2Model
|
||||
|
||||
model = LayoutLMv2Model.from_pretrained("microsoft/layoutxlm-base")
|
||||
```
|
||||
|
||||
Note that LayoutXLM has its own tokenizer, based on
|
||||
[`LayoutXLMTokenizer`]/[`LayoutXLMTokenizerFast`]. You can initialize it as
|
||||
follows:
|
||||
|
||||
```python
|
||||
from transformers import LayoutXLMTokenizer
|
||||
|
||||
tokenizer = LayoutXLMTokenizer.from_pretrained("microsoft/layoutxlm-base")
|
||||
```
|
||||
|
||||
Similar to LayoutLMv2, you can use [`LayoutXLMProcessor`] (which internally applies
|
||||
[`LayoutLMv2FeatureExtractor`] and
|
||||
[`LayoutXLMTokenizer`]/[`LayoutXLMTokenizerFast`] in sequence) to prepare all
|
||||
data for the model.
|
||||
|
||||
As LayoutXLM's architecture is equivalent to that of LayoutLMv2, one can refer to [LayoutLMv2's documentation page](layoutlmv2) for all tips, code examples and notebooks.
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/microsoft/unilm).
|
||||
|
||||
|
||||
## LayoutXLMTokenizer
|
||||
|
||||
[[autodoc]] LayoutXLMTokenizer
|
||||
- __call__
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## LayoutXLMTokenizerFast
|
||||
|
||||
[[autodoc]] LayoutXLMTokenizerFast
|
||||
- __call__
|
||||
|
||||
## LayoutXLMProcessor
|
||||
|
||||
[[autodoc]] LayoutXLMProcessor
|
||||
- __call__
|
||||
117
docs/source/en/model_doc/led.mdx
Normal file
117
docs/source/en/model_doc/led.mdx
Normal file
@@ -0,0 +1,117 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LED
|
||||
|
||||
## Overview
|
||||
|
||||
The LED model was proposed in [Longformer: The Long-Document Transformer](https://arxiv.org/abs/2004.05150) by Iz
|
||||
Beltagy, Matthew E. Peters, Arman Cohan.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformer-based models are unable to process long sequences due to their self-attention operation, which scales
|
||||
quadratically with the sequence length. To address this limitation, we introduce the Longformer with an attention
|
||||
mechanism that scales linearly with sequence length, making it easy to process documents of thousands of tokens or
|
||||
longer. Longformer's attention mechanism is a drop-in replacement for the standard self-attention and combines a local
|
||||
windowed attention with a task motivated global attention. Following prior work on long-sequence transformers, we
|
||||
evaluate Longformer on character-level language modeling and achieve state-of-the-art results on text8 and enwik8. In
|
||||
contrast to most prior work, we also pretrain Longformer and finetune it on a variety of downstream tasks. Our
|
||||
pretrained Longformer consistently outperforms RoBERTa on long document tasks and sets new state-of-the-art results on
|
||||
WikiHop and TriviaQA. We finally introduce the Longformer-Encoder-Decoder (LED), a Longformer variant for supporting
|
||||
long document generative sequence-to-sequence tasks, and demonstrate its effectiveness on the arXiv summarization
|
||||
dataset.*
|
||||
|
||||
Tips:
|
||||
|
||||
- [`LEDForConditionalGeneration`] is an extension of
|
||||
[`BartForConditionalGeneration`] exchanging the traditional *self-attention* layer with
|
||||
*Longformer*'s *chunked self-attention* layer. [`LEDTokenizer`] is an alias of
|
||||
[`BartTokenizer`].
|
||||
- LED works very well on long-range *sequence-to-sequence* tasks where the `input_ids` largely exceed a length of
|
||||
1024 tokens.
|
||||
- LED pads the `input_ids` to be a multiple of `config.attention_window` if required. Therefore a small speed-up is
|
||||
gained, when [`LEDTokenizer`] is used with the `pad_to_multiple_of` argument.
|
||||
- LED makes use of *global attention* by means of the `global_attention_mask` (see
|
||||
[`LongformerModel`]). For summarization, it is advised to put *global attention* only on the first
|
||||
`<s>` token. For question answering, it is advised to put *global attention* on all tokens of the question.
|
||||
- To fine-tune LED on all 16384, it is necessary to enable *gradient checkpointing* by executing
|
||||
`model.gradient_checkpointing_enable()`.
|
||||
- A notebook showing how to evaluate LED, can be accessed [here](https://colab.research.google.com/drive/12INTTR6n64TzS4RrXZxMSXfrOd9Xzamo?usp=sharing).
|
||||
- A notebook showing how to fine-tune LED, can be accessed [here](https://colab.research.google.com/drive/12LjJazBl7Gam0XBPy_y0CTOJZeZ34c2v?usp=sharing).
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten).
|
||||
|
||||
|
||||
## LEDConfig
|
||||
|
||||
[[autodoc]] LEDConfig
|
||||
|
||||
## LEDTokenizer
|
||||
|
||||
[[autodoc]] LEDTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## LEDTokenizerFast
|
||||
|
||||
[[autodoc]] LEDTokenizerFast
|
||||
|
||||
## LED specific outputs
|
||||
|
||||
[[autodoc]] models.led.modeling_led.LEDEncoderBaseModelOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_led.LEDSeq2SeqModelOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_led.LEDSeq2SeqLMOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_led.LEDSeq2SeqSequenceClassifierOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_led.LEDSeq2SeqQuestionAnsweringModelOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_tf_led.TFLEDEncoderBaseModelOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_tf_led.TFLEDSeq2SeqModelOutput
|
||||
|
||||
[[autodoc]] models.led.modeling_tf_led.TFLEDSeq2SeqLMOutput
|
||||
|
||||
## LEDModel
|
||||
|
||||
[[autodoc]] LEDModel
|
||||
- forward
|
||||
|
||||
## LEDForConditionalGeneration
|
||||
|
||||
[[autodoc]] LEDForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## LEDForSequenceClassification
|
||||
|
||||
[[autodoc]] LEDForSequenceClassification
|
||||
- forward
|
||||
|
||||
## LEDForQuestionAnswering
|
||||
|
||||
[[autodoc]] LEDForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFLEDModel
|
||||
|
||||
[[autodoc]] TFLEDModel
|
||||
- call
|
||||
|
||||
## TFLEDForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFLEDForConditionalGeneration
|
||||
- call
|
||||
182
docs/source/en/model_doc/longformer.mdx
Normal file
182
docs/source/en/model_doc/longformer.mdx
Normal file
@@ -0,0 +1,182 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Longformer
|
||||
|
||||
## Overview
|
||||
|
||||
The Longformer model was presented in [Longformer: The Long-Document Transformer](https://arxiv.org/pdf/2004.05150.pdf) by Iz Beltagy, Matthew E. Peters, Arman Cohan.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformer-based models are unable to process long sequences due to their self-attention operation, which scales
|
||||
quadratically with the sequence length. To address this limitation, we introduce the Longformer with an attention
|
||||
mechanism that scales linearly with sequence length, making it easy to process documents of thousands of tokens or
|
||||
longer. Longformer's attention mechanism is a drop-in replacement for the standard self-attention and combines a local
|
||||
windowed attention with a task motivated global attention. Following prior work on long-sequence transformers, we
|
||||
evaluate Longformer on character-level language modeling and achieve state-of-the-art results on text8 and enwik8. In
|
||||
contrast to most prior work, we also pretrain Longformer and finetune it on a variety of downstream tasks. Our
|
||||
pretrained Longformer consistently outperforms RoBERTa on long document tasks and sets new state-of-the-art results on
|
||||
WikiHop and TriviaQA.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Since the Longformer is based on RoBERTa, it doesn't have `token_type_ids`. You don't need to indicate which
|
||||
token belongs to which segment. Just separate your segments with the separation token `tokenizer.sep_token` (or
|
||||
`</s>`).
|
||||
|
||||
This model was contributed by [beltagy](https://huggingface.co/beltagy). The Authors' code can be found [here](https://github.com/allenai/longformer).
|
||||
|
||||
## Longformer Self Attention
|
||||
|
||||
Longformer self attention employs self attention on both a "local" context and a "global" context. Most tokens only
|
||||
attend "locally" to each other meaning that each token attends to its \\(\frac{1}{2} w\\) previous tokens and
|
||||
\\(\frac{1}{2} w\\) succeding tokens with \\(w\\) being the window length as defined in
|
||||
`config.attention_window`. Note that `config.attention_window` can be of type `List` to define a
|
||||
different \\(w\\) for each layer. A selected few tokens attend "globally" to all other tokens, as it is
|
||||
conventionally done for all tokens in `BertSelfAttention`.
|
||||
|
||||
Note that "locally" and "globally" attending tokens are projected by different query, key and value matrices. Also note
|
||||
that every "locally" attending token not only attends to tokens within its window \\(w\\), but also to all "globally"
|
||||
attending tokens so that global attention is *symmetric*.
|
||||
|
||||
The user can define which tokens attend "locally" and which tokens attend "globally" by setting the tensor
|
||||
`global_attention_mask` at run-time appropriately. All Longformer models employ the following logic for
|
||||
`global_attention_mask`:
|
||||
|
||||
- 0: the token attends "locally",
|
||||
- 1: the token attends "globally".
|
||||
|
||||
For more information please also refer to [`~LongformerModel.forward`] method.
|
||||
|
||||
Using Longformer self attention, the memory and time complexity of the query-key matmul operation, which usually
|
||||
represents the memory and time bottleneck, can be reduced from \\(\mathcal{O}(n_s \times n_s)\\) to
|
||||
\\(\mathcal{O}(n_s \times w)\\), with \\(n_s\\) being the sequence length and \\(w\\) being the average window
|
||||
size. It is assumed that the number of "globally" attending tokens is insignificant as compared to the number of
|
||||
"locally" attending tokens.
|
||||
|
||||
For more information, please refer to the official [paper](https://arxiv.org/pdf/2004.05150.pdf).
|
||||
|
||||
|
||||
## Training
|
||||
|
||||
[`LongformerForMaskedLM`] is trained the exact same way [`RobertaForMaskedLM`] is
|
||||
trained and should be used as follows:
|
||||
|
||||
```python
|
||||
input_ids = tokenizer.encode("This is a sentence from [MASK] training data", return_tensors="pt")
|
||||
mlm_labels = tokenizer.encode("This is a sentence from the training data", return_tensors="pt")
|
||||
|
||||
loss = model(input_ids, labels=input_ids, masked_lm_labels=mlm_labels)[0]
|
||||
```
|
||||
|
||||
## LongformerConfig
|
||||
|
||||
[[autodoc]] LongformerConfig
|
||||
|
||||
## LongformerTokenizer
|
||||
|
||||
[[autodoc]] LongformerTokenizer
|
||||
|
||||
## LongformerTokenizerFast
|
||||
|
||||
[[autodoc]] LongformerTokenizerFast
|
||||
|
||||
## Longformer specific outputs
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerBaseModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerBaseModelOutputWithPooling
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerMaskedLMOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerQuestionAnsweringModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerSequenceClassifierOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerMultipleChoiceModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_longformer.LongformerTokenClassifierOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerBaseModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerBaseModelOutputWithPooling
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerMaskedLMOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerQuestionAnsweringModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerSequenceClassifierOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerMultipleChoiceModelOutput
|
||||
|
||||
[[autodoc]] models.longformer.modeling_tf_longformer.TFLongformerTokenClassifierOutput
|
||||
|
||||
## LongformerModel
|
||||
|
||||
[[autodoc]] LongformerModel
|
||||
- forward
|
||||
|
||||
## LongformerForMaskedLM
|
||||
|
||||
[[autodoc]] LongformerForMaskedLM
|
||||
- forward
|
||||
|
||||
## LongformerForSequenceClassification
|
||||
|
||||
[[autodoc]] LongformerForSequenceClassification
|
||||
- forward
|
||||
|
||||
## LongformerForMultipleChoice
|
||||
|
||||
[[autodoc]] LongformerForMultipleChoice
|
||||
- forward
|
||||
|
||||
## LongformerForTokenClassification
|
||||
|
||||
[[autodoc]] LongformerForTokenClassification
|
||||
- forward
|
||||
|
||||
## LongformerForQuestionAnswering
|
||||
|
||||
[[autodoc]] LongformerForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFLongformerModel
|
||||
|
||||
[[autodoc]] TFLongformerModel
|
||||
- call
|
||||
|
||||
## TFLongformerForMaskedLM
|
||||
|
||||
[[autodoc]] TFLongformerForMaskedLM
|
||||
- call
|
||||
|
||||
## TFLongformerForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFLongformerForQuestionAnswering
|
||||
- call
|
||||
|
||||
## TFLongformerForSequenceClassification
|
||||
|
||||
[[autodoc]] TFLongformerForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFLongformerForTokenClassification
|
||||
|
||||
[[autodoc]] TFLongformerForTokenClassification
|
||||
- call
|
||||
|
||||
## TFLongformerForMultipleChoice
|
||||
|
||||
[[autodoc]] TFLongformerForMultipleChoice
|
||||
- call
|
||||
154
docs/source/en/model_doc/luke.mdx
Normal file
154
docs/source/en/model_doc/luke.mdx
Normal file
@@ -0,0 +1,154 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LUKE
|
||||
|
||||
## Overview
|
||||
|
||||
The LUKE model was proposed in [LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention](https://arxiv.org/abs/2010.01057) by Ikuya Yamada, Akari Asai, Hiroyuki Shindo, Hideaki Takeda and Yuji Matsumoto.
|
||||
It is based on RoBERTa and adds entity embeddings as well as an entity-aware self-attention mechanism, which helps
|
||||
improve performance on various downstream tasks involving reasoning about entities such as named entity recognition,
|
||||
extractive and cloze-style question answering, entity typing, and relation classification.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Entity representations are useful in natural language tasks involving entities. In this paper, we propose new
|
||||
pretrained contextualized representations of words and entities based on the bidirectional transformer. The proposed
|
||||
model treats words and entities in a given text as independent tokens, and outputs contextualized representations of
|
||||
them. Our model is trained using a new pretraining task based on the masked language model of BERT. The task involves
|
||||
predicting randomly masked words and entities in a large entity-annotated corpus retrieved from Wikipedia. We also
|
||||
propose an entity-aware self-attention mechanism that is an extension of the self-attention mechanism of the
|
||||
transformer, and considers the types of tokens (words or entities) when computing attention scores. The proposed model
|
||||
achieves impressive empirical performance on a wide range of entity-related tasks. In particular, it obtains
|
||||
state-of-the-art results on five well-known datasets: Open Entity (entity typing), TACRED (relation classification),
|
||||
CoNLL-2003 (named entity recognition), ReCoRD (cloze-style question answering), and SQuAD 1.1 (extractive question
|
||||
answering).*
|
||||
|
||||
Tips:
|
||||
|
||||
- This implementation is the same as [`RobertaModel`] with the addition of entity embeddings as well
|
||||
as an entity-aware self-attention mechanism, which improves performance on tasks involving reasoning about entities.
|
||||
- LUKE treats entities as input tokens; therefore, it takes `entity_ids`, `entity_attention_mask`,
|
||||
`entity_token_type_ids` and `entity_position_ids` as extra input. You can obtain those using
|
||||
[`LukeTokenizer`].
|
||||
- [`LukeTokenizer`] takes `entities` and `entity_spans` (character-based start and end
|
||||
positions of the entities in the input text) as extra input. `entities` typically consist of [MASK] entities or
|
||||
Wikipedia entities. The brief description when inputting these entities are as follows:
|
||||
|
||||
- *Inputting [MASK] entities to compute entity representations*: The [MASK] entity is used to mask entities to be
|
||||
predicted during pretraining. When LUKE receives the [MASK] entity, it tries to predict the original entity by
|
||||
gathering the information about the entity from the input text. Therefore, the [MASK] entity can be used to address
|
||||
downstream tasks requiring the information of entities in text such as entity typing, relation classification, and
|
||||
named entity recognition.
|
||||
- *Inputting Wikipedia entities to compute knowledge-enhanced token representations*: LUKE learns rich information
|
||||
(or knowledge) about Wikipedia entities during pretraining and stores the information in its entity embedding. By
|
||||
using Wikipedia entities as input tokens, LUKE outputs token representations enriched by the information stored in
|
||||
the embeddings of these entities. This is particularly effective for tasks requiring real-world knowledge, such as
|
||||
question answering.
|
||||
|
||||
- There are three head models for the former use case:
|
||||
|
||||
- [`LukeForEntityClassification`], for tasks to classify a single entity in an input text such as
|
||||
entity typing, e.g. the [Open Entity dataset](https://www.cs.utexas.edu/~eunsol/html_pages/open_entity.html).
|
||||
This model places a linear head on top of the output entity representation.
|
||||
- [`LukeForEntityPairClassification`], for tasks to classify the relationship between two entities
|
||||
such as relation classification, e.g. the [TACRED dataset](https://nlp.stanford.edu/projects/tacred/). This
|
||||
model places a linear head on top of the concatenated output representation of the pair of given entities.
|
||||
- [`LukeForEntitySpanClassification`], for tasks to classify the sequence of entity spans, such as
|
||||
named entity recognition (NER). This model places a linear head on top of the output entity representations. You
|
||||
can address NER using this model by inputting all possible entity spans in the text to the model.
|
||||
|
||||
[`LukeTokenizer`] has a `task` argument, which enables you to easily create an input to these
|
||||
head models by specifying `task="entity_classification"`, `task="entity_pair_classification"`, or
|
||||
`task="entity_span_classification"`. Please refer to the example code of each head models.
|
||||
|
||||
A demo notebook on how to fine-tune [`LukeForEntityPairClassification`] for relation
|
||||
classification can be found [here](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/LUKE).
|
||||
|
||||
There are also 3 notebooks available, which showcase how you can reproduce the results as reported in the paper with
|
||||
the HuggingFace implementation of LUKE. They can be found [here](https://github.com/studio-ousia/luke/tree/master/notebooks).
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> from transformers import LukeTokenizer, LukeModel, LukeForEntityPairClassification
|
||||
|
||||
>>> model = LukeModel.from_pretrained("studio-ousia/luke-base")
|
||||
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base")
|
||||
# Example 1: Computing the contextualized entity representation corresponding to the entity mention "Beyoncé"
|
||||
|
||||
>>> text = "Beyoncé lives in Los Angeles."
|
||||
>>> entity_spans = [(0, 7)] # character-based entity span corresponding to "Beyoncé"
|
||||
>>> inputs = tokenizer(text, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> word_last_hidden_state = outputs.last_hidden_state
|
||||
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
|
||||
# Example 2: Inputting Wikipedia entities to obtain enriched contextualized representations
|
||||
|
||||
>>> entities = [
|
||||
... "Beyoncé",
|
||||
... "Los Angeles",
|
||||
>>> ] # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
|
||||
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||
>>> inputs = tokenizer(text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> word_last_hidden_state = outputs.last_hidden_state
|
||||
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
|
||||
# Example 3: Classifying the relationship between two entities using LukeForEntityPairClassification head model
|
||||
|
||||
>>> model = LukeForEntityPairClassification.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
|
||||
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
|
||||
>>> entity_spans = [(0, 7), (17, 28)] # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
|
||||
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> logits = outputs.logits
|
||||
>>> predicted_class_idx = int(logits[0].argmax())
|
||||
>>> print("Predicted class:", model.config.id2label[predicted_class_idx])
|
||||
```
|
||||
|
||||
This model was contributed by [ikuyamada](https://huggingface.co/ikuyamada) and [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/studio-ousia/luke).
|
||||
|
||||
|
||||
## LukeConfig
|
||||
|
||||
[[autodoc]] LukeConfig
|
||||
|
||||
## LukeTokenizer
|
||||
|
||||
[[autodoc]] LukeTokenizer
|
||||
- __call__
|
||||
- save_vocabulary
|
||||
|
||||
## LukeModel
|
||||
|
||||
[[autodoc]] LukeModel
|
||||
- forward
|
||||
|
||||
## LukeForMaskedLM
|
||||
|
||||
[[autodoc]] LukeForMaskedLM
|
||||
- forward
|
||||
|
||||
## LukeForEntityClassification
|
||||
|
||||
[[autodoc]] LukeForEntityClassification
|
||||
- forward
|
||||
|
||||
## LukeForEntityPairClassification
|
||||
|
||||
[[autodoc]] LukeForEntityPairClassification
|
||||
- forward
|
||||
|
||||
## LukeForEntitySpanClassification
|
||||
|
||||
[[autodoc]] LukeForEntitySpanClassification
|
||||
- forward
|
||||
102
docs/source/en/model_doc/lxmert.mdx
Normal file
102
docs/source/en/model_doc/lxmert.mdx
Normal file
@@ -0,0 +1,102 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# LXMERT
|
||||
|
||||
## Overview
|
||||
|
||||
The LXMERT model was proposed in [LXMERT: Learning Cross-Modality Encoder Representations from Transformers](https://arxiv.org/abs/1908.07490) by Hao Tan & Mohit Bansal. It is a series of bidirectional transformer encoders
|
||||
(one for the vision modality, one for the language modality, and then one to fuse both modalities) pretrained using a
|
||||
combination of masked language modeling, visual-language text alignment, ROI-feature regression, masked
|
||||
visual-attribute modeling, masked visual-object modeling, and visual-question answering objectives. The pretraining
|
||||
consists of multiple multi-modal datasets: MSCOCO, Visual-Genome + Visual-Genome Question Answering, VQA 2.0, and GQA.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Vision-and-language reasoning requires an understanding of visual concepts, language semantics, and, most importantly,
|
||||
the alignment and relationships between these two modalities. We thus propose the LXMERT (Learning Cross-Modality
|
||||
Encoder Representations from Transformers) framework to learn these vision-and-language connections. In LXMERT, we
|
||||
build a large-scale Transformer model that consists of three encoders: an object relationship encoder, a language
|
||||
encoder, and a cross-modality encoder. Next, to endow our model with the capability of connecting vision and language
|
||||
semantics, we pre-train the model with large amounts of image-and-sentence pairs, via five diverse representative
|
||||
pretraining tasks: masked language modeling, masked object prediction (feature regression and label classification),
|
||||
cross-modality matching, and image question answering. These tasks help in learning both intra-modality and
|
||||
cross-modality relationships. After fine-tuning from our pretrained parameters, our model achieves the state-of-the-art
|
||||
results on two visual question answering datasets (i.e., VQA and GQA). We also show the generalizability of our
|
||||
pretrained cross-modality model by adapting it to a challenging visual-reasoning task, NLVR, and improve the previous
|
||||
best result by 22% absolute (54% to 76%). Lastly, we demonstrate detailed ablation studies to prove that both our novel
|
||||
model components and pretraining strategies significantly contribute to our strong results; and also present several
|
||||
attention visualizations for the different encoders*
|
||||
|
||||
Tips:
|
||||
|
||||
- Bounding boxes are not necessary to be used in the visual feature embeddings, any kind of visual-spacial features
|
||||
will work.
|
||||
- Both the language hidden states and the visual hidden states that LXMERT outputs are passed through the
|
||||
cross-modality layer, so they contain information from both modalities. To access a modality that only attends to
|
||||
itself, select the vision/language hidden states from the first input in the tuple.
|
||||
- The bidirectional cross-modality encoder attention only returns attention values when the language modality is used
|
||||
as the input and the vision modality is used as the context vector. Further, while the cross-modality encoder
|
||||
contains self-attention for each respective modality and cross-attention, only the cross attention is returned and
|
||||
both self attention outputs are disregarded.
|
||||
|
||||
This model was contributed by [eltoto1219](https://huggingface.co/eltoto1219). The original code can be found [here](https://github.com/airsplay/lxmert).
|
||||
|
||||
|
||||
## LxmertConfig
|
||||
|
||||
[[autodoc]] LxmertConfig
|
||||
|
||||
## LxmertTokenizer
|
||||
|
||||
[[autodoc]] LxmertTokenizer
|
||||
|
||||
## LxmertTokenizerFast
|
||||
|
||||
[[autodoc]] LxmertTokenizerFast
|
||||
|
||||
## Lxmert specific outputs
|
||||
|
||||
[[autodoc]] models.lxmert.modeling_lxmert.LxmertModelOutput
|
||||
|
||||
[[autodoc]] models.lxmert.modeling_lxmert.LxmertForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.lxmert.modeling_lxmert.LxmertForQuestionAnsweringOutput
|
||||
|
||||
[[autodoc]] models.lxmert.modeling_tf_lxmert.TFLxmertModelOutput
|
||||
|
||||
[[autodoc]] models.lxmert.modeling_tf_lxmert.TFLxmertForPreTrainingOutput
|
||||
|
||||
## LxmertModel
|
||||
|
||||
[[autodoc]] LxmertModel
|
||||
- forward
|
||||
|
||||
## LxmertForPreTraining
|
||||
|
||||
[[autodoc]] LxmertForPreTraining
|
||||
- forward
|
||||
|
||||
## LxmertForQuestionAnswering
|
||||
|
||||
[[autodoc]] LxmertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFLxmertModel
|
||||
|
||||
[[autodoc]] TFLxmertModel
|
||||
- call
|
||||
|
||||
## TFLxmertForPreTraining
|
||||
|
||||
[[autodoc]] TFLxmertForPreTraining
|
||||
- call
|
||||
116
docs/source/en/model_doc/m2m_100.mdx
Normal file
116
docs/source/en/model_doc/m2m_100.mdx
Normal file
@@ -0,0 +1,116 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# M2M100
|
||||
|
||||
## Overview
|
||||
|
||||
The M2M100 model was proposed in [Beyond English-Centric Multilingual Machine Translation](https://arxiv.org/abs/2010.11125) by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky,
|
||||
Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy
|
||||
Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Existing work in translation demonstrated the potential of massively multilingual machine translation by training a
|
||||
single model able to translate between any pair of languages. However, much of this work is English-Centric by training
|
||||
only on data which was translated from or to English. While this is supported by large sources of training data, it
|
||||
does not reflect translation needs worldwide. In this work, we create a true Many-to-Many multilingual translation
|
||||
model that can translate directly between any pair of 100 languages. We build and open source a training dataset that
|
||||
covers thousands of language directions with supervised data, created through large-scale mining. Then, we explore how
|
||||
to effectively increase model capacity through a combination of dense scaling and language-specific sparse parameters
|
||||
to create high quality models. Our focus on non-English-Centric models brings gains of more than 10 BLEU when directly
|
||||
translating between non-English directions while performing competitively to the best single systems of WMT. We
|
||||
open-source our scripts so that others may reproduce the data, evaluation, and final M2M-100 model.*
|
||||
|
||||
This model was contributed by [valhalla](https://huggingface.co/valhalla).
|
||||
|
||||
|
||||
### Training and Generation
|
||||
|
||||
M2M100 is a multilingual encoder-decoder (seq-to-seq) model primarily intended for translation tasks. As the model is
|
||||
multilingual it expects the sequences in a certain format: A special language id token is used as prefix in both the
|
||||
source and target text. The source text format is `[lang_code] X [eos]`, where `lang_code` is source language
|
||||
id for source text and target language id for target text, with `X` being the source or target text.
|
||||
|
||||
The [`M2M100Tokenizer`] depends on `sentencepiece` so be sure to install it before running the
|
||||
examples. To install `sentencepiece` run `pip install sentencepiece`.
|
||||
|
||||
- Supervised Training
|
||||
|
||||
```python
|
||||
from transformers import M2M100Config, M2M100ForConditionalGeneration, M2M100Tokenizer
|
||||
|
||||
model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
||||
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", src_lang="en", tgt_lang="fr")
|
||||
|
||||
src_text = "Life is like a box of chocolates."
|
||||
tgt_text = "La vie est comme une boîte de chocolat."
|
||||
|
||||
model_inputs = tokenizer(src_text, return_tensors="pt")
|
||||
with tokenizer.as_target_tokenizer():
|
||||
labels = tokenizer(tgt_text, return_tensors="pt").input_ids
|
||||
|
||||
loss = model(**model_inputs, labels=labels) # forward pass
|
||||
```
|
||||
|
||||
- Generation
|
||||
|
||||
M2M100 uses the `eos_token_id` as the `decoder_start_token_id` for generation with the target language id
|
||||
being forced as the first generated token. To force the target language id as the first generated token, pass the
|
||||
*forced_bos_token_id* parameter to the *generate* method. The following example shows how to translate between
|
||||
Hindi to French and Chinese to English using the *facebook/m2m100_418M* checkpoint.
|
||||
|
||||
```python
|
||||
>>> from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
||||
|
||||
>>> hi_text = "जीवन एक चॉकलेट बॉक्स की तरह है।"
|
||||
>>> chinese_text = "生活就像一盒巧克力。"
|
||||
|
||||
>>> model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
||||
>>> tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
||||
|
||||
>>> # translate Hindi to French
|
||||
>>> tokenizer.src_lang = "hi"
|
||||
>>> encoded_hi = tokenizer(hi_text, return_tensors="pt")
|
||||
>>> generated_tokens = model.generate(**encoded_hi, forced_bos_token_id=tokenizer.get_lang_id("fr"))
|
||||
>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
||||
"La vie est comme une boîte de chocolat."
|
||||
|
||||
>>> # translate Chinese to English
|
||||
>>> tokenizer.src_lang = "zh"
|
||||
>>> encoded_zh = tokenizer(chinese_text, return_tensors="pt")
|
||||
>>> generated_tokens = model.generate(**encoded_zh, forced_bos_token_id=tokenizer.get_lang_id("en"))
|
||||
>>> tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
||||
"Life is like a box of chocolate."
|
||||
```
|
||||
|
||||
## M2M100Config
|
||||
|
||||
[[autodoc]] M2M100Config
|
||||
|
||||
## M2M100Tokenizer
|
||||
|
||||
[[autodoc]] M2M100Tokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## M2M100Model
|
||||
|
||||
[[autodoc]] M2M100Model
|
||||
- forward
|
||||
|
||||
## M2M100ForConditionalGeneration
|
||||
|
||||
[[autodoc]] M2M100ForConditionalGeneration
|
||||
- forward
|
||||
193
docs/source/en/model_doc/marian.mdx
Normal file
193
docs/source/en/model_doc/marian.mdx
Normal file
@@ -0,0 +1,193 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MarianMT
|
||||
|
||||
**Bugs:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=sshleifer&labels=&template=bug-report.md&title)
|
||||
and assign @patrickvonplaten.
|
||||
|
||||
Translations should be similar, but not identical to output in the test set linked to in each model card.
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- Each model is about 298 MB on disk, there are more than 1,000 models.
|
||||
- The list of supported language pairs can be found [here](https://huggingface.co/Helsinki-NLP).
|
||||
- Models were originally trained by [Jörg Tiedemann](https://researchportal.helsinki.fi/en/persons/j%C3%B6rg-tiedemann) using the [Marian](https://marian-nmt.github.io/) C++ library, which supports fast training and translation.
|
||||
- All models are transformer encoder-decoders with 6 layers in each component. Each model's performance is documented
|
||||
in a model card.
|
||||
- The 80 opus models that require BPE preprocessing are not supported.
|
||||
- The modeling code is the same as [`BartForConditionalGeneration`] with a few minor modifications:
|
||||
|
||||
- static (sinusoid) positional embeddings (`MarianConfig.static_position_embeddings=True`)
|
||||
- no layernorm_embedding (`MarianConfig.normalize_embedding=False`)
|
||||
- the model starts generating with `pad_token_id` (which has 0 as a token_embedding) as the prefix (Bart uses
|
||||
`<s/>`),
|
||||
- Code to bulk convert models can be found in `convert_marian_to_pytorch.py`.
|
||||
- This model was contributed by [sshleifer](https://huggingface.co/sshleifer).
|
||||
|
||||
## Naming
|
||||
|
||||
- All model names use the following format: `Helsinki-NLP/opus-mt-{src}-{tgt}`
|
||||
- The language codes used to name models are inconsistent. Two digit codes can usually be found [here](https://developers.google.com/admin-sdk/directory/v1/languages), three digit codes require googling "language
|
||||
code {code}".
|
||||
- Codes formatted like `es_AR` are usually `code_{region}`. That one is Spanish from Argentina.
|
||||
- The models were converted in two stages. The first 1000 models use ISO-639-2 codes to identify languages, the second
|
||||
group use a combination of ISO-639-5 codes and ISO-639-2 codes.
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
- Since Marian models are smaller than many other translation models available in the library, they can be useful for
|
||||
fine-tuning experiments and integration tests.
|
||||
- [Fine-tune on GPU](https://github.com/huggingface/transformers/blob/master/examples/legacy/seq2seq/train_distil_marian_enro.sh)
|
||||
|
||||
## Multilingual Models
|
||||
|
||||
- All model names use the following format: `Helsinki-NLP/opus-mt-{src}-{tgt}`:
|
||||
- If a model can output multiple languages, and you should specify a language code by prepending the desired output
|
||||
language to the `src_text`.
|
||||
- You can see a models's supported language codes in its model card, under target constituents, like in [opus-mt-en-roa](https://huggingface.co/Helsinki-NLP/opus-mt-en-roa).
|
||||
- Note that if a model is only multilingual on the source side, like `Helsinki-NLP/opus-mt-roa-en`, no language
|
||||
codes are required.
|
||||
|
||||
New multi-lingual models from the [Tatoeba-Challenge repo](https://github.com/Helsinki-NLP/Tatoeba-Challenge)
|
||||
require 3 character language codes:
|
||||
|
||||
```python
|
||||
>>> from transformers import MarianMTModel, MarianTokenizer
|
||||
|
||||
>>> src_text = [
|
||||
... ">>fra<< this is a sentence in english that we want to translate to french",
|
||||
... ">>por<< This should go to portuguese",
|
||||
... ">>esp<< And this to Spanish",
|
||||
... ]
|
||||
|
||||
>>> model_name = "Helsinki-NLP/opus-mt-en-roa"
|
||||
>>> tokenizer = MarianTokenizer.from_pretrained(model_name)
|
||||
>>> print(tokenizer.supported_language_codes)
|
||||
['>>zlm_Latn<<', '>>mfe<<', '>>hat<<', '>>pap<<', '>>ast<<', '>>cat<<', '>>ind<<', '>>glg<<', '>>wln<<', '>>spa<<', '>>fra<<', '>>ron<<', '>>por<<', '>>ita<<', '>>oci<<', '>>arg<<', '>>min<<']
|
||||
|
||||
>>> model = MarianMTModel.from_pretrained(model_name)
|
||||
>>> translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True))
|
||||
>>> [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
|
||||
["c'est une phrase en anglais que nous voulons traduire en français",
|
||||
'Isto deve ir para o português.',
|
||||
'Y esto al español']
|
||||
```
|
||||
|
||||
Here is the code to see all available pretrained models on the hub:
|
||||
|
||||
```python
|
||||
from huggingface_hub import list_models
|
||||
|
||||
model_list = list_models()
|
||||
org = "Helsinki-NLP"
|
||||
model_ids = [x.modelId for x in model_list if x.modelId.startswith(org)]
|
||||
suffix = [x.split("/")[1] for x in model_ids]
|
||||
old_style_multi_models = [f"{org}/{s}" for s in suffix if s != s.lower()]
|
||||
```
|
||||
|
||||
## Old Style Multi-Lingual Models
|
||||
|
||||
These are the old style multi-lingual models ported from the OPUS-MT-Train repo: and the members of each language
|
||||
group:
|
||||
|
||||
```python no-style
|
||||
['Helsinki-NLP/opus-mt-NORTH_EU-NORTH_EU',
|
||||
'Helsinki-NLP/opus-mt-ROMANCE-en',
|
||||
'Helsinki-NLP/opus-mt-SCANDINAVIA-SCANDINAVIA',
|
||||
'Helsinki-NLP/opus-mt-de-ZH',
|
||||
'Helsinki-NLP/opus-mt-en-CELTIC',
|
||||
'Helsinki-NLP/opus-mt-en-ROMANCE',
|
||||
'Helsinki-NLP/opus-mt-es-NORWAY',
|
||||
'Helsinki-NLP/opus-mt-fi-NORWAY',
|
||||
'Helsinki-NLP/opus-mt-fi-ZH',
|
||||
'Helsinki-NLP/opus-mt-fi_nb_no_nn_ru_sv_en-SAMI',
|
||||
'Helsinki-NLP/opus-mt-sv-NORWAY',
|
||||
'Helsinki-NLP/opus-mt-sv-ZH']
|
||||
GROUP_MEMBERS = {
|
||||
'ZH': ['cmn', 'cn', 'yue', 'ze_zh', 'zh_cn', 'zh_CN', 'zh_HK', 'zh_tw', 'zh_TW', 'zh_yue', 'zhs', 'zht', 'zh'],
|
||||
'ROMANCE': ['fr', 'fr_BE', 'fr_CA', 'fr_FR', 'wa', 'frp', 'oc', 'ca', 'rm', 'lld', 'fur', 'lij', 'lmo', 'es', 'es_AR', 'es_CL', 'es_CO', 'es_CR', 'es_DO', 'es_EC', 'es_ES', 'es_GT', 'es_HN', 'es_MX', 'es_NI', 'es_PA', 'es_PE', 'es_PR', 'es_SV', 'es_UY', 'es_VE', 'pt', 'pt_br', 'pt_BR', 'pt_PT', 'gl', 'lad', 'an', 'mwl', 'it', 'it_IT', 'co', 'nap', 'scn', 'vec', 'sc', 'ro', 'la'],
|
||||
'NORTH_EU': ['de', 'nl', 'fy', 'af', 'da', 'fo', 'is', 'no', 'nb', 'nn', 'sv'],
|
||||
'SCANDINAVIA': ['da', 'fo', 'is', 'no', 'nb', 'nn', 'sv'],
|
||||
'SAMI': ['se', 'sma', 'smj', 'smn', 'sms'],
|
||||
'NORWAY': ['nb_NO', 'nb', 'nn_NO', 'nn', 'nog', 'no_nb', 'no'],
|
||||
'CELTIC': ['ga', 'cy', 'br', 'gd', 'kw', 'gv']
|
||||
}
|
||||
```
|
||||
|
||||
Example of translating english to many romance languages, using old-style 2 character language codes
|
||||
|
||||
|
||||
```python
|
||||
>>> from transformers import MarianMTModel, MarianTokenizer
|
||||
|
||||
>>> src_text = [
|
||||
... ">>fr<< this is a sentence in english that we want to translate to french",
|
||||
... ">>pt<< This should go to portuguese",
|
||||
... ">>es<< And this to Spanish",
|
||||
... ]
|
||||
|
||||
>>> model_name = "Helsinki-NLP/opus-mt-en-ROMANCE"
|
||||
>>> tokenizer = MarianTokenizer.from_pretrained(model_name)
|
||||
|
||||
>>> model = MarianMTModel.from_pretrained(model_name)
|
||||
>>> translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True))
|
||||
>>> tgt_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
|
||||
["c'est une phrase en anglais que nous voulons traduire en français",
|
||||
'Isto deve ir para o português.',
|
||||
'Y esto al español']
|
||||
```
|
||||
|
||||
## MarianConfig
|
||||
|
||||
[[autodoc]] MarianConfig
|
||||
|
||||
## MarianTokenizer
|
||||
|
||||
[[autodoc]] MarianTokenizer
|
||||
- as_target_tokenizer
|
||||
|
||||
## MarianModel
|
||||
|
||||
[[autodoc]] MarianModel
|
||||
- forward
|
||||
|
||||
## MarianMTModel
|
||||
|
||||
[[autodoc]] MarianMTModel
|
||||
- forward
|
||||
|
||||
## MarianForCausalLM
|
||||
|
||||
[[autodoc]] MarianForCausalLM
|
||||
- forward
|
||||
|
||||
## TFMarianModel
|
||||
|
||||
[[autodoc]] TFMarianModel
|
||||
- call
|
||||
|
||||
## TFMarianMTModel
|
||||
|
||||
[[autodoc]] TFMarianMTModel
|
||||
- call
|
||||
|
||||
## FlaxMarianModel
|
||||
|
||||
[[autodoc]] FlaxMarianModel
|
||||
- __call__
|
||||
|
||||
## FlaxMarianMTModel
|
||||
|
||||
[[autodoc]] FlaxMarianMTModel
|
||||
- __call__
|
||||
71
docs/source/en/model_doc/maskformer.mdx
Normal file
71
docs/source/en/model_doc/maskformer.mdx
Normal file
@@ -0,0 +1,71 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MaskFormer
|
||||
|
||||
<Tip>
|
||||
|
||||
This is a recently introduced model so the API hasn't been tested extensively. There may be some bugs or slight
|
||||
breaking changes to fix it in the future. If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title).
|
||||
|
||||
</Tip>
|
||||
|
||||
## Overview
|
||||
|
||||
The MaskFormer model was proposed in [Per-Pixel Classification is Not All You Need for Semantic Segmentation](https://arxiv.org/abs/2107.06278) by Bowen Cheng, Alexander G. Schwing, Alexander Kirillov. MaskFormer addresses semantic segmentation with a mask classification paradigm instead of performing classic pixel-level classification.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Modern approaches typically formulate semantic segmentation as a per-pixel classification task, while instance-level segmentation is handled with an alternative mask classification. Our key insight: mask classification is sufficiently general to solve both semantic- and instance-level segmentation tasks in a unified manner using the exact same model, loss, and training procedure. Following this observation, we propose MaskFormer, a simple mask classification model which predicts a set of binary masks, each associated with a single global class label prediction. Overall, the proposed mask classification-based method simplifies the landscape of effective approaches to semantic and panoptic segmentation tasks and shows excellent empirical results. In particular, we observe that MaskFormer outperforms per-pixel classification baselines when the number of classes is large. Our mask classification-based method outperforms both current state-of-the-art semantic (55.6 mIoU on ADE20K) and panoptic segmentation (52.7 PQ on COCO) models.*
|
||||
|
||||
Tips:
|
||||
- MaskFormer's Transformer decoder is identical to the decoder of [DETR](detr). During training, the authors of DETR did find it helpful to use auxiliary losses in the decoder, especially to help the model output the correct number of objects of each class. If you set the parameter `use_auxilary_loss` of [`MaskFormerConfig`] to `True`, then prediction feedforward neural networks and Hungarian losses are added after each decoder layer (with the FFNs sharing parameters).
|
||||
- If you want to train the model in a distributed environment across multiple nodes, then one should update the
|
||||
`get_num_masks` function inside in the `MaskFormerLoss` class of `modeling_maskformer.py`. When training on multiple nodes, this should be
|
||||
set to the average number of target masks across all nodes, as can be seen in the original implementation [here](https://github.com/facebookresearch/MaskFormer/blob/da3e60d85fdeedcb31476b5edd7d328826ce56cc/mask_former/modeling/criterion.py#L169).
|
||||
- One can use [`MaskFormerFeatureExtractor`] to prepare images for the model and optional targets for the model.
|
||||
- To get the final segmentation, depending on the task, you can call [`~MaskFormerFeatureExtractor.post_process_semantic_segmentation`] or [`~MaskFormerFeatureExtractor.post_process_panoptic_segmentation`]. Both tasks can be solved using [`MaskFormerForInstanceSegmentation`] output, the latter needs an additional `is_thing_map` to know which instances must be merged together..
|
||||
|
||||
The figure below illustrates the architecture of MaskFormer. Taken from the [original paper](https://arxiv.org/abs/2107.06278).
|
||||
|
||||
<img width="600" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/maskformer_architecture.png"/>
|
||||
|
||||
This model was contributed by [francesco](https://huggingface.co/francesco). The original code can be found [here](https://github.com/facebookresearch/MaskFormer).
|
||||
|
||||
## MaskFormer specific outputs
|
||||
|
||||
[[autodoc]] models.maskformer.modeling_maskformer.MaskFormerModelOutput
|
||||
|
||||
[[autodoc]] models.maskformer.modeling_maskformer.MaskFormerForInstanceSegmentationOutput
|
||||
|
||||
## MaskFormerConfig
|
||||
|
||||
[[autodoc]] MaskFormerConfig
|
||||
|
||||
## MaskFormerFeatureExtractor
|
||||
|
||||
[[autodoc]] MaskFormerFeatureExtractor
|
||||
- __call__
|
||||
- encode_inputs
|
||||
- post_process_segmentation
|
||||
- post_process_semantic_segmentation
|
||||
- post_process_panoptic_segmentation
|
||||
|
||||
## MaskFormerModel
|
||||
|
||||
[[autodoc]] MaskFormerModel
|
||||
- forward
|
||||
|
||||
## MaskFormerForInstanceSegmentation
|
||||
|
||||
[[autodoc]] MaskFormerForInstanceSegmentation
|
||||
- forward
|
||||
229
docs/source/en/model_doc/mbart.mdx
Normal file
229
docs/source/en/model_doc/mbart.mdx
Normal file
@@ -0,0 +1,229 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MBart and MBart-50
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
|
||||
@patrickvonplaten
|
||||
|
||||
## Overview of MBart
|
||||
|
||||
The MBart model was presented in [Multilingual Denoising Pre-training for Neural Machine Translation](https://arxiv.org/abs/2001.08210) by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov Marjan
|
||||
Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
|
||||
|
||||
According to the abstract, MBART is a sequence-to-sequence denoising auto-encoder pretrained on large-scale monolingual
|
||||
corpora in many languages using the BART objective. mBART is one of the first methods for pretraining a complete
|
||||
sequence-to-sequence model by denoising full texts in multiple languages, while previous approaches have focused only
|
||||
on the encoder, decoder, or reconstructing parts of the text.
|
||||
|
||||
This model was contributed by [valhalla](https://huggingface.co/valhalla). The Authors' code can be found [here](https://github.com/pytorch/fairseq/tree/master/examples/mbart)
|
||||
|
||||
### Training of MBart
|
||||
|
||||
MBart is a multilingual encoder-decoder (sequence-to-sequence) model primarily intended for translation task. As the
|
||||
model is multilingual it expects the sequences in a different format. A special language id token is added in both the
|
||||
source and target text. The source text format is `X [eos, src_lang_code]` where `X` is the source text. The
|
||||
target text format is `[tgt_lang_code] X [eos]`. `bos` is never used.
|
||||
|
||||
The regular [`~MBartTokenizer.__call__`] will encode source text format, and it should be wrapped
|
||||
inside the context manager [`~MBartTokenizer.as_target_tokenizer`] to encode target text format.
|
||||
|
||||
- Supervised training
|
||||
|
||||
```python
|
||||
>>> from transformers import MBartForConditionalGeneration, MBartTokenizer
|
||||
|
||||
>>> tokenizer = MBartTokenizer.from_pretrained("facebook/mbart-large-en-ro", src_lang="en_XX", tgt_lang="ro_RO")
|
||||
>>> example_english_phrase = "UN Chief Says There Is No Military Solution in Syria"
|
||||
>>> expected_translation_romanian = "Şeful ONU declară că nu există o soluţie militară în Siria"
|
||||
|
||||
>>> inputs = tokenizer(example_english_phrase, return_tensors="pt")
|
||||
>>> with tokenizer.as_target_tokenizer():
|
||||
... labels = tokenizer(expected_translation_romanian, return_tensors="pt")
|
||||
|
||||
>>> model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-en-ro")
|
||||
>>> # forward pass
|
||||
>>> model(**inputs, labels=batch["labels"])
|
||||
```
|
||||
|
||||
- Generation
|
||||
|
||||
While generating the target text set the `decoder_start_token_id` to the target language id. The following
|
||||
example shows how to translate English to Romanian using the *facebook/mbart-large-en-ro* model.
|
||||
|
||||
```python
|
||||
>>> from transformers import MBartForConditionalGeneration, MBartTokenizer
|
||||
|
||||
>>> tokenizer = MBartTokenizer.from_pretrained("facebook/mbart-large-en-ro", src_lang="en_XX")
|
||||
>>> article = "UN Chief Says There Is No Military Solution in Syria"
|
||||
>>> inputs = tokenizer(article, return_tensors="pt")
|
||||
>>> translated_tokens = model.generate(**inputs, decoder_start_token_id=tokenizer.lang_code_to_id["ro_RO"])
|
||||
>>> tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
||||
"Şeful ONU declară că nu există o soluţie militară în Siria"
|
||||
```
|
||||
|
||||
## Overview of MBart-50
|
||||
|
||||
MBart-50 was introduced in the [Multilingual Translation with Extensible Multilingual Pretraining and Finetuning](https://arxiv.org/abs/2008.00401) paper by Yuqing Tang, Chau Tran, Xian Li, Peng-Jen Chen, Naman Goyal, Vishrav
|
||||
Chaudhary, Jiatao Gu, Angela Fan. MBart-50 is created using the original *mbart-large-cc25* checkpoint by extendeding
|
||||
its embedding layers with randomly initialized vectors for an extra set of 25 language tokens and then pretrained on 50
|
||||
languages.
|
||||
|
||||
According to the abstract
|
||||
|
||||
*Multilingual translation models can be created through multilingual finetuning. Instead of finetuning on one
|
||||
direction, a pretrained model is finetuned on many directions at the same time. It demonstrates that pretrained models
|
||||
can be extended to incorporate additional languages without loss of performance. Multilingual finetuning improves on
|
||||
average 1 BLEU over the strongest baselines (being either multilingual from scratch or bilingual finetuning) while
|
||||
improving 9.3 BLEU on average over bilingual baselines from scratch.*
|
||||
|
||||
|
||||
### Training of MBart-50
|
||||
|
||||
The text format for MBart-50 is slightly different from mBART. For MBart-50 the language id token is used as a prefix
|
||||
for both source and target text i.e the text format is `[lang_code] X [eos]`, where `lang_code` is source
|
||||
language id for source text and target language id for target text, with `X` being the source or target text
|
||||
respectively.
|
||||
|
||||
|
||||
MBart-50 has its own tokenizer [`MBart50Tokenizer`].
|
||||
|
||||
- Supervised training
|
||||
|
||||
```python
|
||||
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
||||
|
||||
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50")
|
||||
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50", src_lang="en_XX", tgt_lang="ro_RO")
|
||||
|
||||
src_text = " UN Chief Says There Is No Military Solution in Syria"
|
||||
tgt_text = "Şeful ONU declară că nu există o soluţie militară în Siria"
|
||||
|
||||
model_inputs = tokenizer(src_text, return_tensors="pt")
|
||||
with tokenizer.as_target_tokenizer():
|
||||
labels = tokenizer(tgt_text, return_tensors="pt").input_ids
|
||||
|
||||
model(**model_inputs, labels=labels) # forward pass
|
||||
```
|
||||
|
||||
- Generation
|
||||
|
||||
To generate using the mBART-50 multilingual translation models, `eos_token_id` is used as the
|
||||
`decoder_start_token_id` and the target language id is forced as the first generated token. To force the
|
||||
target language id as the first generated token, pass the *forced_bos_token_id* parameter to the *generate* method.
|
||||
The following example shows how to translate between Hindi to French and Arabic to English using the
|
||||
*facebook/mbart-50-large-many-to-many* checkpoint.
|
||||
|
||||
```python
|
||||
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
||||
|
||||
article_hi = "संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है"
|
||||
article_ar = "الأمين العام للأمم المتحدة يقول إنه لا يوجد حل عسكري في سوريا."
|
||||
|
||||
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
|
||||
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
|
||||
|
||||
# translate Hindi to French
|
||||
tokenizer.src_lang = "hi_IN"
|
||||
encoded_hi = tokenizer(article_hi, return_tensors="pt")
|
||||
generated_tokens = model.generate(**encoded_hi, forced_bos_token_id=tokenizer.lang_code_to_id["fr_XX"])
|
||||
tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
||||
# => "Le chef de l 'ONU affirme qu 'il n 'y a pas de solution militaire en Syria."
|
||||
|
||||
# translate Arabic to English
|
||||
tokenizer.src_lang = "ar_AR"
|
||||
encoded_ar = tokenizer(article_ar, return_tensors="pt")
|
||||
generated_tokens = model.generate(**encoded_ar, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
||||
tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
||||
# => "The Secretary-General of the United Nations says there is no military solution in Syria."
|
||||
```
|
||||
|
||||
## MBartConfig
|
||||
|
||||
[[autodoc]] MBartConfig
|
||||
|
||||
## MBartTokenizer
|
||||
|
||||
[[autodoc]] MBartTokenizer
|
||||
- as_target_tokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## MBartTokenizerFast
|
||||
|
||||
[[autodoc]] MBartTokenizerFast
|
||||
|
||||
## MBart50Tokenizer
|
||||
|
||||
[[autodoc]] MBart50Tokenizer
|
||||
|
||||
## MBart50TokenizerFast
|
||||
|
||||
[[autodoc]] MBart50TokenizerFast
|
||||
|
||||
## MBartModel
|
||||
|
||||
[[autodoc]] MBartModel
|
||||
|
||||
## MBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] MBartForConditionalGeneration
|
||||
|
||||
## MBartForQuestionAnswering
|
||||
|
||||
[[autodoc]] MBartForQuestionAnswering
|
||||
|
||||
## MBartForSequenceClassification
|
||||
|
||||
[[autodoc]] MBartForSequenceClassification
|
||||
|
||||
## MBartForCausalLM
|
||||
|
||||
[[autodoc]] MBartForCausalLM
|
||||
- forward
|
||||
|
||||
## TFMBartModel
|
||||
|
||||
[[autodoc]] TFMBartModel
|
||||
- call
|
||||
|
||||
## TFMBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFMBartForConditionalGeneration
|
||||
- call
|
||||
|
||||
## FlaxMBartModel
|
||||
|
||||
[[autodoc]] FlaxMBartModel
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxMBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxMBartForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxMBartForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxMBartForSequenceClassification
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxMBartForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxMBartForQuestionAnswering
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
128
docs/source/en/model_doc/megatron-bert.mdx
Normal file
128
docs/source/en/model_doc/megatron-bert.mdx
Normal file
@@ -0,0 +1,128 @@
|
||||
<!--Copyright 2021 NVIDIA Corporation and The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MegatronBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The MegatronBERT model was proposed in [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model
|
||||
Parallelism](https://arxiv.org/abs/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley,
|
||||
Jared Casper and Bryan Catanzaro.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recent work in language modeling demonstrates that training large transformer models advances the state of the art in
|
||||
Natural Language Processing applications. However, very large models can be quite difficult to train due to memory
|
||||
constraints. In this work, we present our techniques for training very large transformer models and implement a simple,
|
||||
efficient intra-layer model parallel approach that enables training transformer models with billions of parameters. Our
|
||||
approach does not require a new compiler or library changes, is orthogonal and complimentary to pipeline model
|
||||
parallelism, and can be fully implemented with the insertion of a few communication operations in native PyTorch. We
|
||||
illustrate this approach by converging transformer based models up to 8.3 billion parameters using 512 GPUs. We sustain
|
||||
15.1 PetaFLOPs across the entire application with 76% scaling efficiency when compared to a strong single GPU baseline
|
||||
that sustains 39 TeraFLOPs, which is 30% of peak FLOPs. To demonstrate that large language models can further advance
|
||||
the state of the art (SOTA), we train an 8.3 billion parameter transformer language model similar to GPT-2 and a 3.9
|
||||
billion parameter model similar to BERT. We show that careful attention to the placement of layer normalization in
|
||||
BERT-like models is critical to achieving increased performance as the model size grows. Using the GPT-2 model we
|
||||
achieve SOTA results on the WikiText103 (10.8 compared to SOTA perplexity of 15.8) and LAMBADA (66.5% compared to SOTA
|
||||
accuracy of 63.2%) datasets. Our BERT model achieves SOTA results on the RACE dataset (90.9% compared to SOTA accuracy
|
||||
of 89.4%).*
|
||||
|
||||
Tips:
|
||||
|
||||
We have provided pretrained [BERT-345M](https://ngc.nvidia.com/catalog/models/nvidia:megatron_bert_345m) checkpoints
|
||||
for use to evaluate or finetuning downstream tasks.
|
||||
|
||||
To access these checkpoints, first [sign up](https://ngc.nvidia.com/signup) for and setup the NVIDIA GPU Cloud (NGC)
|
||||
Registry CLI. Further documentation for downloading models can be found in the [NGC documentation](https://docs.nvidia.com/dgx/ngc-registry-cli-user-guide/index.html#topic_6_4_1).
|
||||
|
||||
Alternatively, you can directly download the checkpoints using:
|
||||
|
||||
BERT-345M-uncased:
|
||||
|
||||
```bash
|
||||
wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_bert_345m/versions/v0.1_uncased/zip
|
||||
-O megatron_bert_345m_v0_1_uncased.zip
|
||||
```
|
||||
|
||||
BERT-345M-cased:
|
||||
|
||||
```bash
|
||||
wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_bert_345m/versions/v0.1_cased/zip -O
|
||||
megatron_bert_345m_v0_1_cased.zip
|
||||
```
|
||||
|
||||
Once you have obtained the checkpoints from NVIDIA GPU Cloud (NGC), you have to convert them to a format that will
|
||||
easily be loaded by Hugging Face Transformers and our port of the BERT code.
|
||||
|
||||
The following commands allow you to do the conversion. We assume that the folder `models/megatron_bert` contains
|
||||
`megatron_bert_345m_v0_1_{cased, uncased}.zip` and that the commands are run from inside that folder:
|
||||
|
||||
```bash
|
||||
python3 $PATH_TO_TRANSFORMERS/models/megatron_bert/convert_megatron_bert_checkpoint.py megatron_bert_345m_v0_1_uncased.zip
|
||||
```
|
||||
|
||||
```bash
|
||||
python3 $PATH_TO_TRANSFORMERS/models/megatron_bert/convert_megatron_bert_checkpoint.py megatron_bert_345m_v0_1_cased.zip
|
||||
```
|
||||
|
||||
This model was contributed by [jdemouth](https://huggingface.co/jdemouth). The original code can be found [here](https://github.com/NVIDIA/Megatron-LM). That repository contains a multi-GPU and multi-node implementation of the
|
||||
Megatron Language models. In particular, it contains a hybrid model parallel approach using "tensor parallel" and
|
||||
"pipeline parallel" techniques.
|
||||
|
||||
## MegatronBertConfig
|
||||
|
||||
[[autodoc]] MegatronBertConfig
|
||||
|
||||
## MegatronBertModel
|
||||
|
||||
[[autodoc]] MegatronBertModel
|
||||
- forward
|
||||
|
||||
## MegatronBertForMaskedLM
|
||||
|
||||
[[autodoc]] MegatronBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## MegatronBertForCausalLM
|
||||
|
||||
[[autodoc]] MegatronBertForCausalLM
|
||||
- forward
|
||||
|
||||
## MegatronBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] MegatronBertForNextSentencePrediction
|
||||
- forward
|
||||
|
||||
## MegatronBertForPreTraining
|
||||
|
||||
[[autodoc]] MegatronBertForPreTraining
|
||||
- forward
|
||||
|
||||
## MegatronBertForSequenceClassification
|
||||
|
||||
[[autodoc]] MegatronBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## MegatronBertForMultipleChoice
|
||||
|
||||
[[autodoc]] MegatronBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## MegatronBertForTokenClassification
|
||||
|
||||
[[autodoc]] MegatronBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## MegatronBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] MegatronBertForQuestionAnswering
|
||||
- forward
|
||||
67
docs/source/en/model_doc/megatron_gpt2.mdx
Normal file
67
docs/source/en/model_doc/megatron_gpt2.mdx
Normal file
@@ -0,0 +1,67 @@
|
||||
<!--Copyright 2021 NVIDIA Corporation and The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MegatronGPT2
|
||||
|
||||
## Overview
|
||||
|
||||
The MegatronGPT2 model was proposed in [Megatron-LM: Training Multi-Billion Parameter Language Models Using Model
|
||||
Parallelism](https://arxiv.org/abs/1909.08053) by Mohammad Shoeybi, Mostofa Patwary, Raul Puri, Patrick LeGresley,
|
||||
Jared Casper and Bryan Catanzaro.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recent work in language modeling demonstrates that training large transformer models advances the state of the art in
|
||||
Natural Language Processing applications. However, very large models can be quite difficult to train due to memory
|
||||
constraints. In this work, we present our techniques for training very large transformer models and implement a simple,
|
||||
efficient intra-layer model parallel approach that enables training transformer models with billions of parameters. Our
|
||||
approach does not require a new compiler or library changes, is orthogonal and complimentary to pipeline model
|
||||
parallelism, and can be fully implemented with the insertion of a few communication operations in native PyTorch. We
|
||||
illustrate this approach by converging transformer based models up to 8.3 billion parameters using 512 GPUs. We sustain
|
||||
15.1 PetaFLOPs across the entire application with 76% scaling efficiency when compared to a strong single GPU baseline
|
||||
that sustains 39 TeraFLOPs, which is 30% of peak FLOPs. To demonstrate that large language models can further advance
|
||||
the state of the art (SOTA), we train an 8.3 billion parameter transformer language model similar to GPT-2 and a 3.9
|
||||
billion parameter model similar to BERT. We show that careful attention to the placement of layer normalization in
|
||||
BERT-like models is critical to achieving increased performance as the model size grows. Using the GPT-2 model we
|
||||
achieve SOTA results on the WikiText103 (10.8 compared to SOTA perplexity of 15.8) and LAMBADA (66.5% compared to SOTA
|
||||
accuracy of 63.2%) datasets. Our BERT model achieves SOTA results on the RACE dataset (90.9% compared to SOTA accuracy
|
||||
of 89.4%).*
|
||||
|
||||
Tips:
|
||||
|
||||
We have provided pretrained [GPT2-345M](https://ngc.nvidia.com/catalog/models/nvidia:megatron_lm_345m) checkpoints
|
||||
for use to evaluate or finetuning downstream tasks.
|
||||
|
||||
To access these checkpoints, first [sign up](https://ngc.nvidia.com/signup) for and setup the NVIDIA GPU Cloud (NGC)
|
||||
Registry CLI. Further documentation for downloading models can be found in the [NGC documentation](https://docs.nvidia.com/dgx/ngc-registry-cli-user-guide/index.html#topic_6_4_1).
|
||||
|
||||
Alternatively, you can directly download the checkpoints using:
|
||||
|
||||
```bash
|
||||
wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_lm_345m/versions/v0.0/zip -O
|
||||
megatron_gpt2_345m_v0_0.zip
|
||||
```
|
||||
|
||||
Once you have obtained the checkpoint from NVIDIA GPU Cloud (NGC), you have to convert it to a format that will easily
|
||||
be loaded by Hugging Face Transformers GPT2 implementation.
|
||||
|
||||
The following command allows you to do the conversion. We assume that the folder `models/megatron_gpt2` contains
|
||||
`megatron_gpt2_345m_v0_0.zip` and that the command is run from that folder:
|
||||
|
||||
```bash
|
||||
python3 $PATH_TO_TRANSFORMERS/models/megatron_gpt2/convert_megatron_gpt2_checkpoint.py megatron_gpt2_345m_v0_0.zip
|
||||
```
|
||||
|
||||
This model was contributed by [jdemouth](https://huggingface.co/jdemouth). The original code can be found [here](https://github.com/NVIDIA/Megatron-LM). That repository contains a multi-GPU and multi-node implementation of the
|
||||
Megatron Language models. In particular, it contains a hybrid model parallel approach using "tensor parallel" and
|
||||
"pipeline parallel" techniques.
|
||||
|
||||
61
docs/source/en/model_doc/mluke.mdx
Normal file
61
docs/source/en/model_doc/mluke.mdx
Normal file
@@ -0,0 +1,61 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# mLUKE
|
||||
|
||||
## Overview
|
||||
|
||||
The mLUKE model was proposed in [mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models](https://arxiv.org/abs/2110.08151) by Ryokan Ri, Ikuya Yamada, and Yoshimasa Tsuruoka. It's a multilingual extension
|
||||
of the [LUKE model](https://arxiv.org/abs/2010.01057) trained on the basis of XLM-RoBERTa.
|
||||
|
||||
It is based on XLM-RoBERTa and adds entity embeddings, which helps improve performance on various downstream tasks
|
||||
involving reasoning about entities such as named entity recognition, extractive question answering, relation
|
||||
classification, cloze-style knowledge completion.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Recent studies have shown that multilingual pretrained language models can be effectively improved with cross-lingual
|
||||
alignment information from Wikipedia entities. However, existing methods only exploit entity information in pretraining
|
||||
and do not explicitly use entities in downstream tasks. In this study, we explore the effectiveness of leveraging
|
||||
entity representations for downstream cross-lingual tasks. We train a multilingual language model with 24 languages
|
||||
with entity representations and show the model consistently outperforms word-based pretrained models in various
|
||||
cross-lingual transfer tasks. We also analyze the model and the key insight is that incorporating entity
|
||||
representations into the input allows us to extract more language-agnostic features. We also evaluate the model with a
|
||||
multilingual cloze prompt task with the mLAMA dataset. We show that entity-based prompt elicits correct factual
|
||||
knowledge more likely than using only word representations.*
|
||||
|
||||
One can directly plug in the weights of mLUKE into a LUKE model, like so:
|
||||
|
||||
```python
|
||||
from transformers import LukeModel
|
||||
|
||||
model = LukeModel.from_pretrained("studio-ousia/mluke-base")
|
||||
```
|
||||
|
||||
Note that mLUKE has its own tokenizer, [`MLukeTokenizer`]. You can initialize it as follows:
|
||||
|
||||
```python
|
||||
from transformers import MLukeTokenizer
|
||||
|
||||
tokenizer = MLukeTokenizer.from_pretrained("studio-ousia/mluke-base")
|
||||
```
|
||||
|
||||
As mLUKE's architecture is equivalent to that of LUKE, one can refer to [LUKE's documentation page](luke) for all
|
||||
tips, code examples and notebooks.
|
||||
|
||||
This model was contributed by [ryo0634](https://huggingface.co/ryo0634). The original code can be found [here](https://github.com/studio-ousia/luke).
|
||||
|
||||
## MLukeTokenizer
|
||||
|
||||
[[autodoc]] MLukeTokenizer
|
||||
- __call__
|
||||
- save_vocabulary
|
||||
142
docs/source/en/model_doc/mobilebert.mdx
Normal file
142
docs/source/en/model_doc/mobilebert.mdx
Normal file
@@ -0,0 +1,142 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MobileBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The MobileBERT model was proposed in [MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices](https://arxiv.org/abs/2004.02984) by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny
|
||||
Zhou. It's a bidirectional transformer based on the BERT model, which is compressed and accelerated using several
|
||||
approaches.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Natural Language Processing (NLP) has recently achieved great success by using huge pre-trained models with hundreds
|
||||
of millions of parameters. However, these models suffer from heavy model sizes and high latency such that they cannot
|
||||
be deployed to resource-limited mobile devices. In this paper, we propose MobileBERT for compressing and accelerating
|
||||
the popular BERT model. Like the original BERT, MobileBERT is task-agnostic, that is, it can be generically applied to
|
||||
various downstream NLP tasks via simple fine-tuning. Basically, MobileBERT is a thin version of BERT_LARGE, while
|
||||
equipped with bottleneck structures and a carefully designed balance between self-attentions and feed-forward networks.
|
||||
To train MobileBERT, we first train a specially designed teacher model, an inverted-bottleneck incorporated BERT_LARGE
|
||||
model. Then, we conduct knowledge transfer from this teacher to MobileBERT. Empirical studies show that MobileBERT is
|
||||
4.3x smaller and 5.5x faster than BERT_BASE while achieving competitive results on well-known benchmarks. On the
|
||||
natural language inference tasks of GLUE, MobileBERT achieves a GLUEscore o 77.7 (0.6 lower than BERT_BASE), and 62 ms
|
||||
latency on a Pixel 4 phone. On the SQuAD v1.1/v2.0 question answering task, MobileBERT achieves a dev F1 score of
|
||||
90.0/79.2 (1.5/2.1 higher than BERT_BASE).*
|
||||
|
||||
Tips:
|
||||
|
||||
- MobileBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather
|
||||
than the left.
|
||||
- MobileBERT is similar to BERT and therefore relies on the masked language modeling (MLM) objective. It is therefore
|
||||
efficient at predicting masked tokens and at NLU in general, but is not optimal for text generation. Models trained
|
||||
with a causal language modeling (CLM) objective are better in that regard.
|
||||
|
||||
This model was contributed by [vshampor](https://huggingface.co/vshampor). The original code can be found [here](https://github.com/google-research/mobilebert).
|
||||
|
||||
## MobileBertConfig
|
||||
|
||||
[[autodoc]] MobileBertConfig
|
||||
|
||||
## MobileBertTokenizer
|
||||
|
||||
[[autodoc]] MobileBertTokenizer
|
||||
|
||||
## MobileBertTokenizerFast
|
||||
|
||||
[[autodoc]] MobileBertTokenizerFast
|
||||
|
||||
## MobileBert specific outputs
|
||||
|
||||
[[autodoc]] models.mobilebert.modeling_mobilebert.MobileBertForPreTrainingOutput
|
||||
|
||||
[[autodoc]] models.mobilebert.modeling_tf_mobilebert.TFMobileBertForPreTrainingOutput
|
||||
|
||||
## MobileBertModel
|
||||
|
||||
[[autodoc]] MobileBertModel
|
||||
- forward
|
||||
|
||||
## MobileBertForPreTraining
|
||||
|
||||
[[autodoc]] MobileBertForPreTraining
|
||||
- forward
|
||||
|
||||
## MobileBertForMaskedLM
|
||||
|
||||
[[autodoc]] MobileBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## MobileBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] MobileBertForNextSentencePrediction
|
||||
- forward
|
||||
|
||||
## MobileBertForSequenceClassification
|
||||
|
||||
[[autodoc]] MobileBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## MobileBertForMultipleChoice
|
||||
|
||||
[[autodoc]] MobileBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## MobileBertForTokenClassification
|
||||
|
||||
[[autodoc]] MobileBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## MobileBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] MobileBertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFMobileBertModel
|
||||
|
||||
[[autodoc]] TFMobileBertModel
|
||||
- call
|
||||
|
||||
## TFMobileBertForPreTraining
|
||||
|
||||
[[autodoc]] TFMobileBertForPreTraining
|
||||
- call
|
||||
|
||||
## TFMobileBertForMaskedLM
|
||||
|
||||
[[autodoc]] TFMobileBertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFMobileBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] TFMobileBertForNextSentencePrediction
|
||||
- call
|
||||
|
||||
## TFMobileBertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFMobileBertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFMobileBertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFMobileBertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFMobileBertForTokenClassification
|
||||
|
||||
[[autodoc]] TFMobileBertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFMobileBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFMobileBertForQuestionAnswering
|
||||
- call
|
||||
117
docs/source/en/model_doc/mpnet.mdx
Normal file
117
docs/source/en/model_doc/mpnet.mdx
Normal file
@@ -0,0 +1,117 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# MPNet
|
||||
|
||||
## Overview
|
||||
|
||||
The MPNet model was proposed in [MPNet: Masked and Permuted Pre-training for Language Understanding](https://arxiv.org/abs/2004.09297) by Kaitao Song, Xu Tan, Tao Qin, Jianfeng Lu, Tie-Yan Liu.
|
||||
|
||||
MPNet adopts a novel pre-training method, named masked and permuted language modeling, to inherit the advantages of
|
||||
masked language modeling and permuted language modeling for natural language understanding.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*BERT adopts masked language modeling (MLM) for pre-training and is one of the most successful pre-training models.
|
||||
Since BERT neglects dependency among predicted tokens, XLNet introduces permuted language modeling (PLM) for
|
||||
pre-training to address this problem. However, XLNet does not leverage the full position information of a sentence and
|
||||
thus suffers from position discrepancy between pre-training and fine-tuning. In this paper, we propose MPNet, a novel
|
||||
pre-training method that inherits the advantages of BERT and XLNet and avoids their limitations. MPNet leverages the
|
||||
dependency among predicted tokens through permuted language modeling (vs. MLM in BERT), and takes auxiliary position
|
||||
information as input to make the model see a full sentence and thus reducing the position discrepancy (vs. PLM in
|
||||
XLNet). We pre-train MPNet on a large-scale dataset (over 160GB text corpora) and fine-tune on a variety of
|
||||
down-streaming tasks (GLUE, SQuAD, etc). Experimental results show that MPNet outperforms MLM and PLM by a large
|
||||
margin, and achieves better results on these tasks compared with previous state-of-the-art pre-trained methods (e.g.,
|
||||
BERT, XLNet, RoBERTa) under the same model setting.*
|
||||
|
||||
Tips:
|
||||
|
||||
- MPNet doesn't have `token_type_ids`, you don't need to indicate which token belongs to which segment. just
|
||||
separate your segments with the separation token `tokenizer.sep_token` (or `[sep]`).
|
||||
|
||||
The original code can be found [here](https://github.com/microsoft/MPNet).
|
||||
|
||||
## MPNetConfig
|
||||
|
||||
[[autodoc]] MPNetConfig
|
||||
|
||||
## MPNetTokenizer
|
||||
|
||||
[[autodoc]] MPNetTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## MPNetTokenizerFast
|
||||
|
||||
[[autodoc]] MPNetTokenizerFast
|
||||
|
||||
## MPNetModel
|
||||
|
||||
[[autodoc]] MPNetModel
|
||||
- forward
|
||||
|
||||
## MPNetForMaskedLM
|
||||
|
||||
[[autodoc]] MPNetForMaskedLM
|
||||
- forward
|
||||
|
||||
## MPNetForSequenceClassification
|
||||
|
||||
[[autodoc]] MPNetForSequenceClassification
|
||||
- forward
|
||||
|
||||
## MPNetForMultipleChoice
|
||||
|
||||
[[autodoc]] MPNetForMultipleChoice
|
||||
- forward
|
||||
|
||||
## MPNetForTokenClassification
|
||||
|
||||
[[autodoc]] MPNetForTokenClassification
|
||||
- forward
|
||||
|
||||
## MPNetForQuestionAnswering
|
||||
|
||||
[[autodoc]] MPNetForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFMPNetModel
|
||||
|
||||
[[autodoc]] TFMPNetModel
|
||||
- call
|
||||
|
||||
## TFMPNetForMaskedLM
|
||||
|
||||
[[autodoc]] TFMPNetForMaskedLM
|
||||
- call
|
||||
|
||||
## TFMPNetForSequenceClassification
|
||||
|
||||
[[autodoc]] TFMPNetForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFMPNetForMultipleChoice
|
||||
|
||||
[[autodoc]] TFMPNetForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFMPNetForTokenClassification
|
||||
|
||||
[[autodoc]] TFMPNetForTokenClassification
|
||||
- call
|
||||
|
||||
## TFMPNetForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFMPNetForQuestionAnswering
|
||||
- call
|
||||
98
docs/source/en/model_doc/mt5.mdx
Normal file
98
docs/source/en/model_doc/mt5.mdx
Normal file
@@ -0,0 +1,98 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# mT5
|
||||
|
||||
## Overview
|
||||
|
||||
The mT5 model was presented in [mT5: A massively multilingual pre-trained text-to-text transformer](https://arxiv.org/abs/2010.11934) by Linting Xue, Noah Constant, Adam Roberts, Mihir Kale, Rami Al-Rfou, Aditya
|
||||
Siddhant, Aditya Barua, Colin Raffel.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*The recent "Text-to-Text Transfer Transformer" (T5) leveraged a unified text-to-text format and scale to attain
|
||||
state-of-the-art results on a wide variety of English-language NLP tasks. In this paper, we introduce mT5, a
|
||||
multilingual variant of T5 that was pre-trained on a new Common Crawl-based dataset covering 101 languages. We detail
|
||||
the design and modified training of mT5 and demonstrate its state-of-the-art performance on many multilingual
|
||||
benchmarks. We also describe a simple technique to prevent "accidental translation" in the zero-shot setting, where a
|
||||
generative model chooses to (partially) translate its prediction into the wrong language. All of the code and model
|
||||
checkpoints used in this work are publicly available.*
|
||||
|
||||
Note: mT5 was only pre-trained on [mC4](https://huggingface.co/datasets/mc4) excluding any supervised training.
|
||||
Therefore, this model has to be fine-tuned before it is useable on a downstream task, unlike the original T5 model.
|
||||
Since mT5 was pre-trained unsupervisedly, there's no real advantage to using a task prefix during single-task
|
||||
fine-tuning. If you are doing multi-task fine-tuning, you should use a prefix.
|
||||
|
||||
Google has released the following variants:
|
||||
|
||||
- [google/mt5-small](https://huggingface.co/google/mt5-small)
|
||||
|
||||
- [google/mt5-base](https://huggingface.co/google/mt5-base)
|
||||
|
||||
- [google/mt5-large](https://huggingface.co/google/mt5-large)
|
||||
|
||||
- [google/mt5-xl](https://huggingface.co/google/mt5-xl)
|
||||
|
||||
- [google/mt5-xxl](https://huggingface.co/google/mt5-xxl).
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The original code can be
|
||||
found [here](https://github.com/google-research/multilingual-t5).
|
||||
|
||||
## MT5Config
|
||||
|
||||
[[autodoc]] MT5Config
|
||||
|
||||
## MT5Tokenizer
|
||||
|
||||
[[autodoc]] MT5Tokenizer
|
||||
|
||||
See [`T5Tokenizer`] for all details.
|
||||
|
||||
|
||||
## MT5TokenizerFast
|
||||
|
||||
[[autodoc]] MT5TokenizerFast
|
||||
|
||||
See [`T5TokenizerFast`] for all details.
|
||||
|
||||
|
||||
## MT5Model
|
||||
|
||||
[[autodoc]] MT5Model
|
||||
|
||||
## MT5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] MT5ForConditionalGeneration
|
||||
|
||||
## MT5EncoderModel
|
||||
|
||||
[[autodoc]] MT5EncoderModel
|
||||
|
||||
## TFMT5Model
|
||||
|
||||
[[autodoc]] TFMT5Model
|
||||
|
||||
## TFMT5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFMT5ForConditionalGeneration
|
||||
|
||||
## TFMT5EncoderModel
|
||||
|
||||
[[autodoc]] TFMT5EncoderModel
|
||||
|
||||
## FlaxMT5Model
|
||||
|
||||
[[autodoc]] FlaxMT5Model
|
||||
|
||||
## FlaxMT5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxMT5ForConditionalGeneration
|
||||
68
docs/source/en/model_doc/nystromformer.mdx
Normal file
68
docs/source/en/model_doc/nystromformer.mdx
Normal file
@@ -0,0 +1,68 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Nyströmformer
|
||||
|
||||
## Overview
|
||||
|
||||
The Nyströmformer model was proposed in [*Nyströmformer: A Nyström-Based Algorithm for Approximating Self-Attention*](https://arxiv.org/abs/2102.03902) by Yunyang Xiong, Zhanpeng Zeng, Rudrasis Chakraborty, Mingxing Tan, Glenn
|
||||
Fung, Yin Li, and Vikas Singh.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformers have emerged as a powerful tool for a broad range of natural language processing tasks. A key component
|
||||
that drives the impressive performance of Transformers is the self-attention mechanism that encodes the influence or
|
||||
dependence of other tokens on each specific token. While beneficial, the quadratic complexity of self-attention on the
|
||||
input sequence length has limited its application to longer sequences -- a topic being actively studied in the
|
||||
community. To address this limitation, we propose Nyströmformer -- a model that exhibits favorable scalability as a
|
||||
function of sequence length. Our idea is based on adapting the Nyström method to approximate standard self-attention
|
||||
with O(n) complexity. The scalability of Nyströmformer enables application to longer sequences with thousands of
|
||||
tokens. We perform evaluations on multiple downstream tasks on the GLUE benchmark and IMDB reviews with standard
|
||||
sequence length, and find that our Nyströmformer performs comparably, or in a few cases, even slightly better, than
|
||||
standard self-attention. On longer sequence tasks in the Long Range Arena (LRA) benchmark, Nyströmformer performs
|
||||
favorably relative to other efficient self-attention methods. Our code is available at this https URL.*
|
||||
|
||||
This model was contributed by [novice03](https://huggingface.co/novice03). The original code can be found [here](https://github.com/mlpen/Nystromformer).
|
||||
|
||||
## NystromformerConfig
|
||||
|
||||
[[autodoc]] NystromformerConfig
|
||||
|
||||
## NystromformerModel
|
||||
|
||||
[[autodoc]] NystromformerModel
|
||||
- forward
|
||||
|
||||
## NystromformerForMaskedLM
|
||||
|
||||
[[autodoc]] NystromformerForMaskedLM
|
||||
- forward
|
||||
|
||||
## NystromformerForSequenceClassification
|
||||
|
||||
[[autodoc]] NystromformerForSequenceClassification
|
||||
- forward
|
||||
|
||||
## NystromformerForMultipleChoice
|
||||
|
||||
[[autodoc]] NystromformerForMultipleChoice
|
||||
- forward
|
||||
|
||||
## NystromformerForTokenClassification
|
||||
|
||||
[[autodoc]] NystromformerForTokenClassification
|
||||
- forward
|
||||
|
||||
## NystromformerForQuestionAnswering
|
||||
|
||||
[[autodoc]] NystromformerForQuestionAnswering
|
||||
- forward
|
||||
117
docs/source/en/model_doc/openai-gpt.mdx
Normal file
117
docs/source/en/model_doc/openai-gpt.mdx
Normal file
@@ -0,0 +1,117 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# OpenAI GPT
|
||||
|
||||
## Overview
|
||||
|
||||
OpenAI GPT model was proposed in [Improving Language Understanding by Generative Pre-Training](https://s3-us-west-2.amazonaws.com/openai-assets/research-covers/language-unsupervised/language_understanding_paper.pdf)
|
||||
by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever. It's a causal (unidirectional) transformer
|
||||
pre-trained using language modeling on a large corpus will long range dependencies, the Toronto Book Corpus.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Natural language understanding comprises a wide range of diverse tasks such as textual entailment, question answering,
|
||||
semantic similarity assessment, and document classification. Although large unlabeled text corpora are abundant,
|
||||
labeled data for learning these specific tasks is scarce, making it challenging for discriminatively trained models to
|
||||
perform adequately. We demonstrate that large gains on these tasks can be realized by generative pretraining of a
|
||||
language model on a diverse corpus of unlabeled text, followed by discriminative fine-tuning on each specific task. In
|
||||
contrast to previous approaches, we make use of task-aware input transformations during fine-tuning to achieve
|
||||
effective transfer while requiring minimal changes to the model architecture. We demonstrate the effectiveness of our
|
||||
approach on a wide range of benchmarks for natural language understanding. Our general task-agnostic model outperforms
|
||||
discriminatively trained models that use architectures specifically crafted for each task, significantly improving upon
|
||||
the state of the art in 9 out of the 12 tasks studied.*
|
||||
|
||||
Tips:
|
||||
|
||||
- GPT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than
|
||||
the left.
|
||||
- GPT was trained with a causal language modeling (CLM) objective and is therefore powerful at predicting the next
|
||||
token in a sequence. Leveraging this feature allows GPT-2 to generate syntactically coherent text as it can be
|
||||
observed in the *run_generation.py* example script.
|
||||
|
||||
[Write With Transformer](https://transformer.huggingface.co/doc/gpt) is a webapp created and hosted by Hugging Face
|
||||
showcasing the generative capabilities of several models. GPT is one of them.
|
||||
|
||||
This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/openai/finetune-transformer-lm).
|
||||
|
||||
Note:
|
||||
|
||||
If you want to reproduce the original tokenization process of the *OpenAI GPT* paper, you will need to install `ftfy`
|
||||
and `SpaCy`:
|
||||
|
||||
```bash
|
||||
pip install spacy ftfy==4.4.3
|
||||
python -m spacy download en
|
||||
```
|
||||
|
||||
If you don't install `ftfy` and `SpaCy`, the [`OpenAIGPTTokenizer`] will default to tokenize
|
||||
using BERT's `BasicTokenizer` followed by Byte-Pair Encoding (which should be fine for most usage, don't worry).
|
||||
|
||||
## OpenAIGPTConfig
|
||||
|
||||
[[autodoc]] OpenAIGPTConfig
|
||||
|
||||
## OpenAIGPTTokenizer
|
||||
|
||||
[[autodoc]] OpenAIGPTTokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## OpenAIGPTTokenizerFast
|
||||
|
||||
[[autodoc]] OpenAIGPTTokenizerFast
|
||||
|
||||
## OpenAI specific outputs
|
||||
|
||||
[[autodoc]] models.openai.modeling_openai.OpenAIGPTDoubleHeadsModelOutput
|
||||
|
||||
[[autodoc]] models.openai.modeling_tf_openai.TFOpenAIGPTDoubleHeadsModelOutput
|
||||
|
||||
## OpenAIGPTModel
|
||||
|
||||
[[autodoc]] OpenAIGPTModel
|
||||
- forward
|
||||
|
||||
## OpenAIGPTLMHeadModel
|
||||
|
||||
[[autodoc]] OpenAIGPTLMHeadModel
|
||||
- forward
|
||||
|
||||
## OpenAIGPTDoubleHeadsModel
|
||||
|
||||
[[autodoc]] OpenAIGPTDoubleHeadsModel
|
||||
- forward
|
||||
|
||||
## OpenAIGPTForSequenceClassification
|
||||
|
||||
[[autodoc]] OpenAIGPTForSequenceClassification
|
||||
- forward
|
||||
|
||||
## TFOpenAIGPTModel
|
||||
|
||||
[[autodoc]] TFOpenAIGPTModel
|
||||
- call
|
||||
|
||||
## TFOpenAIGPTLMHeadModel
|
||||
|
||||
[[autodoc]] TFOpenAIGPTLMHeadModel
|
||||
- call
|
||||
|
||||
## TFOpenAIGPTDoubleHeadsModel
|
||||
|
||||
[[autodoc]] TFOpenAIGPTDoubleHeadsModel
|
||||
- call
|
||||
|
||||
## TFOpenAIGPTForSequenceClassification
|
||||
|
||||
[[autodoc]] TFOpenAIGPTForSequenceClassification
|
||||
- call
|
||||
141
docs/source/en/model_doc/pegasus.mdx
Normal file
141
docs/source/en/model_doc/pegasus.mdx
Normal file
@@ -0,0 +1,141 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Pegasus
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=sshleifer&labels=&template=bug-report.md&title)
|
||||
and assign @patrickvonplaten.
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
The Pegasus model was proposed in [PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization](https://arxiv.org/pdf/1912.08777.pdf) by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu on Dec 18, 2019.
|
||||
|
||||
According to the abstract,
|
||||
|
||||
- Pegasus' pretraining task is intentionally similar to summarization: important sentences are removed/masked from an
|
||||
input document and are generated together as one output sequence from the remaining sentences, similar to an
|
||||
extractive summary.
|
||||
- Pegasus achieves SOTA summarization performance on all 12 downstream tasks, as measured by ROUGE and human eval.
|
||||
|
||||
This model was contributed by [sshleifer](https://huggingface.co/sshleifer). The Authors' code can be found [here](https://github.com/google-research/pegasus).
|
||||
|
||||
|
||||
## Checkpoints
|
||||
|
||||
All the [checkpoints](https://huggingface.co/models?search=pegasus) are fine-tuned for summarization, besides
|
||||
*pegasus-large*, whence the other checkpoints are fine-tuned:
|
||||
|
||||
- Each checkpoint is 2.2 GB on disk and 568M parameters.
|
||||
- FP16 is not supported (help/ideas on this appreciated!).
|
||||
- Summarizing xsum in fp32 takes about 400ms/sample, with default parameters on a v100 GPU.
|
||||
- Full replication results and correctly pre-processed data can be found in this [Issue](https://github.com/huggingface/transformers/issues/6844#issue-689259666).
|
||||
- [Distilled checkpoints](https://huggingface.co/models?search=distill-pegasus) are described in this [paper](https://arxiv.org/abs/2010.13002).
|
||||
|
||||
### Examples
|
||||
|
||||
- [Script](https://github.com/huggingface/transformers/tree/main/examples/research_projects/seq2seq-distillation/finetune_pegasus_xsum.sh) to fine-tune pegasus
|
||||
on the XSUM dataset. Data download instructions at [examples/pytorch/summarization/](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization/README.md).
|
||||
- FP16 is not supported (help/ideas on this appreciated!).
|
||||
- The adafactor optimizer is recommended for pegasus fine-tuning.
|
||||
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- All models are transformer encoder-decoders with 16 layers in each component.
|
||||
- The implementation is completely inherited from [`BartForConditionalGeneration`]
|
||||
- Some key configuration differences:
|
||||
|
||||
- static, sinusoidal position embeddings
|
||||
- the model starts generating with pad_token_id (which has 0 token_embedding) as the prefix.
|
||||
- more beams are used (`num_beams=8`)
|
||||
- All pretrained pegasus checkpoints are the same besides three attributes: `tokenizer.model_max_length` (maximum
|
||||
input size), `max_length` (the maximum number of tokens to generate) and `length_penalty`.
|
||||
- The code to convert checkpoints trained in the author's [repo](https://github.com/google-research/pegasus) can be
|
||||
found in `convert_pegasus_tf_to_pytorch.py`.
|
||||
|
||||
|
||||
## Usage Example
|
||||
|
||||
```python
|
||||
>>> from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
||||
>>> import torch
|
||||
|
||||
>>> src_text = [
|
||||
... """ PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow."""
|
||||
... ]
|
||||
|
||||
... model_name = "google/pegasus-xsum"
|
||||
... device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
... tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
||||
... model = PegasusForConditionalGeneration.from_pretrained(model_name).to(device)
|
||||
... batch = tokenizer(src_text, truncation=True, padding="longest", return_tensors="pt").to(device)
|
||||
... translated = model.generate(**batch)
|
||||
... tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
||||
... assert (
|
||||
... tgt_text[0]
|
||||
... == "California's largest electricity provider has turned off power to hundreds of thousands of customers."
|
||||
... )
|
||||
```
|
||||
|
||||
## PegasusConfig
|
||||
|
||||
[[autodoc]] PegasusConfig
|
||||
|
||||
## PegasusTokenizer
|
||||
|
||||
warning: `add_tokens` does not work at the moment.
|
||||
|
||||
[[autodoc]] PegasusTokenizer
|
||||
|
||||
## PegasusTokenizerFast
|
||||
|
||||
[[autodoc]] PegasusTokenizerFast
|
||||
|
||||
## PegasusModel
|
||||
|
||||
[[autodoc]] PegasusModel
|
||||
- forward
|
||||
|
||||
## PegasusForConditionalGeneration
|
||||
|
||||
[[autodoc]] PegasusForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## PegasusForCausalLM
|
||||
|
||||
[[autodoc]] PegasusForCausalLM
|
||||
- forward
|
||||
|
||||
## TFPegasusModel
|
||||
|
||||
[[autodoc]] TFPegasusModel
|
||||
- call
|
||||
|
||||
## TFPegasusForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFPegasusForConditionalGeneration
|
||||
- call
|
||||
|
||||
## FlaxPegasusModel
|
||||
|
||||
[[autodoc]] FlaxPegasusModel
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxPegasusForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxPegasusForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
215
docs/source/en/model_doc/perceiver.mdx
Normal file
215
docs/source/en/model_doc/perceiver.mdx
Normal file
@@ -0,0 +1,215 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Perceiver
|
||||
|
||||
## Overview
|
||||
|
||||
The Perceiver IO model was proposed in [Perceiver IO: A General Architecture for Structured Inputs &
|
||||
Outputs](https://arxiv.org/abs/2107.14795) by Andrew Jaegle, Sebastian Borgeaud, Jean-Baptiste Alayrac, Carl Doersch,
|
||||
Catalin Ionescu, David Ding, Skanda Koppula, Daniel Zoran, Andrew Brock, Evan Shelhamer, Olivier Hénaff, Matthew M.
|
||||
Botvinick, Andrew Zisserman, Oriol Vinyals, João Carreira.
|
||||
|
||||
Perceiver IO is a generalization of [Perceiver](https://arxiv.org/abs/2103.03206) to handle arbitrary outputs in
|
||||
addition to arbitrary inputs. The original Perceiver only produced a single classification label. In addition to
|
||||
classification labels, Perceiver IO can produce (for example) language, optical flow, and multimodal videos with audio.
|
||||
This is done using the same building blocks as the original Perceiver. The computational complexity of Perceiver IO is
|
||||
linear in the input and output size and the bulk of the processing occurs in the latent space, allowing us to process
|
||||
inputs and outputs that are much larger than can be handled by standard Transformers. This means, for example,
|
||||
Perceiver IO can do BERT-style masked language modeling directly using bytes instead of tokenized inputs.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*The recently-proposed Perceiver model obtains good results on several domains (images, audio, multimodal, point
|
||||
clouds) while scaling linearly in compute and memory with the input size. While the Perceiver supports many kinds of
|
||||
inputs, it can only produce very simple outputs such as class scores. Perceiver IO overcomes this limitation without
|
||||
sacrificing the original's appealing properties by learning to flexibly query the model's latent space to produce
|
||||
outputs of arbitrary size and semantics. Perceiver IO still decouples model depth from data size and still scales
|
||||
linearly with data size, but now with respect to both input and output sizes. The full Perceiver IO model achieves
|
||||
strong results on tasks with highly structured output spaces, such as natural language and visual understanding,
|
||||
StarCraft II, and multi-task and multi-modal domains. As highlights, Perceiver IO matches a Transformer-based BERT
|
||||
baseline on the GLUE language benchmark without the need for input tokenization and achieves state-of-the-art
|
||||
performance on Sintel optical flow estimation.*
|
||||
|
||||
Here's a TLDR explaining how Perceiver works:
|
||||
|
||||
The main problem with the self-attention mechanism of the Transformer is that the time and memory requirements scale
|
||||
quadratically with the sequence length. Hence, models like BERT and RoBERTa are limited to a max sequence length of 512
|
||||
tokens. Perceiver aims to solve this issue by, instead of performing self-attention on the inputs, perform it on a set
|
||||
of latent variables, and only use the inputs for cross-attention. In this way, the time and memory requirements don't
|
||||
depend on the length of the inputs anymore, as one uses a fixed amount of latent variables, like 256 or 512. These are
|
||||
randomly initialized, after which they are trained end-to-end using backpropagation.
|
||||
|
||||
Internally, [`PerceiverModel`] will create the latents, which is a tensor of shape `(batch_size, num_latents,
|
||||
d_latents)`. One must provide `inputs` (which could be text, images, audio, you name it!) to the model, which it will
|
||||
use to perform cross-attention with the latents. The output of the Perceiver encoder is a tensor of the same shape. One
|
||||
can then, similar to BERT, convert the last hidden states of the latents to classification logits by averaging along
|
||||
the sequence dimension, and placing a linear layer on top of that to project the `d_latents` to `num_labels`.
|
||||
|
||||
This was the idea of the original Perceiver paper. However, it could only output classification logits. In a follow-up
|
||||
work, PerceiverIO, they generalized it to let the model also produce outputs of arbitrary size. How, you might ask? The
|
||||
idea is actually relatively simple: one defines outputs of an arbitrary size, and then applies cross-attention with the
|
||||
last hidden states of the latents, using the outputs as queries, and the latents as keys and values.
|
||||
|
||||
So let's say one wants to perform masked language modeling (BERT-style) with the Perceiver. As the Perceiver's input
|
||||
length will not have an impact on the computation time of the self-attention layers, one can provide raw bytes,
|
||||
providing `inputs` of length 2048 to the model. If one now masks out certain of these 2048 tokens, one can define the
|
||||
`outputs` as being of shape: `(batch_size, 2048, 768)`. Next, one performs cross-attention with the final hidden states
|
||||
of the latents to update the `outputs` tensor. After cross-attention, one still has a tensor of shape `(batch_size,
|
||||
2048, 768)`. One can then place a regular language modeling head on top, to project the last dimension to the
|
||||
vocabulary size of the model, i.e. creating logits of shape `(batch_size, 2048, 262)` (as Perceiver uses a vocabulary
|
||||
size of 262 byte IDs).
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/perceiver_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> Perceiver IO architecture. Taken from the <a href="https://arxiv.org/abs/2105.15203">original paper</a> </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found
|
||||
[here](https://github.com/deepmind/deepmind-research/tree/master/perceiver).
|
||||
|
||||
Tips:
|
||||
|
||||
- The quickest way to get started with the Perceiver is by checking the [tutorial
|
||||
notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/Perceiver).
|
||||
- Refer to the [blog post](https://huggingface.co/blog/perceiver) if you want to fully understand how the model works and
|
||||
is implemented in the library. Note that the models available in the library only showcase some examples of what you can do
|
||||
with the Perceiver. There are many more use cases, including question answering, named-entity recognition, object detection,
|
||||
audio classification, video classification, etc.
|
||||
|
||||
**Note**:
|
||||
|
||||
- Perceiver does **not** work with `torch.nn.DataParallel` due to a bug in PyTorch, see [issue #36035](https://github.com/pytorch/pytorch/issues/36035)
|
||||
|
||||
## Perceiver specific outputs
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverModelOutput
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverDecoderOutput
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverMaskedLMOutput
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverClassifierOutput
|
||||
|
||||
## PerceiverConfig
|
||||
|
||||
[[autodoc]] PerceiverConfig
|
||||
|
||||
## PerceiverTokenizer
|
||||
|
||||
[[autodoc]] PerceiverTokenizer
|
||||
- __call__
|
||||
|
||||
## PerceiverFeatureExtractor
|
||||
|
||||
[[autodoc]] PerceiverFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## PerceiverTextPreprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverTextPreprocessor
|
||||
|
||||
## PerceiverImagePreprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverImagePreprocessor
|
||||
|
||||
## PerceiverOneHotPreprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverOneHotPreprocessor
|
||||
|
||||
## PerceiverAudioPreprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverAudioPreprocessor
|
||||
|
||||
## PerceiverMultimodalPreprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverMultimodalPreprocessor
|
||||
|
||||
## PerceiverProjectionDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverProjectionDecoder
|
||||
|
||||
## PerceiverBasicDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverBasicDecoder
|
||||
|
||||
## PerceiverClassificationDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverClassificationDecoder
|
||||
|
||||
## PerceiverOpticalFlowDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverOpticalFlowDecoder
|
||||
|
||||
## PerceiverBasicVideoAutoencodingDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverBasicVideoAutoencodingDecoder
|
||||
|
||||
## PerceiverMultimodalDecoder
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverMultimodalDecoder
|
||||
|
||||
## PerceiverProjectionPostprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverProjectionPostprocessor
|
||||
|
||||
## PerceiverAudioPostprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverAudioPostprocessor
|
||||
|
||||
## PerceiverClassificationPostprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverClassificationPostprocessor
|
||||
|
||||
## PerceiverMultimodalPostprocessor
|
||||
|
||||
[[autodoc]] models.perceiver.modeling_perceiver.PerceiverMultimodalPostprocessor
|
||||
|
||||
## PerceiverModel
|
||||
|
||||
[[autodoc]] PerceiverModel
|
||||
- forward
|
||||
|
||||
## PerceiverForMaskedLM
|
||||
|
||||
[[autodoc]] PerceiverForMaskedLM
|
||||
- forward
|
||||
|
||||
## PerceiverForSequenceClassification
|
||||
|
||||
[[autodoc]] PerceiverForSequenceClassification
|
||||
- forward
|
||||
|
||||
## PerceiverForImageClassificationLearned
|
||||
|
||||
[[autodoc]] PerceiverForImageClassificationLearned
|
||||
- forward
|
||||
|
||||
## PerceiverForImageClassificationFourier
|
||||
|
||||
[[autodoc]] PerceiverForImageClassificationFourier
|
||||
- forward
|
||||
|
||||
## PerceiverForImageClassificationConvProcessing
|
||||
|
||||
[[autodoc]] PerceiverForImageClassificationConvProcessing
|
||||
- forward
|
||||
|
||||
## PerceiverForOpticalFlow
|
||||
|
||||
[[autodoc]] PerceiverForOpticalFlow
|
||||
- forward
|
||||
|
||||
## PerceiverForMultimodalAutoencoding
|
||||
|
||||
[[autodoc]] PerceiverForMultimodalAutoencoding
|
||||
- forward
|
||||
53
docs/source/en/model_doc/phobert.mdx
Normal file
53
docs/source/en/model_doc/phobert.mdx
Normal file
@@ -0,0 +1,53 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# PhoBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The PhoBERT model was proposed in [PhoBERT: Pre-trained language models for Vietnamese](https://www.aclweb.org/anthology/2020.findings-emnlp.92.pdf) by Dat Quoc Nguyen, Anh Tuan Nguyen.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present PhoBERT with two versions, PhoBERT-base and PhoBERT-large, the first public large-scale monolingual
|
||||
language models pre-trained for Vietnamese. Experimental results show that PhoBERT consistently outperforms the recent
|
||||
best pre-trained multilingual model XLM-R (Conneau et al., 2020) and improves the state-of-the-art in multiple
|
||||
Vietnamese-specific NLP tasks including Part-of-speech tagging, Dependency parsing, Named-entity recognition and
|
||||
Natural language inference.*
|
||||
|
||||
Example of use:
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import AutoModel, AutoTokenizer
|
||||
|
||||
>>> phobert = AutoModel.from_pretrained("vinai/phobert-base")
|
||||
>>> tokenizer = AutoTokenizer.from_pretrained("vinai/phobert-base")
|
||||
|
||||
>>> # INPUT TEXT MUST BE ALREADY WORD-SEGMENTED!
|
||||
>>> line = "Tôi là sinh_viên trường đại_học Công_nghệ ."
|
||||
|
||||
>>> input_ids = torch.tensor([tokenizer.encode(line)])
|
||||
|
||||
>>> with torch.no_grad():
|
||||
... features = phobert(input_ids) # Models outputs are now tuples
|
||||
|
||||
>>> # With TensorFlow 2.0+:
|
||||
>>> # from transformers import TFAutoModel
|
||||
>>> # phobert = TFAutoModel.from_pretrained("vinai/phobert-base")
|
||||
```
|
||||
|
||||
This model was contributed by [dqnguyen](https://huggingface.co/dqnguyen). The original code can be found [here](https://github.com/VinAIResearch/PhoBERT).
|
||||
|
||||
## PhobertTokenizer
|
||||
|
||||
[[autodoc]] PhobertTokenizer
|
||||
112
docs/source/en/model_doc/plbart.mdx
Normal file
112
docs/source/en/model_doc/plbart.mdx
Normal file
@@ -0,0 +1,112 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# PLBart
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
|
||||
[@gchhablani](https://www.github.com/gchhablani).
|
||||
|
||||
## Overview of PLBart
|
||||
|
||||
The PLBART model was proposed in [Unified Pre-training for Program Understanding and Generation](https://arxiv.org/abs/2103.06333) by Wasi Uddin Ahmad, Saikat Chakraborty, Baishakhi Ray, Kai-Wei Chang.
|
||||
This is a BART-like model which can be used to perform code-summarization, code-generation, and code-translation tasks. The pre-trained model `plbart-base` has been trained using multilingual denoising task
|
||||
on Java, Python and English.
|
||||
|
||||
According to the abstract
|
||||
|
||||
*Code summarization and generation empower conversion between programming language (PL) and natural language (NL),
|
||||
while code translation avails the migration of legacy code from one PL to another. This paper introduces PLBART,
|
||||
a sequence-to-sequence model capable of performing a broad spectrum of program and language understanding and generation tasks.
|
||||
PLBART is pre-trained on an extensive collection of Java and Python functions and associated NL text via denoising autoencoding.
|
||||
Experiments on code summarization in the English language, code generation, and code translation in seven programming languages
|
||||
show that PLBART outperforms or rivals state-of-the-art models. Moreover, experiments on discriminative tasks, e.g., program
|
||||
repair, clone detection, and vulnerable code detection, demonstrate PLBART's effectiveness in program understanding.
|
||||
Furthermore, analysis reveals that PLBART learns program syntax, style (e.g., identifier naming convention), logical flow
|
||||
(e.g., if block inside an else block is equivalent to else if block) that are crucial to program semantics and thus excels
|
||||
even with limited annotations.*
|
||||
|
||||
This model was contributed by [gchhablani](https://huggingface.co/gchhablani). The Authors' code can be found [here](https://github.com/wasiahmad/PLBART).
|
||||
|
||||
### Training of PLBart
|
||||
|
||||
PLBart is a multilingual encoder-decoder (sequence-to-sequence) model primarily intended for code-to-text, text-to-code, code-to-code tasks. As the
|
||||
model is multilingual it expects the sequences in a different format. A special language id token is added in both the
|
||||
source and target text. The source text format is `X [eos, src_lang_code]` where `X` is the source text. The
|
||||
target text format is `[tgt_lang_code] X [eos]`. `bos` is never used.
|
||||
|
||||
However, for fine-tuning, in some cases no language token is provided in cases where a single language is used. Please refer to [the paper](https://arxiv.org/abs/2103.06333) to learn more about this.
|
||||
|
||||
In cases where the language code is needed, The regular [`~PLBartTokenizer.__call__`] will encode source text format, and it should be wrapped
|
||||
inside the context manager [`~PLBartTokenizer.as_target_tokenizer`] to encode target text format.
|
||||
|
||||
- Supervised training
|
||||
|
||||
```python
|
||||
>>> from transformers import PLBartForConditionalGeneration, PLBartTokenizer
|
||||
|
||||
>>> tokenizer = PLBartTokenizer.from_pretrained("uclanlp/plbart-base", src_lang="en_XX", tgt_lang="python")
|
||||
>>> example_python_phrase = "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])"
|
||||
>>> expected_translation_english = "Returns the maximum value of a b c."
|
||||
>>> inputs = tokenizer(example_python_phrase, return_tensors="pt")
|
||||
>>> with tokenizer.as_target_tokenizer():
|
||||
... labels = tokenizer(expected_translation_english, return_tensors="pt")
|
||||
>>> inputs["labels"] = labels["input_ids"]
|
||||
>>> # forward pass
|
||||
>>> model(**inputs)
|
||||
```
|
||||
|
||||
- Generation
|
||||
|
||||
While generating the target text set the `decoder_start_token_id` to the target language id. The following
|
||||
example shows how to translate Python to English using the `uclanlp/plbart-python-en_XX` model.
|
||||
|
||||
```python
|
||||
>>> from transformers import PLBartForConditionalGeneration, PLBartTokenizer
|
||||
|
||||
>>> tokenizer = PLBartTokenizer.from_pretrained("uclanlp/plbart-python-en_XX", src_lang="python", tgt_lang="en_XX")
|
||||
>>> example_python_phrase = "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])"
|
||||
>>> inputs = tokenizer(example_python_phrase, return_tensors="pt")
|
||||
>>> model = PLBartForConditionalGeneration.from_pretrained("uclanlp/plbart-python-en_XX")
|
||||
>>> translated_tokens = model.generate(**inputs, decoder_start_token_id=tokenizer.lang_code_to_id["en_XX"])
|
||||
>>> tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
||||
"Returns the maximum value of a b c."
|
||||
```
|
||||
|
||||
## PLBartConfig
|
||||
|
||||
[[autodoc]] PLBartConfig
|
||||
|
||||
## PLBartTokenizer
|
||||
|
||||
[[autodoc]] PLBartTokenizer
|
||||
- as_target_tokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## PLBartModel
|
||||
|
||||
[[autodoc]] PLBartModel
|
||||
- forward
|
||||
|
||||
## PLBartForConditionalGeneration
|
||||
|
||||
[[autodoc]] PLBartForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## PLBartForSequenceClassification
|
||||
|
||||
[[autodoc]] PLBartForSequenceClassification
|
||||
- forward
|
||||
|
||||
## PLBartForCausalLM
|
||||
|
||||
[[autodoc]] PLBartForCausalLM
|
||||
- forward
|
||||
61
docs/source/en/model_doc/poolformer.mdx
Normal file
61
docs/source/en/model_doc/poolformer.mdx
Normal file
@@ -0,0 +1,61 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# PoolFormer
|
||||
|
||||
## Overview
|
||||
|
||||
The PoolFormer model was proposed in [MetaFormer is Actually What You Need for Vision](https://arxiv.org/abs/2111.11418) by Sea AI Labs. Instead of designing complicated token mixer to achieve SOTA performance, the target of this work is to demonstrate the competence of transformer models largely stem from the general architecture MetaFormer.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformers have shown great potential in computer vision tasks. A common belief is their attention-based token mixer module contributes most to their competence. However, recent works show the attention-based module in transformers can be replaced by spatial MLPs and the resulted models still perform quite well. Based on this observation, we hypothesize that the general architecture of the transformers, instead of the specific token mixer module, is more essential to the model's performance. To verify this, we deliberately replace the attention module in transformers with an embarrassingly simple spatial pooling operator to conduct only the most basic token mixing. Surprisingly, we observe that the derived model, termed as PoolFormer, achieves competitive performance on multiple computer vision tasks. For example, on ImageNet-1K, PoolFormer achieves 82.1% top-1 accuracy, surpassing well-tuned vision transformer/MLP-like baselines DeiT-B/ResMLP-B24 by 0.3%/1.1% accuracy with 35%/52% fewer parameters and 48%/60% fewer MACs. The effectiveness of PoolFormer verifies our hypothesis and urges us to initiate the concept of "MetaFormer", a general architecture abstracted from transformers without specifying the token mixer. Based on the extensive experiments, we argue that MetaFormer is the key player in achieving superior results for recent transformer and MLP-like models on vision tasks. This work calls for more future research dedicated to improving MetaFormer instead of focusing on the token mixer modules. Additionally, our proposed PoolFormer could serve as a starting baseline for future MetaFormer architecture design.*
|
||||
|
||||
The figure below illustrates the architecture of PoolFormer. Taken from the [original paper](https://arxiv.org/abs/2111.11418).
|
||||
|
||||
<img width="600" src="https://user-images.githubusercontent.com/15921929/142746124-1ab7635d-2536-4a0e-ad43-b4fe2c5a525d.png"/>
|
||||
|
||||
|
||||
Tips:
|
||||
|
||||
- PoolFormer has a hierarchical architecture, where instead of Attention, a simple Average Pooling layer is present. All checkpoints of the model can be found on the [hub](https://huggingface.co/models?other=poolformer).
|
||||
- One can use [`PoolFormerFeatureExtractor`] to prepare images for the model.
|
||||
- As most models, PoolFormer comes in different sizes, the details of which can be found in the table below.
|
||||
|
||||
| **Model variant** | **Depths** | **Hidden sizes** | **Params (M)** | **ImageNet-1k Top 1** |
|
||||
| :---------------: | ------------- | ------------------- | :------------: | :-------------------: |
|
||||
| s12 | [2, 2, 6, 2] | [64, 128, 320, 512] | 12 | 77.2 |
|
||||
| s24 | [4, 4, 12, 4] | [64, 128, 320, 512] | 21 | 80.3 |
|
||||
| s36 | [6, 6, 18, 6] | [64, 128, 320, 512] | 31 | 81.4 |
|
||||
| m36 | [6, 6, 18, 6] | [96, 192, 384, 768] | 56 | 82.1 |
|
||||
| m48 | [8, 8, 24, 8] | [96, 192, 384, 768] | 73 | 82.5 |
|
||||
|
||||
This model was contributed by [heytanay](https://huggingface.co/heytanay). The original code can be found [here](https://github.com/sail-sg/poolformer).
|
||||
|
||||
## PoolFormerConfig
|
||||
|
||||
[[autodoc]] PoolFormerConfig
|
||||
|
||||
## PoolFormerFeatureExtractor
|
||||
|
||||
[[autodoc]] PoolFormerFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## PoolFormerModel
|
||||
|
||||
[[autodoc]] PoolFormerModel
|
||||
- forward
|
||||
|
||||
## PoolFormerForImageClassification
|
||||
|
||||
[[autodoc]] PoolFormerForImageClassification
|
||||
- forward
|
||||
82
docs/source/en/model_doc/prophetnet.mdx
Normal file
82
docs/source/en/model_doc/prophetnet.mdx
Normal file
@@ -0,0 +1,82 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ProphetNet
|
||||
|
||||
**DISCLAIMER:** If you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title) and assign
|
||||
@patrickvonplaten
|
||||
|
||||
## Overview
|
||||
|
||||
The ProphetNet model was proposed in [ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training,](https://arxiv.org/abs/2001.04063) by Yu Yan, Weizhen Qi, Yeyun Gong, Dayiheng Liu, Nan Duan, Jiusheng Chen, Ruofei
|
||||
Zhang, Ming Zhou on 13 Jan, 2020.
|
||||
|
||||
ProphetNet is an encoder-decoder model and can predict n-future tokens for "ngram" language modeling instead of just
|
||||
the next token.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*In this paper, we present a new sequence-to-sequence pretraining model called ProphetNet, which introduces a novel
|
||||
self-supervised objective named future n-gram prediction and the proposed n-stream self-attention mechanism. Instead of
|
||||
the optimization of one-step ahead prediction in traditional sequence-to-sequence model, the ProphetNet is optimized by
|
||||
n-step ahead prediction which predicts the next n tokens simultaneously based on previous context tokens at each time
|
||||
step. The future n-gram prediction explicitly encourages the model to plan for the future tokens and prevent
|
||||
overfitting on strong local correlations. We pre-train ProphetNet using a base scale dataset (16GB) and a large scale
|
||||
dataset (160GB) respectively. Then we conduct experiments on CNN/DailyMail, Gigaword, and SQuAD 1.1 benchmarks for
|
||||
abstractive summarization and question generation tasks. Experimental results show that ProphetNet achieves new
|
||||
state-of-the-art results on all these datasets compared to the models using the same scale pretraining corpus.*
|
||||
|
||||
The Authors' code can be found [here](https://github.com/microsoft/ProphetNet).
|
||||
|
||||
|
||||
## ProphetNetConfig
|
||||
|
||||
[[autodoc]] ProphetNetConfig
|
||||
|
||||
## ProphetNetTokenizer
|
||||
|
||||
[[autodoc]] ProphetNetTokenizer
|
||||
|
||||
## ProphetNet specific outputs
|
||||
|
||||
[[autodoc]] models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqLMOutput
|
||||
|
||||
[[autodoc]] models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqModelOutput
|
||||
|
||||
[[autodoc]] models.prophetnet.modeling_prophetnet.ProphetNetDecoderModelOutput
|
||||
|
||||
[[autodoc]] models.prophetnet.modeling_prophetnet.ProphetNetDecoderLMOutput
|
||||
|
||||
## ProphetNetModel
|
||||
|
||||
[[autodoc]] ProphetNetModel
|
||||
- forward
|
||||
|
||||
## ProphetNetEncoder
|
||||
|
||||
[[autodoc]] ProphetNetEncoder
|
||||
- forward
|
||||
|
||||
## ProphetNetDecoder
|
||||
|
||||
[[autodoc]] ProphetNetDecoder
|
||||
- forward
|
||||
|
||||
## ProphetNetForConditionalGeneration
|
||||
|
||||
[[autodoc]] ProphetNetForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## ProphetNetForCausalLM
|
||||
|
||||
[[autodoc]] ProphetNetForCausalLM
|
||||
- forward
|
||||
159
docs/source/en/model_doc/qdqbert.mdx
Normal file
159
docs/source/en/model_doc/qdqbert.mdx
Normal file
@@ -0,0 +1,159 @@
|
||||
<!--Copyright 2021 NVIDIA Corporation and The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# QDQBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The QDQBERT model can be referenced in [Integer Quantization for Deep Learning Inference: Principles and Empirical
|
||||
Evaluation](https://arxiv.org/abs/2004.09602) by Hao Wu, Patrick Judd, Xiaojie Zhang, Mikhail Isaev and Paulius
|
||||
Micikevicius.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Quantization techniques can reduce the size of Deep Neural Networks and improve inference latency and throughput by
|
||||
taking advantage of high throughput integer instructions. In this paper we review the mathematical aspects of
|
||||
quantization parameters and evaluate their choices on a wide range of neural network models for different application
|
||||
domains, including vision, speech, and language. We focus on quantization techniques that are amenable to acceleration
|
||||
by processors with high-throughput integer math pipelines. We also present a workflow for 8-bit quantization that is
|
||||
able to maintain accuracy within 1% of the floating-point baseline on all networks studied, including models that are
|
||||
more difficult to quantize, such as MobileNets and BERT-large.*
|
||||
|
||||
Tips:
|
||||
|
||||
- QDQBERT model adds fake quantization operations (pair of QuantizeLinear/DequantizeLinear ops) to (i) linear layer
|
||||
inputs and weights, (ii) matmul inputs, (iii) residual add inputs, in BERT model.
|
||||
|
||||
- QDQBERT requires the dependency of [Pytorch Quantization Toolkit](https://github.com/NVIDIA/TensorRT/tree/master/tools/pytorch-quantization). To install `pip install pytorch-quantization --extra-index-url https://pypi.ngc.nvidia.com`
|
||||
|
||||
- QDQBERT model can be loaded from any checkpoint of HuggingFace BERT model (for example *bert-base-uncased*), and
|
||||
perform Quantization Aware Training/Post Training Quantization.
|
||||
|
||||
- A complete example of using QDQBERT model to perform Quatization Aware Training and Post Training Quantization for
|
||||
SQUAD task can be found at [transformers/examples/research_projects/quantization-qdqbert/](examples/research_projects/quantization-qdqbert/).
|
||||
|
||||
This model was contributed by [shangz](https://huggingface.co/shangz).
|
||||
|
||||
|
||||
### Set default quantizers
|
||||
|
||||
QDQBERT model adds fake quantization operations (pair of QuantizeLinear/DequantizeLinear ops) to BERT by
|
||||
`TensorQuantizer` in [Pytorch Quantization Toolkit](https://github.com/NVIDIA/TensorRT/tree/master/tools/pytorch-quantization). `TensorQuantizer` is the module
|
||||
for quantizing tensors, with `QuantDescriptor` defining how the tensor should be quantized. Refer to [Pytorch
|
||||
Quantization Toolkit userguide](https://docs.nvidia.com/deeplearning/tensorrt/pytorch-quantization-toolkit/docs/userguide.html) for more details.
|
||||
|
||||
Before creating QDQBERT model, one has to set the default `QuantDescriptor` defining default tensor quantizers.
|
||||
|
||||
Example:
|
||||
|
||||
```python
|
||||
>>> import pytorch_quantization.nn as quant_nn
|
||||
>>> from pytorch_quantization.tensor_quant import QuantDescriptor
|
||||
|
||||
>>> # The default tensor quantizer is set to use Max calibration method
|
||||
>>> input_desc = QuantDescriptor(num_bits=8, calib_method="max")
|
||||
>>> # The default tensor quantizer is set to be per-channel quantization for weights
|
||||
>>> weight_desc = QuantDescriptor(num_bits=8, axis=((0,)))
|
||||
>>> quant_nn.QuantLinear.set_default_quant_desc_input(input_desc)
|
||||
>>> quant_nn.QuantLinear.set_default_quant_desc_weight(weight_desc)
|
||||
```
|
||||
|
||||
### Calibration
|
||||
|
||||
Calibration is the terminology of passing data samples to the quantizer and deciding the best scaling factors for
|
||||
tensors. After setting up the tensor quantizers, one can use the following example to calibrate the model:
|
||||
|
||||
```python
|
||||
>>> # Find the TensorQuantizer and enable calibration
|
||||
>>> for name, module in model.named_modules():
|
||||
... if name.endswith("_input_quantizer"):
|
||||
... module.enable_calib()
|
||||
... module.disable_quant() # Use full precision data to calibrate
|
||||
|
||||
>>> # Feeding data samples
|
||||
>>> model(x)
|
||||
>>> # ...
|
||||
|
||||
>>> # Finalize calibration
|
||||
>>> for name, module in model.named_modules():
|
||||
... if name.endswith("_input_quantizer"):
|
||||
... module.load_calib_amax()
|
||||
... module.enable_quant()
|
||||
|
||||
>>> # If running on GPU, it needs to call .cuda() again because new tensors will be created by calibration process
|
||||
>>> model.cuda()
|
||||
|
||||
>>> # Keep running the quantized model
|
||||
>>> # ...
|
||||
```
|
||||
|
||||
### Export to ONNX
|
||||
|
||||
The goal of exporting to ONNX is to deploy inference by [TensorRT](https://developer.nvidia.com/tensorrt). Fake
|
||||
quantization will be broken into a pair of QuantizeLinear/DequantizeLinear ONNX ops. After setting static member of
|
||||
TensorQuantizer to use Pytorch’s own fake quantization functions, fake quantized model can be exported to ONNX, follow
|
||||
the instructions in [torch.onnx](https://pytorch.org/docs/stable/onnx.html). Example:
|
||||
|
||||
```python
|
||||
>>> from pytorch_quantization.nn import TensorQuantizer
|
||||
|
||||
>>> TensorQuantizer.use_fb_fake_quant = True
|
||||
|
||||
>>> # Load the calibrated model
|
||||
>>> ...
|
||||
>>> # ONNX export
|
||||
>>> torch.onnx.export(...)
|
||||
```
|
||||
|
||||
## QDQBertConfig
|
||||
|
||||
[[autodoc]] QDQBertConfig
|
||||
|
||||
## QDQBertModel
|
||||
|
||||
[[autodoc]] QDQBertModel
|
||||
- forward
|
||||
|
||||
## QDQBertLMHeadModel
|
||||
|
||||
[[autodoc]] QDQBertLMHeadModel
|
||||
- forward
|
||||
|
||||
## QDQBertForMaskedLM
|
||||
|
||||
[[autodoc]] QDQBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## QDQBertForSequenceClassification
|
||||
|
||||
[[autodoc]] QDQBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## QDQBertForNextSentencePrediction
|
||||
|
||||
[[autodoc]] QDQBertForNextSentencePrediction
|
||||
- forward
|
||||
|
||||
## QDQBertForMultipleChoice
|
||||
|
||||
[[autodoc]] QDQBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## QDQBertForTokenClassification
|
||||
|
||||
[[autodoc]] QDQBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## QDQBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] QDQBertForQuestionAnswering
|
||||
- forward
|
||||
96
docs/source/en/model_doc/rag.mdx
Normal file
96
docs/source/en/model_doc/rag.mdx
Normal file
@@ -0,0 +1,96 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# RAG
|
||||
|
||||
## Overview
|
||||
|
||||
Retrieval-augmented generation ("RAG") models combine the powers of pretrained dense retrieval (DPR) and
|
||||
sequence-to-sequence models. RAG models retrieve documents, pass them to a seq2seq model, then marginalize to generate
|
||||
outputs. The retriever and seq2seq modules are initialized from pretrained models, and fine-tuned jointly, allowing
|
||||
both retrieval and generation to adapt to downstream tasks.
|
||||
|
||||
It is based on the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://arxiv.org/abs/2005.11401) by Patrick Lewis, Ethan Perez, Aleksandara Piktus, Fabio Petroni, Vladimir
|
||||
Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Large pre-trained language models have been shown to store factual knowledge in their parameters, and achieve
|
||||
state-of-the-art results when fine-tuned on downstream NLP tasks. However, their ability to access and precisely
|
||||
manipulate knowledge is still limited, and hence on knowledge-intensive tasks, their performance lags behind
|
||||
task-specific architectures. Additionally, providing provenance for their decisions and updating their world knowledge
|
||||
remain open research problems. Pre-trained models with a differentiable access mechanism to explicit nonparametric
|
||||
memory can overcome this issue, but have so far been only investigated for extractive downstream tasks. We explore a
|
||||
general-purpose fine-tuning recipe for retrieval-augmented generation (RAG) — models which combine pre-trained
|
||||
parametric and non-parametric memory for language generation. We introduce RAG models where the parametric memory is a
|
||||
pre-trained seq2seq model and the non-parametric memory is a dense vector index of Wikipedia, accessed with a
|
||||
pre-trained neural retriever. We compare two RAG formulations, one which conditions on the same retrieved passages
|
||||
across the whole generated sequence, the other can use different passages per token. We fine-tune and evaluate our
|
||||
models on a wide range of knowledge-intensive NLP tasks and set the state-of-the-art on three open domain QA tasks,
|
||||
outperforming parametric seq2seq models and task-specific retrieve-and-extract architectures. For language generation
|
||||
tasks, we find that RAG models generate more specific, diverse and factual language than a state-of-the-art
|
||||
parametric-only seq2seq baseline.*
|
||||
|
||||
This model was contributed by [ola13](https://huggingface.co/ola13).
|
||||
|
||||
|
||||
## RagConfig
|
||||
|
||||
[[autodoc]] RagConfig
|
||||
|
||||
## RagTokenizer
|
||||
|
||||
[[autodoc]] RagTokenizer
|
||||
|
||||
## Rag specific outputs
|
||||
|
||||
[[autodoc]] models.rag.modeling_rag.RetrievAugLMMarginOutput
|
||||
|
||||
[[autodoc]] models.rag.modeling_rag.RetrievAugLMOutput
|
||||
|
||||
## RagRetriever
|
||||
|
||||
[[autodoc]] RagRetriever
|
||||
|
||||
## RagModel
|
||||
|
||||
[[autodoc]] RagModel
|
||||
- forward
|
||||
|
||||
## RagSequenceForGeneration
|
||||
|
||||
[[autodoc]] RagSequenceForGeneration
|
||||
- forward
|
||||
- generate
|
||||
|
||||
## RagTokenForGeneration
|
||||
|
||||
[[autodoc]] RagTokenForGeneration
|
||||
- forward
|
||||
- generate
|
||||
|
||||
## TFRagModel
|
||||
|
||||
[[autodoc]] TFRagModel
|
||||
- call
|
||||
|
||||
## TFRagSequenceForGeneration
|
||||
|
||||
[[autodoc]] TFRagSequenceForGeneration
|
||||
- call
|
||||
- generate
|
||||
|
||||
## TFRagTokenForGeneration
|
||||
|
||||
[[autodoc]] TFRagTokenForGeneration
|
||||
- call
|
||||
- generate
|
||||
85
docs/source/en/model_doc/realm.mdx
Normal file
85
docs/source/en/model_doc/realm.mdx
Normal file
@@ -0,0 +1,85 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# REALM
|
||||
|
||||
## Overview
|
||||
|
||||
The REALM model was proposed in [REALM: Retrieval-Augmented Language Model Pre-Training](https://arxiv.org/abs/2002.08909) by Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Ming-Wei Chang. It's a
|
||||
retrieval-augmented language model that firstly retrieves documents from a textual knowledge corpus and then
|
||||
utilizes retrieved documents to process question answering tasks.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Language model pre-training has been shown to capture a surprising amount of world knowledge, crucial for NLP tasks
|
||||
such as question answering. However, this knowledge is stored implicitly in the parameters of a neural network,
|
||||
requiring ever-larger networks to cover more facts. To capture knowledge in a more modular and interpretable way, we
|
||||
augment language model pre-training with a latent knowledge retriever, which allows the model to retrieve and attend
|
||||
over documents from a large corpus such as Wikipedia, used during pre-training, fine-tuning and inference. For the
|
||||
first time, we show how to pre-train such a knowledge retriever in an unsupervised manner, using masked language
|
||||
modeling as the learning signal and backpropagating through a retrieval step that considers millions of documents. We
|
||||
demonstrate the effectiveness of Retrieval-Augmented Language Model pre-training (REALM) by fine-tuning on the
|
||||
challenging task of Open-domain Question Answering (Open-QA). We compare against state-of-the-art models for both
|
||||
explicit and implicit knowledge storage on three popular Open-QA benchmarks, and find that we outperform all previous
|
||||
methods by a significant margin (4-16% absolute accuracy), while also providing qualitative benefits such as
|
||||
interpretability and modularity.*
|
||||
|
||||
This model was contributed by [qqaatw](https://huggingface.co/qqaatw). The original code can be found
|
||||
[here](https://github.com/google-research/language/tree/master/language/realm).
|
||||
|
||||
## RealmConfig
|
||||
|
||||
[[autodoc]] RealmConfig
|
||||
|
||||
## RealmTokenizer
|
||||
|
||||
[[autodoc]] RealmTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
- batch_encode_candidates
|
||||
|
||||
## RealmTokenizerFast
|
||||
|
||||
[[autodoc]] RealmTokenizerFast
|
||||
- batch_encode_candidates
|
||||
|
||||
## RealmRetriever
|
||||
|
||||
[[autodoc]] RealmRetriever
|
||||
|
||||
## RealmEmbedder
|
||||
|
||||
[[autodoc]] RealmEmbedder
|
||||
- forward
|
||||
|
||||
## RealmScorer
|
||||
|
||||
[[autodoc]] RealmScorer
|
||||
- forward
|
||||
|
||||
## RealmKnowledgeAugEncoder
|
||||
|
||||
[[autodoc]] RealmKnowledgeAugEncoder
|
||||
- forward
|
||||
|
||||
## RealmReader
|
||||
|
||||
[[autodoc]] RealmReader
|
||||
- forward
|
||||
|
||||
## RealmForOpenQA
|
||||
|
||||
[[autodoc]] RealmForOpenQA
|
||||
- block_embedding_to
|
||||
- forward
|
||||
177
docs/source/en/model_doc/reformer.mdx
Normal file
177
docs/source/en/model_doc/reformer.mdx
Normal file
@@ -0,0 +1,177 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Reformer
|
||||
|
||||
**DISCLAIMER:** This model is still a work in progress, if you see something strange, file a [Github Issue](https://github.com/huggingface/transformers/issues/new?assignees=&labels=&template=bug-report.md&title).
|
||||
|
||||
## Overview
|
||||
|
||||
The Reformer model was proposed in the paper [Reformer: The Efficient Transformer](https://arxiv.org/abs/2001.04451.pdf) by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Large Transformer models routinely achieve state-of-the-art results on a number of tasks but training these models can
|
||||
be prohibitively costly, especially on long sequences. We introduce two techniques to improve the efficiency of
|
||||
Transformers. For one, we replace dot-product attention by one that uses locality-sensitive hashing, changing its
|
||||
complexity from O(L^2) to O(Llog(L)), where L is the length of the sequence. Furthermore, we use reversible residual
|
||||
layers instead of the standard residuals, which allows storing activations only once in the training process instead of
|
||||
N times, where N is the number of layers. The resulting model, the Reformer, performs on par with Transformer models
|
||||
while being much more memory-efficient and much faster on long sequences.*
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The Authors' code can be
|
||||
found [here](https://github.com/google/trax/tree/master/trax/models/reformer).
|
||||
|
||||
**Note**:
|
||||
|
||||
- Reformer does **not** work with *torch.nn.DataParallel* due to a bug in PyTorch, see [issue #36035](https://github.com/pytorch/pytorch/issues/36035)
|
||||
|
||||
## Axial Positional Encodings
|
||||
|
||||
Axial Positional Encodings were first implemented in Google's [trax library](https://github.com/google/trax/blob/4d99ad4965bab1deba227539758d59f0df0fef48/trax/layers/research/position_encodings.py#L29)
|
||||
and developed by the authors of this model's paper. In models that are treating very long input sequences, the
|
||||
conventional position id encodings store an embedings vector of size \\(d\\) being the `config.hidden_size` for
|
||||
every position \\(i, \ldots, n_s\\), with \\(n_s\\) being `config.max_embedding_size`. This means that having
|
||||
a sequence length of \\(n_s = 2^{19} \approx 0.5M\\) and a `config.hidden_size` of \\(d = 2^{10} \approx 1000\\)
|
||||
would result in a position encoding matrix:
|
||||
|
||||
$$X_{i,j}, \text{ with } i \in \left[1,\ldots, d\right] \text{ and } j \in \left[1,\ldots, n_s\right]$$
|
||||
|
||||
which alone has over 500M parameters to store. Axial positional encodings factorize \\(X_{i,j}\\) into two matrices:
|
||||
|
||||
$$X^{1}_{i,j}, \text{ with } i \in \left[1,\ldots, d^1\right] \text{ and } j \in \left[1,\ldots, n_s^1\right]$$
|
||||
|
||||
and
|
||||
|
||||
$$X^{2}_{i,j}, \text{ with } i \in \left[1,\ldots, d^2\right] \text{ and } j \in \left[1,\ldots, n_s^2\right]$$
|
||||
|
||||
with:
|
||||
|
||||
$$d = d^1 + d^2 \text{ and } n_s = n_s^1 \times n_s^2 .$$
|
||||
|
||||
Therefore the following holds:
|
||||
|
||||
$$X_{i,j} = \begin{cases}
|
||||
X^{1}_{i, k}, & \text{if }\ i < d^1 \text{ with } k = j \mod n_s^1 \\
|
||||
X^{2}_{i - d^1, l}, & \text{if } i \ge d^1 \text{ with } l = \lfloor\frac{j}{n_s^1}\rfloor
|
||||
\end{cases}$$
|
||||
|
||||
Intuitively, this means that a position embedding vector \\(x_j \in \mathbb{R}^{d}\\) is now the composition of two
|
||||
factorized embedding vectors: \\(x^1_{k, l} + x^2_{l, k}\\), where as the `config.max_embedding_size` dimension
|
||||
\\(j\\) is factorized into \\(k \text{ and } l\\). This design ensures that each position embedding vector
|
||||
\\(x_j\\) is unique.
|
||||
|
||||
Using the above example again, axial position encoding with \\(d^1 = 2^5, d^2 = 2^5, n_s^1 = 2^9, n_s^2 = 2^{10}\\)
|
||||
can drastically reduced the number of parameters to \\(2^{14} + 2^{15} \approx 49000\\) parameters.
|
||||
|
||||
In practice, the parameter `config.axial_pos_embds_dim` is set to a tuple \\((d^1, d^2)\\) which sum has to be
|
||||
equal to `config.hidden_size` and `config.axial_pos_shape` is set to a tuple \\((n_s^1, n_s^2)\\) which
|
||||
product has to be equal to `config.max_embedding_size`, which during training has to be equal to the *sequence
|
||||
length* of the `input_ids`.
|
||||
|
||||
|
||||
## LSH Self Attention
|
||||
|
||||
In Locality sensitive hashing (LSH) self attention the key and query projection weights are tied. Therefore, the key
|
||||
query embedding vectors are also tied. LSH self attention uses the locality sensitive hashing mechanism proposed in
|
||||
[Practical and Optimal LSH for Angular Distance](https://arxiv.org/abs/1509.02897) to assign each of the tied key
|
||||
query embedding vectors to one of `config.num_buckets` possible buckets. The premise is that the more "similar"
|
||||
key query embedding vectors (in terms of *cosine similarity*) are to each other, the more likely they are assigned to
|
||||
the same bucket.
|
||||
|
||||
The accuracy of the LSH mechanism can be improved by increasing `config.num_hashes` or directly the argument
|
||||
`num_hashes` of the forward function so that the output of the LSH self attention better approximates the output
|
||||
of the "normal" full self attention. The buckets are then sorted and chunked into query key embedding vector chunks
|
||||
each of length `config.lsh_chunk_length`. For each chunk, the query embedding vectors attend to its key vectors
|
||||
(which are tied to themselves) and to the key embedding vectors of `config.lsh_num_chunks_before` previous
|
||||
neighboring chunks and `config.lsh_num_chunks_after` following neighboring chunks.
|
||||
|
||||
For more information, see the [original Paper](https://arxiv.org/abs/2001.04451) or this great [blog post](https://www.pragmatic.ml/reformer-deep-dive/).
|
||||
|
||||
Note that `config.num_buckets` can also be factorized into a list \\((n_{\text{buckets}}^1,
|
||||
n_{\text{buckets}}^2)\\). This way instead of assigning the query key embedding vectors to one of \\((1,\ldots,
|
||||
n_{\text{buckets}})\\) they are assigned to one of \\((1-1,\ldots, n_{\text{buckets}}^1-1, \ldots,
|
||||
1-n_{\text{buckets}}^2, \ldots, n_{\text{buckets}}^1-n_{\text{buckets}}^2)\\). This is crucial for very long sequences to
|
||||
save memory.
|
||||
|
||||
When training a model from scratch, it is recommended to leave `config.num_buckets=None`, so that depending on the
|
||||
sequence length a good value for `num_buckets` is calculated on the fly. This value will then automatically be
|
||||
saved in the config and should be reused for inference.
|
||||
|
||||
Using LSH self attention, the memory and time complexity of the query-key matmul operation can be reduced from
|
||||
\\(\mathcal{O}(n_s \times n_s)\\) to \\(\mathcal{O}(n_s \times \log(n_s))\\), which usually represents the memory
|
||||
and time bottleneck in a transformer model, with \\(n_s\\) being the sequence length.
|
||||
|
||||
|
||||
## Local Self Attention
|
||||
|
||||
Local self attention is essentially a "normal" self attention layer with key, query and value projections, but is
|
||||
chunked so that in each chunk of length `config.local_chunk_length` the query embedding vectors only attends to
|
||||
the key embedding vectors in its chunk and to the key embedding vectors of `config.local_num_chunks_before`
|
||||
previous neighboring chunks and `config.local_num_chunks_after` following neighboring chunks.
|
||||
|
||||
Using Local self attention, the memory and time complexity of the query-key matmul operation can be reduced from
|
||||
\\(\mathcal{O}(n_s \times n_s)\\) to \\(\mathcal{O}(n_s \times \log(n_s))\\), which usually represents the memory
|
||||
and time bottleneck in a transformer model, with \\(n_s\\) being the sequence length.
|
||||
|
||||
|
||||
## Training
|
||||
|
||||
During training, we must ensure that the sequence length is set to a value that can be divided by the least common
|
||||
multiple of `config.lsh_chunk_length` and `config.local_chunk_length` and that the parameters of the Axial
|
||||
Positional Encodings are correctly set as described above. Reformer is very memory efficient so that the model can
|
||||
easily be trained on sequences as long as 64000 tokens.
|
||||
|
||||
For training, the [`ReformerModelWithLMHead`] should be used as follows:
|
||||
|
||||
```python
|
||||
input_ids = tokenizer.encode("This is a sentence from the training data", return_tensors="pt")
|
||||
loss = model(input_ids, labels=input_ids)[0]
|
||||
```
|
||||
|
||||
## ReformerConfig
|
||||
|
||||
[[autodoc]] ReformerConfig
|
||||
|
||||
## ReformerTokenizer
|
||||
|
||||
[[autodoc]] ReformerTokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## ReformerTokenizerFast
|
||||
|
||||
[[autodoc]] ReformerTokenizerFast
|
||||
|
||||
## ReformerModel
|
||||
|
||||
[[autodoc]] ReformerModel
|
||||
- forward
|
||||
|
||||
## ReformerModelWithLMHead
|
||||
|
||||
[[autodoc]] ReformerModelWithLMHead
|
||||
- forward
|
||||
|
||||
## ReformerForMaskedLM
|
||||
|
||||
[[autodoc]] ReformerForMaskedLM
|
||||
- forward
|
||||
|
||||
## ReformerForSequenceClassification
|
||||
|
||||
[[autodoc]] ReformerForSequenceClassification
|
||||
- forward
|
||||
|
||||
## ReformerForQuestionAnswering
|
||||
|
||||
[[autodoc]] ReformerForQuestionAnswering
|
||||
- forward
|
||||
128
docs/source/en/model_doc/rembert.mdx
Normal file
128
docs/source/en/model_doc/rembert.mdx
Normal file
@@ -0,0 +1,128 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# RemBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The RemBERT model was proposed in [Rethinking Embedding Coupling in Pre-trained Language Models](https://arxiv.org/abs/2010.12821) by Hyung Won Chung, Thibault Févry, Henry Tsai, Melvin Johnson, Sebastian Ruder.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We re-evaluate the standard practice of sharing weights between input and output embeddings in state-of-the-art
|
||||
pre-trained language models. We show that decoupled embeddings provide increased modeling flexibility, allowing us to
|
||||
significantly improve the efficiency of parameter allocation in the input embedding of multilingual models. By
|
||||
reallocating the input embedding parameters in the Transformer layers, we achieve dramatically better performance on
|
||||
standard natural language understanding tasks with the same number of parameters during fine-tuning. We also show that
|
||||
allocating additional capacity to the output embedding provides benefits to the model that persist through the
|
||||
fine-tuning stage even though the output embedding is discarded after pre-training. Our analysis shows that larger
|
||||
output embeddings prevent the model's last layers from overspecializing to the pre-training task and encourage
|
||||
Transformer representations to be more general and more transferable to other tasks and languages. Harnessing these
|
||||
findings, we are able to train models that achieve strong performance on the XTREME benchmark without increasing the
|
||||
number of parameters at the fine-tuning stage.*
|
||||
|
||||
Tips:
|
||||
|
||||
For fine-tuning, RemBERT can be thought of as a bigger version of mBERT with an ALBERT-like factorization of the
|
||||
embedding layer. The embeddings are not tied in pre-training, in contrast with BERT, which enables smaller input
|
||||
embeddings (preserved during fine-tuning) and bigger output embeddings (discarded at fine-tuning). The tokenizer is
|
||||
also similar to the Albert one rather than the BERT one.
|
||||
|
||||
## RemBertConfig
|
||||
|
||||
[[autodoc]] RemBertConfig
|
||||
|
||||
## RemBertTokenizer
|
||||
|
||||
[[autodoc]] RemBertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## RemBertTokenizerFast
|
||||
|
||||
[[autodoc]] RemBertTokenizerFast
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## RemBertModel
|
||||
|
||||
[[autodoc]] RemBertModel
|
||||
- forward
|
||||
|
||||
## RemBertForCausalLM
|
||||
|
||||
[[autodoc]] RemBertForCausalLM
|
||||
- forward
|
||||
|
||||
## RemBertForMaskedLM
|
||||
|
||||
[[autodoc]] RemBertForMaskedLM
|
||||
- forward
|
||||
|
||||
## RemBertForSequenceClassification
|
||||
|
||||
[[autodoc]] RemBertForSequenceClassification
|
||||
- forward
|
||||
|
||||
## RemBertForMultipleChoice
|
||||
|
||||
[[autodoc]] RemBertForMultipleChoice
|
||||
- forward
|
||||
|
||||
## RemBertForTokenClassification
|
||||
|
||||
[[autodoc]] RemBertForTokenClassification
|
||||
- forward
|
||||
|
||||
## RemBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] RemBertForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFRemBertModel
|
||||
|
||||
[[autodoc]] TFRemBertModel
|
||||
- call
|
||||
|
||||
## TFRemBertForMaskedLM
|
||||
|
||||
[[autodoc]] TFRemBertForMaskedLM
|
||||
- call
|
||||
|
||||
## TFRemBertForCausalLM
|
||||
|
||||
[[autodoc]] TFRemBertForCausalLM
|
||||
- call
|
||||
|
||||
## TFRemBertForSequenceClassification
|
||||
|
||||
[[autodoc]] TFRemBertForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFRemBertForMultipleChoice
|
||||
|
||||
[[autodoc]] TFRemBertForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFRemBertForTokenClassification
|
||||
|
||||
[[autodoc]] TFRemBertForTokenClassification
|
||||
- call
|
||||
|
||||
## TFRemBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFRemBertForQuestionAnswering
|
||||
- call
|
||||
50
docs/source/en/model_doc/resnet.mdx
Normal file
50
docs/source/en/model_doc/resnet.mdx
Normal file
@@ -0,0 +1,50 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ResNet
|
||||
|
||||
## Overview
|
||||
|
||||
The ResNet model was proposed in [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. Our implementation follows the small changes made by [Nvidia](https://catalog.ngc.nvidia.com/orgs/nvidia/resources/resnet_50_v1_5_for_pytorch), we apply the `stride=2` for downsampling in bottleneck's `3x3` conv and not in the first `1x1`. This is generally known as "ResNet v1.5".
|
||||
|
||||
ResNet introduced residual connections, they allow to train networks with an unseen number of layers (up to 1000). ResNet won the 2015 ILSVRC & COCO competition, one important milestone in deep computer vision.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Deeper neural networks are more difficult to train. We present a residual learning framework to ease the training of networks that are substantially deeper than those used previously. We explicitly reformulate the layers as learning residual functions with reference to the layer inputs, instead of learning unreferenced functions. We provide comprehensive empirical evidence showing that these residual networks are easier to optimize, and can gain accuracy from considerably increased depth. On the ImageNet dataset we evaluate residual nets with a depth of up to 152 layers---8x deeper than VGG nets but still having lower complexity. An ensemble of these residual nets achieves 3.57% error on the ImageNet test set. This result won the 1st place on the ILSVRC 2015 classification task. We also present analysis on CIFAR-10 with 100 and 1000 layers.
|
||||
The depth of representations is of central importance for many visual recognition tasks. Solely due to our extremely deep representations, we obtain a 28% relative improvement on the COCO object detection dataset. Deep residual nets are foundations of our submissions to ILSVRC & COCO 2015 competitions, where we also won the 1st places on the tasks of ImageNet detection, ImageNet localization, COCO detection, and COCO segmentation.*
|
||||
|
||||
Tips:
|
||||
|
||||
- One can use [`AutoFeatureExtractor`] to prepare images for the model.
|
||||
|
||||
The figure below illustrates the architecture of ResNet. Taken from the [original paper](https://arxiv.org/abs/1512.03385).
|
||||
|
||||
<img width="600" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/resnet_architecture.png"/>
|
||||
|
||||
This model was contributed by [Francesco](https://huggingface.co/Francesco). The original code can be found [here](https://github.com/KaimingHe/deep-residual-networks).
|
||||
|
||||
## ResNetConfig
|
||||
|
||||
[[autodoc]] ResNetConfig
|
||||
|
||||
|
||||
## ResNetModel
|
||||
|
||||
[[autodoc]] ResNetModel
|
||||
- forward
|
||||
|
||||
|
||||
## ResNetForImageClassification
|
||||
|
||||
[[autodoc]] ResNetForImageClassification
|
||||
- forward
|
||||
40
docs/source/en/model_doc/retribert.mdx
Normal file
40
docs/source/en/model_doc/retribert.mdx
Normal file
@@ -0,0 +1,40 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# RetriBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The RetriBERT model was proposed in the blog post [Explain Anything Like I'm Five: A Model for Open Domain Long Form
|
||||
Question Answering](https://yjernite.github.io/lfqa.html). RetriBERT is a small model that uses either a single or
|
||||
pair of BERT encoders with lower-dimension projection for dense semantic indexing of text.
|
||||
|
||||
This model was contributed by [yjernite](https://huggingface.co/yjernite). Code to train and use the model can be
|
||||
found [here](https://github.com/huggingface/transformers/tree/main/examples/research-projects/distillation).
|
||||
|
||||
|
||||
## RetriBertConfig
|
||||
|
||||
[[autodoc]] RetriBertConfig
|
||||
|
||||
## RetriBertTokenizer
|
||||
|
||||
[[autodoc]] RetriBertTokenizer
|
||||
|
||||
## RetriBertTokenizerFast
|
||||
|
||||
[[autodoc]] RetriBertTokenizerFast
|
||||
|
||||
## RetriBertModel
|
||||
|
||||
[[autodoc]] RetriBertModel
|
||||
- forward
|
||||
162
docs/source/en/model_doc/roberta.mdx
Normal file
162
docs/source/en/model_doc/roberta.mdx
Normal file
@@ -0,0 +1,162 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# RoBERTa
|
||||
|
||||
## Overview
|
||||
|
||||
The RoBERTa model was proposed in [RoBERTa: A Robustly Optimized BERT Pretraining Approach](https://arxiv.org/abs/1907.11692) by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer
|
||||
Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. It is based on Google's BERT model released in 2018.
|
||||
|
||||
It builds on BERT and modifies key hyperparameters, removing the next-sentence pretraining objective and training with
|
||||
much larger mini-batches and learning rates.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Language model pretraining has led to significant performance gains but careful comparison between different
|
||||
approaches is challenging. Training is computationally expensive, often done on private datasets of different sizes,
|
||||
and, as we will show, hyperparameter choices have significant impact on the final results. We present a replication
|
||||
study of BERT pretraining (Devlin et al., 2019) that carefully measures the impact of many key hyperparameters and
|
||||
training data size. We find that BERT was significantly undertrained, and can match or exceed the performance of every
|
||||
model published after it. Our best model achieves state-of-the-art results on GLUE, RACE and SQuAD. These results
|
||||
highlight the importance of previously overlooked design choices, and raise questions about the source of recently
|
||||
reported improvements. We release our models and code.*
|
||||
|
||||
Tips:
|
||||
|
||||
- This implementation is the same as [`BertModel`] with a tiny embeddings tweak as well as a setup
|
||||
for Roberta pretrained models.
|
||||
- RoBERTa has the same architecture as BERT, but uses a byte-level BPE as a tokenizer (same as GPT-2) and uses a
|
||||
different pretraining scheme.
|
||||
- RoBERTa doesn't have `token_type_ids`, you don't need to indicate which token belongs to which segment. Just
|
||||
separate your segments with the separation token `tokenizer.sep_token` (or `</s>`)
|
||||
- [CamemBERT](camembert) is a wrapper around RoBERTa. Refer to this page for usage examples.
|
||||
|
||||
This model was contributed by [julien-c](https://huggingface.co/julien-c). The original code can be found [here](https://github.com/pytorch/fairseq/tree/master/examples/roberta).
|
||||
|
||||
|
||||
## RobertaConfig
|
||||
|
||||
[[autodoc]] RobertaConfig
|
||||
|
||||
## RobertaTokenizer
|
||||
|
||||
[[autodoc]] RobertaTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## RobertaTokenizerFast
|
||||
|
||||
[[autodoc]] RobertaTokenizerFast
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## RobertaModel
|
||||
|
||||
[[autodoc]] RobertaModel
|
||||
- forward
|
||||
|
||||
## RobertaForCausalLM
|
||||
|
||||
[[autodoc]] RobertaForCausalLM
|
||||
- forward
|
||||
|
||||
## RobertaForMaskedLM
|
||||
|
||||
[[autodoc]] RobertaForMaskedLM
|
||||
- forward
|
||||
|
||||
## RobertaForSequenceClassification
|
||||
|
||||
[[autodoc]] RobertaForSequenceClassification
|
||||
- forward
|
||||
|
||||
## RobertaForMultipleChoice
|
||||
|
||||
[[autodoc]] RobertaForMultipleChoice
|
||||
- forward
|
||||
|
||||
## RobertaForTokenClassification
|
||||
|
||||
[[autodoc]] RobertaForTokenClassification
|
||||
- forward
|
||||
|
||||
## RobertaForQuestionAnswering
|
||||
|
||||
[[autodoc]] RobertaForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFRobertaModel
|
||||
|
||||
[[autodoc]] TFRobertaModel
|
||||
- call
|
||||
|
||||
## TFRobertaForCausalLM
|
||||
|
||||
[[autodoc]] TFRobertaForCausalLM
|
||||
- call
|
||||
|
||||
## TFRobertaForMaskedLM
|
||||
|
||||
[[autodoc]] TFRobertaForMaskedLM
|
||||
- call
|
||||
|
||||
## TFRobertaForSequenceClassification
|
||||
|
||||
[[autodoc]] TFRobertaForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFRobertaForMultipleChoice
|
||||
|
||||
[[autodoc]] TFRobertaForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFRobertaForTokenClassification
|
||||
|
||||
[[autodoc]] TFRobertaForTokenClassification
|
||||
- call
|
||||
|
||||
## TFRobertaForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFRobertaForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxRobertaModel
|
||||
|
||||
[[autodoc]] FlaxRobertaModel
|
||||
- __call__
|
||||
|
||||
## FlaxRobertaForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxRobertaForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxRobertaForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxRobertaForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxRobertaForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxRobertaForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxRobertaForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxRobertaForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxRobertaForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxRobertaForQuestionAnswering
|
||||
- __call__
|
||||
155
docs/source/en/model_doc/roformer.mdx
Normal file
155
docs/source/en/model_doc/roformer.mdx
Normal file
@@ -0,0 +1,155 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# RoFormer
|
||||
|
||||
## Overview
|
||||
|
||||
The RoFormer model was proposed in [RoFormer: Enhanced Transformer with Rotary Position Embedding](https://arxiv.org/pdf/2104.09864v1.pdf) by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Position encoding in transformer architecture provides supervision for dependency modeling between elements at
|
||||
different positions in the sequence. We investigate various methods to encode positional information in
|
||||
transformer-based language models and propose a novel implementation named Rotary Position Embedding(RoPE). The
|
||||
proposed RoPE encodes absolute positional information with rotation matrix and naturally incorporates explicit relative
|
||||
position dependency in self-attention formulation. Notably, RoPE comes with valuable properties such as flexibility of
|
||||
being expand to any sequence lengths, decaying inter-token dependency with increasing relative distances, and
|
||||
capability of equipping the linear self-attention with relative position encoding. As a result, the enhanced
|
||||
transformer with rotary position embedding, or RoFormer, achieves superior performance in tasks with long texts. We
|
||||
release the theoretical analysis along with some preliminary experiment results on Chinese data. The undergoing
|
||||
experiment for English benchmark will soon be updated.*
|
||||
|
||||
Tips:
|
||||
|
||||
- RoFormer is a BERT-like autoencoding model with rotary position embeddings. Rotary position embeddings have shown
|
||||
improved performance on classification tasks with long texts.
|
||||
|
||||
|
||||
This model was contributed by [junnyu](https://huggingface.co/junnyu). The original code can be found [here](https://github.com/ZhuiyiTechnology/roformer).
|
||||
|
||||
## RoFormerConfig
|
||||
|
||||
[[autodoc]] RoFormerConfig
|
||||
|
||||
## RoFormerTokenizer
|
||||
|
||||
[[autodoc]] RoFormerTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## RoFormerTokenizerFast
|
||||
|
||||
[[autodoc]] RoFormerTokenizerFast
|
||||
- build_inputs_with_special_tokens
|
||||
|
||||
## RoFormerModel
|
||||
|
||||
[[autodoc]] RoFormerModel
|
||||
- forward
|
||||
|
||||
## RoFormerForCausalLM
|
||||
|
||||
[[autodoc]] RoFormerForCausalLM
|
||||
- forward
|
||||
|
||||
## RoFormerForMaskedLM
|
||||
|
||||
[[autodoc]] RoFormerForMaskedLM
|
||||
- forward
|
||||
|
||||
## RoFormerForSequenceClassification
|
||||
|
||||
[[autodoc]] RoFormerForSequenceClassification
|
||||
- forward
|
||||
|
||||
## RoFormerForMultipleChoice
|
||||
|
||||
[[autodoc]] RoFormerForMultipleChoice
|
||||
- forward
|
||||
|
||||
## RoFormerForTokenClassification
|
||||
|
||||
[[autodoc]] RoFormerForTokenClassification
|
||||
- forward
|
||||
|
||||
## RoFormerForQuestionAnswering
|
||||
|
||||
[[autodoc]] RoFormerForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFRoFormerModel
|
||||
|
||||
[[autodoc]] TFRoFormerModel
|
||||
- call
|
||||
|
||||
## TFRoFormerForMaskedLM
|
||||
|
||||
[[autodoc]] TFRoFormerForMaskedLM
|
||||
- call
|
||||
|
||||
## TFRoFormerForCausalLM
|
||||
|
||||
[[autodoc]] TFRoFormerForCausalLM
|
||||
- call
|
||||
|
||||
## TFRoFormerForSequenceClassification
|
||||
|
||||
[[autodoc]] TFRoFormerForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFRoFormerForMultipleChoice
|
||||
|
||||
[[autodoc]] TFRoFormerForMultipleChoice
|
||||
- call
|
||||
|
||||
## TFRoFormerForTokenClassification
|
||||
|
||||
[[autodoc]] TFRoFormerForTokenClassification
|
||||
- call
|
||||
|
||||
## TFRoFormerForQuestionAnswering
|
||||
|
||||
[[autodoc]] TFRoFormerForQuestionAnswering
|
||||
- call
|
||||
|
||||
## FlaxRoFormerModel
|
||||
|
||||
[[autodoc]] FlaxRoFormerModel
|
||||
- __call__
|
||||
|
||||
## FlaxRoFormerForMaskedLM
|
||||
|
||||
[[autodoc]] FlaxRoFormerForMaskedLM
|
||||
- __call__
|
||||
|
||||
## FlaxRoFormerForSequenceClassification
|
||||
|
||||
[[autodoc]] FlaxRoFormerForSequenceClassification
|
||||
- __call__
|
||||
|
||||
## FlaxRoFormerForMultipleChoice
|
||||
|
||||
[[autodoc]] FlaxRoFormerForMultipleChoice
|
||||
- __call__
|
||||
|
||||
## FlaxRoFormerForTokenClassification
|
||||
|
||||
[[autodoc]] FlaxRoFormerForTokenClassification
|
||||
- __call__
|
||||
|
||||
## FlaxRoFormerForQuestionAnswering
|
||||
|
||||
[[autodoc]] FlaxRoFormerForQuestionAnswering
|
||||
- __call__
|
||||
106
docs/source/en/model_doc/segformer.mdx
Normal file
106
docs/source/en/model_doc/segformer.mdx
Normal file
@@ -0,0 +1,106 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# SegFormer
|
||||
|
||||
## Overview
|
||||
|
||||
The SegFormer model was proposed in [SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers](https://arxiv.org/abs/2105.15203) by Enze Xie, Wenhai Wang, Zhiding Yu, Anima Anandkumar, Jose M. Alvarez, Ping
|
||||
Luo. The model consists of a hierarchical Transformer encoder and a lightweight all-MLP decode head to achieve great
|
||||
results on image segmentation benchmarks such as ADE20K and Cityscapes.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*We present SegFormer, a simple, efficient yet powerful semantic segmentation framework which unifies Transformers with
|
||||
lightweight multilayer perception (MLP) decoders. SegFormer has two appealing features: 1) SegFormer comprises a novel
|
||||
hierarchically structured Transformer encoder which outputs multiscale features. It does not need positional encoding,
|
||||
thereby avoiding the interpolation of positional codes which leads to decreased performance when the testing resolution
|
||||
differs from training. 2) SegFormer avoids complex decoders. The proposed MLP decoder aggregates information from
|
||||
different layers, and thus combining both local attention and global attention to render powerful representations. We
|
||||
show that this simple and lightweight design is the key to efficient segmentation on Transformers. We scale our
|
||||
approach up to obtain a series of models from SegFormer-B0 to SegFormer-B5, reaching significantly better performance
|
||||
and efficiency than previous counterparts. For example, SegFormer-B4 achieves 50.3% mIoU on ADE20K with 64M parameters,
|
||||
being 5x smaller and 2.2% better than the previous best method. Our best model, SegFormer-B5, achieves 84.0% mIoU on
|
||||
Cityscapes validation set and shows excellent zero-shot robustness on Cityscapes-C.*
|
||||
|
||||
The figure below illustrates the architecture of SegFormer. Taken from the [original paper](https://arxiv.org/abs/2105.15203).
|
||||
|
||||
<img width="600" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/segformer_architecture.png"/>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/NVlabs/SegFormer).
|
||||
|
||||
Tips:
|
||||
|
||||
- SegFormer consists of a hierarchical Transformer encoder, and a lightweight all-MLP decode head.
|
||||
[`SegformerModel`] is the hierarchical Transformer encoder (which in the paper is also referred to
|
||||
as Mix Transformer or MiT). [`SegformerForSemanticSegmentation`] adds the all-MLP decode head on
|
||||
top to perform semantic segmentation of images. In addition, there's
|
||||
[`SegformerForImageClassification`] which can be used to - you guessed it - classify images. The
|
||||
authors of SegFormer first pre-trained the Transformer encoder on ImageNet-1k to classify images. Next, they throw
|
||||
away the classification head, and replace it by the all-MLP decode head. Next, they fine-tune the model altogether on
|
||||
ADE20K, Cityscapes and COCO-stuff, which are important benchmarks for semantic segmentation. All checkpoints can be
|
||||
found on the [hub](https://huggingface.co/models?other=segformer).
|
||||
- The quickest way to get started with SegFormer is by checking the [example notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/SegFormer) (which showcase both inference and
|
||||
fine-tuning on custom data). One can also check out the [blog post](https://huggingface.co/blog/fine-tune-segformer) introducing SegFormer and illustrating how it can be fine-tuned on custom data.
|
||||
- SegFormer works on any input size, as it pads the input to be divisible by `config.patch_sizes`.
|
||||
- One can use [`SegformerFeatureExtractor`] to prepare images and corresponding segmentation maps
|
||||
for the model. Note that this feature extractor is fairly basic and does not include all data augmentations used in
|
||||
the original paper. The original preprocessing pipelines (for the ADE20k dataset for instance) can be found [here](https://github.com/NVlabs/SegFormer/blob/master/local_configs/_base_/datasets/ade20k_repeat.py). The most
|
||||
important preprocessing step is that images and segmentation maps are randomly cropped and padded to the same size,
|
||||
such as 512x512 or 640x640, after which they are normalized.
|
||||
- One additional thing to keep in mind is that one can initialize [`SegformerFeatureExtractor`] with
|
||||
`reduce_labels` set to `True` or `False`. In some datasets (like ADE20k), the 0 index is used in the annotated
|
||||
segmentation maps for background. However, ADE20k doesn't include the "background" class in its 150 labels.
|
||||
Therefore, `reduce_labels` is used to reduce all labels by 1, and to make sure no loss is computed for the
|
||||
background class (i.e. it replaces 0 in the annotated maps by 255, which is the *ignore_index* of the loss function
|
||||
used by [`SegformerForSemanticSegmentation`]). However, other datasets use the 0 index as
|
||||
background class and include this class as part of all labels. In that case, `reduce_labels` should be set to
|
||||
`False`, as loss should also be computed for the background class.
|
||||
- As most models, SegFormer comes in different sizes, the details of which can be found in the table below.
|
||||
|
||||
| **Model variant** | **Depths** | **Hidden sizes** | **Decoder hidden size** | **Params (M)** | **ImageNet-1k Top 1** |
|
||||
| :---------------: | ------------- | ------------------- | :---------------------: | :------------: | :-------------------: |
|
||||
| MiT-b0 | [2, 2, 2, 2] | [32, 64, 160, 256] | 256 | 3.7 | 70.5 |
|
||||
| MiT-b1 | [2, 2, 2, 2] | [64, 128, 320, 512] | 256 | 14.0 | 78.7 |
|
||||
| MiT-b2 | [3, 4, 6, 3] | [64, 128, 320, 512] | 768 | 25.4 | 81.6 |
|
||||
| MiT-b3 | [3, 4, 18, 3] | [64, 128, 320, 512] | 768 | 45.2 | 83.1 |
|
||||
| MiT-b4 | [3, 8, 27, 3] | [64, 128, 320, 512] | 768 | 62.6 | 83.6 |
|
||||
| MiT-b5 | [3, 6, 40, 3] | [64, 128, 320, 512] | 768 | 82.0 | 83.8 |
|
||||
|
||||
## SegformerConfig
|
||||
|
||||
[[autodoc]] SegformerConfig
|
||||
|
||||
## SegformerFeatureExtractor
|
||||
|
||||
[[autodoc]] SegformerFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## SegformerModel
|
||||
|
||||
[[autodoc]] SegformerModel
|
||||
- forward
|
||||
|
||||
## SegformerDecodeHead
|
||||
|
||||
[[autodoc]] SegformerDecodeHead
|
||||
- forward
|
||||
|
||||
## SegformerForImageClassification
|
||||
|
||||
[[autodoc]] SegformerForImageClassification
|
||||
- forward
|
||||
|
||||
## SegformerForSemanticSegmentation
|
||||
|
||||
[[autodoc]] SegformerForSemanticSegmentation
|
||||
- forward
|
||||
57
docs/source/en/model_doc/sew-d.mdx
Normal file
57
docs/source/en/model_doc/sew-d.mdx
Normal file
@@ -0,0 +1,57 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# SEW-D
|
||||
|
||||
## Overview
|
||||
|
||||
SEW-D (Squeezed and Efficient Wav2Vec with Disentangled attention) was proposed in [Performance-Efficiency Trade-offs
|
||||
in Unsupervised Pre-training for Speech Recognition](https://arxiv.org/abs/2109.06870) by Felix Wu, Kwangyoun Kim,
|
||||
Jing Pan, Kyu Han, Kilian Q. Weinberger, Yoav Artzi.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*This paper is a study of performance-efficiency trade-offs in pre-trained models for automatic speech recognition
|
||||
(ASR). We focus on wav2vec 2.0, and formalize several architecture designs that influence both the model performance
|
||||
and its efficiency. Putting together all our observations, we introduce SEW (Squeezed and Efficient Wav2vec), a
|
||||
pre-trained model architecture with significant improvements along both performance and efficiency dimensions across a
|
||||
variety of training setups. For example, under the 100h-960h semi-supervised setup on LibriSpeech, SEW achieves a 1.9x
|
||||
inference speedup compared to wav2vec 2.0, with a 13.5% relative reduction in word error rate. With a similar inference
|
||||
time, SEW reduces word error rate by 25-50% across different model sizes.*
|
||||
|
||||
Tips:
|
||||
|
||||
- SEW-D is a speech model that accepts a float array corresponding to the raw waveform of the speech signal.
|
||||
- SEWDForCTC is fine-tuned using connectionist temporal classification (CTC) so the model output has to be decoded
|
||||
using [`Wav2Vec2CTCTokenizer`].
|
||||
|
||||
This model was contributed by [anton-l](https://huggingface.co/anton-l).
|
||||
|
||||
|
||||
## SEWDConfig
|
||||
|
||||
[[autodoc]] SEWDConfig
|
||||
|
||||
## SEWDModel
|
||||
|
||||
[[autodoc]] SEWDModel
|
||||
- forward
|
||||
|
||||
## SEWDForCTC
|
||||
|
||||
[[autodoc]] SEWDForCTC
|
||||
- forward
|
||||
|
||||
## SEWDForSequenceClassification
|
||||
|
||||
[[autodoc]] SEWDForSequenceClassification
|
||||
- forward
|
||||
57
docs/source/en/model_doc/sew.mdx
Normal file
57
docs/source/en/model_doc/sew.mdx
Normal file
@@ -0,0 +1,57 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# SEW
|
||||
|
||||
## Overview
|
||||
|
||||
SEW (Squeezed and Efficient Wav2Vec) was proposed in [Performance-Efficiency Trade-offs in Unsupervised Pre-training
|
||||
for Speech Recognition](https://arxiv.org/abs/2109.06870) by Felix Wu, Kwangyoun Kim, Jing Pan, Kyu Han, Kilian Q.
|
||||
Weinberger, Yoav Artzi.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*This paper is a study of performance-efficiency trade-offs in pre-trained models for automatic speech recognition
|
||||
(ASR). We focus on wav2vec 2.0, and formalize several architecture designs that influence both the model performance
|
||||
and its efficiency. Putting together all our observations, we introduce SEW (Squeezed and Efficient Wav2vec), a
|
||||
pre-trained model architecture with significant improvements along both performance and efficiency dimensions across a
|
||||
variety of training setups. For example, under the 100h-960h semi-supervised setup on LibriSpeech, SEW achieves a 1.9x
|
||||
inference speedup compared to wav2vec 2.0, with a 13.5% relative reduction in word error rate. With a similar inference
|
||||
time, SEW reduces word error rate by 25-50% across different model sizes.*
|
||||
|
||||
Tips:
|
||||
|
||||
- SEW is a speech model that accepts a float array corresponding to the raw waveform of the speech signal.
|
||||
- SEWForCTC is fine-tuned using connectionist temporal classification (CTC) so the model output has to be decoded using
|
||||
[`Wav2Vec2CTCTokenizer`].
|
||||
|
||||
This model was contributed by [anton-l](https://huggingface.co/anton-l).
|
||||
|
||||
|
||||
## SEWConfig
|
||||
|
||||
[[autodoc]] SEWConfig
|
||||
|
||||
## SEWModel
|
||||
|
||||
[[autodoc]] SEWModel
|
||||
- forward
|
||||
|
||||
## SEWForCTC
|
||||
|
||||
[[autodoc]] SEWForCTC
|
||||
- forward
|
||||
|
||||
## SEWForSequenceClassification
|
||||
|
||||
[[autodoc]] SEWForSequenceClassification
|
||||
- forward
|
||||
41
docs/source/en/model_doc/speech-encoder-decoder.mdx
Normal file
41
docs/source/en/model_doc/speech-encoder-decoder.mdx
Normal file
@@ -0,0 +1,41 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Speech Encoder Decoder Models
|
||||
|
||||
The [`SpeechEncoderDecoderModel`] can be used to initialize a speech-sequence-to-text-sequence model
|
||||
with any pretrained speech autoencoding model as the encoder (*e.g.* [Wav2Vec2](wav2vec2), [Hubert](hubert)) and any pretrained autoregressive model as the decoder.
|
||||
|
||||
The effectiveness of initializing speech-sequence-to-text-sequence models with pretrained checkpoints for speech
|
||||
recognition and speech translation has *e.g.* been shown in [Large-Scale Self- and Semi-Supervised Learning for Speech
|
||||
Translation](https://arxiv.org/abs/2104.06678) by Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli,
|
||||
Alexis Conneau.
|
||||
|
||||
An example of how to use a [`SpeechEncoderDecoderModel`] for inference can be seen in
|
||||
[Speech2Text2](speech_to_text_2).
|
||||
|
||||
|
||||
## SpeechEncoderDecoderConfig
|
||||
|
||||
[[autodoc]] SpeechEncoderDecoderConfig
|
||||
|
||||
## SpeechEncoderDecoderModel
|
||||
|
||||
[[autodoc]] SpeechEncoderDecoderModel
|
||||
- forward
|
||||
- from_encoder_decoder_pretrained
|
||||
|
||||
## FlaxSpeechEncoderDecoderModel
|
||||
|
||||
[[autodoc]] FlaxSpeechEncoderDecoderModel
|
||||
- __call__
|
||||
- from_encoder_decoder_pretrained
|
||||
156
docs/source/en/model_doc/speech_to_text.mdx
Normal file
156
docs/source/en/model_doc/speech_to_text.mdx
Normal file
@@ -0,0 +1,156 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Speech2Text
|
||||
|
||||
## Overview
|
||||
|
||||
The Speech2Text model was proposed in [fairseq S2T: Fast Speech-to-Text Modeling with fairseq](https://arxiv.org/abs/2010.05171) by Changhan Wang, Yun Tang, Xutai Ma, Anne Wu, Dmytro Okhonko, Juan Pino. It's a
|
||||
transformer-based seq2seq (encoder-decoder) model designed for end-to-end Automatic Speech Recognition (ASR) and Speech
|
||||
Translation (ST). It uses a convolutional downsampler to reduce the length of speech inputs by 3/4th before they are
|
||||
fed into the encoder. The model is trained with standard autoregressive cross-entropy loss and generates the
|
||||
transcripts/translations autoregressively. Speech2Text has been fine-tuned on several datasets for ASR and ST:
|
||||
[LibriSpeech](http://www.openslr.org/12), [CoVoST 2](https://github.com/facebookresearch/covost), [MuST-C](https://ict.fbk.eu/must-c/).
|
||||
|
||||
This model was contributed by [valhalla](https://huggingface.co/valhalla). The original code can be found [here](https://github.com/pytorch/fairseq/tree/master/examples/speech_to_text).
|
||||
|
||||
|
||||
## Inference
|
||||
|
||||
Speech2Text is a speech model that accepts a float tensor of log-mel filter-bank features extracted from the speech
|
||||
signal. It's a transformer-based seq2seq model, so the transcripts/translations are generated autoregressively. The
|
||||
`generate()` method can be used for inference.
|
||||
|
||||
The [`Speech2TextFeatureExtractor`] class is responsible for extracting the log-mel filter-bank
|
||||
features. The [`Speech2TextProcessor`] wraps [`Speech2TextFeatureExtractor`] and
|
||||
[`Speech2TextTokenizer`] into a single instance to both extract the input features and decode the
|
||||
predicted token ids.
|
||||
|
||||
The feature extractor depends on `torchaudio` and the tokenizer depends on `sentencepiece` so be sure to
|
||||
install those packages before running the examples. You could either install those as extra speech dependencies with
|
||||
`pip install transformers"[speech, sentencepiece]"` or install the packages seperately with `pip install torchaudio sentencepiece`. Also `torchaudio` requires the development version of the [libsndfile](http://www.mega-nerd.com/libsndfile/) package which can be installed via a system package manager. On Ubuntu it can
|
||||
be installed as follows: `apt install libsndfile1-dev`
|
||||
|
||||
|
||||
- ASR and Speech Translation
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
|
||||
>>> from datasets import load_dataset
|
||||
>>> import soundfile as sf
|
||||
|
||||
>>> model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr")
|
||||
>>> processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr")
|
||||
|
||||
|
||||
>>> def map_to_array(batch):
|
||||
... speech, _ = sf.read(batch["file"])
|
||||
... batch["speech"] = speech
|
||||
... return batch
|
||||
|
||||
|
||||
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
||||
>>> ds = ds.map(map_to_array)
|
||||
|
||||
>>> inputs = processor(ds["speech"][0], sampling_rate=16_000, return_tensors="pt")
|
||||
>>> generated_ids = model.generate(input_ids=inputs["input_features"], attention_mask=inputs["attention_mask"])
|
||||
|
||||
>>> transcription = processor.batch_decode(generated_ids)
|
||||
```
|
||||
|
||||
- Multilingual speech translation
|
||||
|
||||
For multilingual speech translation models, `eos_token_id` is used as the `decoder_start_token_id` and
|
||||
the target language id is forced as the first generated token. To force the target language id as the first
|
||||
generated token, pass the `forced_bos_token_id` parameter to the `generate()` method. The following
|
||||
example shows how to transate English speech to French text using the *facebook/s2t-medium-mustc-multilingual-st*
|
||||
checkpoint.
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
|
||||
>>> from datasets import load_dataset
|
||||
>>> import soundfile as sf
|
||||
|
||||
>>> model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-medium-mustc-multilingual-st")
|
||||
>>> processor = Speech2TextProcessor.from_pretrained("facebook/s2t-medium-mustc-multilingual-st")
|
||||
|
||||
|
||||
>>> def map_to_array(batch):
|
||||
... speech, _ = sf.read(batch["file"])
|
||||
... batch["speech"] = speech
|
||||
... return batch
|
||||
|
||||
|
||||
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
||||
>>> ds = ds.map(map_to_array)
|
||||
|
||||
>>> inputs = processor(ds["speech"][0], sampling_rate=16_000, return_tensors="pt")
|
||||
>>> generated_ids = model.generate(
|
||||
... input_ids=inputs["input_features"],
|
||||
... attention_mask=inputs["attention_mask"],
|
||||
... forced_bos_token_id=processor.tokenizer.lang_code_to_id["fr"],
|
||||
... )
|
||||
|
||||
>>> translation = processor.batch_decode(generated_ids)
|
||||
```
|
||||
|
||||
See the [model hub](https://huggingface.co/models?filter=speech_to_text) to look for Speech2Text checkpoints.
|
||||
|
||||
|
||||
## Speech2TextConfig
|
||||
|
||||
[[autodoc]] Speech2TextConfig
|
||||
|
||||
## Speech2TextTokenizer
|
||||
|
||||
[[autodoc]] Speech2TextTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## Speech2TextFeatureExtractor
|
||||
|
||||
[[autodoc]] Speech2TextFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## Speech2TextProcessor
|
||||
|
||||
[[autodoc]] Speech2TextProcessor
|
||||
- __call__
|
||||
- from_pretrained
|
||||
- save_pretrained
|
||||
- batch_decode
|
||||
- decode
|
||||
- as_target_processor
|
||||
|
||||
## Speech2TextModel
|
||||
|
||||
[[autodoc]] Speech2TextModel
|
||||
- forward
|
||||
|
||||
## Speech2TextForConditionalGeneration
|
||||
|
||||
[[autodoc]] Speech2TextForConditionalGeneration
|
||||
- forward
|
||||
|
||||
## TFSpeech2TextModel
|
||||
|
||||
[[autodoc]] TFSpeech2TextModel
|
||||
- call
|
||||
|
||||
## TFSpeech2TextForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFSpeech2TextForConditionalGeneration
|
||||
- call
|
||||
122
docs/source/en/model_doc/speech_to_text_2.mdx
Normal file
122
docs/source/en/model_doc/speech_to_text_2.mdx
Normal file
@@ -0,0 +1,122 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Speech2Text2
|
||||
|
||||
## Overview
|
||||
|
||||
The Speech2Text2 model is used together with [Wav2Vec2](wav2vec2) for Speech Translation models proposed in
|
||||
[Large-Scale Self- and Semi-Supervised Learning for Speech Translation](https://arxiv.org/abs/2104.06678) by
|
||||
Changhan Wang, Anne Wu, Juan Pino, Alexei Baevski, Michael Auli, Alexis Conneau.
|
||||
|
||||
Speech2Text2 is a *decoder-only* transformer model that can be used with any speech *encoder-only*, such as
|
||||
[Wav2Vec2](wav2vec2) or [HuBERT](hubert) for Speech-to-Text tasks. Please refer to the
|
||||
[SpeechEncoderDecoder](speech-encoder-decoder) class on how to combine Speech2Text2 with any speech *encoder-only*
|
||||
model.
|
||||
|
||||
This model was contributed by [Patrick von Platen](https://huggingface.co/patrickvonplaten).
|
||||
|
||||
The original code can be found [here](https://github.com/pytorch/fairseq/blob/1f7ef9ed1e1061f8c7f88f8b94c7186834398690/fairseq/models/wav2vec/wav2vec2_asr.py#L266).
|
||||
|
||||
|
||||
Tips:
|
||||
|
||||
- Speech2Text2 achieves state-of-the-art results on the CoVoST Speech Translation dataset. For more information, see
|
||||
the [official models](https://huggingface.co/models?other=speech2text2) .
|
||||
- Speech2Text2 is always used within the [SpeechEncoderDecoder](speech-encoder-decoder) framework.
|
||||
- Speech2Text2's tokenizer is based on [fastBPE](https://github.com/glample/fastBPE).
|
||||
|
||||
## Inference
|
||||
|
||||
Speech2Text2's [`SpeechEncoderDecoderModel`] model accepts raw waveform input values from speech and
|
||||
makes use of [`~generation_utils.GenerationMixin.generate`] to translate the input speech
|
||||
autoregressively to the target language.
|
||||
|
||||
The [`Wav2Vec2FeatureExtractor`] class is responsible for preprocessing the input speech and
|
||||
[`Speech2Text2Tokenizer`] decodes the generated target tokens to the target string. The
|
||||
[`Speech2Text2Processor`] wraps [`Wav2Vec2FeatureExtractor`] and
|
||||
[`Speech2Text2Tokenizer`] into a single instance to both extract the input features and decode the
|
||||
predicted token ids.
|
||||
|
||||
- Step-by-step Speech Translation
|
||||
|
||||
```python
|
||||
>>> import torch
|
||||
>>> from transformers import Speech2Text2Processor, SpeechEncoderDecoderModel
|
||||
>>> from datasets import load_dataset
|
||||
>>> import soundfile as sf
|
||||
|
||||
>>> model = SpeechEncoderDecoderModel.from_pretrained("facebook/s2t-wav2vec2-large-en-de")
|
||||
>>> processor = Speech2Text2Processor.from_pretrained("facebook/s2t-wav2vec2-large-en-de")
|
||||
|
||||
|
||||
>>> def map_to_array(batch):
|
||||
... speech, _ = sf.read(batch["file"])
|
||||
... batch["speech"] = speech
|
||||
... return batch
|
||||
|
||||
|
||||
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
||||
>>> ds = ds.map(map_to_array)
|
||||
|
||||
>>> inputs = processor(ds["speech"][0], sampling_rate=16_000, return_tensors="pt")
|
||||
>>> generated_ids = model.generate(inputs=inputs["input_values"], attention_mask=inputs["attention_mask"])
|
||||
|
||||
>>> transcription = processor.batch_decode(generated_ids)
|
||||
```
|
||||
|
||||
- Speech Translation via Pipelines
|
||||
|
||||
The automatic speech recognition pipeline can also be used to translate speech in just a couple lines of code
|
||||
|
||||
```python
|
||||
>>> from datasets import load_dataset
|
||||
>>> from transformers import pipeline
|
||||
|
||||
>>> librispeech_en = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
||||
>>> asr = pipeline(
|
||||
... "automatic-speech-recognition",
|
||||
... model="facebook/s2t-wav2vec2-large-en-de",
|
||||
... feature_extractor="facebook/s2t-wav2vec2-large-en-de",
|
||||
... )
|
||||
|
||||
>>> translation_de = asr(librispeech_en[0]["file"])
|
||||
```
|
||||
|
||||
See [model hub](https://huggingface.co/models?filter=speech2text2) to look for Speech2Text2 checkpoints.
|
||||
|
||||
|
||||
## Speech2Text2Config
|
||||
|
||||
[[autodoc]] Speech2Text2Config
|
||||
|
||||
## Speech2TextTokenizer
|
||||
|
||||
[[autodoc]] Speech2Text2Tokenizer
|
||||
- batch_decode
|
||||
- decode
|
||||
- save_vocabulary
|
||||
|
||||
## Speech2Text2Processor
|
||||
|
||||
[[autodoc]] Speech2Text2Processor
|
||||
- __call__
|
||||
- from_pretrained
|
||||
- save_pretrained
|
||||
- batch_decode
|
||||
- decode
|
||||
- as_target_processor
|
||||
|
||||
## Speech2Text2ForCausalLM
|
||||
|
||||
[[autodoc]] Speech2Text2ForCausalLM
|
||||
- forward
|
||||
74
docs/source/en/model_doc/splinter.mdx
Normal file
74
docs/source/en/model_doc/splinter.mdx
Normal file
@@ -0,0 +1,74 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Splinter
|
||||
|
||||
## Overview
|
||||
|
||||
The Splinter model was proposed in [Few-Shot Question Answering by Pretraining Span Selection](https://arxiv.org/abs/2101.00438) by Ori Ram, Yuval Kirstain, Jonathan Berant, Amir Globerson, Omer Levy. Splinter
|
||||
is an encoder-only transformer (similar to BERT) pretrained using the recurring span selection task on a large corpus
|
||||
comprising Wikipedia and the Toronto Book Corpus.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
In several question answering benchmarks, pretrained models have reached human parity through fine-tuning on an order
|
||||
of 100,000 annotated questions and answers. We explore the more realistic few-shot setting, where only a few hundred
|
||||
training examples are available, and observe that standard models perform poorly, highlighting the discrepancy between
|
||||
current pretraining objectives and question answering. We propose a new pretraining scheme tailored for question
|
||||
answering: recurring span selection. Given a passage with multiple sets of recurring spans, we mask in each set all
|
||||
recurring spans but one, and ask the model to select the correct span in the passage for each masked span. Masked spans
|
||||
are replaced with a special token, viewed as a question representation, that is later used during fine-tuning to select
|
||||
the answer span. The resulting model obtains surprisingly good results on multiple benchmarks (e.g., 72.7 F1 on SQuAD
|
||||
with only 128 training examples), while maintaining competitive performance in the high-resource setting.
|
||||
|
||||
Tips:
|
||||
|
||||
- Splinter was trained to predict answers spans conditioned on a special [QUESTION] token. These tokens contextualize
|
||||
to question representations which are used to predict the answers. This layer is called QASS, and is the default
|
||||
behaviour in the [`SplinterForQuestionAnswering`] class. Therefore:
|
||||
- Use [`SplinterTokenizer`] (rather than [`BertTokenizer`]), as it already
|
||||
contains this special token. Also, its default behavior is to use this token when two sequences are given (for
|
||||
example, in the *run_qa.py* script).
|
||||
- If you plan on using Splinter outside *run_qa.py*, please keep in mind the question token - it might be important for
|
||||
the success of your model, especially in a few-shot setting.
|
||||
- Please note there are two different checkpoints for each size of Splinter. Both are basically the same, except that
|
||||
one also has the pretrained wights of the QASS layer (*tau/splinter-base-qass* and *tau/splinter-large-qass*) and one
|
||||
doesn't (*tau/splinter-base* and *tau/splinter-large*). This is done to support randomly initializing this layer at
|
||||
fine-tuning, as it is shown to yield better results for some cases in the paper.
|
||||
|
||||
This model was contributed by [yuvalkirstain](https://huggingface.co/yuvalkirstain) and [oriram](https://huggingface.co/oriram). The original code can be found [here](https://github.com/oriram/splinter).
|
||||
|
||||
## SplinterConfig
|
||||
|
||||
[[autodoc]] SplinterConfig
|
||||
|
||||
## SplinterTokenizer
|
||||
|
||||
[[autodoc]] SplinterTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## SplinterTokenizerFast
|
||||
|
||||
[[autodoc]] SplinterTokenizerFast
|
||||
|
||||
## SplinterModel
|
||||
|
||||
[[autodoc]] SplinterModel
|
||||
- forward
|
||||
|
||||
## SplinterForQuestionAnswering
|
||||
|
||||
[[autodoc]] SplinterForQuestionAnswering
|
||||
- forward
|
||||
88
docs/source/en/model_doc/squeezebert.mdx
Normal file
88
docs/source/en/model_doc/squeezebert.mdx
Normal file
@@ -0,0 +1,88 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# SqueezeBERT
|
||||
|
||||
## Overview
|
||||
|
||||
The SqueezeBERT model was proposed in [SqueezeBERT: What can computer vision teach NLP about efficient neural networks?](https://arxiv.org/abs/2006.11316) by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, Kurt W. Keutzer. It's a
|
||||
bidirectional transformer similar to the BERT model. The key difference between the BERT architecture and the
|
||||
SqueezeBERT architecture is that SqueezeBERT uses [grouped convolutions](https://blog.yani.io/filter-group-tutorial)
|
||||
instead of fully-connected layers for the Q, K, V and FFN layers.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Humans read and write hundreds of billions of messages every day. Further, due to the availability of large datasets,
|
||||
large computing systems, and better neural network models, natural language processing (NLP) technology has made
|
||||
significant strides in understanding, proofreading, and organizing these messages. Thus, there is a significant
|
||||
opportunity to deploy NLP in myriad applications to help web users, social networks, and businesses. In particular, we
|
||||
consider smartphones and other mobile devices as crucial platforms for deploying NLP models at scale. However, today's
|
||||
highly-accurate NLP neural network models such as BERT and RoBERTa are extremely computationally expensive, with
|
||||
BERT-base taking 1.7 seconds to classify a text snippet on a Pixel 3 smartphone. In this work, we observe that methods
|
||||
such as grouped convolutions have yielded significant speedups for computer vision networks, but many of these
|
||||
techniques have not been adopted by NLP neural network designers. We demonstrate how to replace several operations in
|
||||
self-attention layers with grouped convolutions, and we use this technique in a novel network architecture called
|
||||
SqueezeBERT, which runs 4.3x faster than BERT-base on the Pixel 3 while achieving competitive accuracy on the GLUE test
|
||||
set. The SqueezeBERT code will be released.*
|
||||
|
||||
Tips:
|
||||
|
||||
- SqueezeBERT is a model with absolute position embeddings so it's usually advised to pad the inputs on the right
|
||||
rather than the left.
|
||||
- SqueezeBERT is similar to BERT and therefore relies on the masked language modeling (MLM) objective. It is therefore
|
||||
efficient at predicting masked tokens and at NLU in general, but is not optimal for text generation. Models trained
|
||||
with a causal language modeling (CLM) objective are better in that regard.
|
||||
- For best results when finetuning on sequence classification tasks, it is recommended to start with the
|
||||
*squeezebert/squeezebert-mnli-headless* checkpoint.
|
||||
|
||||
This model was contributed by [forresti](https://huggingface.co/forresti).
|
||||
|
||||
|
||||
## SqueezeBertConfig
|
||||
|
||||
[[autodoc]] SqueezeBertConfig
|
||||
|
||||
## SqueezeBertTokenizer
|
||||
|
||||
[[autodoc]] SqueezeBertTokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## SqueezeBertTokenizerFast
|
||||
|
||||
[[autodoc]] SqueezeBertTokenizerFast
|
||||
|
||||
## SqueezeBertModel
|
||||
|
||||
[[autodoc]] SqueezeBertModel
|
||||
|
||||
## SqueezeBertForMaskedLM
|
||||
|
||||
[[autodoc]] SqueezeBertForMaskedLM
|
||||
|
||||
## SqueezeBertForSequenceClassification
|
||||
|
||||
[[autodoc]] SqueezeBertForSequenceClassification
|
||||
|
||||
## SqueezeBertForMultipleChoice
|
||||
|
||||
[[autodoc]] SqueezeBertForMultipleChoice
|
||||
|
||||
## SqueezeBertForTokenClassification
|
||||
|
||||
[[autodoc]] SqueezeBertForTokenClassification
|
||||
|
||||
## SqueezeBertForQuestionAnswering
|
||||
|
||||
[[autodoc]] SqueezeBertForQuestionAnswering
|
||||
66
docs/source/en/model_doc/swin.mdx
Normal file
66
docs/source/en/model_doc/swin.mdx
Normal file
@@ -0,0 +1,66 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Swin Transformer
|
||||
|
||||
## Overview
|
||||
|
||||
The Swin Transformer was proposed in [Swin Transformer: Hierarchical Vision Transformer using Shifted Windows](https://arxiv.org/abs/2103.14030)
|
||||
by Ze Liu, Yutong Lin, Yue Cao, Han Hu, Yixuan Wei, Zheng Zhang, Stephen Lin, Baining Guo.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*This paper presents a new vision Transformer, called Swin Transformer, that capably serves as a general-purpose backbone
|
||||
for computer vision. Challenges in adapting Transformer from language to vision arise from differences between the two domains,
|
||||
such as large variations in the scale of visual entities and the high resolution of pixels in images compared to words in text.
|
||||
To address these differences, we propose a hierarchical Transformer whose representation is computed with \bold{S}hifted
|
||||
\bold{win}dows. The shifted windowing scheme brings greater efficiency by limiting self-attention computation to non-overlapping
|
||||
local windows while also allowing for cross-window connection. This hierarchical architecture has the flexibility to model at
|
||||
various scales and has linear computational complexity with respect to image size. These qualities of Swin Transformer make it
|
||||
compatible with a broad range of vision tasks, including image classification (87.3 top-1 accuracy on ImageNet-1K) and dense
|
||||
prediction tasks such as object detection (58.7 box AP and 51.1 mask AP on COCO test-dev) and semantic segmentation
|
||||
(53.5 mIoU on ADE20K val). Its performance surpasses the previous state-of-the-art by a large margin of +2.7 box AP and
|
||||
+2.6 mask AP on COCO, and +3.2 mIoU on ADE20K, demonstrating the potential of Transformer-based models as vision backbones.
|
||||
The hierarchical design and the shifted window approach also prove beneficial for all-MLP architectures.*
|
||||
|
||||
Tips:
|
||||
- One can use the [`AutoFeatureExtractor`] API to prepare images for the model.
|
||||
- Swin pads the inputs supporting any input height and width (if divisible by `32`).
|
||||
- Swin can be used as a *backbone*. When `output_hidden_states = True`, it will output both `hidden_states` and `reshaped_hidden_states`. The `reshaped_hidden_states` have a shape of `(batch, num_channels, height, width)` rather than `(batch_size, sequence_length, num_channels)`.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/swin_transformer_architecture.png"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> Swin Transformer architecture. Taken from the <a href="https://arxiv.org/abs/2102.03334">original paper</a>.</small>
|
||||
|
||||
This model was contributed by [novice03](https://huggingface.co/novice03>). The original code can be found [here](https://github.com/microsoft/Swin-Transformer).
|
||||
|
||||
|
||||
## SwinConfig
|
||||
|
||||
[[autodoc]] SwinConfig
|
||||
|
||||
|
||||
## SwinModel
|
||||
|
||||
[[autodoc]] SwinModel
|
||||
- forward
|
||||
|
||||
## SwinForMaskedImageModeling
|
||||
|
||||
[[autodoc]] SwinForMaskedImageModeling
|
||||
- forward
|
||||
|
||||
## SwinForImageClassification
|
||||
|
||||
[[autodoc]] transformers.SwinForImageClassification
|
||||
- forward
|
||||
348
docs/source/en/model_doc/t5.mdx
Normal file
348
docs/source/en/model_doc/t5.mdx
Normal file
@@ -0,0 +1,348 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# T5
|
||||
|
||||
## Overview
|
||||
|
||||
The T5 model was presented in [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/pdf/1910.10683.pdf) by Colin Raffel, Noam Shazeer, Adam Roberts, Katherine Lee, Sharan Narang,
|
||||
Michael Matena, Yanqi Zhou, Wei Li, Peter J. Liu.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transfer learning, where a model is first pre-trained on a data-rich task before being fine-tuned on a downstream
|
||||
task, has emerged as a powerful technique in natural language processing (NLP). The effectiveness of transfer learning
|
||||
has given rise to a diversity of approaches, methodology, and practice. In this paper, we explore the landscape of
|
||||
transfer learning techniques for NLP by introducing a unified framework that converts every language problem into a
|
||||
text-to-text format. Our systematic study compares pretraining objectives, architectures, unlabeled datasets, transfer
|
||||
approaches, and other factors on dozens of language understanding tasks. By combining the insights from our exploration
|
||||
with scale and our new "Colossal Clean Crawled Corpus", we achieve state-of-the-art results on many benchmarks covering
|
||||
summarization, question answering, text classification, and more. To facilitate future work on transfer learning for
|
||||
NLP, we release our dataset, pre-trained models, and code.*
|
||||
|
||||
Tips:
|
||||
|
||||
- T5 is an encoder-decoder model pre-trained on a multi-task mixture of unsupervised and supervised tasks and for which
|
||||
each task is converted into a text-to-text format. T5 works well on a variety of tasks out-of-the-box by prepending a
|
||||
different prefix to the input corresponding to each task, e.g., for translation: *translate English to German: ...*,
|
||||
for summarization: *summarize: ...*.
|
||||
|
||||
- T5 uses relative scalar embeddings. Encoder input padding can be done on the left and on the right.
|
||||
|
||||
- See the [training](#training), [inference](#inference) and [scripts](#scripts) sections below for all details regarding usage.
|
||||
|
||||
T5 comes in different sizes:
|
||||
|
||||
- [t5-small](https://huggingface.co/t5-small)
|
||||
|
||||
- [t5-base](https://huggingface.co/t5-base)
|
||||
|
||||
- [t5-large](https://huggingface.co/t5-large)
|
||||
|
||||
- [t5-3b](https://huggingface.co/t5-3b)
|
||||
|
||||
- [t5-11b](https://huggingface.co/t5-11b).
|
||||
|
||||
Based on the original T5 model, Google has released some follow-up works:
|
||||
|
||||
- **T5v1.1**: T5v1.1 is an improved version of T5 with some architectural tweaks, and is pre-trained on C4 only without
|
||||
mixing in the supervised tasks. Refer to the documentation of T5v1.1 which can be found [here](t5v1.1).
|
||||
|
||||
- **mT5**: mT5 is a multilingual T5 model. It is pre-trained on the mC4 corpus, which includes 101 languages. Refer to
|
||||
the documentation of mT5 which can be found [here](mt5).
|
||||
|
||||
- **byT5**: byT5 is a T5 model pre-trained on byte sequences rather than SentencePiece subword token sequences. Refer
|
||||
to the documentation of byT5 which can be found [here](byt5).
|
||||
|
||||
All checkpoints can be found on the [hub](https://huggingface.co/models?search=t5).
|
||||
|
||||
This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/google-research/text-to-text-transfer-transformer).
|
||||
|
||||
<a id='training'></a>
|
||||
|
||||
## Training
|
||||
|
||||
T5 is an encoder-decoder model and converts all NLP problems into a text-to-text format. It is trained using teacher
|
||||
forcing. This means that for training, we always need an input sequence and a corresponding target sequence. The input
|
||||
sequence is fed to the model using `input_ids`. The target sequence is shifted to the right, i.e., prepended by a
|
||||
start-sequence token and fed to the decoder using the `decoder_input_ids`. In teacher-forcing style, the target
|
||||
sequence is then appended by the EOS token and corresponds to the `labels`. The PAD token is hereby used as the
|
||||
start-sequence token. T5 can be trained / fine-tuned both in a supervised and unsupervised fashion.
|
||||
|
||||
One can use [`T5ForConditionalGeneration`] (or the Tensorflow/Flax variant), which includes the
|
||||
language modeling head on top of the decoder.
|
||||
|
||||
- Unsupervised denoising training
|
||||
|
||||
In this setup, spans of the input sequence are masked by so-called sentinel tokens (*a.k.a* unique mask tokens) and
|
||||
the output sequence is formed as a concatenation of the same sentinel tokens and the *real* masked tokens. Each
|
||||
sentinel token represents a unique mask token for this sentence and should start with `<extra_id_0>`,
|
||||
`<extra_id_1>`, ... up to `<extra_id_99>`. As a default, 100 sentinel tokens are available in
|
||||
[`T5Tokenizer`].
|
||||
|
||||
For instance, the sentence "The cute dog walks in the park" with the masks put on "cute dog" and "the" should be
|
||||
processed as follows:
|
||||
|
||||
```python
|
||||
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||
|
||||
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||
|
||||
input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids
|
||||
labels = tokenizer("<extra_id_0> cute dog <extra_id_1> the <extra_id_2>", return_tensors="pt").input_ids
|
||||
# the forward function automatically creates the correct decoder_input_ids
|
||||
loss = model(input_ids=input_ids, labels=labels).loss
|
||||
```
|
||||
|
||||
If you're interested in pre-training T5 on a new corpus, check out the [run_t5_mlm_flax.py](https://github.com/huggingface/transformers/tree/main/examples/flax/language-modeling) script in the Examples
|
||||
directory.
|
||||
|
||||
- Supervised training
|
||||
|
||||
In this setup, the input sequence and output sequence are a standard sequence-to-sequence input-output mapping.
|
||||
Suppose that we want to fine-tune the model for translation for example, and we have a training example: the input
|
||||
sequence "The house is wonderful." and output sequence "Das Haus ist wunderbar.", then they should be prepared for
|
||||
the model as follows:
|
||||
|
||||
```python
|
||||
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||
|
||||
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||
|
||||
input_ids = tokenizer("translate English to German: The house is wonderful.", return_tensors="pt").input_ids
|
||||
labels = tokenizer("Das Haus ist wunderbar.", return_tensors="pt").input_ids
|
||||
# the forward function automatically creates the correct decoder_input_ids
|
||||
loss = model(input_ids=input_ids, labels=labels).loss
|
||||
```
|
||||
|
||||
As you can see, only 2 inputs are required for the model in order to compute a loss: `input_ids` (which are the
|
||||
`input_ids` of the encoded input sequence) and `labels` (which are the `input_ids` of the encoded
|
||||
target sequence). The model will automatically create the `decoder_input_ids` based on the `labels`, by
|
||||
shifting them one position to the right and prepending the `config.decoder_start_token_id`, which for T5 is
|
||||
equal to 0 (i.e. the id of the pad token). Also note the task prefix: we prepend the input sequence with 'translate
|
||||
English to German: ' before encoding it. This will help in improving the performance, as this task prefix was used
|
||||
during T5's pre-training.
|
||||
|
||||
However, the example above only shows a single training example. In practice, one trains deep learning models in
|
||||
batches. This entails that we must pad/truncate examples to the same length. For encoder-decoder models, one
|
||||
typically defines a `max_source_length` and `max_target_length`, which determine the maximum length of the
|
||||
input and output sequences respectively (otherwise they are truncated). These should be carefully set depending on
|
||||
the task.
|
||||
|
||||
In addition, we must make sure that padding token id's of the `labels` are not taken into account by the loss
|
||||
function. In PyTorch and Tensorflow, this can be done by replacing them with -100, which is the `ignore_index`
|
||||
of the `CrossEntropyLoss`. In Flax, one can use the `decoder_attention_mask` to ignore padded tokens from
|
||||
the loss (see the [Flax summarization script](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization) for details). We also pass
|
||||
`attention_mask` as additional input to the model, which makes sure that padding tokens of the inputs are
|
||||
ignored. The code example below illustrates all of this.
|
||||
|
||||
```python
|
||||
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||
import torch
|
||||
|
||||
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||
|
||||
# the following 2 hyperparameters are task-specific
|
||||
max_source_length = 512
|
||||
max_target_length = 128
|
||||
|
||||
# Suppose we have the following 2 training examples:
|
||||
input_sequence_1 = "Welcome to NYC"
|
||||
output_sequence_1 = "Bienvenue à NYC"
|
||||
|
||||
input_sequence_2 = "HuggingFace is a company"
|
||||
output_sequence_2 = "HuggingFace est une entreprise"
|
||||
|
||||
# encode the inputs
|
||||
task_prefix = "translate English to French: "
|
||||
input_sequences = [input_sequence_1, input_sequence_2]
|
||||
encoding = tokenizer(
|
||||
[task_prefix + sequence for sequence in input_sequences],
|
||||
padding="longest",
|
||||
max_length=max_source_length,
|
||||
truncation=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
input_ids, attention_mask = encoding.input_ids, encoding.attention_mask
|
||||
|
||||
# encode the targets
|
||||
target_encoding = tokenizer(
|
||||
[output_sequence_1, output_sequence_2], padding="longest", max_length=max_target_length, truncation=True
|
||||
)
|
||||
labels = target_encoding.input_ids
|
||||
|
||||
# replace padding token id's of the labels by -100
|
||||
labels = torch.tensor(labels)
|
||||
labels[labels == tokenizer.pad_token_id] = -100
|
||||
|
||||
# forward pass
|
||||
loss = model(input_ids=input_ids, attention_mask=attention_mask, labels=labels).loss
|
||||
```
|
||||
|
||||
Additional training tips:
|
||||
|
||||
- T5 models need a slightly higher learning rate than the default one set in the `Trainer` when using the AdamW
|
||||
optimizer. Typically, 1e-4 and 3e-4 work well for most problems (classification, summarization, translation, question
|
||||
answering, question generation). Note that T5 was pre-trained using the AdaFactor optimizer.
|
||||
|
||||
- According to [this forum post](https://discuss.huggingface.co/t/t5-finetuning-tips/684), task prefixes matter when
|
||||
(1) doing multi-task training (2) your task is similar or related to one of the supervised tasks used in T5's
|
||||
pre-training mixture (see Appendix D of the [paper](https://arxiv.org/pdf/1910.10683.pdf) for the task prefixes
|
||||
used).
|
||||
|
||||
- If training on TPU, it is recommended to pad all examples of the dataset to the same length or make use of
|
||||
*pad_to_multiple_of* to have a small number of predefined bucket sizes to fit all examples in. Dynamically padding
|
||||
batches to the longest example is not recommended on TPU as it triggers a recompilation for every batch shape that is
|
||||
encountered during training thus significantly slowing down the training. only padding up to the longest example in a
|
||||
batch) leads to very slow training on TPU.
|
||||
|
||||
<a id='inference'></a>
|
||||
|
||||
## Inference
|
||||
|
||||
At inference time, it is recommended to use [`~generation_utils.GenerationMixin.generate`]. This
|
||||
method takes care of encoding the input and feeding the encoded hidden states via cross-attention layers to the decoder
|
||||
and auto-regressively generates the decoder output. Check out [this blog post](https://huggingface.co/blog/how-to-generate) to know all the details about generating text with Transformers.
|
||||
There's also [this blog post](https://huggingface.co/blog/encoder-decoder#encoder-decoder) which explains how
|
||||
generation works in general in encoder-decoder models.
|
||||
|
||||
```python
|
||||
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||
|
||||
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||
|
||||
input_ids = tokenizer("translate English to German: The house is wonderful.", return_tensors="pt").input_ids
|
||||
outputs = model.generate(input_ids)
|
||||
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
||||
# Das Haus ist wunderbar.
|
||||
```
|
||||
|
||||
Note that T5 uses the `pad_token_id` as the `decoder_start_token_id`, so when doing generation without using
|
||||
[`~generation_utils.GenerationMixin.generate`], make sure you start it with the `pad_token_id`.
|
||||
|
||||
The example above only shows a single example. You can also do batched inference, like so:
|
||||
|
||||
```python
|
||||
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
||||
|
||||
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
||||
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
||||
|
||||
# when generating, we will use the logits of right-most token to predict the next token
|
||||
# so the padding should be on the left
|
||||
tokenizer.padding_side = "left"
|
||||
tokenizer.pad_token = tokenizer.eos_token # to avoid an error
|
||||
|
||||
task_prefix = "translate English to German: "
|
||||
sentences = ["The house is wonderful.", "I like to work in NYC."] # use different length sentences to test batching
|
||||
inputs = tokenizer([task_prefix + sentence for sentence in sentences], return_tensors="pt", padding=True)
|
||||
|
||||
output_sequences = model.generate(
|
||||
input_ids=inputs["input_ids"],
|
||||
attention_mask=inputs["attention_mask"],
|
||||
do_sample=False, # disable sampling to test if batching affects output
|
||||
)
|
||||
|
||||
print(tokenizer.batch_decode(output_sequences, skip_special_tokens=True))
|
||||
|
||||
# ['Das Haus ist wunderbar.', 'Ich arbeite gerne in NYC.']
|
||||
```
|
||||
|
||||
<a id='scripts'></a>
|
||||
|
||||
## Performance
|
||||
|
||||
If you'd like a faster training and inference performance, install [apex](https://github.com/NVIDIA/apex#quick-start) and then the model will automatically use `apex.normalization.FusedRMSNorm` instead of `T5LayerNorm`. The former uses an optimized fused kernel which is several times faster than the latter.
|
||||
|
||||
|
||||
## Example scripts
|
||||
|
||||
T5 is supported by several example scripts, both for pre-training and fine-tuning.
|
||||
|
||||
- pre-training: the [run_t5_mlm_flax.py](https://github.com/huggingface/transformers/blob/main/examples/flax/language-modeling/run_t5_mlm_flax.py)
|
||||
script allows you to further pre-train T5 or pre-train T5 from scratch on your own data. The [t5_tokenizer_model.py](https://github.com/huggingface/transformers/blob/main/examples/flax/language-modeling/t5_tokenizer_model.py)
|
||||
script allows you to further train a T5 tokenizer or train a T5 Tokenizer from scratch on your own data. Note that
|
||||
Flax (a neural network library on top of JAX) is particularly useful to train on TPU hardware.
|
||||
|
||||
- fine-tuning: T5 is supported by the official summarization scripts ([PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/summarization), [Tensorflow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/summarization), and [Flax](https://github.com/huggingface/transformers/tree/main/examples/flax/summarization)) and translation scripts
|
||||
([PyTorch](https://github.com/huggingface/transformers/tree/main/examples/pytorch/translation) and [Tensorflow](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/translation)). These scripts allow
|
||||
you to easily fine-tune T5 on custom data for summarization/translation.
|
||||
|
||||
## T5Config
|
||||
|
||||
[[autodoc]] T5Config
|
||||
|
||||
## T5Tokenizer
|
||||
|
||||
[[autodoc]] T5Tokenizer
|
||||
- build_inputs_with_special_tokens
|
||||
- get_special_tokens_mask
|
||||
- create_token_type_ids_from_sequences
|
||||
- save_vocabulary
|
||||
|
||||
## T5TokenizerFast
|
||||
|
||||
[[autodoc]] T5TokenizerFast
|
||||
|
||||
## T5Model
|
||||
|
||||
[[autodoc]] T5Model
|
||||
- forward
|
||||
- parallelize
|
||||
- deparallelize
|
||||
|
||||
## T5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] T5ForConditionalGeneration
|
||||
- forward
|
||||
- parallelize
|
||||
- deparallelize
|
||||
|
||||
## T5EncoderModel
|
||||
|
||||
[[autodoc]] T5EncoderModel
|
||||
- forward
|
||||
- parallelize
|
||||
- deparallelize
|
||||
|
||||
## TFT5Model
|
||||
|
||||
[[autodoc]] TFT5Model
|
||||
- call
|
||||
|
||||
## TFT5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] TFT5ForConditionalGeneration
|
||||
- call
|
||||
|
||||
## TFT5EncoderModel
|
||||
|
||||
[[autodoc]] TFT5EncoderModel
|
||||
- call
|
||||
|
||||
## FlaxT5Model
|
||||
|
||||
[[autodoc]] FlaxT5Model
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
|
||||
## FlaxT5ForConditionalGeneration
|
||||
|
||||
[[autodoc]] FlaxT5ForConditionalGeneration
|
||||
- __call__
|
||||
- encode
|
||||
- decode
|
||||
61
docs/source/en/model_doc/t5v1.1.mdx
Normal file
61
docs/source/en/model_doc/t5v1.1.mdx
Normal file
@@ -0,0 +1,61 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# T5v1.1
|
||||
|
||||
## Overview
|
||||
|
||||
T5v1.1 was released in the [google-research/text-to-text-transfer-transformer](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511)
|
||||
repository by Colin Raffel et al. It's an improved version of the original T5 model.
|
||||
|
||||
One can directly plug in the weights of T5v1.1 into a T5 model, like so:
|
||||
|
||||
```python
|
||||
from transformers import T5ForConditionalGeneration
|
||||
|
||||
model = T5ForConditionalGeneration.from_pretrained("google/t5-v1_1-base")
|
||||
```
|
||||
|
||||
T5 Version 1.1 includes the following improvements compared to the original T5 model:
|
||||
|
||||
- GEGLU activation in the feed-forward hidden layer, rather than ReLU. See [this paper](https://arxiv.org/abs/2002.05202).
|
||||
|
||||
- Dropout was turned off in pre-training (quality win). Dropout should be re-enabled during fine-tuning.
|
||||
|
||||
- Pre-trained on C4 only without mixing in the downstream tasks.
|
||||
|
||||
- No parameter sharing between the embedding and classifier layer.
|
||||
|
||||
- "xl" and "xxl" replace "3B" and "11B". The model shapes are a bit different - larger `d_model` and smaller
|
||||
`num_heads` and `d_ff`.
|
||||
|
||||
Note: T5 Version 1.1 was only pre-trained on [C4](https://huggingface.co/datasets/c4) excluding any supervised
|
||||
training. Therefore, this model has to be fine-tuned before it is useable on a downstream task, unlike the original T5
|
||||
model. Since t5v1.1 was pre-trained unsupervisedly, there's no real advantage to using a task prefix during single-task
|
||||
fine-tuning. If you are doing multi-task fine-tuning, you should use a prefix.
|
||||
|
||||
Google has released the following variants:
|
||||
|
||||
- [google/t5-v1_1-small](https://huggingface.co/google/t5-v1_1-small)
|
||||
|
||||
- [google/t5-v1_1-base](https://huggingface.co/google/t5-v1_1-base)
|
||||
|
||||
- [google/t5-v1_1-large](https://huggingface.co/google/t5-v1_1-large)
|
||||
|
||||
- [google/t5-v1_1-xl](https://huggingface.co/google/t5-v1_1-xl)
|
||||
|
||||
- [google/t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl).
|
||||
|
||||
One can refer to [T5's documentation page](t5) for all tips, code examples and notebooks.
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The original code can be
|
||||
found [here](https://github.com/google-research/text-to-text-transfer-transformer/blob/main/released_checkpoints.md#t511).
|
||||
615
docs/source/en/model_doc/tapas.mdx
Normal file
615
docs/source/en/model_doc/tapas.mdx
Normal file
@@ -0,0 +1,615 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# TAPAS
|
||||
|
||||
## Overview
|
||||
|
||||
The TAPAS model was proposed in [TAPAS: Weakly Supervised Table Parsing via Pre-training](https://www.aclweb.org/anthology/2020.acl-main.398)
|
||||
by Jonathan Herzig, Paweł Krzysztof Nowak, Thomas Müller, Francesco Piccinno and Julian Martin Eisenschlos. It's a BERT-based model specifically
|
||||
designed (and pre-trained) for answering questions about tabular data. Compared to BERT, TAPAS uses relative position embeddings and has 7
|
||||
token types that encode tabular structure. TAPAS is pre-trained on the masked language modeling (MLM) objective on a large dataset comprising
|
||||
millions of tables from English Wikipedia and corresponding texts.
|
||||
|
||||
For question answering, TAPAS has 2 heads on top: a cell selection head and an aggregation head, for (optionally) performing aggregations (such as counting or summing) among selected cells. TAPAS has been fine-tuned on several datasets:
|
||||
- [SQA](https://www.microsoft.com/en-us/download/details.aspx?id=54253) (Sequential Question Answering by Microsoft)
|
||||
- [WTQ](https://github.com/ppasupat/WikiTableQuestions) (Wiki Table Questions by Stanford University)
|
||||
- [WikiSQL](https://github.com/salesforce/WikiSQL) (by Salesforce).
|
||||
|
||||
It achieves state-of-the-art on both SQA and WTQ, while having comparable performance to SOTA on WikiSQL, with a much simpler architecture.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Answering natural language questions over tables is usually seen as a semantic parsing task. To alleviate the collection cost of full logical forms, one popular approach focuses on weak supervision consisting of denotations instead of logical forms. However, training semantic parsers from weak supervision poses difficulties, and in addition, the generated logical forms are only used as an intermediate step prior to retrieving the denotation. In this paper, we present TAPAS, an approach to question answering over tables without generating logical forms. TAPAS trains from weak supervision, and predicts the denotation by selecting table cells and optionally applying a corresponding aggregation operator to such selection. TAPAS extends BERT's architecture to encode tables as input, initializes from an effective joint pre-training of text segments and tables crawled from Wikipedia, and is trained end-to-end. We experiment with three different semantic parsing datasets, and find that TAPAS outperforms or rivals semantic parsing models by improving state-of-the-art accuracy on SQA from 55.1 to 67.2 and performing on par with the state-of-the-art on WIKISQL and WIKITQ, but with a simpler model architecture. We additionally find that transfer learning, which is trivial in our setting, from WIKISQL to WIKITQ, yields 48.7 accuracy, 4.2 points above the state-of-the-art.*
|
||||
|
||||
In addition, the authors have further pre-trained TAPAS to recognize **table entailment**, by creating a balanced dataset of millions of automatically created training examples which are learned in an intermediate step prior to fine-tuning. The authors of TAPAS call this further pre-training intermediate pre-training (since TAPAS is first pre-trained on MLM, and then on another dataset). They found that intermediate pre-training further improves performance on SQA, achieving a new state-of-the-art as well as state-of-the-art on [TabFact](https://github.com/wenhuchen/Table-Fact-Checking), a large-scale dataset with 16k Wikipedia tables for table entailment (a binary classification task). For more details, see their follow-up paper: [Understanding tables with intermediate pre-training](https://www.aclweb.org/anthology/2020.findings-emnlp.27/) by Julian Martin Eisenschlos, Syrine Krichene and Thomas Müller.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/tapas_architecture.png"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> TAPAS architecture. Taken from the <a href="https://ai.googleblog.com/2020/04/using-neural-networks-to-find-answers.html">original blog post</a>.</small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The Tensorflow version of this model was contributed by [kamalkraj](https://huggingface.co/kamalkraj). The original code can be found [here](https://github.com/google-research/tapas).
|
||||
|
||||
Tips:
|
||||
|
||||
- TAPAS is a model that uses relative position embeddings by default (restarting the position embeddings at every cell of the table). Note that this is something that was added after the publication of the original TAPAS paper. According to the authors, this usually results in a slightly better performance, and allows you to encode longer sequences without running out of embeddings. This is reflected in the `reset_position_index_per_cell` parameter of [`TapasConfig`], which is set to `True` by default. The default versions of the models available on the [hub](https://huggingface.co/models?search=tapas) all use relative position embeddings. You can still use the ones with absolute position embeddings by passing in an additional argument `revision="no_reset"` when calling the `from_pretrained()` method. Note that it's usually advised to pad the inputs on the right rather than the left.
|
||||
- TAPAS is based on BERT, so `TAPAS-base` for example corresponds to a `BERT-base` architecture. Of course, `TAPAS-large` will result in the best performance (the results reported in the paper are from `TAPAS-large`). Results of the various sized models are shown on the [original Github repository](https://github.com/google-research/tapas>).
|
||||
- TAPAS has checkpoints fine-tuned on SQA, which are capable of answering questions related to a table in a conversational set-up. This means that you can ask follow-up questions such as "what is his age?" related to the previous question. Note that the forward pass of TAPAS is a bit different in case of a conversational set-up: in that case, you have to feed every table-question pair one by one to the model, such that the `prev_labels` token type ids can be overwritten by the predicted `labels` of the model to the previous question. See "Usage" section for more info.
|
||||
- TAPAS is similar to BERT and therefore relies on the masked language modeling (MLM) objective. It is therefore efficient at predicting masked tokens and at NLU in general, but is not optimal for text generation. Models trained with a causal language modeling (CLM) objective are better in that regard. Note that TAPAS can be used as an encoder in the EncoderDecoderModel framework, to combine it with an autoregressive text decoder such as GPT-2.
|
||||
|
||||
## Usage: fine-tuning
|
||||
|
||||
Here we explain how you can fine-tune [`TapasForQuestionAnswering`] on your own dataset.
|
||||
|
||||
**STEP 1: Choose one of the 3 ways in which you can use TAPAS - or experiment**
|
||||
|
||||
Basically, there are 3 different ways in which one can fine-tune [`TapasForQuestionAnswering`], corresponding to the different datasets on which Tapas was fine-tuned:
|
||||
|
||||
1. SQA: if you're interested in asking follow-up questions related to a table, in a conversational set-up. For example if you first ask "what's the name of the first actor?" then you can ask a follow-up question such as "how old is he?". Here, questions do not involve any aggregation (all questions are cell selection questions).
|
||||
2. WTQ: if you're not interested in asking questions in a conversational set-up, but rather just asking questions related to a table, which might involve aggregation, such as counting a number of rows, summing up cell values or averaging cell values. You can then for example ask "what's the total number of goals Cristiano Ronaldo made in his career?". This case is also called **weak supervision**, since the model itself must learn the appropriate aggregation operator (SUM/COUNT/AVERAGE/NONE) given only the answer to the question as supervision.
|
||||
3. WikiSQL-supervised: this dataset is based on WikiSQL with the model being given the ground truth aggregation operator during training. This is also called **strong supervision**. Here, learning the appropriate aggregation operator is much easier.
|
||||
|
||||
To summarize:
|
||||
|
||||
| **Task** | **Example dataset** | **Description** |
|
||||
|-------------------------------------|---------------------|---------------------------------------------------------------------------------------------------------|
|
||||
| Conversational | SQA | Conversational, only cell selection questions |
|
||||
| Weak supervision for aggregation | WTQ | Questions might involve aggregation, and the model must learn this given only the answer as supervision |
|
||||
| Strong supervision for aggregation | WikiSQL-supervised | Questions might involve aggregation, and the model must learn this given the gold aggregation operator |
|
||||
|
||||
<frameworkcontent>
|
||||
<pt>
|
||||
Initializing a model with a pre-trained base and randomly initialized classification heads from the hub can be done as shown below. Be sure to have installed the
|
||||
[torch-scatter](https://github.com/rusty1s/pytorch_scatter) dependency:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasConfig, TapasForQuestionAnswering
|
||||
|
||||
>>> # for example, the base sized model with default SQA configuration
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained("google/tapas-base")
|
||||
|
||||
>>> # or, the base sized model with WTQ configuration
|
||||
>>> config = TapasConfig.from_pretrained("google/tapas-base-finetuned-wtq")
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
|
||||
>>> # or, the base sized model with WikiSQL configuration
|
||||
>>> config = TapasConfig("google-base-finetuned-wikisql-supervised")
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
```
|
||||
|
||||
Of course, you don't necessarily have to follow one of these three ways in which TAPAS was fine-tuned. You can also experiment by defining any hyperparameters you want when initializing [`TapasConfig`], and then create a [`TapasForQuestionAnswering`] based on that configuration. For example, if you have a dataset that has both conversational questions and questions that might involve aggregation, then you can do it this way. Here's an example:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasConfig, TapasForQuestionAnswering
|
||||
|
||||
>>> # you can initialize the classification heads any way you want (see docs of TapasConfig)
|
||||
>>> config = TapasConfig(num_aggregation_labels=3, average_logits_per_cell=True)
|
||||
>>> # initializing the pre-trained base sized model with our custom classification heads
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
```
|
||||
</pt>
|
||||
<tf>
|
||||
Initializing a model with a pre-trained base and randomly initialized classification heads from the hub can be done as shown below. Be sure to have installed the [tensorflow_probability](https://github.com/tensorflow/probability) dependency:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasConfig, TFTapasForQuestionAnswering
|
||||
|
||||
>>> # for example, the base sized model with default SQA configuration
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained("google/tapas-base")
|
||||
|
||||
>>> # or, the base sized model with WTQ configuration
|
||||
>>> config = TapasConfig.from_pretrained("google/tapas-base-finetuned-wtq")
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
|
||||
>>> # or, the base sized model with WikiSQL configuration
|
||||
>>> config = TapasConfig("google-base-finetuned-wikisql-supervised")
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
```
|
||||
|
||||
Of course, you don't necessarily have to follow one of these three ways in which TAPAS was fine-tuned. You can also experiment by defining any hyperparameters you want when initializing [`TapasConfig`], and then create a [`TFTapasForQuestionAnswering`] based on that configuration. For example, if you have a dataset that has both conversational questions and questions that might involve aggregation, then you can do it this way. Here's an example:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasConfig, TFTapasForQuestionAnswering
|
||||
|
||||
>>> # you can initialize the classification heads any way you want (see docs of TapasConfig)
|
||||
>>> config = TapasConfig(num_aggregation_labels=3, average_logits_per_cell=True)
|
||||
>>> # initializing the pre-trained base sized model with our custom classification heads
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
```
|
||||
</tf>
|
||||
</frameworkcontent>
|
||||
|
||||
What you can also do is start from an already fine-tuned checkpoint. A note here is that the already fine-tuned checkpoint on WTQ has some issues due to the L2-loss which is somewhat brittle. See [here](https://github.com/google-research/tapas/issues/91#issuecomment-735719340) for more info.
|
||||
|
||||
For a list of all pre-trained and fine-tuned TAPAS checkpoints available on HuggingFace's hub, see [here](https://huggingface.co/models?search=tapas).
|
||||
|
||||
**STEP 2: Prepare your data in the SQA format**
|
||||
|
||||
Second, no matter what you picked above, you should prepare your dataset in the [SQA](https://www.microsoft.com/en-us/download/details.aspx?id=54253) format. This format is a TSV/CSV file with the following columns:
|
||||
|
||||
- `id`: optional, id of the table-question pair, for bookkeeping purposes.
|
||||
- `annotator`: optional, id of the person who annotated the table-question pair, for bookkeeping purposes.
|
||||
- `position`: integer indicating if the question is the first, second, third,... related to the table. Only required in case of conversational setup (SQA). You don't need this column in case you're going for WTQ/WikiSQL-supervised.
|
||||
- `question`: string
|
||||
- `table_file`: string, name of a csv file containing the tabular data
|
||||
- `answer_coordinates`: list of one or more tuples (each tuple being a cell coordinate, i.e. row, column pair that is part of the answer)
|
||||
- `answer_text`: list of one or more strings (each string being a cell value that is part of the answer)
|
||||
- `aggregation_label`: index of the aggregation operator. Only required in case of strong supervision for aggregation (the WikiSQL-supervised case)
|
||||
- `float_answer`: the float answer to the question, if there is one (np.nan if there isn't). Only required in case of weak supervision for aggregation (such as WTQ and WikiSQL)
|
||||
|
||||
The tables themselves should be present in a folder, each table being a separate csv file. Note that the authors of the TAPAS algorithm used conversion scripts with some automated logic to convert the other datasets (WTQ, WikiSQL) into the SQA format. The author explains this [here](https://github.com/google-research/tapas/issues/50#issuecomment-705465960). A conversion of this script that works with HuggingFace's implementation can be found [here](https://github.com/NielsRogge/tapas_utils). Interestingly, these conversion scripts are not perfect (the `answer_coordinates` and `float_answer` fields are populated based on the `answer_text`), meaning that WTQ and WikiSQL results could actually be improved.
|
||||
|
||||
**STEP 3: Convert your data into tensors using TapasTokenizer**
|
||||
|
||||
<frameworkcontent>
|
||||
<pt>
|
||||
Third, given that you've prepared your data in this TSV/CSV format (and corresponding CSV files containing the tabular data), you can then use [`TapasTokenizer`] to convert table-question pairs into `input_ids`, `attention_mask`, `token_type_ids` and so on. Again, based on which of the three cases you picked above, [`TapasForQuestionAnswering`] requires different
|
||||
inputs to be fine-tuned:
|
||||
|
||||
| **Task** | **Required inputs** |
|
||||
|------------------------------------|---------------------------------------------------------------------------------------------------------------------|
|
||||
| Conversational | `input_ids`, `attention_mask`, `token_type_ids`, `labels` |
|
||||
| Weak supervision for aggregation | `input_ids`, `attention_mask`, `token_type_ids`, `labels`, `numeric_values`, `numeric_values_scale`, `float_answer` |
|
||||
| Strong supervision for aggregation | `input ids`, `attention mask`, `token type ids`, `labels`, `aggregation_labels` |
|
||||
|
||||
[`TapasTokenizer`] creates the `labels`, `numeric_values` and `numeric_values_scale` based on the `answer_coordinates` and `answer_text` columns of the TSV file. The `float_answer` and `aggregation_labels` are already in the TSV file of step 2. Here's an example:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasTokenizer
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> model_name = "google/tapas-base"
|
||||
>>> tokenizer = TapasTokenizer.from_pretrained(model_name)
|
||||
|
||||
>>> data = {"Actors": ["Brad Pitt", "Leonardo Di Caprio", "George Clooney"], "Number of movies": ["87", "53", "69"]}
|
||||
>>> queries = [
|
||||
... "What is the name of the first actor?",
|
||||
... "How many movies has George Clooney played in?",
|
||||
... "What is the total number of movies?",
|
||||
... ]
|
||||
>>> answer_coordinates = [[(0, 0)], [(2, 1)], [(0, 1), (1, 1), (2, 1)]]
|
||||
>>> answer_text = [["Brad Pitt"], ["69"], ["209"]]
|
||||
>>> table = pd.DataFrame.from_dict(data)
|
||||
>>> inputs = tokenizer(
|
||||
... table=table,
|
||||
... queries=queries,
|
||||
... answer_coordinates=answer_coordinates,
|
||||
... answer_text=answer_text,
|
||||
... padding="max_length",
|
||||
... return_tensors="pt",
|
||||
... )
|
||||
>>> inputs
|
||||
{'input_ids': tensor([[ ... ]]), 'attention_mask': tensor([[...]]), 'token_type_ids': tensor([[[...]]]),
|
||||
'numeric_values': tensor([[ ... ]]), 'numeric_values_scale: tensor([[ ... ]]), labels: tensor([[ ... ]])}
|
||||
```
|
||||
|
||||
Note that [`TapasTokenizer`] expects the data of the table to be **text-only**. You can use `.astype(str)` on a dataframe to turn it into text-only data.
|
||||
Of course, this only shows how to encode a single training example. It is advised to create a dataloader to iterate over batches:
|
||||
|
||||
```py
|
||||
>>> import torch
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> tsv_path = "your_path_to_the_tsv_file"
|
||||
>>> table_csv_path = "your_path_to_a_directory_containing_all_csv_files"
|
||||
|
||||
|
||||
>>> class TableDataset(torch.utils.data.Dataset):
|
||||
... def __init__(self, data, tokenizer):
|
||||
... self.data = data
|
||||
... self.tokenizer = tokenizer
|
||||
|
||||
... def __getitem__(self, idx):
|
||||
... item = data.iloc[idx]
|
||||
... table = pd.read_csv(table_csv_path + item.table_file).astype(
|
||||
... str
|
||||
... ) # be sure to make your table data text only
|
||||
... encoding = self.tokenizer(
|
||||
... table=table,
|
||||
... queries=item.question,
|
||||
... answer_coordinates=item.answer_coordinates,
|
||||
... answer_text=item.answer_text,
|
||||
... truncation=True,
|
||||
... padding="max_length",
|
||||
... return_tensors="pt",
|
||||
... )
|
||||
... # remove the batch dimension which the tokenizer adds by default
|
||||
... encoding = {key: val.squeeze(0) for key, val in encoding.items()}
|
||||
... # add the float_answer which is also required (weak supervision for aggregation case)
|
||||
... encoding["float_answer"] = torch.tensor(item.float_answer)
|
||||
... return encoding
|
||||
|
||||
... def __len__(self):
|
||||
... return len(self.data)
|
||||
|
||||
|
||||
>>> data = pd.read_csv(tsv_path, sep="\t")
|
||||
>>> train_dataset = TableDataset(data, tokenizer)
|
||||
>>> train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=32)
|
||||
```
|
||||
</pt>
|
||||
<tf>
|
||||
Third, given that you've prepared your data in this TSV/CSV format (and corresponding CSV files containing the tabular data), you can then use [`TapasTokenizer`] to convert table-question pairs into `input_ids`, `attention_mask`, `token_type_ids` and so on. Again, based on which of the three cases you picked above, [`TFTapasForQuestionAnswering`] requires different
|
||||
inputs to be fine-tuned:
|
||||
|
||||
| **Task** | **Required inputs** |
|
||||
|------------------------------------|---------------------------------------------------------------------------------------------------------------------|
|
||||
| Conversational | `input_ids`, `attention_mask`, `token_type_ids`, `labels` |
|
||||
| Weak supervision for aggregation | `input_ids`, `attention_mask`, `token_type_ids`, `labels`, `numeric_values`, `numeric_values_scale`, `float_answer` |
|
||||
| Strong supervision for aggregation | `input ids`, `attention mask`, `token type ids`, `labels`, `aggregation_labels` |
|
||||
|
||||
[`TapasTokenizer`] creates the `labels`, `numeric_values` and `numeric_values_scale` based on the `answer_coordinates` and `answer_text` columns of the TSV file. The `float_answer` and `aggregation_labels` are already in the TSV file of step 2. Here's an example:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasTokenizer
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> model_name = "google/tapas-base"
|
||||
>>> tokenizer = TapasTokenizer.from_pretrained(model_name)
|
||||
|
||||
>>> data = {"Actors": ["Brad Pitt", "Leonardo Di Caprio", "George Clooney"], "Number of movies": ["87", "53", "69"]}
|
||||
>>> queries = [
|
||||
... "What is the name of the first actor?",
|
||||
... "How many movies has George Clooney played in?",
|
||||
... "What is the total number of movies?",
|
||||
... ]
|
||||
>>> answer_coordinates = [[(0, 0)], [(2, 1)], [(0, 1), (1, 1), (2, 1)]]
|
||||
>>> answer_text = [["Brad Pitt"], ["69"], ["209"]]
|
||||
>>> table = pd.DataFrame.from_dict(data)
|
||||
>>> inputs = tokenizer(
|
||||
... table=table,
|
||||
... queries=queries,
|
||||
... answer_coordinates=answer_coordinates,
|
||||
... answer_text=answer_text,
|
||||
... padding="max_length",
|
||||
... return_tensors="tf",
|
||||
... )
|
||||
>>> inputs
|
||||
{'input_ids': tensor([[ ... ]]), 'attention_mask': tensor([[...]]), 'token_type_ids': tensor([[[...]]]),
|
||||
'numeric_values': tensor([[ ... ]]), 'numeric_values_scale: tensor([[ ... ]]), labels: tensor([[ ... ]])}
|
||||
```
|
||||
|
||||
Note that [`TapasTokenizer`] expects the data of the table to be **text-only**. You can use `.astype(str)` on a dataframe to turn it into text-only data.
|
||||
Of course, this only shows how to encode a single training example. It is advised to create a dataloader to iterate over batches:
|
||||
|
||||
```py
|
||||
>>> import tensorflow as tf
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> tsv_path = "your_path_to_the_tsv_file"
|
||||
>>> table_csv_path = "your_path_to_a_directory_containing_all_csv_files"
|
||||
|
||||
|
||||
>>> class TableDataset:
|
||||
... def __init__(self, data, tokenizer):
|
||||
... self.data = data
|
||||
... self.tokenizer = tokenizer
|
||||
|
||||
... def __iter__(self):
|
||||
... for idx in range(self.__len__()):
|
||||
... item = self.data.iloc[idx]
|
||||
... table = pd.read_csv(table_csv_path + item.table_file).astype(
|
||||
... str
|
||||
... ) # be sure to make your table data text only
|
||||
... encoding = self.tokenizer(
|
||||
... table=table,
|
||||
... queries=item.question,
|
||||
... answer_coordinates=item.answer_coordinates,
|
||||
... answer_text=item.answer_text,
|
||||
... truncation=True,
|
||||
... padding="max_length",
|
||||
... return_tensors="tf",
|
||||
... )
|
||||
... # remove the batch dimension which the tokenizer adds by default
|
||||
... encoding = {key: tf.squeeze(val, 0) for key, val in encoding.items()}
|
||||
... # add the float_answer which is also required (weak supervision for aggregation case)
|
||||
... encoding["float_answer"] = tf.convert_to_tensor(item.float_answer, dtype=tf.float32)
|
||||
... yield encoding["input_ids"], encoding["attention_mask"], encoding["numeric_values"], encoding[
|
||||
... "numeric_values_scale"
|
||||
... ], encoding["token_type_ids"], encoding["labels"], encoding["float_answer"]
|
||||
|
||||
... def __len__(self):
|
||||
... return len(self.data)
|
||||
|
||||
|
||||
>>> data = pd.read_csv(tsv_path, sep="\t")
|
||||
>>> train_dataset = TableDataset(data, tokenizer)
|
||||
>>> output_signature = (
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.int32),
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.int32),
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.float32),
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.float32),
|
||||
... tf.TensorSpec(shape=(512, 7), dtype=tf.int32),
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.int32),
|
||||
... tf.TensorSpec(shape=(512,), dtype=tf.float32),
|
||||
... )
|
||||
>>> train_dataloader = tf.data.Dataset.from_generator(train_dataset, output_signature=output_signature).batch(32)
|
||||
```
|
||||
</tf>
|
||||
</frameworkcontent>
|
||||
|
||||
Note that here, we encode each table-question pair independently. This is fine as long as your dataset is **not conversational**. In case your dataset involves conversational questions (such as in SQA), then you should first group together the `queries`, `answer_coordinates` and `answer_text` per table (in the order of their `position`
|
||||
index) and batch encode each table with its questions. This will make sure that the `prev_labels` token types (see docs of [`TapasTokenizer`]) are set correctly. See [this notebook](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) for more info. See [this notebook](https://github.com/kamalkraj/Tapas-Tutorial/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) for more info regarding using the TensorFlow model.
|
||||
|
||||
**STEP 4: Train (fine-tune) the model
|
||||
|
||||
<frameworkcontent>
|
||||
<pt>
|
||||
You can then fine-tune [`TapasForQuestionAnswering`] as follows (shown here for the weak supervision for aggregation case):
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasConfig, TapasForQuestionAnswering, AdamW
|
||||
|
||||
>>> # this is the default WTQ configuration
|
||||
>>> config = TapasConfig(
|
||||
... num_aggregation_labels=4,
|
||||
... use_answer_as_supervision=True,
|
||||
... answer_loss_cutoff=0.664694,
|
||||
... cell_selection_preference=0.207951,
|
||||
... huber_loss_delta=0.121194,
|
||||
... init_cell_selection_weights_to_zero=True,
|
||||
... select_one_column=True,
|
||||
... allow_empty_column_selection=False,
|
||||
... temperature=0.0352513,
|
||||
... )
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
|
||||
>>> optimizer = AdamW(model.parameters(), lr=5e-5)
|
||||
|
||||
>>> model.train()
|
||||
>>> for epoch in range(2): # loop over the dataset multiple times
|
||||
... for batch in train_dataloader:
|
||||
... # get the inputs;
|
||||
... input_ids = batch["input_ids"]
|
||||
... attention_mask = batch["attention_mask"]
|
||||
... token_type_ids = batch["token_type_ids"]
|
||||
... labels = batch["labels"]
|
||||
... numeric_values = batch["numeric_values"]
|
||||
... numeric_values_scale = batch["numeric_values_scale"]
|
||||
... float_answer = batch["float_answer"]
|
||||
|
||||
... # zero the parameter gradients
|
||||
... optimizer.zero_grad()
|
||||
|
||||
... # forward + backward + optimize
|
||||
... outputs = model(
|
||||
... input_ids=input_ids,
|
||||
... attention_mask=attention_mask,
|
||||
... token_type_ids=token_type_ids,
|
||||
... labels=labels,
|
||||
... numeric_values=numeric_values,
|
||||
... numeric_values_scale=numeric_values_scale,
|
||||
... float_answer=float_answer,
|
||||
... )
|
||||
... loss = outputs.loss
|
||||
... loss.backward()
|
||||
... optimizer.step()
|
||||
```
|
||||
</pt>
|
||||
<tf>
|
||||
You can then fine-tune [`TFTapasForQuestionAnswering`] as follows (shown here for the weak supervision for aggregation case):
|
||||
|
||||
```py
|
||||
>>> import tensorflow as tf
|
||||
>>> from transformers import TapasConfig, TFTapasForQuestionAnswering
|
||||
|
||||
>>> # this is the default WTQ configuration
|
||||
>>> config = TapasConfig(
|
||||
... num_aggregation_labels=4,
|
||||
... use_answer_as_supervision=True,
|
||||
... answer_loss_cutoff=0.664694,
|
||||
... cell_selection_preference=0.207951,
|
||||
... huber_loss_delta=0.121194,
|
||||
... init_cell_selection_weights_to_zero=True,
|
||||
... select_one_column=True,
|
||||
... allow_empty_column_selection=False,
|
||||
... temperature=0.0352513,
|
||||
... )
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained("google/tapas-base", config=config)
|
||||
|
||||
>>> optimizer = tf.keras.optimizers.Adam(learning_rate=5e-5)
|
||||
|
||||
>>> for epoch in range(2): # loop over the dataset multiple times
|
||||
... for batch in train_dataloader:
|
||||
... # get the inputs;
|
||||
... input_ids = batch[0]
|
||||
... attention_mask = batch[1]
|
||||
... token_type_ids = batch[4]
|
||||
... labels = batch[-1]
|
||||
... numeric_values = batch[2]
|
||||
... numeric_values_scale = batch[3]
|
||||
... float_answer = batch[6]
|
||||
|
||||
... # forward + backward + optimize
|
||||
... with tf.GradientTape() as tape:
|
||||
... outputs = model(
|
||||
... input_ids=input_ids,
|
||||
... attention_mask=attention_mask,
|
||||
... token_type_ids=token_type_ids,
|
||||
... labels=labels,
|
||||
... numeric_values=numeric_values,
|
||||
... numeric_values_scale=numeric_values_scale,
|
||||
... float_answer=float_answer,
|
||||
... )
|
||||
... grads = tape.gradient(outputs.loss, model.trainable_weights)
|
||||
... optimizer.apply_gradients(zip(grads, model.trainable_weights))
|
||||
```
|
||||
</tf>
|
||||
</frameworkcontent>
|
||||
|
||||
## Usage: inference
|
||||
|
||||
<frameworkcontent>
|
||||
<pt>
|
||||
Here we explain how you can use [`TapasForQuestionAnswering`] or [`TFTapasForQuestionAnswering`] for inference (i.e. making predictions on new data). For inference, only `input_ids`, `attention_mask` and `token_type_ids` (which you can obtain using [`TapasTokenizer`]) have to be provided to the model to obtain the logits. Next, you can use the handy [`~models.tapas.tokenization_tapas.convert_logits_to_predictions`] method to convert these into predicted coordinates and optional aggregation indices.
|
||||
|
||||
However, note that inference is **different** depending on whether or not the setup is conversational. In a non-conversational set-up, inference can be done in parallel on all table-question pairs of a batch. Here's an example of that:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasTokenizer, TapasForQuestionAnswering
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> model_name = "google/tapas-base-finetuned-wtq"
|
||||
>>> model = TapasForQuestionAnswering.from_pretrained(model_name)
|
||||
>>> tokenizer = TapasTokenizer.from_pretrained(model_name)
|
||||
|
||||
>>> data = {"Actors": ["Brad Pitt", "Leonardo Di Caprio", "George Clooney"], "Number of movies": ["87", "53", "69"]}
|
||||
>>> queries = [
|
||||
... "What is the name of the first actor?",
|
||||
... "How many movies has George Clooney played in?",
|
||||
... "What is the total number of movies?",
|
||||
... ]
|
||||
>>> table = pd.DataFrame.from_dict(data)
|
||||
>>> inputs = tokenizer(table=table, queries=queries, padding="max_length", return_tensors="pt")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(
|
||||
... inputs, outputs.logits.detach(), outputs.logits_aggregation.detach()
|
||||
... )
|
||||
|
||||
>>> # let's print out the results:
|
||||
>>> id2aggregation = {0: "NONE", 1: "SUM", 2: "AVERAGE", 3: "COUNT"}
|
||||
>>> aggregation_predictions_string = [id2aggregation[x] for x in predicted_aggregation_indices]
|
||||
|
||||
>>> answers = []
|
||||
>>> for coordinates in predicted_answer_coordinates:
|
||||
... if len(coordinates) == 1:
|
||||
... # only a single cell:
|
||||
... answers.append(table.iat[coordinates[0]])
|
||||
... else:
|
||||
... # multiple cells
|
||||
... cell_values = []
|
||||
... for coordinate in coordinates:
|
||||
... cell_values.append(table.iat[coordinate])
|
||||
... answers.append(", ".join(cell_values))
|
||||
|
||||
>>> display(table)
|
||||
>>> print("")
|
||||
>>> for query, answer, predicted_agg in zip(queries, answers, aggregation_predictions_string):
|
||||
... print(query)
|
||||
... if predicted_agg == "NONE":
|
||||
... print("Predicted answer: " + answer)
|
||||
... else:
|
||||
... print("Predicted answer: " + predicted_agg + " > " + answer)
|
||||
What is the name of the first actor?
|
||||
Predicted answer: Brad Pitt
|
||||
How many movies has George Clooney played in?
|
||||
Predicted answer: COUNT > 69
|
||||
What is the total number of movies?
|
||||
Predicted answer: SUM > 87, 53, 69
|
||||
```
|
||||
</pt>
|
||||
<tf>
|
||||
Here we explain how you can use [`TFTapasForQuestionAnswering`] for inference (i.e. making predictions on new data). For inference, only `input_ids`, `attention_mask` and `token_type_ids` (which you can obtain using [`TapasTokenizer`]) have to be provided to the model to obtain the logits. Next, you can use the handy [`~models.tapas.tokenization_tapas.convert_logits_to_predictions`] method to convert these into predicted coordinates and optional aggregation indices.
|
||||
|
||||
However, note that inference is **different** depending on whether or not the setup is conversational. In a non-conversational set-up, inference can be done in parallel on all table-question pairs of a batch. Here's an example of that:
|
||||
|
||||
```py
|
||||
>>> from transformers import TapasTokenizer, TFTapasForQuestionAnswering
|
||||
>>> import pandas as pd
|
||||
|
||||
>>> model_name = "google/tapas-base-finetuned-wtq"
|
||||
>>> model = TFTapasForQuestionAnswering.from_pretrained(model_name)
|
||||
>>> tokenizer = TapasTokenizer.from_pretrained(model_name)
|
||||
|
||||
>>> data = {"Actors": ["Brad Pitt", "Leonardo Di Caprio", "George Clooney"], "Number of movies": ["87", "53", "69"]}
|
||||
>>> queries = [
|
||||
... "What is the name of the first actor?",
|
||||
... "How many movies has George Clooney played in?",
|
||||
... "What is the total number of movies?",
|
||||
... ]
|
||||
>>> table = pd.DataFrame.from_dict(data)
|
||||
>>> inputs = tokenizer(table=table, queries=queries, padding="max_length", return_tensors="tf")
|
||||
>>> outputs = model(**inputs)
|
||||
>>> predicted_answer_coordinates, predicted_aggregation_indices = tokenizer.convert_logits_to_predictions(
|
||||
... inputs, outputs.logits, outputs.logits_aggregation
|
||||
... )
|
||||
|
||||
>>> # let's print out the results:
|
||||
>>> id2aggregation = {0: "NONE", 1: "SUM", 2: "AVERAGE", 3: "COUNT"}
|
||||
>>> aggregation_predictions_string = [id2aggregation[x] for x in predicted_aggregation_indices]
|
||||
|
||||
>>> answers = []
|
||||
>>> for coordinates in predicted_answer_coordinates:
|
||||
... if len(coordinates) == 1:
|
||||
... # only a single cell:
|
||||
... answers.append(table.iat[coordinates[0]])
|
||||
... else:
|
||||
... # multiple cells
|
||||
... cell_values = []
|
||||
... for coordinate in coordinates:
|
||||
... cell_values.append(table.iat[coordinate])
|
||||
... answers.append(", ".join(cell_values))
|
||||
|
||||
>>> display(table)
|
||||
>>> print("")
|
||||
>>> for query, answer, predicted_agg in zip(queries, answers, aggregation_predictions_string):
|
||||
... print(query)
|
||||
... if predicted_agg == "NONE":
|
||||
... print("Predicted answer: " + answer)
|
||||
... else:
|
||||
... print("Predicted answer: " + predicted_agg + " > " + answer)
|
||||
What is the name of the first actor?
|
||||
Predicted answer: Brad Pitt
|
||||
How many movies has George Clooney played in?
|
||||
Predicted answer: COUNT > 69
|
||||
What is the total number of movies?
|
||||
Predicted answer: SUM > 87, 53, 69
|
||||
```
|
||||
</tf>
|
||||
</frameworkcontent>
|
||||
|
||||
In case of a conversational set-up, then each table-question pair must be provided **sequentially** to the model, such that the `prev_labels` token types can be overwritten by the predicted `labels` of the previous table-question pair. Again, more info can be found in [this notebook](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) (for PyTorch) and [this notebook](https://github.com/kamalkraj/Tapas-Tutorial/blob/master/TAPAS/Fine_tuning_TapasForQuestionAnswering_on_SQA.ipynb) (for TensorFlow).
|
||||
|
||||
## TAPAS specific outputs
|
||||
[[autodoc]] models.tapas.modeling_tapas.TableQuestionAnsweringOutput
|
||||
|
||||
## TapasConfig
|
||||
[[autodoc]] TapasConfig
|
||||
|
||||
## TapasTokenizer
|
||||
[[autodoc]] TapasTokenizer
|
||||
- __call__
|
||||
- convert_logits_to_predictions
|
||||
- save_vocabulary
|
||||
|
||||
## TapasModel
|
||||
[[autodoc]] TapasModel
|
||||
- forward
|
||||
|
||||
## TapasForMaskedLM
|
||||
[[autodoc]] TapasForMaskedLM
|
||||
- forward
|
||||
|
||||
## TapasForSequenceClassification
|
||||
[[autodoc]] TapasForSequenceClassification
|
||||
- forward
|
||||
|
||||
## TapasForQuestionAnswering
|
||||
[[autodoc]] TapasForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## TFTapasModel
|
||||
[[autodoc]] TFTapasModel
|
||||
- call
|
||||
|
||||
## TFTapasForMaskedLM
|
||||
[[autodoc]] TFTapasForMaskedLM
|
||||
- call
|
||||
|
||||
## TFTapasForSequenceClassification
|
||||
[[autodoc]] TFTapasForSequenceClassification
|
||||
- call
|
||||
|
||||
## TFTapasForQuestionAnswering
|
||||
[[autodoc]] TFTapasForQuestionAnswering
|
||||
- call
|
||||
103
docs/source/en/model_doc/transfo-xl.mdx
Normal file
103
docs/source/en/model_doc/transfo-xl.mdx
Normal file
@@ -0,0 +1,103 @@
|
||||
<!--Copyright 2020 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Transformer XL
|
||||
|
||||
## Overview
|
||||
|
||||
The Transformer-XL model was proposed in [Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context](https://arxiv.org/abs/1901.02860) by Zihang Dai, Zhilin Yang, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan
|
||||
Salakhutdinov. It's a causal (uni-directional) transformer with relative positioning (sinusoïdal) embeddings which can
|
||||
reuse previously computed hidden-states to attend to longer context (memory). This model also uses adaptive softmax
|
||||
inputs and outputs (tied).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Transformers have a potential of learning longer-term dependency, but are limited by a fixed-length context in the
|
||||
setting of language modeling. We propose a novel neural architecture Transformer-XL that enables learning dependency
|
||||
beyond a fixed length without disrupting temporal coherence. It consists of a segment-level recurrence mechanism and a
|
||||
novel positional encoding scheme. Our method not only enables capturing longer-term dependency, but also resolves the
|
||||
context fragmentation problem. As a result, Transformer-XL learns dependency that is 80% longer than RNNs and 450%
|
||||
longer than vanilla Transformers, achieves better performance on both short and long sequences, and is up to 1,800+
|
||||
times faster than vanilla Transformers during evaluation. Notably, we improve the state-of-the-art results of
|
||||
bpc/perplexity to 0.99 on enwiki8, 1.08 on text8, 18.3 on WikiText-103, 21.8 on One Billion Word, and 54.5 on Penn
|
||||
Treebank (without finetuning). When trained only on WikiText-103, Transformer-XL manages to generate reasonably
|
||||
coherent, novel text articles with thousands of tokens.*
|
||||
|
||||
Tips:
|
||||
|
||||
- Transformer-XL uses relative sinusoidal positional embeddings. Padding can be done on the left or on the right. The
|
||||
original implementation trains on SQuAD with padding on the left, therefore the padding defaults are set to left.
|
||||
- Transformer-XL is one of the few models that has no sequence length limit.
|
||||
|
||||
This model was contributed by [thomwolf](https://huggingface.co/thomwolf). The original code can be found [here](https://github.com/kimiyoung/transformer-xl).
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
TransformerXL does **not** work with *torch.nn.DataParallel* due to a bug in PyTorch, see [issue #36035](https://github.com/pytorch/pytorch/issues/36035)
|
||||
|
||||
</Tip>
|
||||
|
||||
|
||||
## TransfoXLConfig
|
||||
|
||||
[[autodoc]] TransfoXLConfig
|
||||
|
||||
## TransfoXLTokenizer
|
||||
|
||||
[[autodoc]] TransfoXLTokenizer
|
||||
- save_vocabulary
|
||||
|
||||
## TransfoXL specific outputs
|
||||
|
||||
[[autodoc]] models.transfo_xl.modeling_transfo_xl.TransfoXLModelOutput
|
||||
|
||||
[[autodoc]] models.transfo_xl.modeling_transfo_xl.TransfoXLLMHeadModelOutput
|
||||
|
||||
[[autodoc]] models.transfo_xl.modeling_tf_transfo_xl.TFTransfoXLModelOutput
|
||||
|
||||
[[autodoc]] models.transfo_xl.modeling_tf_transfo_xl.TFTransfoXLLMHeadModelOutput
|
||||
|
||||
## TransfoXLModel
|
||||
|
||||
[[autodoc]] TransfoXLModel
|
||||
- forward
|
||||
|
||||
## TransfoXLLMHeadModel
|
||||
|
||||
[[autodoc]] TransfoXLLMHeadModel
|
||||
- forward
|
||||
|
||||
## TransfoXLForSequenceClassification
|
||||
|
||||
[[autodoc]] TransfoXLForSequenceClassification
|
||||
- forward
|
||||
|
||||
## TFTransfoXLModel
|
||||
|
||||
[[autodoc]] TFTransfoXLModel
|
||||
- call
|
||||
|
||||
## TFTransfoXLLMHeadModel
|
||||
|
||||
[[autodoc]] TFTransfoXLLMHeadModel
|
||||
- call
|
||||
|
||||
## TFTransfoXLForSequenceClassification
|
||||
|
||||
[[autodoc]] TFTransfoXLForSequenceClassification
|
||||
- call
|
||||
|
||||
## Internal Layers
|
||||
|
||||
[[autodoc]] AdaptiveEmbedding
|
||||
|
||||
[[autodoc]] TFAdaptiveEmbedding
|
||||
102
docs/source/en/model_doc/trocr.mdx
Normal file
102
docs/source/en/model_doc/trocr.mdx
Normal file
@@ -0,0 +1,102 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
|
||||
License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License. -->
|
||||
|
||||
# TrOCR
|
||||
|
||||
## Overview
|
||||
|
||||
The TrOCR model was proposed in [TrOCR: Transformer-based Optical Character Recognition with Pre-trained
|
||||
Models](https://arxiv.org/abs/2109.10282) by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang,
|
||||
Zhoujun Li, Furu Wei. TrOCR consists of an image Transformer encoder and an autoregressive text Transformer decoder to
|
||||
perform [optical character recognition (OCR)](https://en.wikipedia.org/wiki/Optical_character_recognition).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Text recognition is a long-standing research problem for document digitalization. Existing approaches for text recognition
|
||||
are usually built based on CNN for image understanding and RNN for char-level text generation. In addition, another language
|
||||
model is usually needed to improve the overall accuracy as a post-processing step. In this paper, we propose an end-to-end
|
||||
text recognition approach with pre-trained image Transformer and text Transformer models, namely TrOCR, which leverages the
|
||||
Transformer architecture for both image understanding and wordpiece-level text generation. The TrOCR model is simple but
|
||||
effective, and can be pre-trained with large-scale synthetic data and fine-tuned with human-labeled datasets. Experiments
|
||||
show that the TrOCR model outperforms the current state-of-the-art models on both printed and handwritten text recognition
|
||||
tasks.*
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/trocr_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> TrOCR architecture. Taken from the <a href="https://arxiv.org/abs/2109.10282">original paper</a>. </small>
|
||||
|
||||
Please refer to the [`VisionEncoderDecoder`] class on how to use this model.
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found
|
||||
[here](https://github.com/microsoft/unilm/tree/6f60612e7cc86a2a1ae85c47231507a587ab4e01/trocr).
|
||||
|
||||
Tips:
|
||||
|
||||
- The quickest way to get started with TrOCR is by checking the [tutorial
|
||||
notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/TrOCR), which show how to use the model
|
||||
at inference time as well as fine-tuning on custom data.
|
||||
- TrOCR is pre-trained in 2 stages before being fine-tuned on downstream datasets. It achieves state-of-the-art results
|
||||
on both printed (e.g. the [SROIE dataset](https://paperswithcode.com/dataset/sroie) and handwritten (e.g. the [IAM
|
||||
Handwriting dataset](https://fki.tic.heia-fr.ch/databases/iam-handwriting-database>) text recognition tasks. For more
|
||||
information, see the [official models](https://huggingface.co/models?other=trocr>).
|
||||
- TrOCR is always used within the [VisionEncoderDecoder](vision-encoder-decoder) framework.
|
||||
|
||||
## Inference
|
||||
|
||||
TrOCR's [`VisionEncoderDecoder`] model accepts images as input and makes use of
|
||||
[`~generation_utils.GenerationMixin.generate`] to autoregressively generate text given the input image.
|
||||
|
||||
The [`ViTFeatureExtractor`/`DeiTFeatureExtractor`] class is responsible for preprocessing the input image and
|
||||
[`RobertaTokenizer`/`XLMRobertaTokenizer`] decodes the generated target tokens to the target string. The
|
||||
[`TrOCRProcessor`] wraps [`ViTFeatureExtractor`/`DeiTFeatureExtractor`] and [`RobertaTokenizer`/`XLMRobertaTokenizer`]
|
||||
into a single instance to both extract the input features and decode the predicted token ids.
|
||||
|
||||
- Step-by-step Optical Character Recognition (OCR)
|
||||
|
||||
``` py
|
||||
>>> from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
||||
>>> import requests
|
||||
>>> from PIL import Image
|
||||
|
||||
>>> processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
||||
>>> model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
||||
|
||||
>>> # load image from the IAM dataset
|
||||
>>> url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"
|
||||
>>> image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
||||
|
||||
>>> pixel_values = processor(image, return_tensors="pt").pixel_values
|
||||
>>> generated_ids = model.generate(pixel_values)
|
||||
|
||||
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
||||
```
|
||||
|
||||
See the [model hub](https://huggingface.co/models?filter=trocr) to look for TrOCR checkpoints.
|
||||
|
||||
## TrOCRConfig
|
||||
|
||||
[[autodoc]] TrOCRConfig
|
||||
|
||||
## TrOCRProcessor
|
||||
|
||||
[[autodoc]] TrOCRProcessor
|
||||
- __call__
|
||||
- from_pretrained
|
||||
- save_pretrained
|
||||
- batch_decode
|
||||
- decode
|
||||
- as_target_processor
|
||||
|
||||
## TrOCRForCausalLM
|
||||
|
||||
[[autodoc]] TrOCRForCausalLM
|
||||
- forward
|
||||
86
docs/source/en/model_doc/unispeech-sat.mdx
Normal file
86
docs/source/en/model_doc/unispeech-sat.mdx
Normal file
@@ -0,0 +1,86 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# UniSpeech-SAT
|
||||
|
||||
## Overview
|
||||
|
||||
The UniSpeech-SAT model was proposed in [UniSpeech-SAT: Universal Speech Representation Learning with Speaker Aware
|
||||
Pre-Training](https://arxiv.org/abs/2110.05752) by Sanyuan Chen, Yu Wu, Chengyi Wang, Zhengyang Chen, Zhuo Chen,
|
||||
Shujie Liu, Jian Wu, Yao Qian, Furu Wei, Jinyu Li, Xiangzhan Yu .
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Self-supervised learning (SSL) is a long-standing goal for speech processing, since it utilizes large-scale unlabeled
|
||||
data and avoids extensive human labeling. Recent years witness great successes in applying self-supervised learning in
|
||||
speech recognition, while limited exploration was attempted in applying SSL for modeling speaker characteristics. In
|
||||
this paper, we aim to improve the existing SSL framework for speaker representation learning. Two methods are
|
||||
introduced for enhancing the unsupervised speaker information extraction. First, we apply the multi-task learning to
|
||||
the current SSL framework, where we integrate the utterance-wise contrastive loss with the SSL objective function.
|
||||
Second, for better speaker discrimination, we propose an utterance mixing strategy for data augmentation, where
|
||||
additional overlapped utterances are created unsupervisely and incorporate during training. We integrate the proposed
|
||||
methods into the HuBERT framework. Experiment results on SUPERB benchmark show that the proposed system achieves
|
||||
state-of-the-art performance in universal representation learning, especially for speaker identification oriented
|
||||
tasks. An ablation study is performed verifying the efficacy of each proposed method. Finally, we scale up training
|
||||
dataset to 94 thousand hours public audio data and achieve further performance improvement in all SUPERB tasks.*
|
||||
|
||||
Tips:
|
||||
|
||||
- UniSpeechSat is a speech model that accepts a float array corresponding to the raw waveform of the speech signal.
|
||||
Please use [`Wav2Vec2Processor`] for the feature extraction.
|
||||
- UniSpeechSat model can be fine-tuned using connectionist temporal classification (CTC) so the model output has to be
|
||||
decoded using [`Wav2Vec2CTCTokenizer`].
|
||||
- UniSpeechSat performs especially well on speaker verification, speaker identification, and speaker diarization tasks.
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The Authors' code can be
|
||||
found [here](https://github.com/microsoft/UniSpeech/tree/main/UniSpeech-SAT).
|
||||
|
||||
|
||||
## UniSpeechSatConfig
|
||||
|
||||
[[autodoc]] UniSpeechSatConfig
|
||||
|
||||
## UniSpeechSat specific outputs
|
||||
|
||||
[[autodoc]] models.unispeech_sat.modeling_unispeech_sat.UniSpeechSatBaseModelOutput
|
||||
|
||||
[[autodoc]] models.unispeech_sat.modeling_unispeech_sat.UniSpeechSatForPreTrainingOutput
|
||||
|
||||
## UniSpeechSatModel
|
||||
|
||||
[[autodoc]] UniSpeechSatModel
|
||||
- forward
|
||||
|
||||
## UniSpeechSatForCTC
|
||||
|
||||
[[autodoc]] UniSpeechSatForCTC
|
||||
- forward
|
||||
|
||||
## UniSpeechSatForSequenceClassification
|
||||
|
||||
[[autodoc]] UniSpeechSatForSequenceClassification
|
||||
- forward
|
||||
|
||||
## UniSpeechSatForAudioFrameClassification
|
||||
|
||||
[[autodoc]] UniSpeechSatForAudioFrameClassification
|
||||
- forward
|
||||
|
||||
## UniSpeechSatForXVector
|
||||
|
||||
[[autodoc]] UniSpeechSatForXVector
|
||||
- forward
|
||||
|
||||
## UniSpeechSatForPreTraining
|
||||
|
||||
[[autodoc]] UniSpeechSatForPreTraining
|
||||
- forward
|
||||
71
docs/source/en/model_doc/unispeech.mdx
Normal file
71
docs/source/en/model_doc/unispeech.mdx
Normal file
@@ -0,0 +1,71 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# UniSpeech
|
||||
|
||||
## Overview
|
||||
|
||||
The UniSpeech model was proposed in [UniSpeech: Unified Speech Representation Learning with Labeled and Unlabeled Data](https://arxiv.org/abs/2101.07597) by Chengyi Wang, Yu Wu, Yao Qian, Kenichi Kumatani, Shujie Liu, Furu Wei, Michael
|
||||
Zeng, Xuedong Huang .
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*In this paper, we propose a unified pre-training approach called UniSpeech to learn speech representations with both
|
||||
unlabeled and labeled data, in which supervised phonetic CTC learning and phonetically-aware contrastive
|
||||
self-supervised learning are conducted in a multi-task learning manner. The resultant representations can capture
|
||||
information more correlated with phonetic structures and improve the generalization across languages and domains. We
|
||||
evaluate the effectiveness of UniSpeech for cross-lingual representation learning on public CommonVoice corpus. The
|
||||
results show that UniSpeech outperforms self-supervised pretraining and supervised transfer learning for speech
|
||||
recognition by a maximum of 13.4% and 17.8% relative phone error rate reductions respectively (averaged over all
|
||||
testing languages). The transferability of UniSpeech is also demonstrated on a domain-shift speech recognition task,
|
||||
i.e., a relative word error rate reduction of 6% against the previous approach.*
|
||||
|
||||
Tips:
|
||||
|
||||
- UniSpeech is a speech model that accepts a float array corresponding to the raw waveform of the speech signal. Please
|
||||
use [`Wav2Vec2Processor`] for the feature extraction.
|
||||
- UniSpeech model can be fine-tuned using connectionist temporal classification (CTC) so the model output has to be
|
||||
decoded using [`Wav2Vec2CTCTokenizer`].
|
||||
|
||||
This model was contributed by [patrickvonplaten](https://huggingface.co/patrickvonplaten). The Authors' code can be
|
||||
found [here](https://github.com/microsoft/UniSpeech/tree/main/UniSpeech).
|
||||
|
||||
|
||||
## UniSpeechConfig
|
||||
|
||||
[[autodoc]] UniSpeechConfig
|
||||
|
||||
## UniSpeech specific outputs
|
||||
|
||||
[[autodoc]] models.unispeech.modeling_unispeech.UniSpeechBaseModelOutput
|
||||
|
||||
[[autodoc]] models.unispeech.modeling_unispeech.UniSpeechForPreTrainingOutput
|
||||
|
||||
## UniSpeechModel
|
||||
|
||||
[[autodoc]] UniSpeechModel
|
||||
- forward
|
||||
|
||||
## UniSpeechForCTC
|
||||
|
||||
[[autodoc]] UniSpeechForCTC
|
||||
- forward
|
||||
|
||||
## UniSpeechForSequenceClassification
|
||||
|
||||
[[autodoc]] UniSpeechForSequenceClassification
|
||||
- forward
|
||||
|
||||
## UniSpeechForPreTraining
|
||||
|
||||
[[autodoc]] UniSpeechForPreTraining
|
||||
- forward
|
||||
51
docs/source/en/model_doc/van.mdx
Normal file
51
docs/source/en/model_doc/van.mdx
Normal file
@@ -0,0 +1,51 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# VAN
|
||||
|
||||
## Overview
|
||||
|
||||
The VAN model was proposed in [Visual Attention Network](https://arxiv.org/abs/2202.09741) by Meng-Hao Guo, Cheng-Ze Lu, Zheng-Ning Liu, Ming-Ming Cheng, Shi-Min Hu.
|
||||
|
||||
This paper introduces a new attention layer based on convolution operations able to capture both local and distant relationships. This is done by combining normal and large kernel convolution layers. The latter uses a dilated convolution to capture distant correlations.
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*While originally designed for natural language processing tasks, the self-attention mechanism has recently taken various computer vision areas by storm. However, the 2D nature of images brings three challenges for applying self-attention in computer vision. (1) Treating images as 1D sequences neglects their 2D structures. (2) The quadratic complexity is too expensive for high-resolution images. (3) It only captures spatial adaptability but ignores channel adaptability. In this paper, we propose a novel large kernel attention (LKA) module to enable self-adaptive and long-range correlations in self-attention while avoiding the above issues. We further introduce a novel neural network based on LKA, namely Visual Attention Network (VAN). While extremely simple, VAN outperforms the state-of-the-art vision transformers and convolutional neural networks with a large margin in extensive experiments, including image classification, object detection, semantic segmentation, instance segmentation, etc. Code is available at [this https URL](https://github.com/Visual-Attention-Network/VAN-Classification).*
|
||||
|
||||
Tips:
|
||||
|
||||
- VAN does not have an embedding layer, thus the `hidden_states` will have a length equal to the number of stages.
|
||||
|
||||
The figure below illustrates the architecture of a Visual Aattention Layer. Taken from the [original paper](https://arxiv.org/abs/2202.09741).
|
||||
|
||||
<img width="600" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/van_architecture.png"/>
|
||||
|
||||
This model was contributed by [Francesco](https://huggingface.co/Francesco). The original code can be found [here](https://github.com/Visual-Attention-Network/VAN-Classification).
|
||||
|
||||
|
||||
## VanConfig
|
||||
|
||||
[[autodoc]] VanConfig
|
||||
|
||||
|
||||
## VanModel
|
||||
|
||||
[[autodoc]] VanModel
|
||||
- forward
|
||||
|
||||
|
||||
## VanForImageClassification
|
||||
|
||||
[[autodoc]] VanForImageClassification
|
||||
- forward
|
||||
|
||||
89
docs/source/en/model_doc/vilt.mdx
Normal file
89
docs/source/en/model_doc/vilt.mdx
Normal file
@@ -0,0 +1,89 @@
|
||||
<!--Copyright 2021 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# ViLT
|
||||
|
||||
## Overview
|
||||
|
||||
The ViLT model was proposed in [ViLT: Vision-and-Language Transformer Without Convolution or Region Supervision](https://arxiv.org/abs/2102.03334)
|
||||
by Wonjae Kim, Bokyung Son, Ildoo Kim. ViLT incorporates text embeddings into a Vision Transformer (ViT), allowing it to have a minimal design
|
||||
for Vision-and-Language Pre-training (VLP).
|
||||
|
||||
The abstract from the paper is the following:
|
||||
|
||||
*Vision-and-Language Pre-training (VLP) has improved performance on various joint vision-and-language downstream tasks.
|
||||
Current approaches to VLP heavily rely on image feature extraction processes, most of which involve region supervision
|
||||
(e.g., object detection) and the convolutional architecture (e.g., ResNet). Although disregarded in the literature, we
|
||||
find it problematic in terms of both (1) efficiency/speed, that simply extracting input features requires much more
|
||||
computation than the multimodal interaction steps; and (2) expressive power, as it is upper bounded to the expressive
|
||||
power of the visual embedder and its predefined visual vocabulary. In this paper, we present a minimal VLP model,
|
||||
Vision-and-Language Transformer (ViLT), monolithic in the sense that the processing of visual inputs is drastically
|
||||
simplified to just the same convolution-free manner that we process textual inputs. We show that ViLT is up to tens of
|
||||
times faster than previous VLP models, yet with competitive or better downstream task performance.*
|
||||
|
||||
Tips:
|
||||
|
||||
- The quickest way to get started with ViLT is by checking the [example notebooks](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/ViLT)
|
||||
(which showcase both inference and fine-tuning on custom data).
|
||||
- ViLT is a model that takes both `pixel_values` and `input_ids` as input. One can use [`ViltProcessor`] to prepare data for the model.
|
||||
This processor wraps a feature extractor (for the image modality) and a tokenizer (for the language modality) into one.
|
||||
- ViLT is trained with images of various sizes: the authors resize the shorter edge of input images to 384 and limit the longer edge to
|
||||
under 640 while preserving the aspect ratio. To make batching of images possible, the authors use a `pixel_mask` that indicates
|
||||
which pixel values are real and which are padding. [`ViltProcessor`] automatically creates this for you.
|
||||
- The design of ViLT is very similar to that of a standard Vision Transformer (ViT). The only difference is that the model includes
|
||||
additional embedding layers for the language modality.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vilt_architecture.jpg"
|
||||
alt="drawing" width="600"/>
|
||||
|
||||
<small> ViLT architecture. Taken from the <a href="https://arxiv.org/abs/2102.03334">original paper</a>. </small>
|
||||
|
||||
This model was contributed by [nielsr](https://huggingface.co/nielsr). The original code can be found [here](https://github.com/dandelin/ViLT).
|
||||
|
||||
## ViltConfig
|
||||
|
||||
[[autodoc]] ViltConfig
|
||||
|
||||
## ViltFeatureExtractor
|
||||
|
||||
[[autodoc]] ViltFeatureExtractor
|
||||
- __call__
|
||||
|
||||
## ViltProcessor
|
||||
|
||||
[[autodoc]] ViltProcessor
|
||||
- __call__
|
||||
|
||||
## ViltModel
|
||||
|
||||
[[autodoc]] ViltModel
|
||||
- forward
|
||||
|
||||
## ViltForMaskedLM
|
||||
|
||||
[[autodoc]] ViltForMaskedLM
|
||||
- forward
|
||||
|
||||
## ViltForQuestionAnswering
|
||||
|
||||
[[autodoc]] ViltForQuestionAnswering
|
||||
- forward
|
||||
|
||||
## ViltForImagesAndTextClassification
|
||||
|
||||
[[autodoc]] ViltForImagesAndTextClassification
|
||||
- forward
|
||||
|
||||
## ViltForImageAndTextRetrieval
|
||||
|
||||
[[autodoc]] ViltForImageAndTextRetrieval
|
||||
- forward
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user