/bert-for-tf2

A Keras TensorFlow 2.0 implementation of BERT, ALBERT and adapter-BERT.

Primary LanguagePythonMIT LicenseMIT

BERT for TensorFlow v2

Build Status Coverage Status Version Status Python Versions Downloads

This repo contains a TensorFlow 2.0 Keras implementation of google-research/bert with support for loading of the original pre-trained weights, and producing activations numerically identical to the one calculated by the original model.

ALBERT and adapter-BERT are also supported by setting the corresponding configuration parameters (shared_layer=True, embedding_size for ALBERT and adapter_size for adapter-BERT). Setting both will result in an adapter-ALBERT by sharing the BERT parameters across all layers while adapting every layer with layer specific adapter.

The implementation is build from scratch using only basic tensorflow operations, following the code in google-research/bert/modeling.py (but skipping dead code and applying some simplifications). It also utilizes kpe/params-flow to reduce common Keras boilerplate code (related to passing model and layer configuration arguments).

bert-for-tf2 should work with both TensorFlow 2.0 and TensorFlow 1.14 or newer.

NEWS

  • 30.Jul.2020 - VERBOSE=0 env variable for suppressing stdout output.
  • 06.Apr.2020 - using latest py-params introducing WithParams base for Layer and Model. See news in kpe/py-params for how to update (_construct() signature has change and requires calling super().__construct()).
  • 06.Jan.2020 - support for loading the tar format weights from google-research/ALBERT.
  • 18.Nov.2019 - ALBERT tokenization added (make sure to import as from bert import albert_tokenization or from bert import bert_tokenization).
  • 08.Nov.2019 - using v2 per default when loading the TFHub/albert weights of google-research/ALBERT.
  • 05.Nov.2019 - minor ALBERT word embeddings refactoring (word_embeddings_2 -> word_embeddings_projector) and related parameter freezing fixes.
  • 04.Nov.2019 - support for extra (task specific) token embeddings using negative token ids.
  • 29.Oct.2019 - support for loading of the pre-trained ALBERT weights released by google-research/ALBERT at TFHub/albert.
  • 11.Oct.2019 - support for loading of the pre-trained ALBERT weights released by brightmart/albert_zh ALBERT for Chinese.
  • 10.Oct.2019 - support for ALBERT through the shared_layer=True and embedding_size=128 params.
  • 03.Sep.2019 - walkthrough on fine tuning with adapter-BERT and storing the fine tuned fraction of the weights in a separate checkpoint (see tests/test_adapter_finetune.py).
  • 02.Sep.2019 - support for extending the token type embeddings of a pre-trained model by returning the mismatched weights in load_stock_weights() (see tests/test_extend_segments.py).
  • 25.Jul.2019 - there are now two colab notebooks under examples/ showing how to fine-tune an IMDB Movie Reviews sentiment classifier from pre-trained BERT weights using an adapter-BERT model architecture on a GPU or TPU in Google Colab.
  • 28.Jun.2019 - v.0.3.0 supports adapter-BERT (google-research/adapter-bert) for "Parameter-Efficient Transfer Learning for NLP", i.e. fine-tuning small overlay adapter layers over BERT's transformer encoders without changing the frozen BERT weights.

LICENSE

MIT. See License File.

Install

bert-for-tf2 is on the Python Package Index (PyPI):

pip install bert-for-tf2

Usage

BERT in bert-for-tf2 is implemented as a Keras layer. You could instantiate it like this:

or by using the bert_config.json from a pre-trained google model:

now you can use the BERT layer in your Keras model like this:

if you choose to use adapter-BERT by setting the adapter_size parameter, you would also like to freeze all the original BERT layers by calling:

and once the model has been build or compiled, the original pre-trained weights can be loaded in the BERT layer:

N.B. see tests/test_bert_activations.py for a complete example.

FAQ

  1. In all the examlpes bellow, please note the line:

for a quick test, you can replace it with something like:

  1. How to use BERT with the google-research/bert pre-trained weights?
  1. How to use ALBERT with the google-research/ALBERT pre-trained weights (fetching from TFHub)?

see tests/nonci/test_load_pretrained_weights.py:

  1. How to use ALBERT with the google-research/ALBERT pre-trained weights (non TFHub)?

see tests/nonci/test_load_pretrained_weights.py:

  1. How to use ALBERT with the brightmart/albert_zh pre-trained weights?

see tests/nonci/test_albert.py:

  1. How to tokenize the input for the google-research/bert models?
  1. How to tokenize the input for brightmart/albert_zh?
  1. How to tokenize the input for the google-research/ALBERT models?
  1. How to tokenize the input for the Chinese google-research/ALBERT models?

Resources