transformers/docs/source/en/model_doc/electra.md

7.7 KiB

ELECTRA

Overview

The ELECTRA model was proposed in the paper ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators. 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.

This model was contributed by lysandre. The original code can be found here.

Usage 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.
  • ELECTRA is a transformer model pretrained with the use of another (small) masked language model. The inputs are corrupted by that language model, which takes an input text that is randomly masked and outputs a text in which ELECTRA has to predict which token is an original and which one has been replaced. Like for GAN training, the small language model is trained for a few steps (but with the original texts as objective, not to fool the ELECTRA model like in a traditional GAN setting) then the ELECTRA model is trained for a few steps.
  • The ELECTRA checkpoints saved using Google Research's implementation 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).

Resources

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

FlaxElectraForCausalLM

autodoc FlaxElectraForCausalLM - call

FlaxElectraForMaskedLM

autodoc FlaxElectraForMaskedLM - call

FlaxElectraForSequenceClassification

autodoc FlaxElectraForSequenceClassification - call

FlaxElectraForMultipleChoice

autodoc FlaxElectraForMultipleChoice - call

FlaxElectraForTokenClassification

autodoc FlaxElectraForTokenClassification - call

FlaxElectraForQuestionAnswering

autodoc FlaxElectraForQuestionAnswering - call