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 C51Agent
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 = C51Agent(
11 env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-C51", 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 C51Agent
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/QbertNoFrameskip-v4-C51")
6# Instantiate the agent
7agent = C51Agent(
8 env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-C51", 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 C51Agent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = C51Agent(env_id="QbertNoFrameskip-v4", exp_name="QbertNoFrameskip-v4-C51")
6# Train the agent
7return_ = agent.train(step=int(20000000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Atari",
12 task_name="QbertNoFrameskip-v4",
13 algo_name="C51",
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/c51.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="./c51/qbert_c51_deploy.py",
20 usage_file_by_huggingface_ding="./c51/qbert_c51_download.py",
21 train_file="./c51/qbert_c51.py",
22 repo_id="OpenDILabCommunity/QbertNoFrameskip-v4-C51",
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': 30000,
14 'n_evaluator_episode': 8,
15 'collector_env_num': 8,
16 'evaluator_env_num': 8,
17 'env_id': 'QbertNoFrameskip-v4',
18 'frame_stack': 4,
19 'env_wrapper': 'atari_default'
20 },
21 'policy': {
22 'model': {
23 'encoder_hidden_size_list': [128, 128, 512],
24 'v_min': -10,
25 'v_max': 10,
26 'n_atom': 51,
27 'obs_shape': [4, 84, 84],
28 'action_shape': 6
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': 100,
40 'save_ckpt_after_iter': 10000,
41 'save_ckpt_after_run': True
42 },
43 'cfg_type': 'BaseLearnerDict'
44 },
45 'update_per_collect': 10,
46 'batch_size': 32,
47 'learning_rate': 0.0001,
48 'target_update_freq': 500,
49 'target_theta': 0.005,
50 'ignore_done': False
51 },
52 'collect': {
53 'collector': {},
54 'n_sample': 100,
55 'unroll_len': 1
56 },
57 'eval': {
58 'evaluator': {
59 'eval_freq': 4000,
60 'render': {
61 'render_freq': -1,
62 'mode': 'train_iter'
63 },
64 'figure_path': None,
65 'cfg_type': 'InteractionSerialEvaluatorDict',
66 'stop_value': 30000,
67 'n_episode': 8
68 }
69 },
70 'other': {
71 'replay_buffer': {
72 'replay_buffer_size': 400000
73 },
74 'eps': {
75 'type': 'exp',
76 'start': 1.0,
77 'end': 0.05,
78 'decay': 1000000
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': 'c51',
87 'priority': True,
88 'priority_IS_weight': False,
89 'discount_factor': 0.99,
90 'nstep': 3,
91 'cfg_type': 'C51PolicyDict'
92 },
93 'exp_name': 'QbertNoFrameskip-v4-C51',
94 'seed': 0,
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}
103