- 资产集市
- 教学
- 实践
- AI说
- 案例库
- 生态合作
- 专区
中国站
简体中文2用户已评价
提供一种可视化的方式,与自己在ModelArts中部署的中国象棋AI对战
本次案例需要先在AI Gallery订阅《中国象棋AlphaZero模型》,并利用该模型前往ModelArts控制台部署在线服务。本案例将调用部署好的在线服务获取AI动作,直接部署模型将调用内置模型。
如果想要先训练自己的中国象棋模型再与之对战,可先在AI Gallery中订阅《CChess中国象棋》算法,并在ModelArts中进行训练。训练完成后获取权重地址,在《中国象棋AlphaZero模型》中导入模型并部署在线服务。详见《训练自己的象棋AI模型并与之对战》
本案例运行环境为 PyTorch-1.4,且建议使用 CPU 运行,请查看《ModelAtrs JupyterLab 硬件规格使用指南》了解切换硬件规格的方法;
如果您是第一次使用 JupyterLab,请查看《ModelAtrs JupyterLab使用指导》了解使用方法;
如果您在使用 JupyterLab 过程中碰到报错,请参考《ModelAtrs JupyterLab常见问题解决办法》尝试解决问题;
第1步:安装基础依赖
要确保所有依赖都安装成功后,再执行之后的代码。如果某些模块因为网络原因导致安装失败,直接重试一次即可。
!pip install ipympl==0.8.8
Looking in indexes: http://repo.myhuaweicloud.com/repository/pypi/simple
Collecting ipympl==0.8.8
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/81/13/12a4761eb01a59e7f185af7b9543a9aef9495c42a5f77d8b8c4d51794f8c/ipympl-0.8.8-py2.py3-none-any.whl (507 kB)
|████████████████████████████████| 507 kB 102.2 MB/s eta 0:00:01
Requirement already satisfied: ipython<9 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (7.31.1)
Requirement already satisfied: pillow in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (6.2.0)
Requirement already satisfied: numpy in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (1.17.0)
Requirement already satisfied: ipython-genutils in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (0.2.0)
Collecting ipywidgets<8,>=7.6.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/86/7d/06b48ec5fd605775c7e85b3ea397d8f0294f66d570bcee59496eb5195fc5/ipywidgets-7.7.0-py2.py3-none-any.whl (123 kB)
|████████████████████████████████| 123 kB 119.5 MB/s eta 0:00:01
Requirement already satisfied: matplotlib<4,>=2.0.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (3.5.1)
Requirement already satisfied: traitlets<6 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipympl==0.8.8) (5.1.1)
Requirement already satisfied: setuptools>=18.5 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (58.0.4)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (3.0.27)
Requirement already satisfied: jedi>=0.16 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (0.18.1)
Requirement already satisfied: decorator in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (5.1.1)
Requirement already satisfied: backcall in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (0.2.0)
Requirement already satisfied: pygments in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (2.11.2)
Requirement already satisfied: matplotlib-inline in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (0.1.3)
Requirement already satisfied: pickleshare in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (0.7.5)
Requirement already satisfied: pexpect>4.3 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipython<9->ipympl==0.8.8) (4.8.0)
Collecting widgetsnbextension~=3.6.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/4e/69/273f584c28805939c6cb45717638cb8edbd78147f02c564b8c7763e1872e/widgetsnbextension-3.6.0-py2.py3-none-any.whl (1.6 MB)
|████████████████████████████████| 1.6 MB 98.4 MB/s eta 0:00:01
Requirement already satisfied: ipykernel>=4.5.1 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipywidgets<8,>=7.6.0->ipympl==0.8.8) (6.7.0)
Collecting nbformat>=4.2.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/2f/9a/97151abb954af0cc5d0e3ff2eb7b6d96704a317ac2c0ce0cc76cef003991/nbformat-5.4.0-py3-none-any.whl (73 kB)
|████████████████████████████████| 73 kB 69.4 MB/s eta 0:00:01
Collecting jupyterlab-widgets>=1.0.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/5f/2c/7331aa9c5041e8b107d712d853268e137f55014b858407816b5487289d11/jupyterlab_widgets-1.1.0-py3-none-any.whl (245 kB)
|████████████████████████████████| 245 kB 108.5 MB/s eta 0:00:01
Requirement already satisfied: debugpy<2.0,>=1.0.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (1.6.0)
Requirement already satisfied: jupyter-client<8.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (7.1.2)
Requirement already satisfied: tornado<7.0,>=4.2 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (6.1)
Requirement already satisfied: nest-asyncio in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (1.5.4)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from jedi>=0.16->ipython<9->ipympl==0.8.8) (0.8.3)
Requirement already satisfied: python-dateutil>=2.1 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (2.8.2)
Requirement already satisfied: jupyter-core>=4.6.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (4.9.1)
Requirement already satisfied: entrypoints in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (0.4)
Requirement already satisfied: pyzmq>=13 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (22.3.0)
Requirement already satisfied: fonttools>=4.22.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from matplotlib<4,>=2.0.0->ipympl==0.8.8) (4.33.2)
Requirement already satisfied: cycler>=0.10 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from matplotlib<4,>=2.0.0->ipympl==0.8.8) (0.11.0)
Requirement already satisfied: pyparsing>=2.2.1 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from matplotlib<4,>=2.0.0->ipympl==0.8.8) (3.0.8)
Requirement already satisfied: packaging>=20.0 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from matplotlib<4,>=2.0.0->ipympl==0.8.8) (21.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from matplotlib<4,>=2.0.0->ipympl==0.8.8) (1.4.2)
Requirement already satisfied: typing-extensions in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from kiwisolver>=1.0.1->matplotlib<4,>=2.0.0->ipympl==0.8.8) (4.2.0)
Collecting fastjsonschema
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/e6/0b/24795939622d60f4b453aa7040f23c6a6f8b44c7c026c3b42d9842e6cc31/fastjsonschema-2.15.3-py3-none-any.whl (22 kB)
Collecting jsonschema>=2.6
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/aa/13/0063ea808b6a1bee57e0780d83bf0c57f0ed25a1b5ed3685524359c485fd/jsonschema-4.6.0-py3-none-any.whl (80 kB)
|████████████████████████████████| 80 kB 24.2 MB/s eta 0:00:01
Collecting pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/50/6b/6be2a63984a4cf0136cda5b4c21bbd6f9c1953de35321718f408a70b3b04/pyrsistent-0.18.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (117 kB)
|████████████████████████████████| 117 kB 22.2 MB/s eta 0:00:01
Requirement already satisfied: importlib-metadata in /home/ma-user/modelarts/modelarts-sdk (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (4.11.3)
Collecting attrs>=17.4.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/be/be/7abce643bfdf8ca01c48afa2ddf8308c2308b0c3b239a44e57d020afa0ef/attrs-21.4.0-py2.py3-none-any.whl (60 kB)
|████████████████████████████████| 60 kB 28.7 MB/s eta 0:00:01
Collecting importlib-resources>=1.4.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/3c/a7/4e4a2176fed10ab233cc39b083ba4ec222ba52de2be606e3e2b5195264e9/importlib_resources-5.8.0-py3-none-any.whl (28 kB)
Requirement already satisfied: zipp>=3.1.0 in /home/ma-user/modelarts/modelarts-sdk (from importlib-resources>=1.4.0->jsonschema>=2.6->nbformat>=4.2.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (3.8.0)
Requirement already satisfied: ptyprocess>=0.5 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from pexpect>4.3->ipython<9->ipympl==0.8.8) (0.7.0)
Requirement already satisfied: wcwidth in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython<9->ipympl==0.8.8) (0.2.5)
Requirement already satisfied: six>=1.5 in /home/ma-user/anaconda3/envs/PyTorch-1.4/lib/python3.7/site-packages (from python-dateutil>=2.1->jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (1.16.0)
Collecting notebook>=4.4.1
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/b5/62/229659241aee54be38990e06e684bcfe5c9c8727185f5e39335be8821583/notebook-6.4.12-py3-none-any.whl (9.9 MB)
|████████████████████████████████| 9.9 MB 95.3 MB/s eta 0:00:01█████ | 4.7 MB 95.3 MB/s eta 0:00:01
Collecting prometheus-client
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/19/e5/7d4b4b3b0d8d2fdc55395cdb4271c6dbfde3c3ff7d6a6dbe63d19c4e2288/prometheus_client-0.14.1-py3-none-any.whl (59 kB)
|████████████████████████████████| 59 kB 26.2 MB/s eta 0:00:01
Requirement already satisfied: jinja2 in /home/ma-user/modelarts/ma-cli (from notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (3.1.2)
Collecting terminado>=0.8.3
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/85/be/6b89563289bc8df86f4089efcc4e28d39feaaa4c0863ddcb32dee18d0957/terminado-0.15.0-py3-none-any.whl (16 kB)
Collecting nbconvert>=5
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/e8/f9/2de57146b8995d7f1b68d6fd0b4751d68c23f52e6f4ad926a7274184e8f2/nbconvert-6.5.0-py3-none-any.whl (561 kB)
|████████████████████████████████| 561 kB 105.2 MB/s eta 0:00:01
Collecting Send2Trash>=1.8.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/47/26/3435896d757335ea53dce5abf8d658ca80757a7a06258451b358f10232be/Send2Trash-1.8.0-py3-none-any.whl (18 kB)
Collecting argon2-cffi
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/a8/07/946d5a9431bae05a776a59746ec385fbb79b526738d25e4202d3e0bbf7f4/argon2_cffi-21.3.0-py3-none-any.whl (14 kB)
Collecting nbclient>=0.5.0
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/33/b7/f94f83283e54d0729994bd46928b06451448b4a08e7476e6950fd1c8a2b2/nbclient-0.6.3-py3-none-any.whl (71 kB)
|████████████████████████████████| 71 kB 29.5 MB/s eta 0:00:01
Collecting tinycss2
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/53/7b/5dba39bf0572f1f28e2844f08f74a73482a381de1d1feac3bbc6b808051e/tinycss2-1.1.1-py3-none-any.whl (21 kB)
Collecting bleach
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/35/8b/bad37525e7ef0669f013059affb3bbcd9311a24d63eda96723339ee0aab7/bleach-5.0.0-py3-none-any.whl (160 kB)
|████████████████████████████████| 160 kB 79.6 MB/s eta 0:00:01
Collecting jupyterlab-pygments
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/c0/7e/c3d1df3ae9b41686e664051daedbd70eea2e1d2bd9d9c33e7e1455bc9f96/jupyterlab_pygments-0.2.2-py2.py3-none-any.whl (21 kB)
Collecting pandocfilters>=1.4.1
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/5e/a8/878258cffd53202a6cc1903c226cf09e58ae3df6b09f8ddfa98033286637/pandocfilters-1.5.0-py2.py3-none-any.whl (8.7 kB)
Collecting beautifulsoup4
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/9c/d8/909c4089dbe4ade9f9705f143c9f13f065049a9d5e7d34c828aefdd0a97c/beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)
|████████████████████████████████| 128 kB 21.1 MB/s eta 0:00:01
Collecting defusedxml
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/07/6c/aa3f2f849e01cb6a001cd8554a88d4c77c5c1a31c95bdf1cf9301e6d9ef4/defusedxml-0.7.1-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: MarkupSafe>=2.0 in /home/ma-user/modelarts/ma-cli (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (2.1.1)
Collecting mistune<2,>=0.8.1
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/09/ec/4b43dae793655b7d8a25f76119624350b4d65eb663459eb9603d7f1f0345/mistune-0.8.4-py2.py3-none-any.whl (16 kB)
Collecting argon2-cffi-bindings
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/ec/f7/378254e6dd7ae6f31fe40c8649eea7d4832a42243acaf0f1fff9083b2bed/argon2_cffi_bindings-21.2.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (86 kB)
|████████████████████████████████| 86 kB 63.1 MB/s eta 0:00:01
Requirement already satisfied: cffi>=1.0.1 in /home/ma-user/modelarts/ma-cli (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (1.15.0)
Requirement already satisfied: pycparser in /home/ma-user/modelarts/ma-cli (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.6.0->ipywidgets<8,>=7.6.0->ipympl==0.8.8) (2.21)
Collecting soupsieve>1.2
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/16/e3/4ad79882b92617e3a4a0df1960d6bce08edfb637737ac5c3f3ba29022e25/soupsieve-2.3.2.post1-py3-none-any.whl (37 kB)
Collecting webencodings
Downloading http://repo.myhuaweicloud.com/repository/pypi/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl (11 kB)
Installing collected packages: pyrsistent, importlib-resources, attrs, jsonschema, fastjsonschema, webencodings, soupsieve, nbformat, tinycss2, pandocfilters, nbclient, mistune, jupyterlab-pygments, defusedxml, bleach, beautifulsoup4, argon2-cffi-bindings, terminado, Send2Trash, prometheus-client, nbconvert, argon2-cffi, notebook, widgetsnbextension, jupyterlab-widgets, ipywidgets, ipympl
Successfully installed Send2Trash-1.8.0 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 attrs-21.4.0 beautifulsoup4-4.11.1 bleach-5.0.0 defusedxml-0.7.1 fastjsonschema-2.15.3 importlib-resources-5.8.0 ipympl-0.8.8 ipywidgets-7.7.0 jsonschema-4.6.0 jupyterlab-pygments-0.2.2 jupyterlab-widgets-1.1.0 mistune-0.8.4 nbclient-0.6.3 nbconvert-6.5.0 nbformat-5.4.0 notebook-6.4.12 pandocfilters-1.5.0 prometheus-client-0.14.1 pyrsistent-0.18.1 soupsieve-2.3.2.post1 terminado-0.15.0 tinycss2-1.1.1 webencodings-0.5.1 widgetsnbextension-3.6.0
WARNING: You are using pip version 21.2.2; however, version 22.1.2 is available.
You should consider upgrading via the '/home/ma-user/anaconda3/envs/PyTorch-1.4/bin/python -m pip install --upgrade pip' command.
第2步: 下载依赖包
import os
import moxing as mox
if not os.path.exists('cchess_gameplay_v2'):
mox.file.copy('obs://modelarts-labs-bj4-v2/course/modelarts/reinforcement_learning/cchess_gameplay/cchess_gameplay_v2.zip',
'cchess_gameplay_v2.zip')
os.system('unzip cchess_gameplay_v2.zip')
INFO:root:Using MoXing-v2.0.1.rc0.ffd1c0c8-ffd1c0c8 INFO:root:Using OBS-Python-SDK-3.20.9.1
"""
cchess_config 说明
mcts_playouts 选填参数,Int类型,默认值100,推荐取值范围100-1600,数值越低,表示AI的思考步数越少,反之表示越多
service_id 必填参数,String类型,在线服务ID
"""
cchess_config = {
"mcts_playouts": 200,
"service_id": "d915e7bc-84ce-4d6f-8752-21dd317edc0d", # 请在此处填写您的在线服务ID,如"d915e7bc-84ce-4d6f-8752-21dd317edc0d"
}
每下完一步后适当晃动鼠标
%matplotlib widget
from modelarts.session import Session
from modelarts.model import Predictor
from cchess_gameplay_v2.cchess_gameplay_client import ChessGamePlay
if __name__ == "__main__":
session = Session()
predictor_instance = Predictor(session, service_id=cchess_config["service_id"])
gp = ChessGamePlay(cchess_config, predictor_instance)
gp.start_game()
INFO:matplotlib.font_manager:generated new fontManager
评论0
5
2人已评价
请登录后评论