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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: SampledAlphaZero
|
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.3 +/- 0.64
|
25 |
+
name: mean_reward
|
26 |
+
---
|
27 |
+
|
28 |
+
# Play **TicTacToe-play-with-bot** with **SampledAlphaZero** Policy
|
29 |
+
|
30 |
+
## Model Description
|
31 |
+
<!-- Provide a longer summary of what this model is. -->
|
32 |
+
|
33 |
+
This implementation applies **SampledAlphaZero** 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 SampledAlphaZeroAgent
|
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 = SampledAlphaZeroAgent(
|
75 |
+
env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero", 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 SampledAlphaZeroAgent
|
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-SampledAlphaZero")
|
101 |
+
# Instantiate the agent
|
102 |
+
agent = SampledAlphaZeroAgent(
|
103 |
+
env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero", 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 SampledAlphaZeroAgent
|
127 |
+
from huggingface_ding import push_model_to_hub
|
128 |
+
|
129 |
+
# Instantiate the agent
|
130 |
+
agent = SampledAlphaZeroAgent(env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero")
|
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="SampledAlphaZero",
|
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="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero_deploy.py",
|
147 |
+
usage_file_by_huggingface_ding="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero_download.py",
|
148 |
+
train_file="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero.py",
|
149 |
+
repo_id="OpenDILabCommunity/TicTacToe-play-with-bot-SampledAlphaZero",
|
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-SampledAlphaZero',
|
167 |
+
'seed': 0,
|
168 |
+
'env': {
|
169 |
+
'env_id': 'TicTacToe-play-with-bot',
|
170 |
+
'board_size': 3,
|
171 |
+
'battle_mode': 'play_with_bot_mode',
|
172 |
+
'bot_action_type': 'v0',
|
173 |
+
'channel_last': False,
|
174 |
+
'collector_env_num': 8,
|
175 |
+
'evaluator_env_num': 5,
|
176 |
+
'n_evaluator_episode': 5,
|
177 |
+
'manager': {
|
178 |
+
'shared_memory': False
|
179 |
+
},
|
180 |
+
'agent_vs_human': False,
|
181 |
+
'prob_random_agent': 0,
|
182 |
+
'prob_expert_agent': 0,
|
183 |
+
'scale': True,
|
184 |
+
'alphazero_mcts_ctree': False,
|
185 |
+
'save_replay_gif': False,
|
186 |
+
'replay_path_gif': './replay_gif'
|
187 |
+
},
|
188 |
+
'policy': {
|
189 |
+
'on_policy': False,
|
190 |
+
'cuda': True,
|
191 |
+
'multi_gpu': False,
|
192 |
+
'bp_update_sync': True,
|
193 |
+
'traj_len_inf': False,
|
194 |
+
'model': {
|
195 |
+
'observation_shape': [3, 3, 3],
|
196 |
+
'action_space_size': 9,
|
197 |
+
'num_res_blocks': 1,
|
198 |
+
'num_channels': 16,
|
199 |
+
'fc_value_layers': [8],
|
200 |
+
'fc_policy_layers': [8]
|
201 |
+
},
|
202 |
+
'torch_compile': False,
|
203 |
+
'tensor_float_32': False,
|
204 |
+
'sampled_algo': False,
|
205 |
+
'gumbel_algo': False,
|
206 |
+
'update_per_collect': 50,
|
207 |
+
'model_update_ratio': 0.1,
|
208 |
+
'batch_size': 256,
|
209 |
+
'optim_type': 'Adam',
|
210 |
+
'learning_rate': 0.003,
|
211 |
+
'weight_decay': 0.0001,
|
212 |
+
'momentum': 0.9,
|
213 |
+
'grad_clip_value': 0.5,
|
214 |
+
'value_weight': 1.0,
|
215 |
+
'collector_env_num': 8,
|
216 |
+
'evaluator_env_num': 5,
|
217 |
+
'lr_piecewise_constant_decay': False,
|
218 |
+
'threshold_training_steps_for_final_lr': 500000,
|
219 |
+
'manual_temperature_decay': False,
|
220 |
+
'threshold_training_steps_for_final_temperature': 100000,
|
221 |
+
'fixed_temperature_value': 0.25,
|
222 |
+
'mcts': {
|
223 |
+
'num_simulations': 25
|
224 |
+
},
|
225 |
+
'other': {
|
226 |
+
'replay_buffer': {
|
227 |
+
'replay_buffer_size': 1000000,
|
228 |
+
'save_episode': False
|
229 |
+
}
|
230 |
+
},
|
231 |
+
'cfg_type': 'AlphaZeroPolicyDict',
|
232 |
+
'mcts_ctree': False,
|
233 |
+
'simulation_env_name': 'tictactoe',
|
234 |
+
'simulation_env_config_type': 'play_with_bot',
|
235 |
+
'board_size': 3,
|
236 |
+
'entropy_weight': 0.0,
|
237 |
+
'n_episode': 8,
|
238 |
+
'eval_freq': 2000
|
239 |
+
},
|
240 |
+
'wandb_logger': {
|
241 |
+
'gradient_logger': False,
|
242 |
+
'video_logger': False,
|
243 |
+
'plot_logger': False,
|
244 |
+
'action_logger': False,
|
245 |
+
'return_logger': False
|
246 |
+
}
|
247 |
+
},
|
248 |
+
'create_config': {
|
249 |
+
'env': {
|
250 |
+
'type': 'tictactoe',
|
251 |
+
'import_names': ['zoo.board_games.tictactoe.envs.tictactoe_env']
|
252 |
+
},
|
253 |
+
'env_manager': {
|
254 |
+
'type': 'subprocess'
|
255 |
+
},
|
256 |
+
'policy': {
|
257 |
+
'type': 'alphazero',
|
258 |
+
'import_names': ['lzero.policy.alphazero']
|
259 |
+
},
|
260 |
+
'collector': {
|
261 |
+
'type': 'episode_alphazero',
|
262 |
+
'import_names': ['lzero.worker.alphazero_collector']
|
263 |
+
},
|
264 |
+
'evaluator': {
|
265 |
+
'type': 'alphazero',
|
266 |
+
'import_names': ['lzero.worker.alphazero_evaluator']
|
267 |
+
}
|
268 |
+
}
|
269 |
+
}
|
270 |
+
|
271 |
+
```
|
272 |
+
</details>
|
273 |
+
|
274 |
+
**Training Procedure**
|
275 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
276 |
+
- **Weights & Biases (wandb):** [monitor link](<TODO>)
|
277 |
+
|
278 |
+
## Model Information
|
279 |
+
<!-- Provide the basic links for the model. -->
|
280 |
+
- **Github Repository:** [repo link](https://github.com/opendilab/LightZero)
|
281 |
+
- **Doc**: [Algorithm link](<TODO>)
|
282 |
+
- **Configuration:** [config link](https://huggingface.co/OpenDILabCommunity/TicTacToe-play-with-bot-SampledAlphaZero/blob/main/policy_config.py)
|
283 |
+
- **Demo:** [video](https://huggingface.co/OpenDILabCommunity/TicTacToe-play-with-bot-SampledAlphaZero/blob/main/replay.mp4)
|
284 |
+
<!-- Provide the size information for the model. -->
|
285 |
+
- **Parameters total size:** 51.13 KB
|
286 |
+
- **Last Update Date:** 2024-02-01
|
287 |
+
|
288 |
+
## Environments
|
289 |
+
<!-- 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. -->
|
290 |
+
- **Benchmark:** OpenAI/Gym/Atari
|
291 |
+
- **Task:** TicTacToe-play-with-bot
|
292 |
+
- **Gym version:** 0.25.1
|
293 |
+
- **DI-engine version:** v0.5.0
|
294 |
+
- **PyTorch version:** 2.0.1+cu117
|
295 |
+
- **Doc**: [Environments link](<TODO>)
|