All Projects → fredriko → bert-tensorflow-pytorch-spacy-conversion

fredriko / bert-tensorflow-pytorch-spacy-conversion

Licence: other
Instructions for how to convert a BERT Tensorflow model to work with HuggingFace's pytorch-transformers, and spaCy. This walk-through uses DeepPavlov's RuBERT as example.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to bert-tensorflow-pytorch-spacy-conversion

contextualSpellCheck
✔️Contextual word checker for better suggestions
Stars: ✭ 274 (+953.85%)
Mutual labels:  spacy, bert
spacy-sentence-bert
Sentence transformers models for SpaCy
Stars: ✭ 88 (+238.46%)
Mutual labels:  spacy, bert
turing
✨ 🧬 Turing AI - Semantic Navigation, Chatbot using Search Engine and Many NLP Vendors.
Stars: ✭ 30 (+15.38%)
Mutual labels:  spacy, spacy-nlp
Kashgari
Kashgari is a production-level NLP Transfer learning framework built on top of tf.keras for text-labeling and text-classification, includes Word2Vec, BERT, and GPT2 Language Embedding.
Stars: ✭ 2,235 (+8496.15%)
Mutual labels:  bert, bert-model
label-studio-transformers
Label data using HuggingFace's transformers and automatically get a prediction service
Stars: ✭ 117 (+350%)
Mutual labels:  bert, pytorch-transformers
Transformers
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
Stars: ✭ 55,742 (+214292.31%)
Mutual labels:  bert, pytorch-transformers
augmenty
Augmenty is an augmentation library based on spaCy for augmenting texts.
Stars: ✭ 101 (+288.46%)
Mutual labels:  spacy, spacy-nlp
bert-movie-reviews-sentiment-classifier
Build a Movie Reviews Sentiment Classifier with Google's BERT Language Model
Stars: ✭ 12 (-53.85%)
Mutual labels:  bert, bert-model
Semantic-Textual-Similarity
Natural Language Processing using NLTK and Spacy
Stars: ✭ 30 (+15.38%)
Mutual labels:  spacy, spacy-nlp
FinBERT-QA
Financial Domain Question Answering with pre-trained BERT Language Model
Stars: ✭ 70 (+169.23%)
Mutual labels:  bert, bert-model
SIGIR2021 Conure
One Person, One Model, One World: Learning Continual User Representation without Forgetting
Stars: ✭ 23 (-11.54%)
Mutual labels:  bert, bert-model
nlp-cheat-sheet-python
NLP Cheat Sheet, Python, spacy, LexNPL, NLTK, tokenization, stemming, sentence detection, named entity recognition
Stars: ✭ 69 (+165.38%)
Mutual labels:  spacy, spacy-nlp
PIE
Fast + Non-Autoregressive Grammatical Error Correction using BERT. Code and Pre-trained models for paper "Parallel Iterative Edit Models for Local Sequence Transduction": www.aclweb.org/anthology/D19-1435.pdf (EMNLP-IJCNLP 2019)
Stars: ✭ 164 (+530.77%)
Mutual labels:  bert, bert-model
anonymisation
Anonymization of legal cases (Fr) based on Flair embeddings
Stars: ✭ 85 (+226.92%)
Mutual labels:  spacy, bert
HugsVision
HugsVision is a easy to use huggingface wrapper for state-of-the-art computer vision
Stars: ✭ 154 (+492.31%)
Mutual labels:  bert, pytorch-transformers
tweets-preprocessor
Repo containing the Twitter preprocessor module, developed by the AUTH OSWinds team
Stars: ✭ 26 (+0%)
Mutual labels:  spacy, spacy-nlp
DrFAQ
DrFAQ is a plug-and-play question answering NLP chatbot that can be generally applied to any organisation's text corpora.
Stars: ✭ 29 (+11.54%)
Mutual labels:  spacy, bert
NLP Quickbook
NLP in Python with Deep Learning
Stars: ✭ 516 (+1884.62%)
Mutual labels:  spacy, spacy-nlp
gender-unbiased BERT-based pronoun resolution
Source code for the ACL workshop paper and Kaggle competition by Google AI team
Stars: ✭ 42 (+61.54%)
Mutual labels:  bert, bert-model
Text-Summarization
Abstractive and Extractive Text summarization using Transformers.
Stars: ✭ 38 (+46.15%)
Mutual labels:  bert

How to convert a BERT model from Tensorflow to PyTorch and spaCy

This repository contains instructions and sample code for converting a BERT Tensorflow model to work with Hugging Face's pytorch-transformers and as a package for explosion.ai's spaCy via spacy-pytorch-transformers.

The instructions use the Russian BERT model (RuBERT) created by DeepPavlov as working example.

Pre-requisites

