Profile

Settings

Loading…

← Papers

Help

FAQ

Which features require signing in?

You can read the commentary, paper info, Model Structure and related papers of public papers without signing in. Sign in to manage your account or contact us about your inquiries.

Can I upload a PDF file?

Upload papers with an arXiv or DOI link; the commentary is public to everyone. Papers whose source and terms of use can't be verified, so the commentary can't be made public, can't be uploaded. Uploading PDF files and keeping papers private are for subscribers only, and a paper uploaded as a file is saved as private only. Searching for official info by title runs only when you press the button.

Are the figures and AI commentary accurate?

Figures come only from the original paper, and only those whose terms of use are verified are shown. The commentary is written by AI and may be wrong. Check important claims, numbers and figure context against the evidence in the original paper.

I want to report a copyright issue.

Terms of use and the source of the original paper are on the Paper info tab. If there's a problem, choose "Paper content, figures and copyright" under Contact us and send the paper link, the part in question, and evidence that you hold the rights. Receiving your report doesn't confirm any rights or remove anything right away. You can see the result of the review under My inquiries.

I forgot my password.

If you signed up with email, use Forgot password in the sign-in window to reset it with your verified email. If you signed up with Google, Naver or Kakao, change your password with that service.

What should I include in an inquiry?

Tell us which screen you were on and what you were doing when the problem happened, along with the paper link. Don't include personal information such as passwords, API keys, verification codes or phone numbers. Inquiries and replies are stored in Refivu and you can see them under My inquiries.

How do I delete my account?

In Settings, under My account, press Delete account. Review what will be deleted and complete identity verification to delete your account. For other requests, choose "Account" under Contact us.

Paper commentary

How to Fine-Tune BERT for Text Classification?

Fine-tuning recipe lifts BERT to new text classification results

  • Problem Pre-trained BERT is widely used for text classification, but few choices for fine-tuning it have been tested. Long inputs, layer choice, and learning rates each change results, so teams have no tested recipe.
  • Method The authors combine further pre-training on task or domain text, optional multi-task fine-tuning, and target-task fine-tuning. For long inputs they keep the first 128 and last 382 tokens instead of only the head.
  • Results The recipe reaches new state-of-the-art results on eight text classification datasets. On IMDb it lowers test error from 5.40 to 4.37 with an IMDb-only pre-trained model.

Why BERT needs a fine-tuning recipe

Text classification has been handled with convolutional and recurrent encoders, attention models, and word or sentence embeddings used as features. Pre-trained language models learn general representations from large unlabelled corpora, so a downstream task can reuse them without training from scratch. BERT builds on bidirectional Transformer layers, but the authors note that little work has tried to raise its performance on target tasks further.

The paper answers this with a three-step recipe: further pre-training on within-task or in-domain text, optional multi-task fine-tuning when related tasks exist, and then fine-tuning on the target task. The fine-tuning choices examined include long-text handling, layer selection, layer-wise learning rates, catastrophic forgetting and low-shot learning.

Experiments cover seven English datasets and one Chinese news dataset, and the authors report new state-of-the-art results on these eight datasets.

BERT as a text classifier

BERT-base has 12 Transformer blocks, 12 self-attention heads and a hidden size of 768. It takes at most 512 tokens, and the first token is a special classification token, with a separator token between segments. The final hidden state h of the classification token serves as the representation of the whole input, and a softmax layer on top gives \\( p(c \\mid h) = \\mathrm{softmax}(W h) \\), where W is a task-specific matrix.

Layer-wise learning rates

The lower layers of BERT are assumed to hold more general information, so the paper fine-tunes them with smaller learning rates than the upper layers. The parameters are split into one group per layer and each group is updated with its own rate: \\[ \\theta_l^{t}=\\theta_l^{t-1}-\\eta_l\\cdot\\nabla_{\\theta_l}J(\\theta) \\] where \\( \\eta_l \\) is the learning rate of layer l.

The top layer gets the base rate \\( \\eta_L \\), and each layer below uses \\( \\eta_{k-1} = \\xi \\cdot \\eta_k \\) with decay factor \\( \\xi \\le 1 \\). With \\( \\xi = 1 \\) all layers share one rate, which the paper treats as ordinary stochastic gradient descent.

