{"id":20373535,"url":"https://github.com/megengine/cheat_sheet_for_pytorch_immigrant","last_synced_at":"2026-03-19T15:32:08.511Z","repository":{"id":103459048,"uuid":"474852807","full_name":"MegEngine/cheat_sheet_for_pytorch_immigrant","owner":"MegEngine","description":"一份给从 PyTorch 迁移过来的用户的小抄","archived":false,"fork":false,"pushed_at":"2022-03-28T05:19:20.000Z","size":2,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-15T06:50:32.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MegEngine.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":"2022-03-28T05:05:59.000Z","updated_at":"2022-04-06T02:19:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"7e37fffa-6980-4d6d-9b97-9571ffb1177d","html_url":"https://github.com/MegEngine/cheat_sheet_for_pytorch_immigrant","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/MegEngine%2Fcheat_sheet_for_pytorch_immigrant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fcheat_sheet_for_pytorch_immigrant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fcheat_sheet_for_pytorch_immigrant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MegEngine%2Fcheat_sheet_for_pytorch_immigrant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MegEngine","download_url":"https://codeload.github.com/MegEngine/cheat_sheet_for_pytorch_immigrant/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241921824,"owners_count":20042763,"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-11-15T01:18:52.133Z","updated_at":"2026-02-04T02:06:12.803Z","avatar_url":"https://github.com/MegEngine.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cheat Sheet for PyTorch Immigrant\n# 一份给从 PyTorch 迁移过来的用户的小抄\n\n## 如何从 PyTorch 迁移 weight 到 MegEngine 中？\n\n首先确保两边的 Module 结构是类似的，在 PyTorch 里保存 state_dict，然后在 MegEngine 中将这个 weight 载入即可。\n\n```python\n# 在 PyTorch 中保存权重\nimport pickle\nwith open('torch-weight.pkl', 'wb') as f:\n    states = net.state_dict()\n    weights = {k: v.numpy() for k, v in states.items()}\n    pickle.dump(weights, f)\n```\n\n```python\n# 在 MegEngine 中读取权重\nimport pickle\nwith open('torch-weight.pkl', 'rb') as f:\n    w = pickle.load(f)\nweights = {}\nfor k, v in w.items():\n    if k.endswith('bias') and v.ndim == 1:\n        v = v.reshape(1, -1, 1, 1)\n    weights[k] = v\n\nnet.load_state_dict(weights, strict=False)\n```\n\n在这个过程中可能会遇到一些 warning，可能是一些统计量没 load 成功之类的，一般问题不大。\n\n原始帖子：https://discuss.megengine.org.cn/t/topic/1243\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegengine%2Fcheat_sheet_for_pytorch_immigrant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegengine%2Fcheat_sheet_for_pytorch_immigrant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegengine%2Fcheat_sheet_for_pytorch_immigrant/lists"}