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,video]1# running with trained model
2python3 -u run.py1from ding.bonus import PPOF
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").cfg_dict)
9# Instantiate the agent
10agent = PPOF(
11 env_id="LunarLander-v2", exp_name="LunarLander-v2-PPO", 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 PPOF
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-PPO")
6# Instantiate the agent
7agent = PPOF(
8 env_id="LunarLander-v2", exp_name="LunarLander-v2-PPO", 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 PPOF
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = PPOF(env_id="LunarLander-v2", exp_name="LunarLander-v2-PPO")
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="PPO",
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,video]",
19 usage_file_by_git_clone="./ppo/lunarlander_ppo_deploy.py",
20 usage_file_by_huggingface_ding="./ppo/lunarlander_ppo_download.py",
21 train_file="./ppo/lunarlander_ppo.py",
22 repo_id="OpenDILabCommunity/LunarLander-v2-PPO",
23 create_repo=False
24)
251exp_config = {
2 'type': 'ppo',
3 'on_policy': True,
4 'cuda': True,
5 'action_space': 'discrete',
6 'discount_factor': 0.99,
7 'gae_lambda': 0.95,
8 'epoch_per_collect': 10,
9 'batch_size': 64,
10 'learning_rate': 0.0003,
11 'lr_scheduler': None,
12 'weight_decay': 0,
13 'value_weight': 0.5,
14 'entropy_weight': 0.001,
15 'clip_ratio': 0.2,
16 'adv_norm': True,
17 'value_norm': 'popart',
18 'ppo_param_init': True,
19 'grad_norm': 0.5,
20 'n_sample': 512,
21 'unroll_len': 1,
22 'deterministic_eval': True,
23 'model': {},
24 'cfg_type': 'PPOFPolicyDict',
25 'env_id': 'LunarLander-v2',
26 'exp_name': 'LunarLander-v2-PPO'
27}
28