手把手教你运行一个五子棋模型AlphaZero_Gomoku

2023-02-06 21:31:12 浏览数 (3)

项目地址GitHub - junxiaosong/AlphaZero_Gomoku:Gomoku的AlphaZero算法的实现(也称为Gobang或Five in a Row)

我的配置、环境

GPU计算型GN7 - 20核 80G(Tesla T4)

Windows Server 2019 数据中心版 64位 中文版

一、安装驱动

GPU 云服务器 安装 NVIDIA Tesla 驱动-操作指南-文档中心-腾讯云-腾讯云 (tencent.com)

GPU 云服务器 安装 CUDA 驱动-操作指南-文档中心-腾讯云-腾讯云 (tencent.com)

开启 GPU 的 OpenGL 或 DirectX 图形加速能力(可选)

GPU 云服务器 安装 NVIDIA GRID 驱动-操作指南-文档中心-腾讯云-腾讯云 (tencent.com)

也可以在文件分享 (weiyun.com)免注册下载GRID

注意:选择的版本要到Build from source on Windows  |  TensorFlow (google.cn)查询后选择对应版本

因此我选的是cuda11.2版本,cudnn8.2.1

cudnn可到此处下载cuDNN Download | NVIDIA Developer

由于cudnn需要会员,所以正常访问很容易登录不上去,所以建议自备访问国外网站工具

二、环境配置

