CSSLab / maia-chess

Maia is a human-like neural network chess engine trained on millions of human games.
https://maiachess.com
GNU General Public License v3.0
964 stars 121 forks source link

How to use blunder prediction for given position/fen + next move #32

Open int8 opened 2 years ago

int8 commented 2 years ago

Hello there,

I am trying to use blunder prediction for my hobby project I plan to work on - what I need is to predict blunder probability for given move at given position - how do I do it ?

P.S. I would rather avoid training my own blunder prediction model

reidmcy commented 2 years ago

You should be able to load our models with the standard Pytorch load function. They don't use or predict the next move though, just the board (and metadata for some models) to predict the binary will_blunder. Then the code in the validate_batch method is how we run the analysis. It's all done in large batches for efficiency so running on a singe position might require some tinkering.

int8 commented 2 years ago

all right, seems like your advice + https://github.com/CSSLab/maia-chess/issues/10 helped me :)

The next step for me is to map FEN into tensor so I can pass it to forward call - any helper function I could use?

int8 commented 2 years ago

ok - found it,

now I am here:

from haibrid_chess_utils import fen_to_vec

model = torch.load(
    "blunder_prediction/model_weights/leela_extra_blunder_00001-234000.pt", 
    map_location=torch.device('cpu')
)

t = torch.from_numpy(
    fen_to_vec.fenToVec(
        "r1bqkb1r/ppp2ppp/2n2n2/3Pp1N1/2B5/8/PPPP1PPP/RNBQK2R b KQkq - 0 5"
    )).reshape(-1, 17, 8, 8)

result = model.forward(
    input_x=t, 
    extra_x={
        'clock_percent': torch.tensor([.05]).float(), 
        'opponent_elo': torch.tensor([1800]).float(), 
        'active_elo': torch.tensor([1800]).float(), 
        'cp_rel': torch.tensor([0.2]).float(), 
        'move_ply': torch.tensor([30]).float()}
)

which is sort of critical moment of fried liver attack: https://lichess.org/analysis/standard/r1bqkb1r/ppp2ppp/2n2n2/3Pp1N1/2B5/8/PPPP1PPP/RNBQK2R_b_KQkq_-_0_5

  1. Can you sanity check that on your installation and check what is probability of a blunder in that setup ?
  2. What is the extra_x dict exactly, I think I understand active_elo (player to move elo, right?), opponent_elo (obvious I hope) but I am very unsure about move_ply, clock_percent and cp_rel
reidmcy commented 2 years ago

I don't have an easy way to run the models. This project is in an archive and the machine we used has been reallocated. So I can't check that.

You're correct about the first two values, move_ply is the ply of the current move the first move is ply 1, same indexing as Lichess. clock_percentage is the remaining time as a percentage of the initial clock, I think we assume game will be 30 moves/60 ply if there's an increment. And cp_rel is the centipawn, times 100 so actually pawns, evaluation of the position by stockfish, we do it relative instead of absolute so multiply by -1 if the active player is black.

zodwick commented 5 months ago

hey @int8 any progress on this, have you figured out how to do the prediction based on fen ?