Move Prediction#
Setup#
[1]:
MODE = "local" # "colab" | "colab-dev" | "local"
[ ]:
if MODE == "colab":
!pip install -q lczerolens
elif MODE == "colab-dev":
!rm -r lczerolens
!git clone https://github.com/Xmaster6y/lczerolens -b main
!pip install -q ./lczerolens
[ ]:
!gdown 1TI429e9mr2de7LjHp2IIl7ouMoUaDjjZ -O leela-network.onnx
Load a Model#
Load a leela network from file (already converted to onnx):
[4]:
from lczerolens import LczeroModel
model = LczeroModel.from_path("leela-network.onnx")
To convert original weights see the section Convert Official Weights.
Predict a Move#
The defined model natively integrates with python-cess. Use the utils to predict a policy vector and obtain an UCI move:
[5]:
from lczerolens import LczeroBoard
# Get the model prediction
board = LczeroBoard()
out = model(board)
policy = out["policy"]
# Use the prediction
best_move_index = policy.argmax()
move = board.decode_move(best_move_index)
board.push(move)
display(board)
As most network are trained only on legal moves the argmax should only be computed on them:
[6]:
out = model(board)
policy = out["policy"]
legal_indices = board.get_legal_indices()
legal_policy = policy[0].gather(0, legal_indices)
best_move_index = legal_indices[legal_policy.argmax()]
move = board.decode_move(best_move_index)
board.push(move)
display(board)
To simplify this process, you can use the Sampler class as shown below:
[7]:
from lczerolens.play import PolicySampler
sampler = PolicySampler(model, use_argmax=True)
move, _ = next(iter(sampler.get_next_moves([board])))
board.push(move)
display(board)