On IMDb, a base rate of 2.0e-5 with decay factor \\( \\xi = 0.95 \\) gave 5.40 test error, against 5.42 for the uniform rate at the same base rate. Decay factors of 0.85 were the worst in the grid at both base rates, with 5.58 at 2.5e-5 and 5.65 at 2.0e-5.

Handling inputs longer than 512 tokens

Because BERT accepts at most 512 tokens, the paper compares three truncation schemes and three hierarchical schemes on IMDb and Sogou News. Head-only keeps the first 510 tokens and tail-only keeps the last 510. Head+tail keeps the first 128 and the last 382 tokens. Hierarchical methods split the input into fractions of up to 510 tokens, encode each fraction with BERT, and pool the fraction representations by mean, max or self-attention.

On IMDb, head+tail gave 5.42 test error, against 5.44 for tail-only, 5.49 for hierarchical self-attention, 5.63 for head-only and 5.89 for hierarchical mean. On Sogou News, head+tail gave 2.43, against 2.47 for hierarchical max, 2.58 for head-only and 3.17 for tail-only. The IMDb margin over tail-only is small, but head+tail is the lowest of the six on both datasets, so the authors use it for all later experiments.

Which BERT layer to read features from

BERT-base has an embedding layer, a 12-layer encoder and a pooling layer, and the paper compares features taken from individual encoder layers and from combinations of layers on IMDb. Test error fell steadily from 11.07 at Layer-0 to 5.42 at Layer-11, so the last layer alone gave the lowest single-layer error. Concatenating the last four layers gave 5.43 and their max gave 5.42, close to the last layer alone, while the first four layers gave 8.69 or higher.

The authors therefore use the last layer of BERT for the following experiments.

Learning rate and catastrophic forgetting

Catastrophic forgetting, in which pre-trained knowledge is erased while a model learns a new task, is a common concern in transfer learning, so the authors checked whether BERT shows it. Fine-tuning on IMDb was run at learning rates of 2e-5, 5e-5, 1e-4 and 4e-4, and the error curves are shown in the figure. The authors find that a lower rate such as 2e-5 is needed to overcome forgetting, and that at 4e-4 the training set fails to converge.

Models were trained for at most 4 epochs and the best validation checkpoint was tested, so the non-convergence at 4e-4 is a result within that budget.

Further pre-training objectives and settings

Further pre-training continues BERT's masked language model and next sentence prediction objectives on domain data before the target-task fine-tuning. Within-task pre-training uses the target task's own training data. In-domain pre-training uses data from the same domain, possibly merging the training data of several related tasks. Cross-domain pre-training mixes same-domain data with data from other domains.

English datasets are split into sentences with spaCy, and the Chinese Sogou News data is split at Chinese full stops, question marks and exclamation marks. The further pre-training runs used a batch size of 32, a maximum sequence length of 128, a learning rate of 5e-5, 100,000 training steps and 10,000 warm-up steps.

On IMDb, within-task further pre-training improved the target-task result, and the best performance came after 100K training steps.

In-domain and cross-domain results

The seven English datasets were grouped into sentiment, question and topic domains, and each task was also used alone as a separate domain for cross-task pre-training. Almost every further pre-trained model beat the original BERT-base on the seven datasets, and in-domain pre-training generally did better than within-task pre-training.

On IMDb, the original BERT-base reached 5.40, the sentiment-domain model 4.88 and the IMDb-only model 4.37. On TREC, within-task pre-training raised error from 2.80 to 3.20, while pre-training on Yahoo! Answers data lowered it to 1.80. The all-data model matched the original on TREC at 2.80 and had lower error on the other six datasets.

IMDb and Yelp did not help each other in the sentiment domain, which the authors suggest may come from movie and food reviews having different data distributions.

Comparison with BERT-Feat and earlier methods

