Giter VIP home page Giter VIP logo

attention-viz's Introduction

attention-viz

attention-viz is a lightweight visualization for attention mechanisms in deep neural networks. It supports one score per token, producing HTML code that can be run in the browser (with no additional dependencies). Note that the scores don't necessarily need to form a valid probability distribution, so this tool can also be used to visualize other types of magnitude-based scores (e.g., gradient saliency).

Requirements

  • Python 3.6+
  • matplotlib (3.1.1)

Usage

If matplotlib is not installed globally, set up a virtual environment and activate it:

$ virtualenv python3.6 venv
$ source venv/bin/activate
$ pip install matplotlib

The script viz.py takes in two arguments: --text_path (path to file of tokens, delimited by new lines) and --score_path (path to file of scores, also delimited by new lines). By default, the BuGn colormap is used, but this can be easily configured by setting the --cmap flag. Different colormap options are enumerated here.

For convenience, viz.py's output can be piped to an index.html file. Open this file in your browser to see the text overlaid with the attention heatmap:

$ python3 viz.py --text_path text --score_path score > index.html
$ open index.html  # opens file with default browser

If the attention distribution is highly concentrated, it may be difficult to read text with extremely dark backgrounds. You can use the --alpha flag to adjust the visualization. Generally, lower values of --alpha will result in more color concentration while higher values of --alpha will result in lesser color concentration:

--alpha 0.0

--alpha 0.5

--alpha 5.0

Citation

If you found this tool useful, please consider putting a link to this repository in your work.

attention-viz's People

Contributors

shreydesai avatar

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.