深度學習與圍棋遊戲 筆記 2

2021-09-18 04:16:23 字數 4406 閱讀 6384

教機器學習的內容

布局模型庫。由高手布局棋譜中的高頻率走步構成。

建立棋局狀態搜尋樹。

儘量減少備選的下一步棋。

估算每步棋的價值。

衡量機械人的棋力

傳統的日本級段制。

與機械人或者人模擬賽測定。

包括以下內容:

建立目錄 dlgo,其下建 3 個檔案:

檔案__init__.py內容為空。

在模組 gotype.py 中定義棋手和棋盤點位

import enum

class

player

(enum.enum)

: black =

1 white =2

@property

defother

(self)

:return player.black if self == player.white else player.white

from collections import namedtuple

class

point

(namedtuple(

'point'

,'row col'))

:def

neighbors

(self)

:return

[ point(self.row -

1, self.col)

, point(self.row +

1, self.col)

, point(self.row, self.col -1)

, point(self.row, self.col +1)

,]

player 的用法

player = player.black

print

(player.value)

print

(player.other.value)

輸出結果是 1 和 2

point的用法

p = point(row=

3,col=3)

print

(p.neighbors(

))

輸出結果是:

[point(row=2, col=3),

point(row=4, col=3),

point(row=3, col=2),

point(row=3, col=4)]

在模組 goboard_slow.py 中定義落子、放棄一手和認輸

import copy

from gotypes import player

class

move()

:def

__init__

(self, point=

none

, is_pass=

false

, is_resign=

false):

assert

(point is

notnone

)^ is_pass ^ is_resign

self.point = point

self.is_play =

(self.point is

notnone

) self.is_pass = is_pass

self.is_resign = is_resign

@classmethod

defplay

(cls, point)

:return move(point=point)

@classmethod

defpass_turn

(cls)

:return move(is_pass=

true

) @classmethod

defresign

(cls)

:return move(is_resign=

true

)

move 的用法

move.play(point(3,

3))move = move.play(point(3,

3))print

(move.is_play)

print

(move.point.row)

move = move.resign(

)print

(move.is_resign)

輸出結果是:

true

3true

用 assert 測試 move 是否合法

下例,落子與pass同時發生,將引發報錯:

move = move(point(2,

2),is_pass=

true

,is_resign=

false

)

定義棋串

class

gostring()

:def

__init__

(self, color, stones, liberties)

: self.color = color

self.stones =

set(stones)

self.liberties =

set(liberties)

defremove_liberty

(self, point)

: self.liberties.remove(point)

defadd_liberty

(self, point)

: self.liberties.add(point)

defmerged_with

(self, go_string)

:assert go_string.color == self.color

combined_stones = self.stones | go_string.stones

return gostring(self.color,combined_stones,

(self.liberties | go_string.liberties)

- ombined_stones)

@property

defnum_liberties

(self)

:return

len(self.liberties)

def__eq__

(self, other)

:return

isinstance

(other, gostring)

and \

self.color == other.color and \

self.stones == other.stones and \

self.liberties == other.liberties

注意,__eq__ 用於判別類的例項是否相等

gostring 的用法

a = gostring(player.black,

[point(3,

3),point(3,

4),point(3,

5)],

[3,2

,2])

b = gostring(player.black,

[point(3,

3),point(3,

4),point(3,

5)],

[3,2

,2])

print

(a==b)

a = gostring(player.black,

[point(3,

3),point(3,

4),point(3,

5)],

[3,2

,2])

b = gostring(player.black,

[point(13,

3),point(13,

4),point(13,

5)],

[3,2

,2])

print

(a==b)

輸出結果是:

true

false

深度學習與圍棋遊戲 筆記 1

最近,搞到一本書 deep learning and the game of go 歐美書商好像有種習慣,科技方面尤其程式設計相關的電子書,銷售一段時間後會公開放到網上,供人免費下載。這本書就來源於此。估計,有社會資本支援書商的善舉。希望國內資本雄厚的企業,向西方學習,鼓勵支援國內書商也這樣做。第1...

深度學習筆記(2)

2 分詞 對每個句子進行分詞,也就是將乙個句子劃分成若干個詞 token 轉換為乙個詞的序列。3 建立字典 將每個詞對映到乙個唯一的索引 index 為了方便模型處理,我們需要將字串轉換為數字。因此我們需要先構建乙個字典 vocabulary 將每個詞對映到乙個唯一的索引編號。4 將詞轉為索引 使用...

學習 《神經網路與深度學習》 筆記2

2019年5月23日 晚上十點 s型神經元 現在我們試圖對於感知器網路中的權重和偏置進行乙個微小的改動,試圖只是引起輸出乙個微小的變化。例如對於手寫體輸出的識別,其中網路錯誤的將一幅 9 的影象識別成 8 的影象,我們嘗試著通過對於權重和偏置微小的調整矯正這個錯誤。但是當我們這個網路是感知器網路的時...