Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SAZ2 自己対局教師データ

rsshogi.sazpack は SAZ2 形式の自己対局教師データを読み書きするモジュールです。

from rsshogi.sazpack import (
    SazGame,
    SazPolicyEntry,
    SazPosition,
    SazWdl,
    decode_sazpack,
    decode_sazpack_file,
    write_sazpack,
    write_sazpack_file,
)

SAZ2 は初期局面、対局結果、各手の WDL 分布、policy、探索メタデータをまとめて保持します。

SazWdl

SazWdl(win: int, draw: int, loss: int)

windrawloss はそれぞれ非負の int です。

プロパティ説明
winint勝利の重み
drawint引き分けの重み
lossint敗北の重み

write_sazpack() は WDL 分布の合計が形式が要求する値と一致することを検証します。

SazPolicyEntry

SazPolicyEntry(mv, prior, raw_prior, visits_before, visits_after, lower, upper)

mvMoveMove32int、または USI 文字列を受け付けます。

プロパティ説明
mvstrUSI 形式の候補手
priorint掃索後の policy 重み
raw_priorintネットワーク出力の policy 重み
visits_before / visits_afterint対応するスナップショットの訪問数
lower / upperint結果区間の下限と上限

write_sazpack() は各局面の priorraw_prior を別々に検証し、visits_after >= visits_before を要求します。

SazPosition

SazPosition(
    played,
    root_wdl,
    outcome_wdl,
    raw_wdl,
    raw_mate,
    raw_moves_left,
    plies_left,
    requested_visits,
    target_weight_milli,
    exploration_flags,
    policy,
    mate=None,
)

1 局面分の着手と自己対局データを保持します。

プロパティ説明
playedstrUSI 形式の実際の着手
root_wdlSazWdl根の WDL 分布
outcome_wdlSazWdl対局結果の WDL 分布
raw_wdlSazWdlネットワーク出力の WDL 分布
raw_mate / raw_moves_leftintネットワーク出力の詰みと残り手数
plies_leftint対局結果に基づく残り手数
requested_visitsint要求した訪問数
target_weight_milliint対象重みの 1/1000 単位表現
exploration_flagsint探索フラグ
mate`intNone`
policylist[SazPolicyEntry]policy 候補手

SazGame

SazGame(stem, game_result, termination_reason, entering_king_rule, positions)

stem は SFEN 文字列または 32 バイトの Packed SFEN を受け付けます。

プロパティ説明
stem_packed_sfenbytes32 バイトの初期局面
game_resultGameResult対局結果
termination_reasonintSAZ2 の終局理由コード
entering_king_ruleintSAZ2 の入玉ルールコード
positionslist[SazPosition]着手順の局面データ

読み書き

関数説明
write_sazpack(games)Sequence[SazGame] を SAZ2 バイナリにシリアライズして bytes を返す
decode_sazpack(data)`bytes
write_sazpack_file(path, games)SAZ2 バイナリをファイルへ書き込む
decode_sazpack_file(path)SAZ2 バイナリファイルから復元する
from rsshogi.record import GameResult
from rsshogi.sazpack import SazGame, SazPolicyEntry, SazPosition, SazWdl, decode_sazpack, write_sazpack

wdl = SazWdl(65535, 0, 0)
policy = [SazPolicyEntry("7g7f", 65535, 65535, 0, 1, 0, 2)]
position = SazPosition("7g7f", wdl, wdl, wdl, 0, 0, 1, 1, 1000, 0, policy)
game = SazGame("lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1", GameResult.BLACK_WIN, 7, 0, [position])

data = write_sazpack([game])
decoded = decode_sazpack(data)
assert decoded[0].positions[0].played == "7g7f"

関連項目