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 A2CAgent
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 = A2CAgent(
11 env="lunarlander_discrete", exp_name="Lunarlander-v2-A2C", 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 A2CAgent
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-A2C")
6# Instantiate the agent
7agent = A2CAgent(
8 env="lunarlander_discrete", exp_name="Lunarlander-v2-A2C", 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 A2CAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = A2CAgent(env="lunarlander_discrete", exp_name="Lunarlander-v2-A2C")
6# Train the agent
7return_ = agent.train(step=int(20000000), collector_env_num=8, evaluator_env_num=8, debug=False)
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="A2C",
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/a2c.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="./a2c/lunarlander_a2c_deploy.py",
20 usage_file_by_huggingface_ding="./a2c/lunarlander_a2c_download.py",
21 train_file="./a2c/lunarlander_a2c.py",
22 repo_id="OpenDILabCommunity/Lunarlander-v2-A2C"
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 'collector_env_num': 8,
15 'evaluator_env_num': 8,
16 'env_id': 'LunarLander-v2',
17 'n_evaluator_episode': 8
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': 1,
40 'batch_size': 160,
41 'learning_rate': 0.0003,
42 'betas': [0.9, 0.999],
43 'eps': 1e-08,
44 'grad_norm': 0.5,
45 'value_weight': 0.5,
46 'entropy_weight': 0.001,
47 'adv_norm': True,
48 'ignore_done': False
49 },
50 'collect': {
51 'collector': {},
52 'unroll_len': 1,
53 'discount_factor': 0.99,
54 'gae_lambda': 0.95,
55 'n_sample': 320
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 'n_episode': 8,
66 'stop_value': 240
67 }
68 },
69 'other': {
70 'replay_buffer': {}
71 },
72 'on_policy': True,
73 'cuda': True,
74 'multi_gpu': False,
75 'bp_update_sync': True,
76 'traj_len_inf': False,
77 'type': 'a2c',
78 'priority': False,
79 'priority_IS_weight': False,
80 'cfg_type': 'A2CPolicyDict'
81 },
82 'exp_name': 'Lunarlander-v2-A2C',
83 'wandb_logger': {
84 'gradient_logger': True,
85 'video_logger': True,
86 'plot_logger': True,
87 'action_logger': True,
88 'return_logger': False
89 },
90 'seed': 0
91}
92