Views
No views yet
1# install huggingface_ding
2git clone https://github.com/opendilab/huggingface_ding.git
3pip3 install -e ./huggingface_ding/
4# install environment dependencies if needed
5pip3 install DI-engine[common_env]1# running with trained model
2python3 -u run.py1from ding.bonus import PPOOffPolicyAgent
2from ding.config import Config
3from easydict import EasyDict
4import torch
5
6# Pull model from files which are git cloned from huggingface
7policy_state_dict = torch.load("pytorch_model.bin", map_location=torch.device("cpu"))
8cfg = EasyDict(Config.file_to_dict("policy_config.py"))
9# Instantiate the agent
10agent = PPOOffPolicyAgent(
11 env="lunarlander_discrete", exp_name="LunarLander-v2-PPOOffPolicy", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
12)
13# Continue training
14agent.train(step=5000)
15# Render the new agent performance
16agent.deploy(enable_save_replay=True)
171# running with trained model
2python3 -u run.py1from ding.bonus import PPOOffPolicyAgent
2from huggingface_ding import pull_model_from_hub
3
4# Pull model from Hugggingface hub
5policy_state_dict, cfg = pull_model_from_hub(repo_id="OpenDILabCommunity/LunarLander-v2-PPOOffPolicy")
6# Instantiate the agent
7agent = PPOOffPolicyAgent(
8 env="lunarlander_discrete", exp_name="LunarLander-v2-PPOOffPolicy", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
9)
10# Continue training
11agent.train(step=5000)
12# Render the new agent performance
13agent.deploy(enable_save_replay=True)
141#Training Your Own Agent
2python3 -u train.py1from ding.bonus import PPOOffPolicyAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = PPOOffPolicyAgent("lunarlander_discrete", exp_name="LunarLander-v2-PPOOffPolicy")
6# Train the agent
7return_ = agent.train(step=int(4000000), collector_env_num=4, evaluator_env_num=4)
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Box2d",
12 task_name="LunarLander-v2",
13 algo_name="PPOOffPolicy",
14 wandb_url=return_.wandb_url,
15 github_repo_url="https://github.com/opendilab/DI-engine",
16 github_doc_model_url="https://di-engine-docs.readthedocs.io/en/latest/12_policies/ppo.html",
17 github_doc_env_url="https://di-engine-docs.readthedocs.io/en/latest/13_envs/lunarlander.html",
18 installation_guide="pip3 install DI-engine[common_env]",
19 usage_file_by_git_clone="./ppo_offpolicy/lunarlander_ppo_offpolicy_deploy.py",
20 usage_file_by_huggingface_ding="./ppo_offpolicy/lunarlander_ppo_offpolicy_download.py",
21 train_file="./ppo_offpolicy/lunarlander_ppo_offpolicy.py",
22 repo_id="OpenDILabCommunity/LunarLander-v2-PPOOffPolicy"
23)
241exp_config = {
2 'env': {
3 'manager': {
4 'episode_num': float("inf"),
5 'max_retry': 1,
6 'retry_type': 'reset',
7 'auto_reset': True,
8 'step_timeout': None,
9 'reset_timeout': None,
10 'retry_waiting_time': 0.1,
11 'cfg_type': 'BaseEnvManagerDict'
12 },
13 'stop_value': 240,
14 'n_evaluator_episode': 8,
15 'collector_env_num': 8,
16 'evaluator_env_num': 8,
17 'env_id': 'LunarLander-v2'
18 },
19 'policy': {
20 'model': {
21 'obs_shape': 8,
22 'action_shape': 4
23 },
24 'learn': {
25 'learner': {
26 'train_iterations': 1000000000,
27 'dataloader': {
28 'num_workers': 0
29 },
30 'log_policy': True,
31 'hook': {
32 'load_ckpt_before_run': '',
33 'log_show_after_iter': 100,
34 'save_ckpt_after_iter': 10000,
35 'save_ckpt_after_run': True
36 },
37 'cfg_type': 'BaseLearnerDict'
38 },
39 'update_per_collect': 4,
40 'batch_size': 64,
41 'learning_rate': 0.001,
42 'value_weight': 0.5,
43 'entropy_weight': 0.01,
44 'clip_ratio': 0.2,
45 'adv_norm': True,
46 'ignore_done': False,
47 'nstep': 1,
48 'nstep_return': False
49 },
50 'collect': {
51 'collector': {},
52 'unroll_len': 1,
53 'discount_factor': 0.99,
54 'gae_lambda': 0.95,
55 'n_sample': 128
56 },
57 'eval': {
58 'evaluator': {
59 'eval_freq': 1000,
60 'render': {
61 'render_freq': -1,
62 'mode': 'train_iter'
63 },
64 'cfg_type': 'InteractionSerialEvaluatorDict',
65 'stop_value': 240,
66 'n_episode': 8
67 }
68 },
69 'other': {
70 'replay_buffer': {
71 'replay_buffer_size': 10000
72 }
73 },
74 'on_policy': False,
75 'cuda': True,
76 'multi_gpu': False,
77 'bp_update_sync': True,
78 'traj_len_inf': False,
79 'type': 'ppo',
80 'priority': False,
81 'priority_IS_weight': False,
82 'nstep_return': False,
83 'nstep': 3,
84 'transition_with_policy_data': True,
85 'cfg_type': 'PPOOffPolicyDict'
86 },
87 'exp_name': 'LunarLander-v2-PPOOffPolicy',
88 'wandb_logger': {
89 'gradient_logger': True,
90 'video_logger': True,
91 'plot_logger': True,
92 'action_logger': True,
93 'return_logger': False
94 },
95 'seed': 0
96}
97