ALBERT is designed to address memory limitations of scaling and training of BERT. It adds two parameter reduction techniques. The first, factorized embedding parametrization,…
ALBERT
This model was published in HF papers on 2019-09-26 and contributed to Hugging Face Transformers on 2020-11-16.
ALBERT
ALBERT is designed to address memory limitations of scaling and training of BERT. It adds two parameter reduction techniques. The first, factorized embedding parametrization, splits the larger vocabulary embedding matrix into two smaller matrices so you can grow the hidden size without adding a lot more parameters. The second, cross-layer parameter sharing, allows layer to share parameters which keeps the number of learnable parameters lower.
ALBERT was created to address problems like -- GPU/TPU memory limitations, longer training times, and unexpected model degradation in BERT. ALBERT uses two parameter-reduction techniques to lower memory consumption and increase the training speed of BERT:
- Factorized embedding parameterization: The large vocabulary embedding matrix is decomposed into two smaller matrices, reducing memory consumption.
- Cross-layer parameter sharing: Instead of learning separate parameters for each transformer layer, ALBERT shares parameters across layers, further reducing the number of learnable weights.
ALBERT uses absolute position embeddings (like BERT) so padding is applied on the right. Size of embeddings is 128. While BERT uses 768. ALBERT can process maximum 512 tokens at a time.
You can find all the original ALBERT checkpoints under the ALBERT community organization.
Tip Click on the ALBERT models in the right sidebar for more examples of how to apply ALBERT to different language tasks.
The example below demonstrates how to predict the [MASK] token with Pipeline, AutoModel, and from the command line.
Pipeline
from transformers import pipeline
pipeline = pipeline(
task="fill-mask",
model="albert-base-v2",
device=0
)
pipeline("Plants create [MASK] through a process known as photosynthesis.", top_k=5)
AutoModel
import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("albert/albert-base-v2")
model = AutoModelForMaskedLM.from_pretrained(
"albert/albert-base-v2",
attn_implementation="sdpa",
device_map="auto"
)
prompt = "Plants create energy through a process known as [MASK]."
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model(**inputs)
mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
predictions = outputs.logits[0, mask_token_index]
top_k = torch.topk(predictions, k=5).indices.tolist()
for token_id in top_k[0]:
print(f"Prediction: {tokenizer.decode([token_id])}")
Notes
- Inputs should be padded on the right because BERT uses absolute position embeddings.
- The embedding size
Eis different from the hidden sizeHbecause the embeddings are context independent (one embedding vector represents one token) and the hidden states are context dependent (one hidden state represents a sequence of tokens). The embedding matrix is also larger becauseV x EwhereVis the vocabulary size. As a result, it's more logical ifH >> E. IfE < H, the model has less parameters.
Resources
The resources provided in the following sections consist of a list of official Hugging Face and community (indicated by 🌎) resources to help you get started with ALBERT. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource.
Check the Text classification task guide on how to use the model.
Token classification chapter of the 🤗 Hugging Face Course.
Check the Token classification task guide on how to use the model.
AlbertForMaskedLMis supported by this notebook.Masked language modeling chapter of the 🤗 Hugging Face Course.
Check the Masked language modeling task guide on how to use the model.
AlbertForQuestionAnsweringis supported by this notebook.Question answering chapter of the 🤗 Hugging Face Course.
Check the Question answering task guide on how to use the model.
Multiple choice
AlbertForMultipleChoiceis supported by this notebook.- Check the Multiple choice task guide on how to use the model.
AlbertConfig
[[autodoc]] AlbertConfig
AlbertTokenizer
[[autodoc]] AlbertTokenizer - get_special_tokens_mask - save_vocabulary
Albert specific outputs
[[autodoc]] models.albert.modeling_albert.AlbertForPreTrainingOutput
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
Source and license
Source: Transformers documentation, adapted for this site. Licensing and attribution follow the terms of the upstream project.