Giter Site home page Giter Site logo

xmaster6y / lczerolens Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 639 KB

๐Ÿ”ฌ Interpretability for Leela Chess Zero networks.

Home Page: https://lczerolens.readthedocs.io

License: MIT License

Makefile 0.22% Shell 0.29% Python 99.49%
chess lc0 xai

lczerolens's Introduction

logo

lczerolens ๐Ÿ”

ci publish docs

Leela Chess Zero (lc0) Lens: a set of utilities to make the analysis of Leela Chess Zero networks easy.

Getting Started

Installs

pip install lczerolens

Load a Model

Build a model from leela file (convert then load):

from lczerolens import AutoBuilder

model = AutoBuilder.from_path(
    "leela-network.onnx"
)

Predict a Move

Use a wrapper and the utils to predict a policy vector and obtain an UCI move:

import chess
import torch

from lczerolens import move_utils, ModelWrapper

# Wrap the model
wrapper = ModelWrapper(model)
board = chess.Board()

# Get the model predictions
out = wrapper.predict(board)
policy = out["policy"]

# Use the prediction
best_move_index = policy.argmax()
move = move_utils.decode_move(best_move_index)
board.push(move)

print(uci_move, board)

Compute a Heatmap

Use a lens to compute a heatmap

from lczerolens import visualisation_utils, LrpLens

# Get the lens
lens = LrpLens()

# Compute the relevance
assert lens.is_compatible(wrapper)
relevance = lens.compute_heatmap(board, wrapper)

# Choose a plane index and render the heatmap on the board
plane_index = 0
heatmap = relevance_tensor[plane_index].view(64)
if board.turn == chess.BLACK:
    heatmap = heatmap.view(8, 8).flip(0).view(64)
svg_board, fig = visualisation_utils.render_heatmap(
    board, heatmap, normalise="abs"
)

Convert a Network

To convert a network you'll need to have installed the lc0 extra:

pip install lczerolens[lc0]
from lczerolens import lczero as lczero_utils

lczero_utils.convert_to_onnx(
    "leela-network.pb.gz",
    "leela-network.onnx"
)

Demo

Additionally, you can run the gradio demo locally (also deployed on HF). First you'll need gradio, which is packaged in the demo extra:

pip install lczerolens[demo]

And then launch the demo (running on port 8000):

make demo

Full Documentation

๐Ÿ”ด Documentation coming soon.

Contribute

See the guidelines in CONTRIBUTING.md.

lczerolens's People

Contributors

xmaster6y avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lczerolens's Issues

Update Demo & Docs

Description

With the new API changes, the demo and the documentation need to be updated.

Acceptance Criterion

  • The code is documented
  • Utilities and class test are written
  • The code was reviewed

Tasks

Setup Docs

Description

Setup documentation using sphinx.

Tasks

  • Presentation and logo
  • WIP pages
  • Basic features
  • Deploy pipeline

Rewrite dataset analysis with generator

Description

More memory friendly.

Acceptance Criterion

  • The code is documented
  • Utilities and class test are written
  • The code was reviewed

Tasks

  • Change the code to make dataset lense iterators

Batching label prediction

Description

Batch the label prediction.

Acceptance Criterion

  • The code is documented
  • Utilities and class tests are written
  • The code was reviewed

Tasks

  • Compute labels per batch option
  • Make the concept dataset batch labelise
  • Optionally make it on device if available

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.