The baselines include CNN models such as Char-level CNN, VDCNN and DPCNN, recurrent models such as D-LSTM, Skim-LSTM and hierarchical attention networks, feature-based methods such as region embedding and CoVe, and ULMFiT. BERT-Feat uses BERT features as the input embedding of a biLSTM with self-attention, so it uses BERT as a frozen-feature source rather than fine-tuning it.

Most earlier scores are taken from their papers, while the Sogou entries for earlier models come from the authors' own implementation.

BERT-Feat beats the other baselines except ULMFiT, and BERT-FiT beats BERT-Feat on seven of the eight datasets, the exception being DBPedia.

Multi-task fine-tuning results

Multi-task fine-tuning shares the BERT layers and the embedding layer, and each task keeps its own final classifier. Four datasets were used: IMDb, Yelp P., AG’s News and DBPedia. Yelp F. was left out because its test set overlaps the Yelp P. training set, and the two question datasets were also excluded. After joint training, each dataset gets extra steps at a lower learning rate so that each subtask is tuned for its own data.

Starting from the original BERT-base, multi-task fine-tuning lowered test error on all four datasets: IMDb from 5.40 to 5.36, Yelp P. from 2.28 to 2.19, AG’s News from 5.25 to 5.20 and DBPedia from 0.71 to 0.68. Starting from the cross-domain model, the picture changes. BERT-CDPT-MFiT-FiT gives 4.96 on IMDb, against 5.18 for BERT-CDPT-FiT, but 2.06 on Yelp P.

against 1.97, and 5.13 on AG’s News against 5.08. The authors suggest the two approaches may be alternatives, since the cross-domain model already carries rich domain-specific information.

Training with small subsets of IMDb

With only 0.4% of the training data, BERT-FiT reached 17.26% test error and BERT-ITPT-FiT reached 9.23%. The authors read this as BERT giving a large gain on small data, with further pre-training adding more, though these numbers come from IMDb only.

Applying the same steps to BERT-Large

BERT-Large was further pre-trained for 120K steps with batch size 24 and maximum sequence length 128, then fine-tuned on four GPUs with maximum sequence length 512. Table 8 covers five datasets: IMDb, Yelp P., Yelp F., AG’s News and DBPedia. Without further pre-training, BERT-Large has lower error than ULMFiT on four of the five datasets, and ULMFiT is lower only on IMDb, with 4.60 against 4.86.

Task-specific further pre-training takes BERT-base below ULMFiT on all five datasets, and BERT-Large with further pre-training has the lowest error in the table, from 4.21 on IMDb to 0.61 on DBPedia.

Limitations

The domain grouping of the seven English datasets is acknowledged as not strictly correct, so the in-domain results depend on that partition. Multi-task fine-tuning did not improve BERT-CDPT on Yelp P. and AG’s News. The few-shot experiment uses only IMDb, and the catastrophic forgetting curves are also for IMDb. English experiments use the uncased BERT-base model and the Sogou experiments use the Chinese BERT-base model, while BERT-Large results cover only the five datasets in Table 8.

The Sogou dataset was built by the authors from the SogouCA and SogouCS corpora, with six categories and 9,000 training and 1,000 test samples per class.

Paper info

Authors
Chi Sun, Xipeng Qiu, Yige Xu, Xuanjing Huang
Venue
arXiv · Preprint
First published
2019-5-14
Added
2026-11-12
Updated
2020-2-5
Version
v3
arXiv
1905.05583
Category
cs.CL
Original paper terms
arXiv non-exclusive 1.0
What we show
Paper info and commentary

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

Quote from the paper

Chi Sun, Xipeng Qiu, Yige Xu, Xuanjing Huang (2019). How to Fine-Tune BERT for Text Classification?. arXiv:1905.05583v3. https://arxiv.org/abs/1905.05583v3

BibTeX
@misc{sun2019fine,
  title = {{How to Fine-Tune BERT for Text Classification?}},
  author = {Chi Sun and Xipeng Qiu and Yige Xu and Xuanjing Huang},
  year = {2019},
  eprint = {1905.05583},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1905.05583v3}
}

See related papersBrowse other papers

Paper preview
Account security

Connect Google account

If you connect a Google account, you can sign in with it too.

Refivu

Delete account

Refivu
Refivu

Sign up