Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 5-7

TOP PAGE

7.5~7.7 read_kifu.py read_kifu() Argument: A text file that lists the paths of the game record files Output: ("Phase diagram", "Move", "Win / Loss") is calculated for each phase, one phase is added to the list as one element, and when all the game records have been read, it is output as one list. The data for one phase consists of the following five elements. -piece_bb: 15 elements -occupied: 2 elements -pieces_in_hand: 2 elements -move_label: 1 element -win: 1 element The final output is [([15 elements], [2 elements], [2 elements], [1 element], [1 element]), (same set), ... is the number of steps x number of games]

python-dlshogi\pydlshogi\read_kifu.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import shogi
import shogi.CSA
import copy

from pydlshogi.features import *

from pydlshogi.common import *  ##################test


# read kifu
def read_kifu(kifu_list_file):
    i = 0
    positions = []
    with open(kifu_list_file, 'r') as f:
        for line in f.readlines():
            filepath = line.rstrip('\r\n')
            i += 1
            print(i)
            #Assign the game record data to the variable kifu.
            #The game record data is a dictionary with four keys: names, sfen, moves, and win.
            #This dictionary is included in the list as an element.[0]Take out only the dictionary with.
            kifu = shogi.CSA.Parser.parse_file(filepath)[0]
            win_color = shogi.BLACK if kifu['win'] == 'b' else shogi.WHITE
            board = shogi.Board()
            for move in kifu['moves']:
                # ■board:print(board)You can display the board in two dimensions with.
                # ■piece_bb: An array of 15 elements. Each element indicates the arrangement of each piece. 0:Blank, 1:Ayumu, 2:Incense ...
                #bit board. The bit board is as follows.
                #Each element has 81 digits (=It is a decimal number display of a binary number (that is, a bit board) of 81 squares).
                #Print if you want to display 81 binary digits('{:0=81b}'.format(Decimal value))Can be done with.
                #■ occupied: An array of two elements. Each element is the position of the piece occupied by the first move and the second move. bit board.
                # ■pieces_in_hand: An array of two elements. Which piece is the first move and the second move for each element(=key)How many(=value)A dictionary type that indicates whether you have it.
                if board.turn == shogi.BLACK:
                    piece_bb = copy.deepcopy(board.piece_bb)
                    occupied = copy.deepcopy((board.occupied[shogi.BLACK], board.occupied[shogi.WHITE]))
                    pieces_in_hand = copy.deepcopy((board.pieces_in_hand[shogi.BLACK], board.pieces_in_hand[shogi.WHITE]))
                else:
                    piece_bb = [bb_rotate_180(bb) for bb in board.piece_bb]
                    occupied = (bb_rotate_180(board.occupied[shogi.WHITE]), bb_rotate_180(board.occupied[shogi.BLACK]))
                    pieces_in_hand = copy.deepcopy((board.pieces_in_hand[shogi.WHITE], board.pieces_in_hand[shogi.BLACK]))


                # move label
                i_move = shogi.Move.from_usi(move) #Create an instance of the Move class with the move variable move as an argument
                move_label = make_output_label(i_move, board.turn)
                #■ Move class
                #   from_square variable: The value of the movement source when the board surface is represented by a numerical value from 0 to 80.
                #The quotient when divided by 9 is the y coordinate, and the remainder is the x coordinate. The xy coordinate is 0 origin.
                #   to_square variable: Same as above (destination).
                #
                #x coordinate
                #   0   1   2   3   4   5   6   7   8
                #
                #0 1 2 3 4 5 6 7 8 0 y coordinates
                #   9   10  11  12  13  14  15  16  17      1
                #   18  19  20  21  22  23  24  25  26      2
                #   27  28  29  30  31  32  33  34  35      3
                #   36  37  38  39  40  41  42  43  44      4
                #   45  46  47  48  49  50  51  52  53      5
                #   54  55  56  57  58  59  60  61  62      6
                #   63  64  65  66  67  68  69  70  71      7
                #   72  73  74  75  76  77  78  79  80      8
                #
                # print(board)
                #
                # try:
                #     y_from, x_from = divmod(s.from_square, 9)
                #     y_to, x_to = divmod(s.to_square, 9)
                #     print('from:',x_from, y_from)
                #     print('to  :',x_to, y_to)
                #
                #     move_direction = DOWN
                #     print('moved:', move_direction)
                # except:
                #     pass

                # result
                win = 1 if win_color == board.turn else 0

                #Added aspects (first three variables), moves in that aspect, and wins / losses to the variable positions
                positions.append((piece_bb, occupied, pieces_in_hand, move_label, win))

                #1 move forward
                board.push_usi(move)
    return positions

Recommended Posts

Deep Learning with Shogi AI on Mac and Google Colab Chapter 11
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7
Deep Learning with Shogi AI on Mac and Google Colab Chapter 10 6-9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 10
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 5-7
Deep Learning with Shogi AI on Mac and Google Colab Chapter 9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 1-2
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3 ~ 5
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8 5-9
Deep Learning with Shogi AI on Mac and Google Colab Chapter 8 1-4
Deep Learning with Shogi AI on Mac and Google Colab Chapter 12 3
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 8
Deep Learning with Shogi AI on Mac and Google Colab Chapter 7 1-4
Deep Learning with Shogi AI on Mac and Google Colab
Learn with Shogi AI Deep Learning on Mac and Google Colab Use Google Colab
Deep Learning on Mac and Google Colab Words Learned with Shogi AI
Machine learning with Pytorch on Google Colab
About learning with google colab
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
Play with Turtle on Google Colab
Use MeCab and neologd with Google Colab
"Learning word2vec" and "Visualization with Tensorboard" on Colaboratory
Install selenium on Mac and try it with python
Deep learning image analysis starting with Kaggle and Keras
[AI] Deep Metric Learning
Extract music features with Deep Learning and predict tags
"Deep Learning from scratch" Self-study memo (No. 14) Run the program in Chapter 4 on Google Colaboratory
[Google Colab] How to interrupt learning and then resume it
Recognize your boss and hide the screen with Deep Learning
An error that stumbled upon learning YOLO on Google Colab
Machine learning environment settings based on Python 3 on Mac (coexistence with Python 2)
HIKAKIN and Max Murai with live game video and deep learning
Easy deep learning web app with NNC and Python + Flask
Try deep learning with TensorFlow
Deep Kernel Learning with Pyro
Plotly Dash on Google Colab
Try Deep Learning with FPGA
Catalina on Mac and pyenv
Generate Pokemon with Deep Learning
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Build a Python environment on your Mac with Anaconda and PyCharm
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
How to run Jupyter and Spark on Mac with minimal settings
The strongest way to use MeCab and CaboCha with Google Colab
[Reading Notes] Hands-on Machine Learning with Scikit-Learn, Keras, and TensorFlow Chapter 1
Install lp_solve on Mac OS X and call it with python.
Deep Learning / Deep Learning from Zero 2 Chapter 4 Memo
Try Deep Learning with FPGA-Select Cucumbers
Cat breed identification with deep learning
Deep Learning / Deep Learning from Zero Chapter 3 Memo
tensor flow with anaconda on mac
MQTT on Raspberry Pi and Mac
Make ASCII art with deep learning
Deep Learning / Deep Learning from Zero 2 Chapter 5 Memo
Try deep learning with TensorFlow Part 2