Skip to content
Minh-Thien Nguyen
Go back

Pre-training GPT2, BERT and BART

Tiếng Việt

Table of contents

Open Table of contents

Pre-training GPT2

Pre-training objective

GPT2 is pre-trained with an autoregressive language modeling objective.

Given an input sentence, the model learns to predict the next token of the sentence. For example, take a document like “bóng chày là môn thể thao phổ biến nhất tại Puerto Rico và nước này có hẳn một mùa giải bóng chày chuyên nghiệp riêng được tổ chức vào mùa đông” with the bold part as the input. The prediction can be illustrated as follows:

next token
bóngchày
bóngchày
bóngchàymôn
bóngchàymônthể
bóngchàymônthểthao
bóngchàymônthểthaophổ
bóngchàymônthểthaophổbiến
bóngchàymônthểthaophổbiếnnhất

So the label for the input above is: chày là môn thể thao phổ biến nhất.

Pre-training

The parameters used:

ParameterValue
hidden_size384384
num_layers66
num_heads66
intermediate_size15361536
dropout0.10.1
seq_length256256
activationGELU
optimAdamW
weight_decay0.00010.0001
lr0.50.5
warmup_steps40004000
batch_size3232
max_grad_norm1.01.0

The model was trained on 50MB of text extracted from corpus.viwiki for a total of 40K40K steps. It has 35M parameters. The figure below shows the model’s loss during training.

GPT2 training loss

GPT2 training loss

Some text generated by the model:

python gpt2/generate.py \
    --model ./checkpoints/gpt2-40000.pt \
    --tokenizer ./checkpoints/tokenizer.json \
    --seed 42 \
    --max-new-tokens 120 \
    --temperature 1

result:

>> Nghiên cứu là một quá
Nghiên cứu là một quá trình tạo ra các chất hút điện ngoài sự vật chất . Có hai lĩnh vực điện xuất nhằm tạo ra các loại nguyên tử tự nhiên thù của các kim loại - kim loại . Gốc vụ có thể có các loại kim loại như hiđrua liti , Gd và các loại sulfua kim loại khác . Các chất phóng xạ khác gồm các đồng ( 90 %), 1 %, ") [ 25 ] 6H2O ( 20 % tương đương với 66 %.
Các hợp chất ở dạng O2 là hyđrô sulfit ở nhiệt độ thấp , là một trong các điều kiện quan trọng cho sự cháy cháy . Các chất này là nguồn
python gpt2/generate.py \
    --model ./checkpoints/gpt2-40000.pt \
    --tokenizer ./checkpoints/tokenizer.json \
    --seed 42 \
    --max-new-tokens 40 \
    --temperature 0.5

result:

>> Đầu năm 2021
Đầu năm 2021 , một nhóm hội nghị quốc tế đã được thành lập tại Paris năm 1973 , và cuộc họp này đã được tổ chức bởi các đảng hợp nhất . Năm 1999 , Hội nghị thượng đỉnh thứ nhất được

Pre-training BERT

Pre-training objective

BERT is pre-trained on two tasks: masked language modeling (MLM) and next sentence prediction (NSP).

Pre-training

The parameters used (BERT-medium):

ParameterValue
hidden_size512512
num_layers88
num_heads88
intermediate_size20482048
dropout0.150.15
attention_dropout0.150.15
pooler_dropout0.30.3
seq_length128128
activationGELU
optimAdamW
weight_decay0.0050.005
lr0.60.6
warmup_steps1000010000
batch_size6464
max_grad_norm1.01.0

The model was trained on 500MB of text from the OSCAR dataset. After processing, the dataset has about 1.4 million samples. The dataset is built as the original authors describe.

The model has about 58M parameters and trains for 128K128K steps. On the test set it reaches an accuracy of 0.5610.561 on MLM and 0.9460.946 on NSP.

BERT model accuracy for MLM and NSP

Model accuracy for MLM and NSP

NSP converges quickly. MLM converges more slowly.

Example of the model’s results on [MASK] prediction:

sentence = '[CLS] Anh [MASK] là một [MASK] trai thấp [MASK] [SEP]'
>> predictions = [
    {
        'token': 'cũng',
        'score': 0.12953687
    },
    {
        'token': 'chàng',
        'score': 0.85561085
    },
    {
        'token': 'giọng',
        'score': 0.1861668
    },
]

Pre-training BART

Pre-training objective

BART is pre-trained with a denoising objective.

Pre-training

The parameters used:

ParameterValue
hidden_size512512
num_layers66
num_heads88
intermediate_size20482048
dropout0.10.1
attention_dropout0.10.1
src_seq_length128128
target_seq_length156156
activationGELU
optimAdamW
weight_decay0.0050.005
lr0.50.5
warmup_steps80008000
batch_size3232
max_grad_norm1.01.0
beam_size44

The model was trained on 500MB of text from the OSCAR dataset, following the data processing the original authors describe. It was tested on 4 datasets with about 750K750K training samples. The transforms and their ratios:

The model has about 61M parameters and trains for 40K40K steps. Some of the model’s results:

BART BLEU score on the validation set

BLEU score on the validation set

BLEU is highest with text infilling, above token masking and the combined masking + deletion + infilling. With infilling, a run of consecutive tokens is replaced by one [MASK] token, so the sentence has far fewer [MASK] tokens than the other two setups. Token deletion is the hardest for the model.

BART training and validation loss

Model training and validation loss

Example results with token masking:

Example results with token deletion:

Example results with text infilling:


Share this post:

Next Post
Examples of using bit compression