You need the following software installed on your computer to be able to install and run the examples in this guide.

  • Git
  • Python 3.6 or later
  • pip3
  • virtualenv

Download the example BERT model

Download RuBERT from http://files.deeppavlov.ai/deeppavlov_data/bert/rubert_cased_L-12_H-768_A-12_v1.tar.gz

For the sake of this example, place the downloaded RuBERT file in your user's root directory, and unpack it with

tar zxvf rubert_cased_L-12_H-768_A-12_v1.tar.gz

The unpacked model is now available in ~/rubert_cased_L-12_H-768_a-12_v1/

Set-up the working environment

Clone this repository, create a virtual environment, and install the dependencies by giving the following commands in a shell:

git clone https://github.com/fredriko/bert-tensorflow-pytorch-spacy-conversion.git
cd bert-tensorflow-pytorch-spacy-conversion
virtualenv -p python3 ~/venv/bert-tensorflow-pytorch-spacy-conversion
source ~/venv/bert-tensorflow-pytorch-spacy-conversion/bin/activate
pip3 install -r requirements.txt

Convert the BERT Tensorflow model to work with Hugging Face's pytorch-transformers

Convert the Tensorflow RuBERT model to a PyTorch equivalent with this command:

$ python3 -m pytorch_transformers.convert_tf_checkpoint_to_pytorch \
--tf_checkpoint_path ~/rubert_cased_L-12_H-768_A-12_v1/bert_model.ckpt.index \ 
--bert_config_file ~/rubert_cased_L-12_H-768_A-12_v1/bert_config.json \
--pytorch_dump_path ~/rubert_cased_L-12_H-768_A-12_v1/pytorch_model.bin

After the conversion, copy the required files to a separate directory; ~/pytorch-rubert/:

mkdir ~/pytorch-rubert
cp ~/rubert_cased_L-12_H-768_A-12_v1/rubert_pytorch.bin ~/pytorch-rubert/.
cp ~/rubert_cased_L-12_H-768_A-12_v1/vocab.txt ~/pytorch-rubert/.
cp ~/rubert_cased_L-12_H-768_A-12_v1/bert_config.json ~/pytorch-rubert/config.json

You now have the files required to use RuBERT in pytorch-transformers. The following code snippet is an example of how the PyTorch model can be loaded and used in pytorch-transformers (source):

import torch
from pytorch_transformers import *
from pathlib import Path

sample_text = "Рад познакомиться с вами."
my_model_dir = str(Path.home() / "pytorch-rubert")

tokenizer = BertTokenizer.from_pretrained(my_model_dir)
model = BertModel.from_pretrained(my_model_dir, output_hidden_states=True)

input_ids = torch.tensor([tokenizer.encode(sample_text, add_special_tokens=True)])
print(f"Input ids: {input_ids}")
with torch.no_grad():
    last_hidden_states = model(input_ids)[0]
    print(f"Shape of last hidden states: {last_hidden_states.shape}")
    print(last_hidden_states)

Convert the pytorch-transformer model to a spaCy package

In order to create a spaCy package of the PyTorch model, it first has to be saved to disk as a serialized pipeline. First, create the directory in which to save the pipeline, then run the script for serializing and saving it.

mkdir ~/spacy-rubert
python3 -m src.serialize_spacy_nlp_pipeline

You now have all you need to create a spaCy package in ~/spacy-rubert.

OPTIONAL: fill in the appropriate information in ~/spacy-rubert/meta.json before proceeding.

Run the following commands to create a spaCy package from the serialized pipeline and save it to ~/spacy-rubert-package:

mkdir ~/spacy-rubert-package
python3 -m spacy package ~/spacy-rubert ~/spacy-rubert-package

NOTE: that the name of the model directory under ~/spacy-rubert-package depends on the information you supplied in ~/spacy-rubert/meta.json in the previous step. The name used below originates from a raw meta.json file.

cd ~/spacy-rubert-package/ru_model-0.0.0
python3 setup.py sdist

After successful completion of the above commands, the RuBERT model is available as a spaCy package in:

~/spacy-rubert-package/ru_model-0.0.0/dist/ru_model-0.0.0.tar.gz

Install it with:

pip3 install ~/spacy-rubert-package/ru_model-0.0.0/dist/ru_model-0.0.0.tar.gz

Verify its presence in the current virtualenv:

pip3 freeze | grep ru-model
> ru-model==0.0.0

Here is an example of how the package can be loaded and used (source):

import spacy

nlp = spacy.load("ru_model")
doc = nlp("Рад познакомиться с вами.")
print(doc.vector)
print(doc[0].similarity(doc[0]))
print(doc[0].similarity(doc[1]))

NOTE: that the above example does not make use of a GPU. For that to happen, you need a different installation of spaCy than the one specified in the requirements.txt in this repository.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].