github.com-huggingface-transformers_-_2019-11-27_05-31-18

github.com-huggingface-transformers_-_2019-11-27_05-31-18

Posted by

Download
ITEM TILE – File Size: 10.4K

🤗 Transformers: State-of-the-art Natural Language Processing for TensorFlow 2.0 and PyTorch.

transformers_logo_name.png

Build GitHub Documentation GitHub release

State-of-the-art Natural Language Processing for TensorFlow 2.0 and PyTorch

🤗 Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides state-of-the-art general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet, CTRL…) for Natural Language Understanding (NLU) and Natural Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between TensorFlow 2.0 and PyTorch.

Features

  • As easy to use as pytorch-transformers
  • As powerful and concise as Keras
  • High performance on NLU and NLG tasks
  • Low barrier to entry for educators and practitioners

State-of-the-art NLP for everyone- Deep learning researchers- Hands-on practitioners- AI/ML/NLP teachers and educators

Lower compute costs, smaller carbon footprint- Researchers can share trained models instead of always retraining- Practitioners can reduce compute time and production costs- 10 architectures with over 30 pretrained models, some in more than 100 languages

Choose the right framework for every part of a model’s lifetime- Train state-of-the-art models in 3 lines of code- Deep interoperability between TensorFlow 2.0 and PyTorch models- Move a single model between TF2.0/PyTorch frameworks at will- Seamlessly pick the right framework for training, evaluation, production

