| 19 | |
| 20 | |
| 21 | def build_actor(state_dict, device): |
| 22 | keys = sorted( |
| 23 | [k for k in state_dict if k.startswith("mlp.") and k.endswith(".weight")], |
| 24 | key=lambda k: int(k.split(".")[1]), |
| 25 | ) |
| 26 | mods = [] |
| 27 | for i, k in enumerate(keys): |
| 28 | w = state_dict[k] |
| 29 | mods.append(nn.Linear(w.shape[1], w.shape[0])) |
| 30 | if i < len(keys) - 1: |
| 31 | mods.append(nn.ELU()) |
| 32 | actor = nn.Sequential(*mods).to(device) |
| 33 | actor.load_state_dict( |
| 34 | {k[len("mlp.") :]: v for k, v in state_dict.items() if k.startswith("mlp.")}, |
| 35 | strict=False, |
| 36 | ) |
| 37 | actor.eval() |
| 38 | return actor |
| 39 | |
| 40 | |
| 41 | def main(): |