Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dirichlet noise added to prior probabilities during self play #186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Coach.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, game, nnet, args):
self.nnet = nnet
self.pnet = self.nnet.__class__(self.game) # the competitor network
self.args = args
self.mcts = MCTS(self.game, self.nnet, self.args)
self.mcts = MCTS(self.game, self.nnet, self.args, dirichlet_noise=True)
self.trainExamplesHistory = [] # history of examples from args.numItersForTrainExamplesHistory latest iterations
self.skipFirstSelfPlay = False # can be overriden in loadTrainExamples()

Expand Down Expand Up @@ -82,7 +82,7 @@ def learn(self):
end = time.time()

for eps in range(self.args.numEps):
self.mcts = MCTS(self.game, self.nnet, self.args) # reset search tree
self.mcts = MCTS(self.game, self.nnet, self.args, dirichlet_noise=True) # reset search tree
iterationTrainExamples += self.executeEpisode()

# bookkeeping + plot progress
Expand Down
22 changes: 19 additions & 3 deletions MCTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class MCTS():
This class handles the MCTS tree.
"""

def __init__(self, game, nnet, args):
def __init__(self, game, nnet, args, dirichlet_noise=False):
self.game = game
self.nnet = nnet
self.args = args
self.dirichlet_noise = dirichlet_noise
self.Qsa = {} # stores Q values for s,a (as defined in the paper)
self.Nsa = {} # stores #times edge s,a was visited
self.Ns = {} # stores #times board s was visited
Expand All @@ -29,7 +30,8 @@ def getActionProb(self, canonicalBoard, temp=1):
proportional to Nsa[(s,a)]**(1./temp)
"""
for i in range(self.args.numMCTSSims):
self.search(canonicalBoard)
dir_noise = (i == 0 and self.dirichlet_noise)
jrbuhl93 marked this conversation as resolved.
Show resolved Hide resolved
self.search(canonicalBoard, dirichlet_noise=dir_noise)

s = self.game.stringRepresentation(canonicalBoard)
counts = [self.Nsa[(s,a)] if (s,a) in self.Nsa else 0 for a in range(self.game.getActionSize())]
Expand All @@ -46,7 +48,7 @@ def getActionProb(self, canonicalBoard, temp=1):
return probs


def search(self, canonicalBoard):
def search(self, canonicalBoard, dirichlet_noise=False):
"""
This function performs one iteration of MCTS. It is recursively called
till a leaf node is found. The action chosen at each node is one that
Expand Down Expand Up @@ -79,6 +81,8 @@ def search(self, canonicalBoard):
self.Ps[s], v = self.nnet.predict(canonicalBoard)
valids = self.game.getValidMoves(canonicalBoard, 1)
self.Ps[s] = self.Ps[s]*valids # masking invalid moves
if dirichlet_noise:
self.applyDirNoise(s, valids)
jrbuhl93 marked this conversation as resolved.
Show resolved Hide resolved
sum_Ps_s = np.sum(self.Ps[s])
if sum_Ps_s > 0:
self.Ps[s] /= sum_Ps_s # renormalize
Expand All @@ -96,6 +100,10 @@ def search(self, canonicalBoard):
return -v

valids = self.Vs[s]
if dirichlet_noise:
self.applyDirNoise(s, valids)
sum_Ps_s = np.sum(self.Ps[s])
self.Ps[s] /= sum_Ps_s # renormalize
Copy link

@greghe greghe Apr 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applyDirNoise has already calculated a normalized sequence when it added two normalized sequences (scaled by 0.75 and 0.25, respectively), so there is no need to renormalize Ps[s] here – since doing so will have no effect

cur_best = -float('inf')
best_act = -1

Expand Down Expand Up @@ -127,3 +135,11 @@ def search(self, canonicalBoard):

self.Ns[s] += 1
return -v

def applyDirNoise(self, s, valids):
dir_values = np.random.dirichlet([self.args.dirichletAlpha] * np.count_nonzero(valids))
dir_idx = 0
for idx in range(len(self.Ps[s])):
if self.Ps[s][idx]:
self.Ps[s][idx] = (0.75 * self.Ps[s][idx]) + (0.25 * dir_values[dir_idx])
dir_idx += 1
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'numMCTSSims': 25, # Number of games moves for MCTS to simulate.
'arenaCompare': 40, # Number of games to play during arena play to determine if new net will be accepted.
'cpuct': 1,
'dirichletAlpha': 0.6, # α = {0.3, 0.15, 0.03} for chess, shogi and Go respectively, scaled in inverse proportion to the approximate number of legal moves in a typical position

'checkpoint': './temp/',
'load_model': False,
Expand Down