コード例 #1
0
    if i == len(policy)-1:
      moves_and_probs.append((Board.PASS_LOC,policy[i]))
    elif board.would_be_legal(pla,move) and not board.is_simple_eye(pla,move):
      moves_and_probs.append((move,policy[i]))
  return moves_and_probs

# Basic parsing --------------------------------------------------------
colstr = 'ABCDEFGHJKLMNOPQRST'
def str_coord(loc,board):
  if loc == Board.PASS_LOC:
    return 'pass'
  x = board.loc_x(loc)
  y = board.loc_y(loc)
  return '%c%d' % (colstr[x], board.size - y)

(metadata,setups,sgfmoves,rules) = data.load_sgf_moves_exn(sgf_file)

board_size = metadata.size
board = Board(size=board_size)
moves = []
boards = [board.copy()]

def setstone(pla,loc):
  board.play(pla,loc)
  moves.clear()
  boards.clear()
  boards.append(board.copy())
def play(pla,loc):
  board.play(pla,loc)
  moves.append((pla,loc))
  boards.append(board.copy())
コード例 #2
0
ファイル: eval_sgf.py プロジェクト: MarkTakken/KataGo

def setstone(pla, loc):
    board.play(pla, loc)
    moves.clear()
    boards.clear()
    boards.append(board.copy())


def play(pla, loc):
    board.play(pla, loc)
    moves.append((pla, loc))
    boards.append(board.copy())


(metadata, setups, moves) = data.load_sgf_moves_exn(sgf_file)
assert (metadata.size == 19)  #Neural net only works with 19x19 right now

for (pla, loc) in setups:
    setstone(pla, loc)

for i in range(movenum):
    (pla, loc) = moves[i]
    play(pla, loc)

print(board.to_string())

