{"id":15601044,"url":"https://github.com/lucidrains/mlm-pytorch","last_synced_at":"2025-07-20T16:32:25.145Z","repository":{"id":47574207,"uuid":"287613872","full_name":"lucidrains/mlm-pytorch","owner":"lucidrains","description":"An implementation of masked language modeling for Pytorch, made as concise and simple as possible","archived":false,"fork":false,"pushed_at":"2023-08-09T13:49:37.000Z","size":19,"stargazers_count":179,"open_issues_count":3,"forks_count":24,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-31T11:04:09.984Z","etag":null,"topics":["artificial-intelligence","deep-learning","pre-training","transformers","unsupervised-learning"],"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/lucidrains.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-14T19:56:51.000Z","updated_at":"2025-03-30T10:12:54.000Z","dependencies_parsed_at":"2024-10-23T02:02:58.851Z","dependency_job_id":null,"html_url":"https://github.com/lucidrains/mlm-pytorch","commit_stats":{"total_commits":13,"total_committers":2,"mean_commits":6.5,"dds":0.07692307692307687,"last_synced_commit":"1569ae2ad67fc4c973bcae00ac01b3eaae9fe2a2"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fmlm-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fmlm-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fmlm-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidrains%2Fmlm-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidrains","download_url":"https://codeload.github.com/lucidrains/mlm-pytorch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247648977,"owners_count":20972945,"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":["artificial-intelligence","deep-learning","pre-training","transformers","unsupervised-learning"],"created_at":"2024-10-03T02:12:56.518Z","updated_at":"2025-04-07T12:06:10.988Z","avatar_url":"https://github.com/lucidrains.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## MLM (Masked Language Modeling) Pytorch\n\nThis repository allows you to quickly setup unsupervised training for your transformer off a corpus of sequence data.\n\n## Install\n\n```bash\n$ pip install mlm-pytorch\n```\n\n## Usage\n\nFirst `pip install x-transformers`, then run the following example to see what one iteration of the unsupervised training is like\n\n```python\nimport torch\nfrom torch import nn\nfrom torch.optim import Adam\nfrom mlm_pytorch import MLM\n\n# instantiate the language model\n\nfrom x_transformers import TransformerWrapper, Encoder\n\ntransformer = TransformerWrapper(\n    num_tokens = 20000,\n    max_seq_len = 1024,\n    attn_layers = Encoder(\n        dim = 512,\n        depth = 6,\n        heads = 8\n    )\n)\n\n# plugin the language model into the MLM trainer\n\ntrainer = MLM(\n    transformer,\n    mask_token_id = 2,          # the token id reserved for masking\n    pad_token_id = 0,           # the token id for padding\n    mask_prob = 0.15,           # masking probability for masked language modeling\n    replace_prob = 0.90,        # ~10% probability that token will not be masked, but included in loss, as detailed in the epaper\n    mask_ignore_token_ids = []  # other tokens to exclude from masking, include the [cls] and [sep] here\n).cuda()\n\n# optimizer\n\nopt = Adam(trainer.parameters(), lr=3e-4)\n\n# one training step (do this for many steps in a for loop, getting new `data` each time)\n\ndata = torch.randint(0, 20000, (8, 1024)).cuda()\n\nloss = trainer(data)\nloss.backward()\nopt.step()\nopt.zero_grad()\n\n# after much training, the model should have improved for downstream tasks\n\ntorch.save(transformer, f'./pretrained-model.pt')\n```\n\nDo the above for many steps, and your model should improve.\n\n## Citation\n\n```bibtex\n@misc{devlin2018bert,\n    title   = {BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding},\n    author  = {Jacob Devlin and Ming-Wei Chang and Kenton Lee and Kristina Toutanova},\n    year    = {2018},\n    eprint  = {1810.04805},\n    archivePrefix = {arXiv},\n    primaryClass = {cs.CL}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmlm-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidrains%2Fmlm-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidrains%2Fmlm-pytorch/lists"}