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="PongNoFrameskip", exp_name="PongNoFrameskip-v4-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/PongNoFrameskip-v4-PPOOffPolicy")
6# Instantiate the agent
7agent = PPOOffPolicyAgent(
8 env="PongNoFrameskip", exp_name="PongNoFrameskip-v4-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(env="PongNoFrameskip", exp_name="PongNoFrameskip-v4-PPOOffPolicy")
6# Train the agent
7return_ = agent.train(step=int(10000000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Atari",
12 task_name="PongNoFrameskip-v4",
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/atari.html",
18 installation_guide="pip3 install DI-engine[common_env]",
19 usage_file_by_git_clone="./ppo_offpolicy/pong_ppo_offpolicy_deploy.py",
20 usage_file_by_huggingface_ding="./ppo_offpolicy/pong_ppo_offpolicy_download.py",
21 train_file="./ppo_offpolicy/pong_ppo_offpolicy.py",
22 repo_id="OpenDILabCommunity/PongNoFrameskip-v4-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': 20,
14 'n_evaluator_episode': 8,
15 'collector_env_num': 8,
16 'evaluator_env_num': 8,
17 'env_id': 'PongNoFrameskip-v4',
18 'frame_stack': 4
19 },
20 'policy': {
21 'model': {
22 'obs_shape': [4, 84, 84],
23 'action_shape': 6,
24 'action_space': 'discrete',
25 'encoder_hidden_size_list': [64, 64, 128],
26 'actor_head_hidden_size': 128,
27 'critic_head_hidden_size': 128
28 },
29 'learn': {
30 'learner': {
31 'train_iterations': 1000000000,
32 'dataloader': {
33 'num_workers': 0
34 },
35 'log_policy': True,
36 'hook': {
37 'load_ckpt_before_run': '',
38 'log_show_after_iter': 100,
39 'save_ckpt_after_iter': 10000,
40 'save_ckpt_after_run': True
41 },
42 'cfg_type': 'BaseLearnerDict'
43 },
44 'update_per_collect': 10,
45 'batch_size': 320,
46 'learning_rate': 0.0003,
47 'value_weight': 0.5,
48 'entropy_weight': 0.001,
49 'clip_ratio': 0.2,
50 'adv_norm': True,
51 'ignore_done': False,
52 'grad_clip_type': 'clip_norm',
53 'grad_clip_value': 0.5
54 },
55 'collect': {
56 'collector': {},
57 'unroll_len': 1,
58 'discount_factor': 0.99,
59 'gae_lambda': 0.95,
60 'n_sample': 3200
61 },
62 'eval': {
63 'evaluator': {
64 'eval_freq': 1000,
65 'render': {
66 'render_freq': -1,
67 'mode': 'train_iter'
68 },
69 'cfg_type': 'InteractionSerialEvaluatorDict',
70 'stop_value': 20,
71 'n_episode': 8
72 }
73 },
74 'other': {
75 'replay_buffer': {
76 'replay_buffer_size': 10000
77 }
78 },
79 'on_policy': False,
80 'cuda': True,
81 'multi_gpu': False,
82 'bp_update_sync': True,
83 'traj_len_inf': False,
84 'type': 'ppo',
85 'priority': False,
86 'priority_IS_weight': False,
87 'nstep_return': False,
88 'nstep': 3,
89 'transition_with_policy_data': True,
90 'cfg_type': 'PPOOffPolicyDict',
91 'recompute_adv': True,
92 'action_space': 'discrete'
93 },
94 'exp_name': 'PongNoFrameskip-v4-PPOOffPolicy',
95 'wandb_logger': {
96 'gradient_logger': True,
97 'video_logger': True,
98 'plot_logger': True,
99 'action_logger': True,
100 'return_logger': False
101 },
102 'seed': 0
103}
104