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 TD3Agent
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 = TD3Agent(
11 env_id="LunarLanderContinuous-v2",
12 exp_name="LunarLander-v2-TD3",
13 cfg=cfg.exp_config,
14 policy_state_dict=policy_state_dict
15)
16# Continue training
17agent.train(step=5000)
18# Render the new agent performance
19agent.deploy(enable_save_replay=True)
201# running with trained model
2python3 -u run.py1from ding.bonus import TD3Agent
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-TD3")
6# Instantiate the agent
7agent = TD3Agent(
8 env_id="LunarLanderContinuous-v2",
9 exp_name="LunarLander-v2-TD3",
10 cfg=cfg.exp_config,
11 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#Training Your Own Agent
2python3 -u train.py1from ding.bonus import TD3Agent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = TD3Agent(env_id="LunarLanderContinuous-v2", exp_name="LunarLander-v2-TD3")
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="TD3",
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/td3.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="./td3/lunarlander_td3_deploy.py",
20 usage_file_by_huggingface_ding="./td3/lunarlander_td3_download.py",
21 train_file="./td3/lunarlander_td3.py",
22 repo_id="OpenDILabCommunity/LunarLander-v2-TD3",
23 create_repo=False
24)
251exp_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 'env_id': 'LunarLanderContinuous-v2',
16 'collector_env_num': 4,
17 'evaluator_env_num': 8,
18 'act_scale': True
19 },
20 'policy': {
21 'model': {
22 'twin_critic': True,
23 'obs_shape': 8,
24 'action_shape': 2,
25 'action_space': 'regression'
26 },
27 'learn': {
28 'learner': {
29 'train_iterations': 1000000000,
30 'dataloader': {
31 'num_workers': 0
32 },
33 'log_policy': True,
34 'hook': {
35 'load_ckpt_before_run': '',
36 'log_show_after_iter': 100,
37 'save_ckpt_after_iter': 10000,
38 'save_ckpt_after_run': True
39 },
40 'cfg_type': 'BaseLearnerDict'
41 },
42 'update_per_collect': 256,
43 'batch_size': 256,
44 'learning_rate_actor': 0.0003,
45 'learning_rate_critic': 0.001,
46 'ignore_done': False,
47 'target_theta': 0.005,
48 'discount_factor': 0.99,
49 'actor_update_freq': 2,
50 'noise': True,
51 'noise_sigma': 0.1,
52 'noise_range': {
53 'min': -0.5,
54 'max': 0.5
55 }
56 },
57 'collect': {
58 'collector': {},
59 'unroll_len': 1,
60 'noise_sigma': 0.1,
61 'n_sample': 256
62 },
63 'eval': {
64 'evaluator': {
65 'eval_freq': 1000,
66 'render': {
67 'render_freq': -1,
68 'mode': 'train_iter'
69 },
70 'figure_path': None,
71 'cfg_type': 'InteractionSerialEvaluatorDict',
72 'stop_value': 240,
73 'n_episode': 8
74 }
75 },
76 'other': {
77 'replay_buffer': {
78 'replay_buffer_size': 100000
79 }
80 },
81 'on_policy': False,
82 'cuda': True,
83 'multi_gpu': False,
84 'bp_update_sync': True,
85 'traj_len_inf': False,
86 'type': 'td3',
87 'priority': False,
88 'priority_IS_weight': False,
89 'random_collect_size': 10000,
90 'transition_with_policy_data': False,
91 'action_space': 'continuous',
92 'reward_batch_norm': False,
93 'multi_agent': False,
94 'cfg_type': 'TD3PolicyDict'
95 },
96 'exp_name': 'LunarLander-v2-TD3',
97 'seed': 0,
98 'wandb_logger': {
99 'gradient_logger': True,
100 'video_logger': True,
101 'plot_logger': True,
102 'action_logger': True,
103 'return_logger': False
104 }
105}
106