Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

機器學習

W16_ 具深度學習能力的黑白棋程式開發
01
深度學習黑白棋製作
套件規劃
• othello
• OthelloUtil,py
• OthelloGame.py
• bots
• Random.py
• DeepLearning
• model
• __init__.py
• OthelloModel.py

01 深度學習黑白棋製作 3
OthelloModel.py
屬性
model_name: str ( 模型名稱 )
model: tf.keras.engine.training.Model (CNN, 輸入棋盤 , 輸出每個位置的機
率)
方法
fit(data: list, batch_size: int, epochs: int): void
• data 內容: [ [ 棋盤 array, 每個位置的機率 array], [ 棋盤 array, 每個位置的機率 array], ......
]
save_weights(): void
load_weights(): void
predict(board: np.array): np.array

01 深度學習黑白棋製作 4
__init__.py
屬性
board_size: int ( 棋盤大小 )
model: OthelloModel
方法
getAction(game: OthelloGame, color: int): tuple
self_play_train(args: dictionary): void

01 深度學習黑白棋製作 5
__init__.py
import numpy as np
from othello.OthelloUtil import getValidMoves
from othello.bots.DeepLearning.OthelloModel import OthelloModel
from othello.OthelloGame import OthelloGame
建構子輸入棋盤大小
class BOT():
如果 model 目錄內有模型就會載
def __init__(self, board_size, *args, **kargs):

self.board_size=board_size
self.model = OthelloModel( input_shape=(self.board_size, self.board_size) )
try:
self.model.load_weights()
print('model loaded')
except:
print('no model exist')
pass

self.collect_gaming_data=False # 用於訓練
self.history=[] # 用於訓練

01 深度學習黑白棋製作 6
__init__.py
import numpy as np
from othello.OthelloUtil import getValidMoves
from othello.bots.DeepLearning.OthelloModel import OthelloModel
from othello.OthelloGame import OthelloGame

class BOT():
def getAction(self, game, color):
predict = self.model.predict( game )
模型預測輸出每個位置的機率
valid_positions=getValidMoves(game, color)
取 valid move 中機率最高的位置
valids=np.zeros((game.size), dtype='int')
valids[ [i[0]*game.n+i[1] for i in valid_positions] ]=1
predict*=valids
position = np.argmax(predict)

if self.collect_gaming_data: # 訓練時會從這邊蒐集棋盤
tmp=np.zeros_like(predict)
tmp[position]=1.0
self.history.append([np.array(game.copy()), tmp, color])

