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 SampledAlphaZeroAgent
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 = SampledAlphaZeroAgent(
11 env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero", 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 SampledAlphaZeroAgent
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/TicTacToe-play-with-bot-SampledAlphaZero")
6# Instantiate the agent
7agent = SampledAlphaZeroAgent(
8 env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero", 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 SampledAlphaZeroAgent
2from huggingface_ding import push_model_to_hub
3
4# Instantiate the agent
5agent = SampledAlphaZeroAgent(env_id="TicTacToe-play-with-bot", exp_name="TicTacToe-play-with-bot-SampledAlphaZero")
6# Train the agent
7return_ = agent.train(step=int(500000))
8# Push model to huggingface hub
9push_model_to_hub(
10 agent=agent.best,
11 env_name="OpenAI/Gym/Atari",
12 task_name="TicTacToe-play-with-bot",
13 algo_name="SampledAlphaZero",
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="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero_deploy.py",
22 usage_file_by_huggingface_ding="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero_download.py",
23 train_file="./sampled_alphazero/tictactoe_play_with_bot_sampled_alphazero.py",
24 repo_id="OpenDILabCommunity/TicTacToe-play-with-bot-SampledAlphaZero",
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': 'TicTacToe-play-with-bot-SampledAlphaZero',
4 'seed': 0,
5 'env': {
6 'env_id': 'TicTacToe-play-with-bot',
7 'board_size': 3,
8 'battle_mode': 'play_with_bot_mode',
9 'bot_action_type': 'v0',
10 'channel_last': False,
11 'collector_env_num': 8,
12 'evaluator_env_num': 5,
13 'n_evaluator_episode': 5,
14 'manager': {
15 'shared_memory': False
16 },
17 'agent_vs_human': False,
18 'prob_random_agent': 0,
19 'prob_expert_agent': 0,
20 'scale': True,
21 'alphazero_mcts_ctree': False,
22 'save_replay_gif': False,
23 'replay_path_gif': './replay_gif'
24 },
25 'policy': {
26 'on_policy': False,
27 'cuda': True,
28 'multi_gpu': False,
29 'bp_update_sync': True,
30 'traj_len_inf': False,
31 'model': {
32 'observation_shape': [3, 3, 3],
33 'action_space_size': 9,
34 'num_res_blocks': 1,
35 'num_channels': 16,
36 'fc_value_layers': [8],
37 'fc_policy_layers': [8]
38 },
39 'torch_compile': False,
40 'tensor_float_32': False,
41 'sampled_algo': False,
42 'gumbel_algo': False,
43 'update_per_collect': 50,
44 'model_update_ratio': 0.1,
45 'batch_size': 256,
46 'optim_type': 'Adam',
47 'learning_rate': 0.003,
48 'weight_decay': 0.0001,
49 'momentum': 0.9,
50 'grad_clip_value': 0.5,
51 'value_weight': 1.0,
52 'collector_env_num': 8,
53 'evaluator_env_num': 5,
54 'lr_piecewise_constant_decay': False,
55 'threshold_training_steps_for_final_lr': 500000,
56 'manual_temperature_decay': False,
57 'threshold_training_steps_for_final_temperature': 100000,
58 'fixed_temperature_value': 0.25,
59 'mcts': {
60 'num_simulations': 25
61 },
62 'other': {
63 'replay_buffer': {
64 'replay_buffer_size': 1000000,
65 'save_episode': False
66 }
67 },
68 'cfg_type': 'AlphaZeroPolicyDict',
69 'mcts_ctree': False,
70 'simulation_env_name': 'tictactoe',
71 'simulation_env_config_type': 'play_with_bot',
72 'board_size': 3,
73 'entropy_weight': 0.0,
74 'n_episode': 8,
75 'eval_freq': 2000
76 },
77 'wandb_logger': {
78 'gradient_logger': False,
79 'video_logger': False,
80 'plot_logger': False,
81 'action_logger': False,
82 'return_logger': False
83 }
84 },
85 'create_config': {
86 'env': {
87 'type': 'tictactoe',
88 'import_names': ['zoo.board_games.tictactoe.envs.tictactoe_env']
89 },
90 'env_manager': {
91 'type': 'subprocess'
92 },
93 'policy': {
94 'type': 'alphazero',
95 'import_names': ['lzero.policy.alphazero']
96 },
97 'collector': {
98 'type': 'episode_alphazero',
99 'import_names': ['lzero.worker.alphazero_collector']
100 },
101 'evaluator': {
102 'type': 'alphazero',
103 'import_names': ['lzero.worker.alphazero_evaluator']
104 }
105 }
106}
107