{"id":23159604,"url":"https://github.com/skblaz/attviz","last_synced_at":"2025-08-18T02:31:18.446Z","repository":{"id":37661670,"uuid":"213426157","full_name":"SkBlaz/attviz","owner":"SkBlaz","description":"Dissecting Transformers via attention visualization","archived":false,"fork":false,"pushed_at":"2023-10-17T22:07:58.000Z","size":7277,"stargazers_count":5,"open_issues_count":13,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T17:36:05.384Z","etag":null,"topics":["attention-is-all-you-need","attention-mechanism","interactive","language-model","language-models","machine-learning","node-js","nodejs","python","visualization","visualizations"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SkBlaz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-10-07T15:57:17.000Z","updated_at":"2023-11-14T21:29:20.000Z","dependencies_parsed_at":"2023-01-25T08:16:44.177Z","dependency_job_id":"291539a5-7b70-4dc7-80f2-66419a4e7997","html_url":"https://github.com/SkBlaz/attviz","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SkBlaz/attviz","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkBlaz%2Fattviz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkBlaz%2Fattviz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkBlaz%2Fattviz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkBlaz%2Fattviz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SkBlaz","download_url":"https://codeload.github.com/SkBlaz/attviz/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkBlaz%2Fattviz/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270933536,"owners_count":24670428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attention-is-all-you-need","attention-mechanism","interactive","language-model","language-models","machine-learning","node-js","nodejs","python","visualization","visualizations"],"created_at":"2024-12-17T22:41:16.034Z","updated_at":"2025-08-18T02:31:16.735Z","avatar_url":"https://github.com/SkBlaz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ![Logo](images/logo.png)  _AttViz_ - Self attention made *simple*\n\nNeural language models are the state-of-the-art for most language-related tasks. One of the ways to explore their behavior, however, is via _visualization_. We present AttViz, a simple webserver suitable for *exploration* of instance-level attention, online.\nThe server is live at [AttViz](http://attviz.ijs.si).\n\nCurrent preprint:\n```\n@inproceedings{skrlj-etal-2021-exploring,\n    title = \"Exploring Neural Language Models via Analysis of Local and Global Self-Attention Spaces\",\n    author = \"{\\v{S}}krlj, Bla{\\v{z}}  and\n      Sheehan, Shane  and\n      Er{\\v{z}}en, Nika  and\n      Robnik-{\\v{S}}ikonja, Marko  and\n      Luz, Saturnino  and\n      Pollak, Senja\",\n    booktitle = \"Proceedings of the EACL Hackashop on News Media Content Analysis and Automated Report Generation\",\n    month = apr,\n    year = \"2021\",\n    address = \"Online\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://www.aclweb.org/anthology/2021.hackashop-1.11\",\n    pages = \"76--83\",\n    abstract = \"Large pretrained language models using the transformer neural network architecture are becoming a dominant methodology for many natural language processing tasks, such as question answering, text classification, word sense disambiguation, text completion and machine translation. Commonly comprising hundreds of millions of parameters, these models offer state-of-the-art performance, but at the expense of interpretability. The attention mechanism is the main component of transformer networks. We present AttViz, a method for exploration of self-attention in transformer networks, which can help in explanation and debugging of the trained models by showing associations between text tokens in an input sequence. We show that existing deep learning pipelines can be explored with AttViz, which offers novel visualizations of the attention heads and their aggregations. We implemented the proposed methods in an online toolkit and an offline library. Using examples from news analysis, we demonstrate how AttViz can be used to inspect and potentially better understand what a model has learned.\",\n}\n```\n![AttViz image](images/exampleview1.png)\n\n# Requirements\nTo follow the example below, please install the requirements from requirements.txt, by doing e.g.,\n```\npip install -r requirements.txt\n```\n\nAttViz is fully compatible with [pytorch_transformers](https://github.com/huggingface/transformers) library!\n# How to prepare inputs?\nAttViz accepts attention space, encoded in the form of JSON objects, that can be constructed by using the provided scripts. An end-to-end example, which first trains a BERT-based model on a multiclass classification task, and next uses it to obtain attention data\nis given next.\n\n```python\n\n## first build a model, then generate server input.\nfrom generate_server_input import *\nfrom build_bert_classifier import *\n\n##  STEP 1: A vanilla BERT-base model.\ntrain_seq, dev_seq, train_tar, dev_tar = read_dataset(\"data\",\"hatespeech\") ## hatespeech or bbc are in the repo!\n\nbert_model = get_bert_base(train_seq, dev_seq, train_tar, dev_tar, weights_dir = \"transformer_weights\", cuda = False) ## for cuda, you might need the apex library\n\n##  STEP 2: Predict, attend to and generate final json.\nweights = \"transformer_weights\" ## Any HuggingFace model dump can be used!\ntest_data = \"data/hatespeech/test.tsv\"\ndelimiter = \"\\t\"\ntext_field = \"text_a\"\nlabel_field = \"label\"\nnumber_of_attention_heads = 12\nlabel_names = [\"no\",\"some\",\"very\",\"disturbing\"] #['business','entertainment','politics','sport']\nsegment_delimiter = \"|||\"\n\n## Obtain the attention information and create the output object.\n## Subsample = True takes each 10th sample, useful for prototyping.\n\nmodel = BertForSequenceClassification.from_pretrained(weights,\n                                                          num_labels = len(label_names),\n                                                          output_hidden_states=True,\n                                                          output_attentions=True)\n\ntokenizer = BertTokenizer.from_pretrained(\"bert-base-uncased\")\nout_obj = get_json_from_weights(model,\n                                tokenizer,\n                                test_data = test_data,\n                                delimiter = delimiter,\n                                text_field = text_field,\n                                label_field = label_field,\n                                number_of_attention_heads = number_of_attention_heads,\n                                label_names = label_names,\n                                segment_delimiter = segment_delimiter,\n                                subsample = True)\n\n## Output the extracted information to AttViz-suitable json.\nwith open('generated_json_inputs/example_hatespeech_explanations.json', 'w') as fp:\n    json.dump(out_obj, fp)\n    \n## That's it! Simply upload this json to the attvis.ijs.si and explore!\n\n```\n\nThe server also comes with some pre-loaded examples (generated_json_inputs), check these also (upload to server and explore).\n\n# How to host locally?\nIf, due to privacy concerns, one wishes to host AttViz locally, follow the instructions next.\nInstall Node.js. Then,\n```\nnpm install express\n```\n\nNext, it is as simple as:\n```\nnode embViz.js\n```\n\nGo to the browser and type \"localhost:3310\". Change the ports at will.\n\n# Disclaimer\n\nThe tool is under active development, changes are at least to some extent expected. Use at own risk.\n\n# Contributing\nPlease, open a pull request, an issue or write to us if you have some interesting use cases of this methodology!\n\n# About\nThe code was developed with equal contributions from Nika Eržen And Blaž Škrlj\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskblaz%2Fattviz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskblaz%2Fattviz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskblaz%2Fattviz/lists"}