{"id":21984454,"url":"https://github.com/nightmachinery/libragrad","last_synced_at":"2025-03-23T01:44:38.867Z","repository":{"id":264493741,"uuid":"892076155","full_name":"NightMachinery/LibraGrad","owner":"NightMachinery","description":"Balancing Gradient Flow for Universally Better Vision Transformer Attributions","archived":false,"fork":false,"pushed_at":"2024-11-27T12:48:57.000Z","size":80497,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T05:12:22.462Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NightMachinery.png","metadata":{"files":{"readme":"readme.org","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-21T13:20:12.000Z","updated_at":"2024-12-12T23:50:42.000Z","dependencies_parsed_at":"2024-11-24T18:34:33.068Z","dependency_job_id":null,"html_url":"https://github.com/NightMachinery/LibraGrad","commit_stats":null,"previous_names":["nightmachinery/libragrad"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2FLibraGrad","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2FLibraGrad/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2FLibraGrad/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NightMachinery%2FLibraGrad/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NightMachinery","download_url":"https://codeload.github.com/NightMachinery/LibraGrad/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245044493,"owners_count":20551898,"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","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":[],"created_at":"2024-11-29T18:09:35.293Z","updated_at":"2025-03-23T01:44:38.849Z","avatar_url":"https://github.com/NightMachinery.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"#+TITLE: LibraGrad: Balancing Gradient Flow for Universally Better Vision Transformer Attributions\n#+options: toc:3\n#+EXCLUDE_TAGS: noexport\n\nTo understand model decisions, attribution methods measure how input features contribute to outputs. LibraGrad enhances all gradient-based attribution methods for Transformers through theoretically-motivated gradient flow balancing,  leading to fast and faithful post-hoc explanations. For theoretical foundations and comprehensive quantitative and qualitative evaluations, see our [[https://arxiv.org/pdf/2411.16760][paper]].\n\n# Read the [[https://arxiv.org/pdf/2411.16760][paper]].\n# Read the [[https://raw.githack.com/NightMachinery/LibraGrad/master/paper/LibraGrad_v2.4.5_with_appendices.pdf][paper]].\n# Appendix C of the paper contains many multi-object qualitative examples.\n\n# [[./images/fig_1.png]]\n# [[./images/main_001_cropped.png]]\n# [[./images/main_002_cropped.png]]\n[[./images/main_001_cropped.jpeg]]\n[[./images/main_002_cropped.jpeg]]\n\n* COMMENT _ :noexport:\n** [[https://arxiv.org/abs/2411.16760][{2411.16760} LibraGrad: Balancing Gradient Flow for Universally Better Vision Transformer Attributions]]\n\n** [[id:9b138a2b-f5b3-46a5-a496-7452c77cc109][Github/pages/create]]\n:PROPERTIES:\n:ID:       648abab6-2601-40e6-9612-1a7f474d9884\n:END:\n*** [[https://nightmachinery.github.io/LibraGrad/][LibraGrad: Balancing Gradient Flow for Universally Better Vision Transformer Attributions]]\n:PROPERTIES:\n:ID:       f6ba8bcc-d17c-4429-91f0-117e34703142\n:END:\n\n*** Add the correct favicon\n:PROPERTIES:\n:ID:       e6a9d4a5-a657-4171-8b7f-2dbb6d75a80a\n:END:\n#+begin_src zsh :eval never\ndeus org-export-recursive readme.org ; gmv readme.html index.html\n\n# html_favicon.py favicon.svg --input=index.html --output=index.html\n#: The favicon will be added automatically when it exists.\n#+end_src\n\n**** [[https://fontawesome.com/v5/icons/balance-scale?s=solid][Balance Scale Icon | Font Awesome]]\n:PROPERTIES:\n:ID:       f3e274c6-cb6e-47dc-9f3f-b82a7116a68b\n:END:\n\n* Abstract\n:PROPERTIES:\n:ID:       2d8d8584-ff9f-4eb8-b400-01d060112415\n:END:\nWhy do gradient-based explanations struggle with Transformers, and how can we improve them? We identify gradient flow imbalances in Transformers that violate FullGrad-completeness, a critical property for attribution faithfulness that CNNs naturally possess. To address this issue, we introduce LibraGrad—a theoretically grounded post-hoc approach that corrects gradient imbalances through pruning and scaling of backward paths, without changing the forward pass or adding computational overhead. We evaluate LibraGrad using three metric families: Faithfulness, which quantifies prediction changes under perturbations of the most and least relevant features; Completeness Error, which measures attribution conservation relative to model outputs; and Segmentation AP, which assesses alignment with human perception. Extensive experiments across 8 architectures, 4 model sizes, and 4 datasets show that LibraGrad universally enhances gradient-based methods, outperforming existing white-box methods—including Transformer-specific approaches—across all metrics. We demonstrate superior qualitative results through two complementary evaluations: precise text-prompted region highlighting on CLIP models and accurate class discrimination between co-occurring animals on ImageNet-finetuned models—two settings on which existing methods often struggle. LibraGrad is effective even on the attention-free MLP-Mixer architecture, indicating potential for extension to other modern architectures.\n\n* Code\n:PROPERTIES:\n:ID:       57bad82d-f0e8-4421-bff1-85590fbbd38e\n:END:\nThe code of this paper is distributed across three repositories:\n- [[https://github.com/NightMachinery/decompv-libragrad-v1][NightMachinery/decompv-libragrad-v1]]\n\n- [[https://github.com/NightMachinery/pytorch-image-models-libragrad-v1][NightMachinery/pytorch-image-models-libragrad-v1]]\n  \n\n- [[https://github.com/NightMachinery/PyNight][NightMachinery/PyNight: My Python utility library.]]\n\n** Notebooks\n:PROPERTIES:\n:ID:       c6676863-c8a7-4133-8f78-bba3ea73d9e8\n:END:\nWe offer two interactive notebooks:\n\n1. [[https://colab.research.google.com/github/NightMachinery/LibraGrad/blob/master/notebooks/llama3.ipynb][Implementing LibraGrad From Scratch for Llama 3]]\n   - This comprehensive, self-contained notebook serves as the primary reference implementation for LibraGrad\n   - Ideal for practitioners who have studied the theoretical foundations in the paper\n   - Enables visualization of attribution maps for next token prediction tasks on textual data\n\n2. [[https://colab.research.google.com/github/NightMachinery/LibraGrad/blob/master/notebooks/CLIP.ipynb][CLIP Feature Attribution via LibraGrad]]\n   - Enables visualization of attribution maps for any combination of images and text prompts\n   - Features automatic setup that handles all repository installations and dependencies\n\n* Support\n:PROPERTIES:\n:ID:       56e6fdff-5190-4490-896e-6dee43aa55d1\n:END:\nFound LibraGrad helpful? Star our repository ([[https://github.com/NightMachinery/LibraGrad][github.com/NightMachinery/LibraGrad]]) and cite our LibraGrad and FullGrad+ papers:\n\n# @todo update the bibtex for LibraGrad, use bibtidy\n#+begin_src bibtex\n@misc{mehri-libragrad,\n  title         = {{LibraGrad}: Balancing Gradient Flow for Universally Better Vision Transformer Attributions},\n  author        = {Faridoun Mehri and Mahdieh Soleymani Baghshah and Mohammad Taher Pilehvar},\n  year          = {2024},\n  eprint        = {2411.16760},\n  archiveprefix = {arXiv},\n  primaryclass  = {cs.CV},\n  url           = {https://arxiv.org/abs/2411.16760},\n}\n\n@inproceedings{mehri-skipplus-cvpr24,\n  title         = {{SkipPLUS}: Skip the First Few Layers to Better Explain Vision Transformers},\n  author        = {Faridoun Mehri and Mohsen Fayyaz and Mahdieh Soleymani Baghshah and Mohammad Taher Pilehvar},\n  booktitle     = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) Workshops},\n  month         = {June},\n  year          = {2024},\n  pages         = {204--215},\n  url           = {https://openaccess.thecvf.com/content/CVPR2024W/TCV2024/html/Mehri\\%5FSkipPLUS\\%5FSkip\\%5Fthe\\%5FFirst\\%5FFew\\%5FLayers\\%5Fto\\%5FBetter\\%5FExplain\\%5FVision\\%5FCVPRW\\%5F2024\\%5Fpaper.html},\n}\n#+end_src\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnightmachinery%2Flibragrad","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnightmachinery%2Flibragrad","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnightmachinery%2Flibragrad/lists"}