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="bipedalwalker", exp_name="BipedalWalker-v3-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/BipedalWalker-v3-A2C")
6# Instantiate the agent
7agent = A2CAgent(
8 env="bipedalwalker",
9 exp_name="BipedalWalker-v3-A2C",
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 A2CAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = A2CAgent("bipedalwalker", exp_name="BipedalWalker-v3-A2C")
6# Train the agent
7return_ = agent.train(step=int(5000000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Box2d",
12 task_name="BipedalWalker-v3",
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/bipedalwalker.html",
18 installation_guide="pip3 install DI-engine[common_env]",
19 usage_file_by_git_clone="./a2c/bipedalwalker_a2c_deploy.py",
20 usage_file_by_huggingface_ding="./a2c/bipedalwalker_a2c_download.py",
21 train_file="./a2c/bipedalwalker_a2c.py",
22 repo_id="OpenDILabCommunity/BipedalWalker-v3-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': 10000000000,
14 'n_evaluator_episode': 8,
15 'env_id': 'BipedalWalker-v3',
16 'collector_env_num': 8,
17 'evaluator_env_num': 8,
18 'act_scale': True,
19 'rew_clip': True
20 },
21 'policy': {
22 'model': {
23 'action_space': 'continuous',
24 'obs_shape': 24,
25 'action_shape': 4
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': 1,
43 'batch_size': 64,
44 'learning_rate': 0.0003,
45 'betas': [0.9, 0.999],
46 'eps': 1e-08,
47 'grad_norm': 0.5,
48 'value_weight': 0.7,
49 'entropy_weight': 0.0005,
50 'adv_norm': True,
51 'ignore_done': False,
52 'discount_factor': 0.99
53 },
54 'collect': {
55 'collector': {},
56 'unroll_len': 1,
57 'discount_factor': 0.99,
58 'gae_lambda': 0.95,
59 'n_sample': 64
60 },
61 'eval': {
62 'evaluator': {
63 'eval_freq': 1000,
64 'render': {
65 'render_freq': -1,
66 'mode': 'train_iter'
67 },
68 'cfg_type': 'InteractionSerialEvaluatorDict',
69 'stop_value': 10000000000,
70 'n_episode': 8
71 }
72 },
73 'other': {
74 'replay_buffer': {}
75 },
76 'on_policy': True,
77 'cuda': True,
78 'multi_gpu': False,
79 'bp_update_sync': True,
80 'traj_len_inf': False,
81 'type': 'a2c',
82 'priority': False,
83 'priority_IS_weight': False,
84 'action_space': 'continuous',
85 'cfg_type': 'A2CPolicyDict'
86 },
87 'exp_name': 'BipedalWalker-v3-A2C',
88 'seed': 0,
89 'wandb_logger': {
90 'gradient_logger': True,
91 'video_logger': True,
92 'plot_logger': True,
93 'action_logger': True,
94 'return_logger': False
95 }
96}
97