zjowowen commited on
Commit
eeac7cc
1 Parent(s): 92d4087

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +305 -0
README.md ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: en
3
+ license: apache-2.0
4
+ library_name: pytorch
5
+ tags:
6
+ - deep-reinforcement-learning
7
+ - reinforcement-learning
8
+ - DI-engine
9
+ - TicTacToe-play-with-bot
10
+ benchmark_name: OpenAI/Gym/Atari
11
+ task_name: TicTacToe-play-with-bot
12
+ pipeline_tag: reinforcement-learning
13
+ model-index:
14
+ - name: MuZero
15
+ results:
16
+ - task:
17
+ type: reinforcement-learning
18
+ name: reinforcement-learning
19
+ dataset:
20
+ name: TicTacToe-play-with-bot
21
+ type: TicTacToe-play-with-bot
22
+ metrics:
23
+ - type: mean_reward
24
+ value: 0.5 +/- 0.81
25
+ name: mean_reward
26
+ ---
27
+
28
+ # Play **TicTacToe-play-with-bot** with **MuZero** Policy
29
+
30
+ ## Model Description
31
+ <!-- Provide a longer summary of what this model is. -->
32
+
33
+ This implementation applies **MuZero** to the OpenAI/Gym/Atari **TicTacToe-play-with-bot** environment using [LightZero](https://github.com/opendilab/LightZero) and [DI-engine](https://github.com/opendilab/di-engine).
34
+
35
+ **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).
36
+
37
+ ## Model Usage
38
+ ### Install the Dependencies
39
+ <details close>
40
+ <summary>(Click for Details)</summary>
41
+
42
+ ```shell
43
+ # install huggingface_ding
44
+ git clone https://github.com/opendilab/huggingface_ding.git
45
+ pip3 install -e ./huggingface_ding/
46
+ # install environment dependencies if needed
47
+
48
+ pip3 install DI-engine[common_env,video]
49
+ pip3 install LightZero
50
+
51
+ ```
52
+ </details>
53
+
54
+ ### Git Clone from Huggingface and Run the Model
55
+
56
+ <details close>
57
+ <summary>(Click for Details)</summary>
58
+
59
+ ```shell
60
+ # running with trained model
61
+ python3 -u run.py
62
+ ```
63
+ **run.py**
64
+ ```python
65
+ from lzero.agent import MuZeroAgent
66
+ from ding.config import Config
67
+ from easydict import EasyDict
68
+ import torch
69
+
70
+ # Pull model from files which are git cloned from huggingface
71
+ policy_state_dict = torch.load("pytorch_model.bin", map_location=torch.device("cpu"))
72
+ cfg = EasyDict(Config.file_to_dict("policy_config.py").cfg_dict)
73
+ # Instantiate the agent
74
+ agent = MuZeroAgent(
75
+ env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-MuZero", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
76
+ )
77
+ # Continue training
78
+ agent.train(step=5000)
79
+ # Render the new agent performance
80
+ agent.deploy(enable_save_replay=True)
81
+
82
+ ```
83
+ </details>
84
+
85
+ ### Run Model by Using Huggingface_ding
86
+
87
+ <details close>
88
+ <summary>(Click for Details)</summary>
89
+
90
+ ```shell
91
+ # running with trained model
92
+ python3 -u run.py
93
+ ```
94
+ **run.py**
95
+ ```python
96
+ from lzero.agent import MuZeroAgent
97
+ from huggingface_ding import pull_model_from_hub
98
+
99
+ # Pull model from Hugggingface hub
100
+ policy_state_dict, cfg = pull_model_from_hub(repo_id="OpenDILabCommunity/TicTacToe-play-with-bot-MuZero")
101
+ # Instantiate the agent
102
+ agent = MuZeroAgent(
103
+ env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-MuZero", cfg=cfg.exp_config, policy_state_dict=policy_state_dict
104
+ )
105
+ # Continue training
106
+ agent.train(step=5000)
107
+ # Render the new agent performance
108
+ agent.deploy(enable_save_replay=True)
109
+
110
+ ```
111
+ </details>
112
+
113
+ ## Model Training
114
+
115
+ ### Train the Model and Push to Huggingface_hub
116
+
117
+ <details close>
118
+ <summary>(Click for Details)</summary>
119
+
120
+ ```shell
121
+ #Training Your Own Agent
122
+ python3 -u train.py
123
+ ```
124
+ **train.py**
125
+ ```python
126
+ from lzero.agent import MuZeroAgent
127
+ from huggingface_ding import push_model_to_hub
128
+
129
+ # Instantiate the agent
130
+ agent = MuZeroAgent(env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-MuZero")
131
+ # Train the agent
132
+ return_ = agent.train(step=int(500000))
133
+ # Push model to huggingface hub
134
+ push_model_to_hub(
135
+ agent=agent.best,
136
+ env_name="OpenAI/Gym/Atari",
137
+ task_name="TicTacToe-play-with-bot",
138
+ algo_name="MuZero",
139
+ github_repo_url="https://github.com/opendilab/LightZero",
140
+ github_doc_model_url=None,
141
+ github_doc_env_url=None,
142
+ installation_guide='''
143
+ pip3 install DI-engine[common_env,video]
144
+ pip3 install LightZero
145
+ ''',
146
+ usage_file_by_git_clone="./muzero/tictactoe_play_with_bot_muzero_deploy.py",
147
+ usage_file_by_huggingface_ding="./muzero/tictactoe_play_with_bot_muzero_download.py",
148
+ train_file="./muzero/tictactoe_play_with_bot_muzero.py",
149
+ repo_id="OpenDILabCommunity/TicTacToe-play-with-bot-MuZero",
150
+ platform_info="[LightZero](https://github.com/opendilab/LightZero) and [DI-engine](https://github.com/opendilab/di-engine)",
151
+ 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).",
152
+ create_repo=True
153
+ )
154
+
155
+ ```
156
+ </details>
157
+
158
+ **Configuration**
159
+ <details close>
160
+ <summary>(Click for Details)</summary>
161
+
162
+
163
+ ```python
164
+ exp_config = {
165
+ 'main_config': {
166
+ 'exp_name': 'TicTacToe-play-with-bot-MuZero',
167
+ 'seed': 0,
168
+ 'env': {
169
+ 'env_id': 'TicTacToe-play-with-bot',
170
+ 'battle_mode': 'play_with_bot_mode',
171
+ 'collector_env_num': 8,
172
+ 'evaluator_env_num': 5,
173
+ 'n_evaluator_episode': 5,
174
+ 'manager': {
175
+ 'shared_memory': False
176
+ }
177
+ },
178
+ 'policy': {
179
+ 'on_policy': False,
180
+ 'cuda': True,
181
+ 'multi_gpu': False,
182
+ 'bp_update_sync': True,
183
+ 'traj_len_inf': False,
184
+ 'model': {
185
+ 'observation_shape': [3, 3, 3],
186
+ 'action_space_size': 9,
187
+ 'image_channel': 3,
188
+ 'num_res_blocks': 1,
189
+ 'num_channels': 16,
190
+ 'fc_reward_layers': [8],
191
+ 'fc_value_layers': [8],
192
+ 'fc_policy_layers': [8],
193
+ 'support_scale': 10,
194
+ 'reward_support_size': 21,
195
+ 'value_support_size': 21,
196
+ 'norm_type': 'BN'
197
+ },
198
+ 'use_rnd_model': False,
199
+ 'sampled_algo': False,
200
+ 'gumbel_algo': False,
201
+ 'mcts_ctree': True,
202
+ 'collector_env_num': 8,
203
+ 'evaluator_env_num': 5,
204
+ 'env_type': 'board_games',
205
+ 'action_type': 'varied_action_space',
206
+ 'battle_mode': 'play_with_bot_mode',
207
+ 'monitor_extra_statistics': True,
208
+ 'game_segment_length': 5,
209
+ 'transform2string': False,
210
+ 'gray_scale': False,
211
+ 'use_augmentation': False,
212
+ 'augmentation': ['shift', 'intensity'],
213
+ 'ignore_done': False,
214
+ 'update_per_collect': 50,
215
+ 'model_update_ratio': 0.1,
216
+ 'batch_size': 256,
217
+ 'optim_type': 'Adam',
218
+ 'learning_rate': 0.003,
219
+ 'target_update_freq': 100,
220
+ 'target_update_freq_for_intrinsic_reward': 1000,
221
+ 'weight_decay': 0.0001,
222
+ 'momentum': 0.9,
223
+ 'grad_clip_value': 0.5,
224
+ 'n_episode': 8,
225
+ 'num_simulations': 25,
226
+ 'discount_factor': 1,
227
+ 'td_steps': 9,
228
+ 'num_unroll_steps': 3,
229
+ 'reward_loss_weight': 1,
230
+ 'value_loss_weight': 0.25,
231
+ 'policy_loss_weight': 1,
232
+ 'policy_entropy_loss_weight': 0,
233
+ 'ssl_loss_weight': 0,
234
+ 'lr_piecewise_constant_decay': False,
235
+ 'threshold_training_steps_for_final_lr': 50000,
236
+ 'manual_temperature_decay': False,
237
+ 'threshold_training_steps_for_final_temperature': 100000,
238
+ 'fixed_temperature_value': 0.25,
239
+ 'use_ture_chance_label_in_chance_encoder': False,
240
+ 'use_priority': True,
241
+ 'priority_prob_alpha': 0.6,
242
+ 'priority_prob_beta': 0.4,
243
+ 'root_dirichlet_alpha': 0.3,
244
+ 'root_noise_weight': 0.25,
245
+ 'random_collect_episode_num': 0,
246
+ 'eps': {
247
+ 'eps_greedy_exploration_in_collect': False,
248
+ 'type': 'linear',
249
+ 'start': 1.0,
250
+ 'end': 0.05,
251
+ 'decay': 100000
252
+ },
253
+ 'cfg_type': 'MuZeroPolicyDict',
254
+ 'reanalyze_ratio': 0.0,
255
+ 'eval_freq': 2000,
256
+ 'replay_buffer_size': 10000
257
+ },
258
+ 'wandb_logger': {
259
+ 'gradient_logger': False,
260
+ 'video_logger': False,
261
+ 'plot_logger': False,
262
+ 'action_logger': False,
263
+ 'return_logger': False
264
+ }
265
+ },
266
+ 'create_config': {
267
+ 'env': {
268
+ 'type': 'tictactoe',
269
+ 'import_names': ['zoo.board_games.tictactoe.envs.tictactoe_env']
270
+ },
271
+ 'env_manager': {
272
+ 'type': 'subprocess'
273
+ },
274
+ 'policy': {
275
+ 'type': 'muzero',
276
+ 'import_names': ['lzero.policy.muzero']
277
+ }
278
+ }
279
+ }
280
+
281
+ ```
282
+ </details>
283
+
284
+ **Training Procedure**
285
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
286
+ - **Weights & Biases (wandb):** [monitor link](<TODO>)
287
+
288
+ ## Model Information
289
+ <!-- Provide the basic links for the model. -->
290
+ - **Github Repository:** [repo link](https://github.com/opendilab/LightZero)
291
+ - **Doc**: [Algorithm link](<TODO>)
292
+ - **Configuration:** [config link](https://huggingface.co/OpenDILabCommunity/TicTacToe-play-with-bot-MuZero/blob/main/policy_config.py)
293
+ - **Demo:** [video](https://huggingface.co/OpenDILabCommunity/TicTacToe-play-with-bot-MuZero/blob/main/replay.mp4)
294
+ <!-- Provide the size information for the model. -->
295
+ - **Parameters total size:** 91.5 KB
296
+ - **Last Update Date:** 2023-12-20
297
+
298
+ ## Environments
299
+ <!-- Address questions around what environment the model is intended to be trained and deployed at, including the necessary information needed to be provided for future users. -->
300
+ - **Benchmark:** OpenAI/Gym/Atari
301
+ - **Task:** TicTacToe-play-with-bot
302
+ - **Gym version:** 0.25.1
303
+ - **DI-engine version:** v0.5.0
304
+ - **PyTorch version:** 2.0.1+cu117
305
+ - **Doc**: [Environments link](<TODO>)