| Section | Description ||-|-|| Installation | How to install the package || Model architectures | Architectures (with pretrained weights) || Online demo | Experimenting with this repo’s text generation capabilities || Quick tour: Usage | Tokenizers & models usage: Bert and GPT-2 || Quick tour: TF 2.0 and PyTorch | Train a TF 2.0 model in 10 lines of code, load it in PyTorch || Quick tour: Fine-tuning/usage scripts | Using provided scripts: GLUE, SQuAD and Text generation || Migrating from pytorch-transformers to transformers | Migrating your code from pytorch-transformers to transformers || Migrating from pytorch-pretrained-bert to pytorch-transformers | Migrating your code from pytorch-pretrained-bert to transformers || [Documentation][(v2.2.0)](https://huggingface.co/transformers/v2.2.0) (v2.1.1) (v2.0.0) (v1.2.0) (v1.1.0) (v1.0.0) (master) | Full API documentation and more |

Installation

This repo is tested on Python 2.7 and 3.5+ (examples are tested only on python 3.5+), PyTorch 1.0.0+ and TensorFlow 2.0.0-rc1

With pip

First you need to install one of, or both, TensorFlow 2.0 and PyTorch.Please refer to TensorFlow installation page and/or PyTorch installation page regarding the specific install command for your platform.

When TensorFlow 2.0 and/or PyTorch has been installed, 🤗 Transformers can be installed using pip as follows:

bashpip install transformers

From source

Here also, you first need to install one of, or both, TensorFlow 2.0 and PyTorch.Please refer to TensorFlow installation page and/or PyTorch installation page regarding the specific install command for your platform.

When TensorFlow 2.0 and/or PyTorch has been installed, you can install from source by cloning the repository and running:

bashpip install [--editable] .

Run the examples

Examples are included in the repository but are not shipped with the library.Therefore, in order to run the latest versions of the examples you also need to install from source. To do so, create a new virtual environment and follow these steps:

bashgit clone git@github.com:huggingface/transformerscd transformerspip install [--editable] .

Tests

A series of tests are included for the library and the example scripts. Library tests can be found in the tests folder and examples tests in the examples folder.

These tests can be run using pytest (install pytest if needed with pip install pytest).

Depending on which framework is installed (TensorFlow 2.0 and/or PyTorch), the irrelevant tests will be skipped. Ensure that both frameworks are installed if you want to execute all tests.

You can run the tests from the root of the cloned repository with the commands:

bashpython -m pytest -sv ./transformers/tests/python -m pytest -sv ./examples/

Do you want to run a Transformer model on a mobile device?

You should check out our swift-coreml-transformers repo.

It contains a set of tools to convert PyTorch or TensorFlow 2.0 trained Transformer models (currently contains GPT-2, DistilGPT-2, BERT, and DistilBERT) to CoreML models that run on iOS devices.

At some point in the future, you’ll be able to seamlessly move from pre-training or fine-tuning models to productizing them in CoreML, or prototype a model or an app in CoreML then research its hyperparameters or architecture from TensorFlow 2.0 and/or PyTorch. Super exciting!

Model architectures

🤗 Transformers currently provides 10 NLU/NLG architectures:

  1. BERT (from Google) released with the paper BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
  2. GPT (from OpenAI) released with the paper Improving Language Understanding by Generative Pre-Training by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
  3. GPT-2 (from OpenAI) released with the paper Language Models are Unsupervised Multitask Learners by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei* and Ilya Sutskever*.
  4. Transformer-XL (from Google/CMU) released with the paper Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context by Zihang Dai, Zhilin Yang, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
  5. XLNet (from Google/CMU) released with the paper ​XLNet: Generalized Autoregressive Pretraining for Language Understanding by Zhilin Yang, Zihang Dai, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
  6. XLM (from Facebook) released together with the paper Cross-lingual Language Model Pretraining by Guillaume Lample and Alexis Conneau.
  7. RoBERTa (from Facebook), released together with the paper a Robustly Optimized BERT Pretraining Approach by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
  8. DistilBERT (from HuggingFace), released together with the paper DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into DistilGPT2.
  9. CTRL (from Salesforce) released with the paper CTRL: A Conditional Transformer Language Model for Controllable Generation by Nitish Shirish Keskar, Bryan McCann, Lav R. Varshney, Caiming Xiong and Richard Socher.
  10. CamemBERT (from Inria/Facebook/Sorbonne) released with the paper CamemBERT: a Tasty French Language Model 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.
  11. ALBERT (from Google Research and the Toyota Technological Institute at Chicago) released with the paper ALBERT: A Lite BERT for Self-supervised Learning of Language Representations, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
  12. Want to contribute a new model? We have added a detailed guide and templates to guide you in the process of adding a new model. You can find them in the templates folder of the repository. Be sure to check the contributing guidelines and contact the maintainers or open an issue to collect feedbacks before starting your PR.

These implementations have been tested on several datasets (see the example scripts) and should match the performances of the original implementations (e.g. ~93 F1 on SQuAD for BERT Whole-Word-Masking, ~88 F1 on RocStories for OpenAI GPT, ~18.3 perplexity on WikiText 103 for Transformer-XL, ~0.916 Peason R coefficient on STS-B for XLNet). You can find more details on the performances in the Examples section of the documentation.

Online demo

Write With Transformer, built by the Hugging Face team at transformer.huggingface.co, is the official demo of this repo’s text generation capabilities.You can use it to experiment with completions generated by GPT2Model, TransfoXLModel, and XLNetModel.

“🦄 Write with transformer is to writing what calculators are to calculus.”

<img src="//web.archive.org/web/2im_/https://transformer.huggingface.co/front/assets/thumbnail-large.png" alt="writewithtransformer” />

Quick tour

Let’s do a very quick overview of the model architectures in 🤗 Transformers. Detailed examples for each model architecture (Bert, GPT, GPT-2, Transformer-XL, XLNet and XLM) can be found in the full documentation.

“`pythonimport torchfrom transformers import *

Transformers has a unified API

for 8 transformer architectures and 30 pretrained weights.

Model | Tokenizer | Pretrained weights shortcut

MODELS = [(BertModel, BertTokenizer, ‘bert-base-uncased’), (OpenAIGPTModel, OpenAIGPTTokenizer, ‘openai-gpt’), (GPT2Model, GPT2Tokenizer, ‘gpt2’), (CTRLModel, CTRLTokenizer, ‘ctrl’), (TransfoXLModel, TransfoXLTokenizer, ‘transfo-xl-wt103’), (XLNetModel, XLNetTokenizer, ‘xlnet-base-cased’), (XLMModel, XLMTokenizer, ‘xlm-mlm-enfr-1024’), (DistilBertModel, DistilBertTokenizer, ‘distilbert-base-uncased’), (RobertaModel, RobertaTokenizer, ‘roberta-base’)]

To use TensorFlow 2.0 versions of the models, simply prefix the class names with ‘TF’, e.g. TFRobertaModel is the TF 2.0 counterpart of the PyTorch model RobertaModel

Let’s encode some text in a sequence of hidden-states using each model:

for modelclass, tokenizerclass, pretrainedweights in MODELS: # Load pretrained model/tokenizer tokenizer = tokenizerclass.frompretrained(pretrainedweights) model = modelclass.frompretrained(pretrained_weights)

# Encode textinput_ids = torch.tensor([tokenizer.encode("Here is some text to encode", add_special_tokens=True)])  # Add special tokens takes care of adding [CLS], [SEP], ... tokens in the right way for each model.with torch.no_grad():    last_hidden_states = model(input_ids)[0]  # Models outputs are now tuples

Each architecture is provided with several class for fine-tuning on down-stream tasks, e.g.

BERTMODELCLASSES = [BertModel, BertForPreTraining, BertForMaskedLM, BertForNextSentencePrediction, BertForSequenceClassification, BertForTokenClassification, BertForQuestionAnswering]

All the classes for an architecture can be initiated from pretrained weights for this architecture

Note that additional weights added for fine-tuning are only initialized

and need to be trained on the down-stream task

pretrainedweights = ‘bert-base-uncased’tokenizer = BertTokenizer.frompretrained(pretrainedweights)for modelclass in BERTMODELCLASSES: # Load pretrained model/tokenizer model = modelclass.frompretrained(pretrained_weights)

# Models can return full list of hidden-states & attentions weights at each layermodel = model_class.from_pretrained(pretrained_weights,                                    output_hidden_states=True,                                    output_attentions=True)input_ids = torch.tensor([tokenizer.encode("Let's see all hidden-states and attentions on this text")])all_hidden_states, all_attentions = model(input_ids)[-2:]# Models are compatible with Torchscriptmodel = model_class.from_pretrained(pretrained_weights, torchscript=True)traced_model = torch.jit.trace(model, (input_ids,))# Simple serialization for models and tokenizersmodel.save_pretrained('./directory/to/save/')  # savemodel = model_class.from_pretrained('./directory/to/save/')  # re-loadtokenizer.save_pretrained('./directory/to/save/')  # savetokenizer = BertTokenizer.from_pretrained('./directory/to/save/')  # re-load# SOTA examples for GLUE, SQUAD, text generation...

“`

Quick tour TF 2.0 training and PyTorch interoperability

Let’s do a quick example of how a TensorFlow 2.0 model can be trained in 12 lines of code with 🤗 Transformers and then loaded in PyTorch for fast inspection/tests.

“`pythonimport tensorflow as tfimport tensorflow_datasetsfrom transformers import *

Load dataset, tokenizer, model from pretrained model/vocabulary

tokenizer = BertTokenizer.frompretrained(‘bert-base-cased’)model = TFBertForSequenceClassification.frompretrained(‘bert-base-cased’)data = tensorflow_datasets.load(‘glue/mrpc’)

Prepare dataset for GLUE as a tf.data.Dataset instance

traindataset = glueconvertexamplestofeatures(data[‘train’], tokenizer, maxlength=128, task=’mrpc’)validdataset = glueconvertexamplestofeatures(data[‘validation’], tokenizer, maxlength=128, task=’mrpc’)traindataset = traindataset.shuffle(100).batch(32).repeat(2)validdataset = validdataset.batch(64)

Prepare training: Compile tf.keras model with optimizer, loss and learning rate schedule

optimizer = tf.keras.optimizers.Adam(learningrate=3e-5, epsilon=1e-08, clipnorm=1.0)loss = tf.keras.losses.SparseCategoricalCrossentropy(fromlogits=True)metric = tf.keras.metrics.SparseCategoricalAccuracy(‘accuracy’)model.compile(optimizer=optimizer, loss=loss, metrics=[metric])

Train and evaluate using tf.keras.Model.fit()

history = model.fit(traindataset, epochs=2, stepsperepoch=115, validationdata=validdataset, validationsteps=7)

Load the TensorFlow model in PyTorch for inspection

model.savepretrained(‘./save/’)pytorchmodel = BertForSequenceClassification.frompretrained(‘./save/’, fromtf=True)

Quickly test a few predictions – MRPC is a paraphrasing task, let’s see if our model learned the task

sentence0 = “This research was consistent with his findings.”sentence1 = “His findings were compatible with this research.”sentence2 = “His findings were not compatible with this research.”inputs1 = tokenizer.encodeplus(sentence0, sentence1, addspecialtokens=True, returntensors=’pt’)inputs2 = tokenizer.encodeplus(sentence0, sentence2, addspecialtokens=True, return_tensors=’pt’)

pred1 = pytorchmodel(inputs1[‘inputids’], tokentypeids=inputs1[‘tokentypeids’])[0].argmax().item()pred2 = pytorchmodel(inputs2[‘inputids’], tokentypeids=inputs2[‘tokentypeids’])[0].argmax().item()

print(“sentence1 is”, “a paraphrase” if pred1 else “not a paraphrase”, “of sentence0″)print(“sentence2 is”, “a paraphrase” if pred2 else “not a paraphrase”, “of sentence0″)“`

Quick tour of the fine-tuning/usage scripts

Important
Before running the fine-tuning scripts, please read theinstructions on how tosetup your environment to run the examples.

The library comprises several example scripts with SOTA performances for NLU and NLG tasks:

  • run_glue.py: an example fine-tuning Bert, XLNet and XLM on nine different GLUE tasks (sequence-level classification)
  • run_squad.py: an example fine-tuning Bert, XLNet and XLM on the question answering dataset SQuAD 2.0 (token-level classification)
  • run_generation.py: an example using GPT, GPT-2, CTRL, Transformer-XL and XLNet for conditional language generation
  • other model-specific examples (see the documentation).

Here are three quick usage examples for these scripts:

run_glue.py: Fine-tuning on GLUE tasks for sequence classification

The General Language Understanding Evaluation (GLUE) benchmark is a collection of nine sentence- or sentence-pair language understanding tasks for evaluating and analyzing natural language understanding systems.

Before running anyone of these GLUE tasks you should download theGLUE data by runningthis scriptand unpack it to some directory $GLUE_DIR.

You should also install the additional packages required by the examples:

shellpip install -r ./examples/requirements.txt

“`shellexport GLUEDIR=/path/to/glueexport TASKNAME=MRPC

python ./examples/runglue.py –modeltype bert –modelnameorpath bert-base-uncased –taskname $TASKNAME –dotrain –doeval –dolowercase –datadir $GLUEDIR/$TASKNAME –maxseqlength 128 –pergpuevalbatchsize=8 –pergputrainbatchsize=8 –learningrate 2e-5 –numtrainepochs 3.0 –outputdir /tmp/$TASK_NAME/“`

where task name can be one of CoLA, SST-2, MRPC, STS-B, QQP, MNLI, QNLI, RTE, WNLI.

The dev set results will be present within the text file ‘evalresults.txt’ in the specified outputdir. In case of MNLI, since there are two separate dev sets, matched and mismatched, there will be a separate output folder called ‘/tmp/MNLI-MM/’ in addition to ‘/tmp/MNLI/’.

Fine-tuning XLNet model on the STS-B regression task

This example code fine-tunes XLNet on the STS-B corpus using parallel training on a server with 4 V100 GPUs.Parallel training is a simple way to use several GPUs (but is slower and less flexible than distributed training, see below).

“`shellexport GLUE_DIR=/path/to/glue

python ./examples/runglue.py –modeltype xlnet –modelnameorpath xlnet-large-cased –dotrain –doeval –taskname=sts-b –datadir=${GLUEDIR}/STS-B –outputdir=./procdata/sts-b-110 –maxseqlength=128 –pergpuevalbatchsize=8 –pergputrainbatchsize=8 –gradientaccumulationsteps=1 –maxsteps=1200 –modelname=xlnet-large-cased –overwriteoutputdir –overwritecache –warmupsteps=120“`

On this machine we thus have a batch size of 32, please increase gradient_accumulation_steps to reach the same batch size if you have a smaller machine. These hyper-parameters should result in a Pearson correlation coefficient of +0.917 on the development set.

Fine-tuning Bert model on the MRPC classification task

This example code fine-tunes the Bert Whole Word Masking model on the Microsoft Research Paraphrase Corpus (MRPC) corpus using distributed training on 8 V100 GPUs to reach a F1 > 92.

bashpython -m torch.distributed.launch --nproc_per_node 8 ./examples/run_glue.py --model_type bert --model_name_or_path bert-large-uncased-whole-word-masking --task_name MRPC --do_train --do_eval --do_lower_case --data_dir $GLUE_DIR/MRPC/ --max_seq_length 128 --per_gpu_eval_batch_size=8 --per_gpu_train_batch_size=8 --learning_rate 2e-5 --num_train_epochs 3.0 --output_dir /tmp/mrpc_output/ --overwrite_output_dir --overwrite_cache

Training with these hyper-parameters gave us the following results:

bash acc = 0.8823529411764706 acc_and_f1 = 0.901702786377709 eval_loss = 0.3418912578906332 f1 = 0.9210526315789473 global_step = 174 loss = 0.07231863956341798

run_squad.py: Fine-tuning on SQuAD for question-answering

This example code fine-tunes BERT on the SQuAD dataset using distributed training on 8 V100 GPUs and Bert Whole Word Masking uncased model to reach a F1 > 93 on SQuAD:

bashpython -m torch.distributed.launch --nproc_per_node=8 ./examples/run_squad.py --model_type bert --model_name_or_path bert-large-uncased-whole-word-masking --do_train --do_eval --do_lower_case --train_file $SQUAD_DIR/train-v1.1.json --predict_file $SQUAD_DIR/dev-v1.1.json --learning_rate 3e-5 --num_train_epochs 2 --max_seq_length 384 --doc_stride 128 --output_dir ../models/wwm_uncased_finetuned_squad/ --per_gpu_eval_batch_size=3 --per_gpu_train_batch_size=3

Training with these hyper-parameters gave us the following results:

bashpython $SQUAD_DIR/evaluate-v1.1.py $SQUAD_DIR/dev-v1.1.json ../models/wwm_uncased_finetuned_squad/predictions.json{"exact_match": 86.91579943235573, "f1": 93.1532499015869}

This is the model provided as bert-large-uncased-whole-word-masking-finetuned-squad.

run_generation.py: Text generation with GPT, GPT-2, CTRL, Transformer-XL and XLNet

A conditional generation script is also included to generate text from a prompt.The generation script includes the tricks proposed by Aman Rusia to get high-quality generation with memory models like Transformer-XL and XLNet (include a predefined text to make short inputs longer).

Here is how to run the script with the small version of OpenAI GPT-2 model:

shellpython ./examples/run_generation.py --model_type=gpt2 --length=20 --model_name_or_path=gpt2

and from the Salesforce CTRL model: shellpython ./examples/run_generation.py --model_type=ctrl --length=20 --model_name_or_path=ctrl --temperature=0 --repetition_penalty=1.2

Migrating from pytorch-transformers to transformers

Here is a quick summary of what you should take care of when migrating from pytorch-transformers to transformers.

Positional order of some models’ keywords inputs (attention_mask, token_type_ids…) changed

To be able to use Torchscript (see #1010, #1204 and #1195) the specific order of some models keywords inputs (attention_mask, token_type_ids…) has been changed.

If you used to call the models with keyword names for keyword arguments, e.g. model(inputs_ids, attention_mask=attention_mask, token_type_ids=token_type_ids), this should not cause any change.

If you used to call the models with positional inputs for keyword arguments, e.g. model(inputs_ids, attention_mask, token_type_ids), you may have to double check the exact order of input arguments.

Migrating from pytorch-pretrained-bert to transformers

Here is a quick summary of what you should take care of when migrating from pytorch-pretrained-bert to transformers.

Models always output tuples

The main breaking change when migrating from pytorch-pretrained-bert to transformers is that every model’s forward method always outputs a tuple with various elements depending on the model and the configuration parameters.

The exact content of the tuples for each model is detailed in the models’ docstrings and the documentation.

In pretty much every case, you will be fine by taking the first element of the output as the output you previously used in pytorch-pretrained-bert.

Here is a pytorch-pretrained-bert to transformers conversion example for a BertForSequenceClassification classification model:

“`python

Let’s load our model

model = BertForSequenceClassification.from_pretrained(‘bert-base-uncased’)

If you used to have this line in pytorch-pretrained-bert:

loss = model(input_ids, labels=labels)

Now just use this line in transformers to extract the loss from the output tuple:

outputs = model(input_ids, labels=labels)loss = outputs[0]

In transformers you can also have access to the logits:

loss, logits = outputs[:2]

And even the attention weights if you configure the model to output them (and other outputs too, see the docstrings and documentation)

model = BertForSequenceClassification.frompretrained(‘bert-base-uncased’, outputattentions=True)outputs = model(input_ids, labels=labels)loss, logits, attentions = outputs“`

Using hidden states

By enabling the configuration option output_hidden_states, it was possible to retrieve the last hidden states of the encoder. In pytorch-transformers as well as transformers the return value has changed slightly: all_hidden_states now also includes the hidden state of the embeddings in addition to those of the encoding layers. This allows users to easily access the embeddings final state.

Serialization

Breaking change in the from_pretrained() method:

  1. Models are now set in evaluation mode by default when instantiated with the from_pretrained() method. To train them, don’t forget to set them back in training mode (model.train()) to activate the dropout modules.

  2. The additional *input and **kwargs arguments supplied to the from_pretrained() method used to be directly passed to the underlying model’s class __init__() method. They are now used to update the model configuration attribute instead, which can break derived model classes built based on the previous BertForSequenceClassification examples. We are working on a way to mitigate this breaking change in #866 by forwarding the the model’s __init__() method (i) the provided positional arguments and (ii) the keyword arguments which do not match any configuration class attributes.

Also, while not a breaking change, the serialization methods have been standardized and you probably should switch to the new method save_pretrained(save_directory) if you were using any other serialization method before.

Here is an example:

“`python

Let’s load a model and tokenizer

model = BertForSequenceClassification.frompretrained(‘bert-base-uncased’)tokenizer = BertTokenizer.frompretrained(‘bert-base-uncased’)

Do some stuff to our model and tokenizer

Ex: add new tokens to the vocabulary and embeddings of our model

tokenizer.addtokens([‘[SPECIALTOKEN1]’, ‘[SPECIALTOKEN2]’])model.resizetoken_embeddings(len(tokenizer))

Train our model

train(model)

Now let’s save our model and tokenizer to a directory

model.savepretrained(‘./mysavedmodeldirectory/’)tokenizer.savepretrained(‘./mysavedmodeldirectory/’)

Reload the model and the tokenizer

model = BertForSequenceClassification.frompretrained(‘./mysavedmodeldirectory/’)tokenizer = BertTokenizer.frompretrained(‘./mysavedmodeldirectory/’)“`

Optimizers: BertAdam & OpenAIAdam are now AdamW, schedules are standard PyTorch schedules

The two optimizers previously included, BertAdam and OpenAIAdam, have been replaced by a single AdamW optimizer which has a few differences:

  • it only implements weights decay correction,
  • schedules are now externals (see below),
  • gradient clipping is now also external (see below).

The new optimizer AdamW matches PyTorch Adam optimizer API and let you use standard PyTorch or apex methods for the schedule and clipping.

The schedules are now standard PyTorch learning rate schedulers and not part of the optimizer anymore.

Here is a conversion examples from BertAdam with a linear warmup and decay schedule to AdamW and the same schedule:

“`python

Parameters:

lr = 1e-3maxgradnorm = 1.0numtrainingsteps = 1000numwarmupsteps = 100warmupproportion = float(numwarmupsteps) / float(numtraining_steps) # 0.1

Previously BertAdam optimizer was instantiated like this:

optimizer = BertAdam(model.parameters(), lr=lr, schedule=’warmuplinear’, warmup=warmupproportion, ttotal=numtraining_steps)

and used like this:

for batch in train_data: loss = model(batch) loss.backward() optimizer.step()

In Transformers, optimizer and schedules are splitted and instantiated like this:

optimizer = AdamW(model.parameters(), lr=lr, correctbias=False) # To reproduce BertAdam specific behavior set correctbias=Falsescheduler = getlinearschedulewithwarmup(optimizer, numwarmupsteps=numwarmupsteps, numtrainingsteps=numtrainingsteps) # PyTorch scheduler

and used like this:

for batch in traindata: model.train() loss = model(batch) loss.backward() torch.nn.utils.clipgradnorm(model.parameters(), maxgradnorm) # Gradient clipping is not in AdamW anymore (so you can use amp without issue) optimizer.step() scheduler.step() optimizer.zero_grad()“`

Citation

We now have a paper you can cite for the 🤗 Transformers library:@article{Wolf2019HuggingFacesTS, title={HuggingFace's Transformers: State-of-the-art Natural Language Processing}, author={Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and R'emi Louf and Morgan Funtowicz and Jamie Brew}, journal={ArXiv}, year={2019}, volume={abs/1910.03771}}

To restore the repository download the bundle

wget https://archive.org/download/github.com-huggingface-transformers_-_2019-11-27_05-31-18/huggingface-transformers_-_2019-11-27_05-31-18.bundle

and run:

 git clone huggingface-transformers_-_2019-11-27_05-31-18.bundle 

Source: https://github.com/huggingface/transformers
Uploader: huggingface
Upload date: 2019-11-27