{"id":13935769,"url":"https://github.com/fredriko/bert-tensorflow-pytorch-spacy-conversion","last_synced_at":"2025-07-19T21:30:36.013Z","repository":{"id":39736063,"uuid":"206161697","full_name":"fredriko/bert-tensorflow-pytorch-spacy-conversion","owner":"fredriko","description":"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.","archived":false,"fork":false,"pushed_at":"2023-03-24T23:34:28.000Z","size":8,"stargazers_count":27,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-27T03:34:35.671Z","etag":null,"topics":["bert","bert-model","how-to","keras","nlp","pytorch-transformers","spacy","spacy-models","spacy-nlp","spacy-package","spacy-pytorch-transformers","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fredriko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-03T19:57:55.000Z","updated_at":"2022-10-24T16:12:35.000Z","dependencies_parsed_at":"2024-11-27T03:41:30.832Z","dependency_job_id":null,"html_url":"https://github.com/fredriko/bert-tensorflow-pytorch-spacy-conversion","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fredriko/bert-tensorflow-pytorch-spacy-conversion","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredriko%2Fbert-tensorflow-pytorch-spacy-conversion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredriko%2Fbert-tensorflow-pytorch-spacy-conversion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredriko%2Fbert-tensorflow-pytorch-spacy-conversion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredriko%2Fbert-tensorflow-pytorch-spacy-conversion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fredriko","download_url":"https://codeload.github.com/fredriko/bert-tensorflow-pytorch-spacy-conversion/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredriko%2Fbert-tensorflow-pytorch-spacy-conversion/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266019657,"owners_count":23864916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bert","bert-model","how-to","keras","nlp","pytorch-transformers","spacy","spacy-models","spacy-nlp","spacy-package","spacy-pytorch-transformers","tensorflow"],"created_at":"2024-08-07T23:02:04.894Z","updated_at":"2025-07-19T21:30:36.004Z","avatar_url":"https://github.com/fredriko.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"## How to convert a BERT model from Tensorflow to PyTorch and spaCy\n\nThis repository contains instructions and sample code for converting a BERT Tensorflow model\nto work with Hugging Face's [pytorch-transformers](https://github.com/huggingface/pytorch-transformers)\nand as a package for explosion.ai's [spaCy](https://spacy.io/) via [spacy-pytorch-transformers](https://github.com/explosion/spacy-pytorch-transformers). \n\nThe instructions use the Russian BERT model (RuBERT) created by [DeepPavlov](https://deeppavlov.ai) as working example.\n\n### Pre-requisites\n\nYou need the following software installed on your computer to be able to install and run the examples in this guide.\n\n* Git\n* Python 3.6 or later\n* pip3\n* virtualenv\n\n### Download the example BERT model\n\nDownload [RuBERT](http://docs.deeppavlov.ai/en/master/features/pretrained_vectors.html#bert) from http://files.deeppavlov.ai/deeppavlov_data/bert/rubert_cased_L-12_H-768_A-12_v1.tar.gz\n\nFor the sake of this example, place the downloaded RuBERT file in your user's root directory, and unpack it with\n\n```\ntar zxvf rubert_cased_L-12_H-768_A-12_v1.tar.gz\n```\n\nThe unpacked model is now available in `~/rubert_cased_L-12_H-768_a-12_v1/`\n\n\n### Set-up the working environment\n\nClone this repository, create a virtual environment, and install the dependencies by giving the following commands in a shell:\n\n```\ngit clone https://github.com/fredriko/bert-tensorflow-pytorch-spacy-conversion.git\ncd bert-tensorflow-pytorch-spacy-conversion\nvirtualenv -p python3 ~/venv/bert-tensorflow-pytorch-spacy-conversion\nsource ~/venv/bert-tensorflow-pytorch-spacy-conversion/bin/activate\npip3 install -r requirements.txt\n```\n\n\n### Convert the BERT Tensorflow model to work with Hugging Face's pytorch-transformers\n\nConvert the Tensorflow RuBERT model to a PyTorch equivalent with this command:\n\n```\n$ python3 -m pytorch_transformers.convert_tf_checkpoint_to_pytorch \\\n--tf_checkpoint_path ~/rubert_cased_L-12_H-768_A-12_v1/bert_model.ckpt.index \\ \n--bert_config_file ~/rubert_cased_L-12_H-768_A-12_v1/bert_config.json \\\n--pytorch_dump_path ~/rubert_cased_L-12_H-768_A-12_v1/pytorch_model.bin\n```\n\nAfter the conversion, copy the required files to a separate directory; `~/pytorch-rubert/`:\n```\nmkdir ~/pytorch-rubert\ncp ~/rubert_cased_L-12_H-768_A-12_v1/rubert_pytorch.bin ~/pytorch-rubert/.\ncp ~/rubert_cased_L-12_H-768_A-12_v1/vocab.txt ~/pytorch-rubert/.\ncp ~/rubert_cased_L-12_H-768_A-12_v1/bert_config.json ~/pytorch-rubert/config.json\n```\n\nYou 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](src/pytorch_transformers_example.py)):\n\n```\nimport torch\nfrom pytorch_transformers import *\nfrom pathlib import Path\n\nsample_text = \"Рад познакомиться с вами.\"\nmy_model_dir = str(Path.home() / \"pytorch-rubert\")\n\ntokenizer = BertTokenizer.from_pretrained(my_model_dir)\nmodel = BertModel.from_pretrained(my_model_dir, output_hidden_states=True)\n\ninput_ids = torch.tensor([tokenizer.encode(sample_text, add_special_tokens=True)])\nprint(f\"Input ids: {input_ids}\")\nwith torch.no_grad():\n    last_hidden_states = model(input_ids)[0]\n    print(f\"Shape of last hidden states: {last_hidden_states.shape}\")\n    print(last_hidden_states)\n```\n\n### Convert the pytorch-transformer model to a spaCy package\n\nIn order to create a spaCy package of the PyTorch model, it first has to be saved to disk\nas a serialized pipeline. First, create the directory in which to save the pipeline, then run\nthe [script](src/serialize_spacy_nlp_pipeline.py) for serializing and saving it.\n\n```\nmkdir ~/spacy-rubert\npython3 -m src.serialize_spacy_nlp_pipeline\n```\n\nYou now have all you need to create a spaCy package in `~/spacy-rubert`. \n\n**OPTIONAL:** fill in the appropriate information in `~/spacy-rubert/meta.json` \nbefore proceeding.\n\nRun the following commands to create a spaCy package from the serialized pipeline and save it to `~/spacy-rubert-package`:\n\n```\nmkdir ~/spacy-rubert-package\npython3 -m spacy package ~/spacy-rubert ~/spacy-rubert-package\n```\n**NOTE:** that the name of the model directory under `~/spacy-rubert-package` depends on the \ninformation you supplied in `~/spacy-rubert/meta.json` in the previous step. The name used below\noriginates from a raw `meta.json` file.\n```\ncd ~/spacy-rubert-package/ru_model-0.0.0\npython3 setup.py sdist\n```\n\nAfter successful completion of the above commands, the RuBERT model is available as a spaCy package in:\n\n```\n~/spacy-rubert-package/ru_model-0.0.0/dist/ru_model-0.0.0.tar.gz\n```\n\nInstall it with:\n\n```\npip3 install ~/spacy-rubert-package/ru_model-0.0.0/dist/ru_model-0.0.0.tar.gz\n```\n\nVerify its presence in the current virtualenv:\n\n```\npip3 freeze | grep ru-model\n\u003e ru-model==0.0.0\n```\n\nHere is an example of how the package can be loaded and used ([source](src/spacy_example.py)):\n\n```\nimport spacy\n\nnlp = spacy.load(\"ru_model\")\ndoc = nlp(\"Рад познакомиться с вами.\")\nprint(doc.vector)\nprint(doc[0].similarity(doc[0]))\nprint(doc[0].similarity(doc[1]))\n```\n\n**NOTE:** that the above example does not make use of a GPU. For that to happen, \nyou need a different installation of spaCy than the one specified in the `requirements.txt`\nin this repository.\n \n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredriko%2Fbert-tensorflow-pytorch-spacy-conversion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffredriko%2Fbert-tensorflow-pytorch-spacy-conversion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredriko%2Fbert-tensorflow-pytorch-spacy-conversion/lists"}