3目並べを利用したAlphaGoの学習 tensorflow-1編 データ作成
1.概要
AlphaGoの勉強過程で3目並べを学んでいます。前回までプログラムロジックを作成していましたが、今回から3目並べの全ての組合せデータを利用してAIでプログラミングに勝てるかを試します。DeepLearningとしてtensorflowの1.xと2.xの両方を試してみます。内容が多いので複数回に分割して記載をします。
2.詳細
(1) 概要
3目並べのフィールドを3☓3のイメージと考えて、手書き文字認識の手法を利用します。最初に利用する環境はtensorflow-1.15です。入力データはminimax法で活用したすべての手順(9!=362880)の組み合わせの中から勝負が決まった時点の3目並べのフィールド情報と結果(勝ち、負け、引き分け)を利用します。
tensorflowで利用できる形式に変換し、学習をしてモデルを作成し、モデルを利用して3目並べの対戦をします。元情報がminimax法で解析した情報なので、tensorflowによる学習結果がminimax法まで到達できると最高の結果です。大まかな手順は以下の通りです。
(1) minimax法の解析を利用して学習用入力データを作成
(2) 作成した学習用入力データを利用して、tensorflowでモデル作成
(3) tensorflowのモデルを利用して実際に対戦
上記手順を3回に分けて記述します。
(2) 詳細
(1) minimax法の解析を利用して学習用入力データを作成
dlmakedata.pyを作成します。この中で利用するtictactoe.pyはmontecarlo版を利用します。
titactoeのコードは本ブログのTictactoe like the montecarlo(2023/12/30参照)
学習用データは、dl1_data.npy(フィールドデータ)、dl2_data.npy(結果データ)です。
プログラムを実行した結果、学習用データの件数は、255,168件でした。
これは9!=362880よりも少なくなります。最低5手で勝負が決着する場合などがあるためです。
from tictactoe import Tictactoe
import numpy as np
def minimax_select(actions):
r1 = []
r2 = []
for action in actions:
score = obj.do_game(action)
minimax(obj.next_action(), r1, r2)
obj.undo_game(action)
return [r1, r2]
def minimax(actions, r1, r2):
for action in actions:
score = obj.do_game(action)
if score == 1:
s1 = ",".join(map(str,obj.fields))
s2 = "1,0,0"
r1.append(s1)
r2.append(s2)
elif score == -1:
s1 = ",".join(map(str,obj.fields))
s2 = "0,1,0"
r1.append(s1)
r2.append(s2)
elif score == 0:
s1 = ",".join(map(str,obj.fields))
s2 = "0,0,1"
r1.append(s1)
r2.append(s2)
else:
minimax(obj.next_action(), r1, r2)
obj.undo_game(action)
def string_to_array(strlist):
r1 = []
for item in strlist:
f1 = item.split(",")
l1 = []
for s1 in f1:
l1.append(int(s1))
r1.append(l1)
a1 = np.array(r1)
a2 = a1.astype(np.float32)
return a2
if __name__ == "__main__":
obj = Tictactoe()
actions = [0,1,2,3,4,5,6,7,8]
result = minimax_select(actions)
print(len(result[0]), len(result[1]))
r1 = string_to_array(result[0])
r2 = string_to_array(result[1])
print(r1, r1.dtype, r1.shape)
print(r2, r2.dtype, r2.shape)
np.save('dl1_data', r1)
np.save('dl2_data', r2)
y1 = np.load('dl1_data.npy')
y2 = np.load('dl2_data.npy')
print(y1, y1.dtype, y1.shape)
print(y2, y2.dtype, y2.shape)
参考
AlphaZero 深層学習・強化学習・探索 人工知能プログラミング実践入門
布留川 英一 著
コメント
コメントを投稿