{"id":13534935,"url":"https://github.com/nghuyong/ERNIE-Pytorch","last_synced_at":"2025-04-02T00:31:20.257Z","repository":{"id":38261547,"uuid":"186358163","full_name":"nghuyong/ERNIE-Pytorch","owner":"nghuyong","description":"ERNIE Pytorch Version","archived":false,"fork":false,"pushed_at":"2023-07-26T22:31:18.000Z","size":60,"stargazers_count":926,"open_issues_count":7,"forks_count":120,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-03-28T00:06:35.723Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nghuyong.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-05-13T06:25:20.000Z","updated_at":"2025-03-26T06:04:41.000Z","dependencies_parsed_at":"2024-01-07T05:34:14.523Z","dependency_job_id":"1ad4e96d-204a-49d1-8a84-d7e3f6b61777","html_url":"https://github.com/nghuyong/ERNIE-Pytorch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nghuyong%2FERNIE-Pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nghuyong%2FERNIE-Pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nghuyong%2FERNIE-Pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nghuyong%2FERNIE-Pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nghuyong","download_url":"https://codeload.github.com/nghuyong/ERNIE-Pytorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246735041,"owners_count":20825212,"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":[],"created_at":"2024-08-01T08:00:47.128Z","updated_at":"2025-04-02T00:31:20.247Z","avatar_url":"https://github.com/nghuyong.png","language":"Python","funding_links":[],"categories":["implement of BERT besides tensorflow:","NLU系列"],"sub_categories":["ERNIE"],"readme":"\u003ch1 align=\"center\"\u003eERNIE-Pytorch\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003eThis project is to convert ERNIE from paddlepaddle to huggingface's format (in Pytorch).\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/nghuyong/ERNIE-Pytorch/stargazers\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/stars/nghuyong/ERNIE-Pytorch.svg?colorA=orange\u0026colorB=orange\u0026logo=github\"\n         alt=\"GitHub stars\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/nghuyong/ERNIE-Pytorch/issues\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/issues/nghuyong/ERNIE-Pytorch.svg\"\n             alt=\"GitHub issues\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/nghuyong/ERNIE-Pytorch/\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/last-commit/nghuyong/ERNIE-Pytorch.svg\"\u003e\n  \u003c/a\u003e\n   \u003ca href=\"https://github.com/nghuyong/ERNIE-Pytorch/blob/master/LICENSE\"\u003e\n        \u003cimg src=\"https://img.shields.io/github/license/nghuyong/ERNIE-Pytorch.svg\"\u003e\n  \u003c/a\u003e\n  \n\u003c/p\u003e\n\n\n\n**News: ERNIE has been merged\ninto [huggingface/transformers@v4.22.0](https://github.com/huggingface/transformers/releases/tag/v4.22.0) !!**\n\n\n## Get Started\n\n```\npip install --upgrade transformers\n```\n\nTake `ernie-1.0-base-zh` as an example:\n\n```Python\nfrom transformers import BertTokenizer, ErnieModel\n\ntokenizer = BertTokenizer.from_pretrained(\"nghuyong/ernie-1.0-base-zh\")\nmodel = ErnieModel.from_pretrained(\"nghuyong/ernie-1.0-base-zh\")\n```\n\n### Supported Models\n\n|     Model Name      | Language |           Description           |\n|:-------------------:|:--------:|:-------------------------------:|\n|  ernie-1.0-base-zh  | Chinese  | Layer:12, Heads:12, Hidden:768  |\n|  ernie-2.0-base-en  | English  | Layer:12, Heads:12, Hidden:768  |\n| ernie-2.0-large-en  | English  | Layer:24, Heads:16, Hidden:1024 |\n| ernie-3.0-xbase-zh  | Chinese  | Layer:20, Heads:16, Hidden:1024 |\n|  ernie-3.0-base-zh  | Chinese  | Layer:12, Heads:12, Hidden:768  |\n| ernie-3.0-medium-zh | Chinese  |  Layer:6, Heads:12, Hidden:768  |\n|  ernie-3.0-mini-zh  | Chinese  |  Layer:6, Heads:12, Hidden:384  |\n| ernie-3.0-micro-zh  | Chinese  |  Layer:4, Heads:12, Hidden:384  |\n|  ernie-3.0-nano-zh  | Chinese  |  Layer:4, Heads:12, Hidden:312  |\n|   ernie-health-zh   | Chinese  | Layer:12, Heads:12, Hidden:768  |\n|    ernie-gram-zh    | Chinese  | Layer:12, Heads:12, Hidden:768  |\n\nYou can find all the supported models from huggingface's model\nhub: [huggingface.co/nghuyong](https://huggingface.co/nghuyong),\nand model details from paddle's official\nrepo: [PaddleNLP](https://paddlenlp.readthedocs.io/zh/latest/model_zoo/transformers/ERNIE/contents.html)\nand [ERNIE](https://github.com/PaddlePaddle/ERNIE/blob/repro).\n\n## Details\n\n\u003cdetails\u003e\n    \u003csummary\u003eI want to convert the model from paddle version by myself 😉\u003c/summary\u003e\n\n\nThe following will take `ernie-1.0-base-zh` as an example to show how to convert.\n\n1. Download the paddle-paddle version ERNIE model. Execute the following code\n  ```\n  import paddlenlp\n  tokenizer = paddlenlp.transformers.ErnieTokenizer.from_pretrained(\"ernie-1.0-base-zh\")\n  model = paddlenlp.transformers.ErnieForMaskedLM.from_pretrained(\"ernie-1.0-base-zh\")\n  ```\n  And then you will get the model in `~/.paddlenlp/models/ernie-1.0-base-zh/`, move to this project path.\n  \n2. ```pip install -r requirements.txt```\n3. ```python convert.py```\n4. Now, a folder named `convert` will be in the project path, and there will be three files in this\n   folder: `config.json`,`pytorch_model.bin` and `vocab.txt`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eI want to check the calculation results before and after model conversion 😁\u003c/summary\u003e\n\n```bash\npython test.py --task logit_check\n```\n\nYou will get the output:\n\n```output\nhuggingface result\npool output: [-1.         -1.          0.9981035  -0.9996652  -0.78173476 -1.          -0.9994901   0.97012603  0.85954666  0.9854131 ]\n\npaddle result\npool output: [-0.99999976 -0.99999976  0.9981028  -0.9996651  -0.7815545  -0.99999976  -0.9994898   0.97014064  0.8594844   0.985419  ]\n```\n\nIt can be seen that the result of our convert version is the same with the official paddlepaddle's version.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n    \u003csummary\u003eI want to reproduce the cloze test in ERNIE1.0's paper 😆\u003c/summary\u003e\n\n```bash\npython test.py --task cloze_check\n```\n\nYou will get the output:\n\n```bash\nhuggingface result\nprediction shape:\t torch.Size([47, 18000])\npredict result:\t ['西', '游', '记', '是', '中', '国', '神', '魔', '小', '说', '的', '经', '典', '之', '作', '，', '与', '《', '三', '国', '演', '义', '》', '《', '水', '浒', '传', '》', '《', '红', '楼', '梦', '》', '并', '称', '为', '中', '国', '古', '典', '四', '大', '名', '著', '。']\n[CLS] logit:\t [-15.693626 -19.522263 -10.429456 ... -11.800728 -12.253127 -14.375117]\n\npaddle result\nprediction shape:\t [47, 18000]\npredict result:\t ['西', '游', '记', '是', '中', '国', '神', '魔', '小', '说', '的', '经', '典', '之', '作', '，', '与', '《', '三', '国', '演', '义', '》', '《', '水', '浒', '传', '》', '《', '红', '楼', '梦', '》', '并', '称', '为', '中', '国', '古', '典', '四', '大', '名', '著', '。']\n[CLS] logit:\t [-15.693538 -19.521954 -10.429307 ... -11.800765 -12.253114 -14.375412]\n```\n\n\u003c/details\u003e\n\n## Citation\n\nIf you use this work in a scientific publication, I would appreciate that you can also cite the following BibTex entry:\n\n```latex\n@misc{nghuyong2019@ERNIE-Pytorch,\n  title={ERNIEPytorch},\n  author={Yong Hu},\n  howpublished={\\url{https://github.com/nghuyong/ERNIE-Pytorch}},\n  year={2019}\n}\n```\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnghuyong%2FERNIE-Pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnghuyong%2FERNIE-Pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnghuyong%2FERNIE-Pytorch/lists"}