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 SACAgent
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 = SACAgent(
11 env_id="BipedalWalker-v3", exp_name="BipedalWalker-v3-SAC", 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 SACAgent
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-SAC")
6# Instantiate the agent
7agent = SACAgent(
8 env_id="BipedalWalker-v3",
9 exp_name="BipedalWalker-v3-SAC",
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 SACAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = SACAgent(env_id="BipedalWalker-v3", exp_name="BipedalWalker-v3-SAC")
6# Train the agent
7return_ = agent.train(step=int(200000))
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="SAC",
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/sac.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="./sac/bipedalwalker_sac_deploy.py",
20 usage_file_by_huggingface_ding="./sac/bipedalwalker_sac_download.py",
21 train_file="./sac/bipedalwalker_sac.py",
22 repo_id="OpenDILabCommunity/BipedalWalker-v3-SAC",
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': 10000000000,
14 'n_evaluator_episode': 5,
15 'env_id': 'BipedalWalker-v3',
16 'collector_env_num': 8,
17 'evaluator_env_num': 5,
18 'act_scale': True,
19 'rew_clip': True
20 },
21 'policy': {
22 'model': {
23 'twin_critic': True,
24 'action_space': 'reparameterization',
25 'obs_shape': 24,
26 'action_shape': 4,
27 'actor_head_hidden_size': 128,
28 'critic_head_hidden_size': 128
29 },
30 'learn': {
31 'learner': {
32 'train_iterations': 1000000000,
33 'dataloader': {
34 'num_workers': 0
35 },
36 'log_policy': True,
37 'hook': {
38 'load_ckpt_before_run': '',
39 'log_show_after_iter': 1000,
40 'save_ckpt_after_iter': 10000,
41 'save_ckpt_after_run': True
42 },
43 'cfg_type': 'BaseLearnerDict'
44 },
45 'update_per_collect': 64,
46 'batch_size': 256,
47 'learning_rate_q': 0.0003,
48 'learning_rate_policy': 0.0003,
49 'learning_rate_alpha': 0.0003,
50 'target_theta': 0.005,
51 'discount_factor': 0.99,
52 'alpha': 0.2,
53 'auto_alpha': True,
54 'log_space': True,
55 'target_entropy': None,
56 'ignore_done': False,
57 'init_w': 0.003
58 },
59 'collect': {
60 'collector': {},
61 'n_sample': 64,
62 'unroll_len': 1,
63 'collector_logit': False
64 },
65 'eval': {
66 'evaluator': {
67 'eval_freq': 1000,
68 'render': {
69 'render_freq': -1,
70 'mode': 'train_iter'
71 },
72 'figure_path': None,
73 'cfg_type': 'InteractionSerialEvaluatorDict',
74 'stop_value': 10000000000,
75 'n_episode': 5
76 }
77 },
78 'other': {
79 'replay_buffer': {
80 'replay_buffer_size': 300000
81 }
82 },
83 'on_policy': False,
84 'cuda': True,
85 'multi_gpu': False,
86 'bp_update_sync': True,
87 'traj_len_inf': False,
88 'type': 'sac',
89 'priority': False,
90 'priority_IS_weight': False,
91 'random_collect_size': 10000,
92 'transition_with_policy_data': True,
93 'multi_agent': False,
94 'cfg_type': 'SACPolicyDict'
95 },
96 'exp_name': 'BipedalWalker-v3-SAC',
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