saver = tf.compat.v1.train.Saver(
    max_to_keep=10000,
    save_relative_paths=True,
)
コード例 #3
0
    def __iter__(self):
        worker_info = torch.utils.data.get_worker_info()
        if worker_info is None:
            rand = random.Random(os.urandom(32))
        else:
            rand = random.Random(
                os.urandom(32) + "#SgfDataset#".encode() +
                str(worker_info.id).encode())

        files = self.files
        cpudevice = torch.device("cpu")

        try:
            while True:
                rand.shuffle(files)
                file_count = 0
                error_count = 0
                print("Iterator beginning reading of files %d / %d" %
                      (file_count, len(files)),
                      flush=True)
                for filename in files:
                    try:
                        (metadata, setup, moves,
                         rules) = data.load_sgf_moves_exn(filename)
                    except Exception as e:
                        error_count += 1
                        continue
                    # Only even 19x19 games!
                    if metadata.size != 19 or len(setup) != 0 or (
                            metadata.handicap is not None
                            and metadata.handicap != 0):
                        continue
                    board = Board(size=metadata.size)
                    turn_number = 0
                    for (pla, loc) in moves:

                        if rand.random() < self.sample_prob:
                            inputs = torch.zeros(
                                (8, metadata.size, metadata.size),
                                dtype=torch.float32,
                                device=cpudevice)
                            result = torch.zeros((3, ),
                                                 dtype=torch.float32,
                                                 device=cpudevice)
                            aux = torch.zeros(
                                (3, metadata.size, metadata.size),
                                dtype=torch.float32,
                                device=cpudevice)

                            (alwaysknownxmin,
                             alwaysknownxmax) = random_subinterval(
                                 rand, metadata.size)
                            (alwaysknownymin,
                             alwaysknownymax) = random_subinterval(
                                 rand, metadata.size)

                            if alwaysknownxmin <= 0 and alwaysknownxmax >= metadata.size - 1 and alwaysknownymin <= 0 and alwaysknownymax >= metadata.size - 1:
                                pass
                            else:
                                # Channel 1: On-board
                                inputs[1, :, :].fill_(1.0)

                                num_always_known_poses = 0
                                if alwaysknownxmax < 0 or alwaysknownxmin >= metadata.size or alwaysknownymax < 0 or alwaysknownymin >= metadata.size:
                                    num_always_known_poses = 0
                                else:
                                    num_always_known_poses = (
                                        (min(alwaysknownxmax,
                                             metadata.size - 1) -
                                         max(alwaysknownxmin, 0) + 1) *
                                        (min(alwaysknownymax, metadata.size -
                                             1) - max(alwaysknownymin, 0) + 1))
                                num_not_always_known_poses = metadata.size * metadata.size - num_always_known_poses
                                inferenceidx = rand.randint(
                                    0, num_not_always_known_poses - 1)

                                flipx = rand.random() < 0.5
                                flipy = rand.random() < 0.5
                                swapxy = rand.random() < 0.5

                                idx = 0
                                for y in range(metadata.size):
                                    for x in range(metadata.size):
                                        pos = y * metadata.size + x
                                        always_known = (
                                            x >= alwaysknownxmin
                                            and x <= alwaysknownxmax
                                            and y >= alwaysknownymin
                                            and y <= alwaysknownymax)

                                        sx = x
                                        sy = y
                                        if flipx:
                                            sx = metadata.size - sx - 1
                                        if flipy:
                                            sy = metadata.size - sy - 1
                                        if swapxy:
                                            tmp = sx
                                            sx = sy
                                            sy = tmp
                                        stone = board.board[board.loc(sx, sy)]

                                        # Channel 4: Unknown
                                        if idx > inferenceidx and not always_known:
                                            inputs[4, y, x] = 1.0
                                        # Channel 0: Next inference point
                                        elif idx == inferenceidx and not always_known:
                                            inputs[0, y, x] = 1.0
                                            result
                                            if stone == Board.BLACK:
                                                result[1] = 1.0
                                            elif stone == Board.WHITE:
                                                result[2] = 1.0
                                            else:
                                                result[0] = 1.0
                                        else:
                                            # Channel 2: Black
                                            if stone == Board.BLACK:
                                                inputs[2, y, x] = 1.0
                                            # Channel 3: White
                                            elif stone == Board.WHITE:
                                                inputs[3, y, x] = 1.0

                                        if stone == Board.BLACK:
                                            aux[1, y, x] = 1.0
                                        elif stone == Board.WHITE:
                                            aux[2, y, x] = 1.0
                                        else:
                                            aux[0, y, x] = 1.0

                                        if not always_known:
                                            idx += 1

                                assert (idx == num_not_always_known_poses)

                                if rand.random() < 0.3:
                                    turn_noise_stdev = 0.0
                                    reported_turn = turn_number
                                else:
                                    turn_noise_stdev = (rand.random()**
                                                        2.0) * 100
                                    reported_turn = turn_number + rand.normalvariate(
                                        0.0, turn_noise_stdev)

                                # Channel 5: Turn number / 100
                                inputs[5, :, :].fill_(reported_turn / 100.0)
                                # Channel 6: Noise stdev in turn number / 50
                                inputs[6, :, :].fill_(turn_noise_stdev / 50.0)
                                # Channel 7: Source
                                is_kgs = ("/kgs" in filename) or (
                                    "\\KGS" in filename) or (
                                        "/KGS" in filename) or ("\\KGS"
                                                                in filename)
                                is_fox = ("/fox" in filename) or (
                                    "\\fox" in filename) or (
                                        "/FOX" in filename) or ("\\FOX"
                                                                in filename)
                                if is_kgs:
                                    inputs[7, :, :].fill_(1.0)
                                elif is_fox:
                                    inputs[7, :, :].fill_(-1.0)

                                if rand.random() < 0.5:
                                    if rand.random() < 0.5:
                                        inputs = torch.flip(inputs, [1, 2])
                                        aux = torch.flip(aux, [1, 2])
                                    else:
                                        inputs = torch.flip(inputs, [1])
                                        aux = torch.flip(aux, [1])
                                else:
                                    if rand.random() < 0.5:
                                        inputs = torch.flip(inputs, [2])
                                        aux = torch.flip(aux, [2])
                                    else:
                                        pass

                                if rand.random() < 0.5:
                                    inputs = torch.transpose(inputs, 1, 2)
                                    aux = torch.transpose(aux, 1, 2)

                                yield (inputs, result, aux)

                        try:
                            board.play(pla, loc)
                        except IllegalMoveError as e:
                            # On illegal move in the SGF, don't attempt to recover, just move on to new game
                            print("Illegal move, skipping file " + filename +
                                  ":" + str(e),
                                  flush=True)
                            break
                        turn_number += 1
                        if turn_number > self.max_turn:
                            break
                        if rand.random() < self.break_prob_per_turn:
                            break

                    file_count += 1
                    if file_count % 200 == 0:
                        print("Read through file %d / %d  (error count %d)" %
                              (file_count, len(files), error_count),
                              flush=True)

                if not self.endless:
                    break

        except GeneratorExit:
            pass
        except Exception as e:
            print("EXCEPTION IN GENERATOR: " + str(e))
            traceback.print_exc()
            print("---", flush=True)
            yield e