Skip to main content

FOCUS

Code for "FOCUS: Effective Embedding Initialization for Monolingual Specialization of Multilingual Models" accepted at the EMNLP 2023 main conference.

Paper on arXiv: https://arxiv.org/abs/2305.14481.

Installation

We provide the package via pip install deepfocus.

Alternatively, you can simply copy the deepfocus folder and drop it into your project. The necessary dependencies are listed in requirements.txt (pip install -r requirements.txt).

Usage

The following example shows how to use FOCUS to specialize xlm-roberta-base on German with a custom, language-specific tokenizer. The code is also available in focus_example.py.

from transformers import AutoModelForMaskedLM, AutoTokenizer
from deepfocus import FOCUS

source_tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
source_model = AutoModelForMaskedLM.from_pretrained("xlm-roberta-base")

target_tokenizer = AutoTokenizer.from_pretrained(
    "./tokenizers/de/xlmr-unigram-50k"
)

# Example for training a new tokenizer:
# target_tokenizer = source_tokenizer.train_new_from_iterator(
#     load_dataset("cc100", lang="de", split="train")["text"],
#     vocab_size=50_048
# )
# target_tokenizer.save_pretrained("./target_tokenizer_test")

target_embeddings = FOCUS(
    source_embeddings=source_model.get_input_embeddings().weight,
    source_tokenizer=source_tokenizer,
    target_tokenizer=target_tokenizer,
    target_training_data_path="/path/to/data.txt"
    # fasttext_model_path="/path/to/fasttext.bin", # or directly provide path to token-level fasttext model 

    # In the paper, we use `target_training_data_path` but we also implement using
    # WECHSEL's word-to-subword mapping if the language has pretrained fasttext word embeddings available online
    # To use, supply a two-letter `language_identifier` (e.g. "de" for German) instead of `target_training_data_path` and set:
    # auxiliary_embedding_mode="fasttext-wordlevel",
    # language_identifier="de",

)
source_model.resize_token_embeddings(len(target_tokenizer))
source_model.get_input_embeddings().weight.data = target_embeddings

# Continue training the model on the target language with `target_tokenizer`.
# ...

Checkpoints

We publish the checkpoints trained with FOCUS on HuggingFace:

Language Vocabulary Replacement (preferred) Vocabulary Extension
German konstantindobler/xlm-roberta-base-focus-german konstantindobler/xlm-roberta-base-focus-extend-german
Arabic konstantindobler/xlm-roberta-base-focus-arabic konstantindobler/xlm-roberta-base-focus-extend-arabic
Kiswahili konstantindobler/xlm-roberta-base-focus-kiswahili konstantindobler/xlm-roberta-base-focus-extend-kiswahili
Hausa konstantindobler/xlm-roberta-base-focus-hausa konstantindobler/xlm-roberta-base-focus-extend-hausa
isiXhosa konstantindobler/xlm-roberta-base-focus-isixhosa konstantindobler/xlm-roberta-base-focus-extend-isixhosa

In our experiments, full vocabulary replacement coupled with FOCUS outperformed extending XLM-R's original vocabulary, while also resulting in a smaller model and being faster to train.

Citation

You can cite FOCUS like this:

@misc{dobler-demelo-2023-focus,
    title={FOCUS: Effective Embedding Initialization for Monolingual Specialization of Multilingual Models},
    author={Konstantin Dobler and Gerard de Melo},
    year={2023},
    eprint={2305.14481},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}

If you use the "WECHSEL-style" word-to-subword mapping, please consider also citing their original work.

Metadata

Release files for deepfocus 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deepfocus 1.0.1
File Size Uploaded Provenance Actions
deepfocus-1.0.1.tar.gz 37.6 MB Without provenance Details for deepfocus-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for deepfocus 1.0.1
File Interpreter ABI Platform Actions
deepfocus-1.0.1-py3-none-any.whl Python 3 none any Details for deepfocus-1.0.1-py3-none-any.whl

Total release size: 37.6 MB

Release files / deepfocus-1.0.1.tar.gz

Download URL deepfocus-1.0.1.tar.gz
Size 37.6 MB
Tags Source
SHA-256 checksum
How to use checksums
687a6f311ae5d013bca14dfc7a41db4bd793822729589b265f2b50a6cb83977f
BLAKE2b-256 checksum
How to use checksums
89aa5be1f9b91aa6cf3649dce660b512082f909bbaab33da9060259ce5449593
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / deepfocus-1.0.1-py3-none-any.whl

Download URL deepfocus-1.0.1-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8b93e8394867083a1bfdf8051c4d8564e51e7862928ec2c6d64205294f62451
BLAKE2b-256 checksum
How to use checksums
74faedf7b4da1bc7d422eda66ef9b55ada7bc7963efd364035f15ec3cf0c7c29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Security for deepfocus 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page