position=(position//game.n, position%game.n)
01 深度學習黑白棋製作 7
return position
__init__.py
import numpy as np
from othello.OthelloUtil import getValidMoves
from othello.bots.DeepLearning.OthelloModel import OthelloModel
from othello.OthelloGame import OthelloGame

class BOT():
def self_play_train(self, args):
蒐集自我對下的棋譜
self.collect_gaming_data=True
執行訓練
def gen_data():
儲存權重
next page

data=[]
for i in range(args['num_of_generate_data_for_train']):
if args['verbose']:
print('self playing', i+1)
data+=gen_data()
self.collect_gaming_data=False

self.model.fit(data, batch_size = args['batch_size'], epochs = args['epochs'])


01 深度學習黑白棋製作 8
self.model.save_weights()
__init__.py - gen_data
def self_play_train(self, args):
def gen_data():
def getSymmetries(board, pi):
next page
建立 OthelloGame
self.history=[] 黑白都設成 self
history=[] 並將蒐集的棋譜翻轉、
game=OthelloGame(self.board_size) 全轉來增量
game.play(self, self, verbose=args['verbose'])
for step, (board, probs, player) in enumerate(self.history):
sym = getSymmetries(board, probs)
for b,p in sym:
history.append([b, p, player])
self.history.clear()
game_result=game.isEndGame() 勝利或平手都蒐集
return [(x[0],x[1]) for x in history if (game_result==0 or x[2]==game_result)]

01 深度學習黑白棋製作 9
__init__.py - gen_data - getSymmetries
def self_play_train(self, args):
def gen_data():
def getSymmetries(board, pi):
# mirror, rotation
pi_board = np.reshape(pi, (len(board), len(board))) 翻轉、旋轉增量
l = []
for i in range(1, 5):
for j in [True, False]:
newB = np.rot90(board, i)
newPi = np.rot90(pi_board, i)
if j:
newB = np.fliplr(newB)
newPi = np.fliplr(newPi)
l += [( newB, list(newPi.ravel()) )]
return l

01 深度學習黑白棋製作 10
使用方法
from othello import OthelloGame
from othello.bots.DeepLearning import BOT

class Human:
def getAction(self, game, color):
print('input coordinate:', end='')
coor=input()
return (int(coor[1])-1, ord(coor[0])-ord('A'))
BOARD_SIZE=8

bot=BOT(board_size=BOARD_SIZE)
game=OthelloGame(BOARD_SIZE)
game.play(black=bot, white=Human())

01 深度學習黑白棋製作 11
使用方法 - 自我對下訓練
from othello import OthelloGame
from othello.bots.DeepLearning import BOT

class Human:
def getAction(self, game, color):
print('input coordinate:', end='')
coor=input()
return (int(coor[1])-1, ord(coor[0])-ord('A'))
BOARD_SIZE=8

bot=BOT(board_size=BOARD_SIZE)
args={
'num_of_generate_data_for_train': 8,
'epochs': 5,
'batch_size': 4,
'verbose': True
}
while True: # 無限輪訓練
bot.self_play_train(args)

01 深度學習黑白棋製作 12
02
黑白棋競賽平台
加入競賽步驟 - 下載通訊套件
http://aiia.csie.ncnu.edu.tw/competition/test

02 黑白棋競賽平台 14
加入競賽步驟 - 下載通訊套件
下載 AIGamePlatform.zip 解壓縮會看到

> pip install -r requirements.txt


安裝執行環境

00 Name of Chapter or Course 15


02 黑白棋競賽平台
加入競賽步驟 - 查看競賽 ID

02 黑白棋競賽平台 16
加入競賽步驟 - 加入競賽
from AIGamePlatform import Othello
from othello.bots.Random import BOT

app=Othello() # 會開啟瀏覽器登入 Google Account ,目前只接受 @mail1.ncnu.edu.tw 及 @mail.ncnu.edu.tw


bot=BOT()

@app.competition(competition_id='Lg3a6fwqs2')
def _callback_(board, color): # 函數名稱可以自訂, board 是當前盤面, color 代表黑子或白子
return bot.getAction(board, color) # 回傳要落子的座標

02 黑白棋競賽平台 17
加入競賽步驟 - 加入競賽

設定好後執行 .py 會自動開啟瀏覽器


使用 NCNU 的帳號登入

不要使用 spyder 執行 .py ,經測試發現無法輸入


用 cmd 執行沒問題

02 黑白棋競賽平台 18
加入競賽步驟 - 加入競賽

加入成功可以即時在競賽頁面內看到帳號

02 黑白棋競賽平台 19
開始競賽

競賽開始後
每一輪如果數量不是 2 的倍數
就會隨機抽一個種子
種子不會連續兩輪

02 黑白棋競賽平台 20
開始競賽

每一局開始前需要兩位玩家自行協商顏色
雙方的 client 程式會顯示可輸入 B 或 W 的提醒
B 代表黑色、 W 代表白色
選黑色的會當先手

02 黑白棋競賽平台 21
開始競賽

雙方都輸入正確就
可以看到棋盤的實況
會先比兩場,顏色輪流
如果兩場比完平手
就會繼續輪,直到某一方贏

總時間不夠或該出手時斷線就算輸

02 黑白棋競賽平台 22
開始競賽

競賽結束可以再去看下棋的過程記錄

02 黑白棋競賽平台 23
THE END.

You might also like