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
5
6pip3 install DI-engine[common_env,video]
7pip3 install LightZero
81# running with trained model
2python3 -u run.py1from lzero.agent import GumbelMuZeroAgent
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 = GumbelMuZeroAgent(
11 env_id="CartPole-v0", exp_name="CartPole-v0-GumbelMuZero", 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 lzero.agent import GumbelMuZeroAgent
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/CartPole-v0-GumbelMuZero")
6# Instantiate the agent
7agent = GumbelMuZeroAgent(
8 env_id="CartPole-v0", exp_name="CartPole-v0-GumbelMuZero", 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 lzero.agent import GumbelMuZeroAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = GumbelMuZeroAgent(env_id="CartPole-v0", exp_name="CartPole-v0-GumbelMuZero")
6# Train the agent
7return_ = agent.train(step=int(10000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Box2d",
12 task_name="CartPole-v0",
13 algo_name="GumbelMuZero",
14 github_repo_url="https://github.com/opendilab/LightZero",
15 github_doc_model_url=None,
16 github_doc_env_url=None,
17 installation_guide='''
18pip3 install DI-engine[common_env,video]
19pip3 install LightZero
20''',
21 usage_file_by_git_clone="./gumbel_muzero/cartpole_gumbel_muzero_deploy.py",
22 usage_file_by_huggingface_ding="./gumbel_muzero/cartpole_gumbel_muzero_download.py",
23 train_file="./gumbel_muzero/cartpole_gumbel_muzero.py",
24 repo_id="OpenDILabCommunity/CartPole-v0-GumbelMuZero",
25 platform_info="[LightZero](https://github.com/opendilab/LightZero) and [DI-engine](https://github.com/opendilab/di-engine)",
26 model_description="**LightZero** is an efficient, easy-to-understand open-source toolkit that merges Monte Carlo Tree Search (MCTS) with Deep Reinforcement Learning (RL), simplifying their integration for developers and researchers. More details are in paper [LightZero: A Unified Benchmark for Monte Carlo Tree Search in General Sequential Decision Scenarios](https://huggingface.co/papers/2310.08348).",
27 create_repo=True
28)
291exp_config = {
2 'main_config': {
3 'exp_name': 'CartPole-v0-GumbelMuZero',
4 'seed': 0,
5 'env': {
6 'env_id': 'CartPole-v0',
7 'continuous': False,
8 'manually_discretization': False,
9 'collector_env_num': 8,
10 'evaluator_env_num': 3,
11 'n_evaluator_episode': 3,
12 'manager': {
13 'shared_memory': False
14 }
15 },
16 'policy': {
17 'on_policy': False,
18 'cuda': True,
19 'multi_gpu': False,
20 'bp_update_sync': True,
21 'traj_len_inf': False,
22 'model': {
23 'observation_shape': 4,
24 'action_space_size': 2,
25 'model_type': 'mlp',
26 'lstm_hidden_size': 128,
27 'latent_state_dim': 128,
28 'self_supervised_learning_loss': True,
29 'discrete_action_encoding_type': 'one_hot',
30 'norm_type': 'BN'
31 },
32 'use_rnd_model': False,
33 'sampled_algo': False,
34 'gumbel_algo': True,
35 'mcts_ctree': True,
36 'collector_env_num': 8,
37 'evaluator_env_num': 3,
38 'env_type': 'not_board_games',
39 'action_type': 'fixed_action_space',
40 'battle_mode': 'play_with_bot_mode',
41 'monitor_extra_statistics': True,
42 'game_segment_length': 50,
43 'transform2string': False,
44 'gray_scale': False,
45 'use_augmentation': False,
46 'augmentation': ['shift', 'intensity'],
47 'ignore_done': False,
48 'update_per_collect': 100,
49 'model_update_ratio': 0.1,
50 'batch_size': 256,
51 'optim_type': 'Adam',
52 'learning_rate': 0.003,
53 'target_update_freq': 100,
54 'target_update_freq_for_intrinsic_reward': 1000,
55 'weight_decay': 0.0001,
56 'momentum': 0.9,
57 'grad_clip_value': 10,
58 'n_episode': 8,
59 'num_simulations': 25,
60 'discount_factor': 0.997,
61 'td_steps': 5,
62 'num_unroll_steps': 5,
63 'reward_loss_weight': 1,
64 'value_loss_weight': 0.25,
65 'policy_loss_weight': 1,
66 'policy_entropy_loss_weight': 0,
67 'ssl_loss_weight': 2,
68 'lr_piecewise_constant_decay': False,
69 'threshold_training_steps_for_final_lr': 50000,
70 'manual_temperature_decay': False,
71 'threshold_training_steps_for_final_temperature': 100000,
72 'fixed_temperature_value': 0.25,
73 'use_ture_chance_label_in_chance_encoder': False,
74 'use_priority': True,
75 'priority_prob_alpha': 0.6,
76 'priority_prob_beta': 0.4,
77 'root_dirichlet_alpha': 0.3,
78 'root_noise_weight': 0.25,
79 'random_collect_episode_num': 0,
80 'eps': {
81 'eps_greedy_exploration_in_collect': False,
82 'type': 'linear',
83 'start': 1.0,
84 'end': 0.05,
85 'decay': 100000
86 },
87 'cfg_type': 'GumbelMuZeroPolicyDict',
88 'max_num_considered_actions': 2,
89 'reanalyze_ratio': 0,
90 'eval_freq': 200,
91 'replay_buffer_size': 1000000
92 },
93 'wandb_logger': {
94 'gradient_logger': False,
95 'video_logger': False,
96 'plot_logger': False,
97 'action_logger': False,
98 'return_logger': False
99 }
100 },
101 'create_config': {
102 'env': {
103 'type':
104 'cartpole_lightzero',
105 'import_names':
106 ['zoo.classic_control.cartpole.envs.cartpole_lightzero_env']
107 },
108 'env_manager': {
109 'type': 'subprocess'
110 },
111 'policy': {
112 'type': 'gumbel_muzero',
113 'import_names': ['lzero.policy.gumbel_muzero']
114 }
115 }
116}
117