1、anaconda(Anaconda | The World's Most Popular Data Science Platform

一路next,此处把勾去掉

下面是最重要的,配置环境变量

点击4次“新建”

分别输入

代码语言:javascript复制
C:ProgramDataAnaconda3
C:ProgramDataAnaconda3Librarymingw-w64bin
C:ProgramDataAnaconda3Librarybin
C:ProgramDataAnaconda3Scripts

前面的路径取决于实际情况‘

点开这个,输入conda,如果输出

代码语言:javascript复制
(base) PS C:UsersAdministrator> conda
usage: conda-script.py [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    compare      Compare packages between conda environments.
    config       Modify configuration values in .condarc. This is modeled after the git config command. Writes to the
                 user .condarc file (C:UsersAdministrator.condarc) by default.
    create       Create a new conda environment from a list of specified packages.
    help         Displays a list of available conda commands and their help strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment. [Experimental]
    search       Search for packages and display associated information. The input is a MatchSpec, a query language
                 for conda packages. See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  build
  content-trust
  convert
  debug
  develop
  env
  index
  inspect
  metapackage
  pack
  render
  repo
  server
  skeleton
  token
  verify

congratulations!你成功了

2、cuda环境变量

“=”前面的为新建变量名,后面是变量值,像下图

代码语言:javascript复制
CUDA_SDK_PATH = C:ProgramDataNVIDIA CorporationCUDA Samplesv11.2
CUDA_LIB_PATH = %CUDA_PATH%libx64
CUDA_BIN_PATH = %CUDA_PATH%bin
CUDA_SDK_BIN_PATH = %CUDA_SDK_PATH%binwin64
CUDA_SDK_LIB_PATH = %CUDA_SDK_PATH%commonlibx64
另外在Path变量里面新建以下几个
C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2bin
C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2extrasCUPTIlib64
C:Program FilesNVIDIA GPU Computing ToolkitCUDAv11.2include
C:toolscudabin

老规矩,路径靠自己

最后结果

3、TensorFlow

打开这个,随便运行个东西

代码语言:javascript复制
conda install python==3.8

然后喜提报错亿堆

代码语言:javascript复制
Collecting package metadata (current_repodata.json): failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/current_repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

'https://repo.anaconda.com/pkgs/main/win-64'

打开用户所在目录(cmd根目录)

找到这个文件,用记事本打开

将里面的内容替换成

代码语言:javascript复制
channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

然后保存

运行

代码语言:javascript复制
conda update --all

成功解决

正式开始安装

代码语言:javascript复制
pip install tensorflow-gpu

一眼望去,3h,可以睡一觉了(zzz)

换成国内源

代码语言:javascript复制
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade tensorflow    
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade tensorflow-gpu 

这速度舒服多了

然后检验一下是否安装好了

代码语言:javascript复制
python
代码语言:javascript复制
import tensorflow as tf

print('GPU',tf.test.is_gpu_available())

a = tf.constant(2.)
b = tf.constant(4.)

print(a * b)

如果大家像我一样脸黑,可能会看到如下结果

代码语言:javascript复制
import tensorflow as tf
>>>
>>> print('GPU',tf.test.is_gpu_available())
WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2022-05-10 18:54:39.374509: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-05-10 18:54:39.563020: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2022-05-10 18:54:39.568594: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
GPU False #false就是GPU未安装好
>>>
>>> a = tf.constant(2.)
2022-05-10 18:54:39.696552: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
>>> b = tf.constant(4.)
>>>
>>> print(a * b)
tf.Tensor(8.0, shape=(), dtype=float32)

之后我又执行了一下官方的安装

代码语言:javascript复制
pip install tensorflow

再次检测

代码语言:javascript复制
(base) PS C:UsersAdministrator> python
Python 3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> print('GPU',tf.test.is_gpu_available())
WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2022-05-10 19:10:13.966849: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-05-10 19:10:14.733664: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /device:GPU:0 with 13933 MB memory:  -> device: 0, name: Tesla T4, pci bus id: 0000:00:06.0, compute capability: 7.5
GPU True
>>>
>>> a = tf.constant(2.)
2022-05-10 19:10:14.779080: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13933 MB memory:  -> device: 0, name: Tesla T4, pci bus id: 0000:00:06.0, compute capability: 7.5
>>> b = tf.constant(4.)
>>>
>>> print(a * b)
tf.Tensor(8.0, shape=(), dtype=float32)

就变成true了

4、numpy

不绕弯子,直接国内源安装

代码语言:javascript复制
pip3 install numpy scipy matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

结果如下

代码语言:javascript复制
(base) PS C:UsersAdministrator> pip3 install numpy scipy matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: numpy in c:programdataanaconda3libsite-packages (1.21.5)
Requirement already satisfied: scipy in c:programdataanaconda3libsite-packages (1.7.3)
Requirement already satisfied: matplotlib in c:programdataanaconda3libsite-packages (3.5.1)
Requirement already satisfied: python-dateutil>=2.7 in c:programdataanaconda3libsite-packages (from matplotlib) (2.8.2)
Requirement already satisfied: pillow>=6.2.0 in c:programdataanaconda3libsite-packages (from matplotlib) (9.0.1)
Requirement already satisfied: pyparsing>=2.2.1 in c:programdataanaconda3libsite-packages (from matplotlib) (3.0.4)
Requirement already satisfied: packaging>=20.0 in c:programdataanaconda3libsite-packages (from matplotlib) (21.3)
Requirement already satisfied: fonttools>=4.22.0 in c:programdataanaconda3libsite-packages (from matplotlib) (4.25.0)
Requirement already satisfied: cycler>=0.10 in c:programdataanaconda3libsite-packages (from matplotlib) (0.11.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:programdataanaconda3libsite-packages (from matplotlib) (1.3.2)
Requirement already satisfied: six>=1.5 in c:programdataanaconda3libsite-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)

三、正式开始

从项目地址下载压缩包并解压

从anaconda里的powershell,cd到解压后的文件夹

打开这个文件

由于咱们用的是TensorFlow训练,所以需要修改注释行,让模型用TensorFlow来训练

修改方法如下

在cd到文件夹后运行

代码语言:javascript复制
python train.py

锵锵锵,报错了

问题是我们使用的是tensorflow2.0以上版本,代码中的函数是1.0的版本

打开这个文件

将这个

改为

代码语言:javascript复制
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

然后再次运行

代码语言:javascript复制
python train.py

训练就启动了

如下

代码语言:javascript复制
(base) PS C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-master> python train.py
WARNING:tensorflow:From C:ProgramDataAnaconda3libsite-packagestensorflowpythoncompatv2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:25: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  self.conv1 = tf.layers.conv2d(inputs=self.input_state,
C:ProgramDataAnaconda3libsite-packageskeraslegacy_tf_layersconvolutional.py:575: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
  return layer.apply(inputs)
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:29: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  self.conv2 = tf.layers.conv2d(inputs=self.conv1, filters=64,
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:33: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  self.conv3 = tf.layers.conv2d(inputs=self.conv2, filters=128,
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:38: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  self.action_conv = tf.layers.conv2d(inputs=self.conv3, filters=4,
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:47: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  self.action_fc = tf.layers.dense(inputs=self.action_conv_flat,
C:ProgramDataAnaconda3libsite-packageskeraslegacy_tf_layerscore.py:261: UserWarning: `layer.apply` is deprecated and will be removed in a future version. Please use `layer.__call__` method instead.
  return layer.apply(inputs)
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:51: UserWarning: `tf.layers.conv2d` is deprecated and will be removed in a future version. Please Use `tf.keras.layers.Conv2D` instead.
  self.evaluation_conv = tf.layers.conv2d(inputs=self.conv3, filters=2,
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:58: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  self.evaluation_fc1 = tf.layers.dense(inputs=self.evaluation_conv_flat,
C:UsersAdministratorDownloadsAlphaZero_Gomoku-masterAlphaZero_Gomoku-masterpolicy_value_net_tensorflow.py:61: UserWarning: `tf.layers.dense` is deprecated and will be removed in a future version. Please use `tf.keras.layers.Dense` instead.
  self.evaluation_fc2 = tf.layers.dense(inputs=self.evaluation_fc1,
2022-05-10 19:30:29.152093: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-05-10 19:30:29.748293: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13933 MB memory:  -> device: 0, name: Tesla T4, pci bus id: 0000:00:06.0, compute capability: 7.5
2022-05-10 19:30:33.976398: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8201
batch i:1, episode_len:12
batch i:2, episode_len:20
batch i:3, episode_len:17
batch i:4, episode_len:11
batch i:5, episode_len:17
kl:0.00399,lr_multiplier:1.500,loss:4.44102668762207,entropy:3.580728054046631,explained_var_old:0.005,explained_var_new:0.204
batch i:6, episode_len:20
kl:0.00509,lr_multiplier:2.250,loss:4.374798774719238,entropy:3.5816354751586914,explained_var_old:0.065,explained_var_new:0.251
batch i:7, episode_len:7
kl:0.01145,lr_multiplier:2.250,loss:4.090033054351807,entropy:3.5747854709625244,explained_var_old:0.237,explained_var_new:0.572
batch i:8, episode_len:14
kl:0.00808,lr_multiplier:3.375,loss:3.8602192401885986,entropy:3.5711326599121094,explained_var_old:0.549,explained_var_new:0.777
batch i:9, episode_len:16
kl:0.03588,lr_multiplier:3.375,loss:3.7816779613494873,entropy:3.548457145690918,explained_var_old:0.709,explained_var_new:0.781
batch i:10, episode_len:9
kl:0.06818,lr_multiplier:2.250,loss:3.767848491668701,entropy:3.4980945587158203,explained_var_old:0.560,explained_var_new:0.718
batch i:11, episode_len:14
kl:0.06377,lr_multiplier:1.500,loss:3.8209140300750732,entropy:3.450775146484375,explained_var_old:0.448,explained_var_new:0.643
batch i:12, episode_len:17
kl:0.04083,lr_multiplier:1.000,loss:3.767989158630371,entropy:3.417010545730591,explained_var_old:0.411,explained_var_new:0.648
batch i:13, episode_len:16
kl:0.02567,lr_multiplier:1.000,loss:3.982619524002075,entropy:3.4054667949676514,explained_var_old:0.256,explained_var_new:0.412
batch i:14, episode_len:17
kl:0.03405,lr_multiplier:1.000,loss:3.8700919151306152,entropy:3.3119544982910156,explained_var_old:0.314,explained_var_new:0.421

默默等待就可以得到一个比较理想的模型了

进行人机对战可以运行

代码语言:javascript复制
python human_play.py  

0 人点赞