kevaday / alphazero-general

A fast, generalized, and modified implementation of Deepmind's distinguished AlphaZero in PyTorch.
MIT License
66 stars 21 forks source link

Exception ignored in: 'alphazero.MCTS.MCTS._add_root_noise' #33

Closed coder-free closed 1 year ago

coder-free commented 1 year ago

I'm training gobang (10x10 board with 5 connect), I got this error:

FloatingPointError: underflow encountered in cast
Exception ignored in: 'alphazero.MCTS.MCTS._add_root_noise'
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()

The train is not interrupt. But I don't know what will happened. I training it with python 3.8

coder-free commented 1 year ago

I fixed this bug. In file MCTS.pyx line 197, change function _add_root_noise: from

    cpdef void _add_root_noise(self):
        cdef int num_valid_moves = len(self._root._children)
        cdef float[:] noise = np.array(np.random.dirichlet(
            [NOISE_ALPHA_RATIO / num_valid_moves] * num_valid_moves
        ), dtype=np.float32)
        cdef Node c
        cdef float n

        for n, c in zip(noise, self._root._children):
            c.p = c.p * (1 - self.root_noise_frac) + self.root_noise_frac * n

change to:

    cpdef void _add_root_noise(self):
        cdef int num_valid_moves = len(self._root._children)
        cdef double[:] noise = np.array(np.random.dirichlet(
            [NOISE_ALPHA_RATIO / num_valid_moves] * num_valid_moves
        ))
        cdef Node c
        cdef double n
        cdef float nf

        for n, c in zip(noise, self._root._children):
            nf = <float>n
            c.p = c.p * (1 - self.root_noise_frac) + self.root_noise_frac * nf