gradiend 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. gradiend-0.1.0/CITATION.cff +20 -0
  2. gradiend-0.1.0/LICENSE +201 -0
  3. gradiend-0.1.0/MANIFEST.in +6 -0
  4. gradiend-0.1.0/PKG-INFO +130 -0
  5. gradiend-0.1.0/PYPI.md +77 -0
  6. gradiend-0.1.0/README.md +227 -0
  7. gradiend-0.1.0/gradiend/__init__.py +103 -0
  8. gradiend-0.1.0/gradiend/data/__init__.py +23 -0
  9. gradiend-0.1.0/gradiend/data/core/__init__.py +7 -0
  10. gradiend-0.1.0/gradiend/data/core/base_loader.py +121 -0
  11. gradiend-0.1.0/gradiend/data/core/spacy_util.py +63 -0
  12. gradiend-0.1.0/gradiend/data/text/__init__.py +16 -0
  13. gradiend-0.1.0/gradiend/data/text/filter_config.py +93 -0
  14. gradiend-0.1.0/gradiend/data/text/prediction/__init__.py +10 -0
  15. gradiend-0.1.0/gradiend/data/text/prediction/creator.py +771 -0
  16. gradiend-0.1.0/gradiend/data/text/prediction/filter_engine.py +359 -0
  17. gradiend-0.1.0/gradiend/data/text/preprocess.py +241 -0
  18. gradiend-0.1.0/gradiend/evaluator/__init__.py +22 -0
  19. gradiend-0.1.0/gradiend/evaluator/config.py +30 -0
  20. gradiend-0.1.0/gradiend/evaluator/decoder.py +951 -0
  21. gradiend-0.1.0/gradiend/evaluator/decoder_eval_utils.py +55 -0
  22. gradiend-0.1.0/gradiend/evaluator/encoder.py +243 -0
  23. gradiend-0.1.0/gradiend/evaluator/encoder_metrics.py +398 -0
  24. gradiend-0.1.0/gradiend/evaluator/evaluator.py +280 -0
  25. gradiend-0.1.0/gradiend/model/__init__.py +14 -0
  26. gradiend-0.1.0/gradiend/model/core/__init__.py +17 -0
  27. gradiend-0.1.0/gradiend/model/core/backbone.py +236 -0
  28. gradiend-0.1.0/gradiend/model/layers.py +129 -0
  29. gradiend-0.1.0/gradiend/model/model.py +791 -0
  30. gradiend-0.1.0/gradiend/model/model_with_gradiend.py +890 -0
  31. gradiend-0.1.0/gradiend/model/param_mapped.py +916 -0
  32. gradiend-0.1.0/gradiend/model/utils.py +376 -0
  33. gradiend-0.1.0/gradiend/py.typed +0 -0
  34. gradiend-0.1.0/gradiend/trainer/__init__.py +70 -0
  35. gradiend-0.1.0/gradiend/trainer/config.py +25 -0
  36. gradiend-0.1.0/gradiend/trainer/core/__init__.py +23 -0
  37. gradiend-0.1.0/gradiend/trainer/core/arguments.py +335 -0
  38. gradiend-0.1.0/gradiend/trainer/core/callbacks.py +473 -0
  39. gradiend-0.1.0/gradiend/trainer/core/config.py +15 -0
  40. gradiend-0.1.0/gradiend/trainer/core/dataset.py +215 -0
  41. gradiend-0.1.0/gradiend/trainer/core/feature_definition.py +672 -0
  42. gradiend-0.1.0/gradiend/trainer/core/protocols.py +65 -0
  43. gradiend-0.1.0/gradiend/trainer/core/pruning.py +385 -0
  44. gradiend-0.1.0/gradiend/trainer/core/stats.py +152 -0
  45. gradiend-0.1.0/gradiend/trainer/core/training.py +549 -0
  46. gradiend-0.1.0/gradiend/trainer/factory.py +80 -0
  47. gradiend-0.1.0/gradiend/trainer/text/__init__.py +36 -0
  48. gradiend-0.1.0/gradiend/trainer/text/common/__init__.py +21 -0
  49. gradiend-0.1.0/gradiend/trainer/text/common/dataset.py +51 -0
  50. gradiend-0.1.0/gradiend/trainer/text/common/dataset_base.py +148 -0
  51. gradiend-0.1.0/gradiend/trainer/text/common/lm_eval.py +505 -0
  52. gradiend-0.1.0/gradiend/trainer/text/common/loading.py +140 -0
  53. gradiend-0.1.0/gradiend/trainer/text/common/model_base.py +97 -0
  54. gradiend-0.1.0/gradiend/trainer/text/prediction/__init__.py +19 -0
  55. gradiend-0.1.0/gradiend/trainer/text/prediction/dataset.py +237 -0
  56. gradiend-0.1.0/gradiend/trainer/text/prediction/decoder_eval_utils.py +392 -0
  57. gradiend-0.1.0/gradiend/trainer/text/prediction/decoder_only_mlm.py +405 -0
  58. gradiend-0.1.0/gradiend/trainer/text/prediction/model_with_gradiend.py +184 -0
  59. gradiend-0.1.0/gradiend/trainer/text/prediction/trainer.py +2199 -0
  60. gradiend-0.1.0/gradiend/trainer/text/prediction/unified_data.py +680 -0
  61. gradiend-0.1.0/gradiend/trainer/text/prediction/unified_schema.py +26 -0
  62. gradiend-0.1.0/gradiend/trainer/trainer.py +1668 -0
  63. gradiend-0.1.0/gradiend/util/__init__.py +2 -0
  64. gradiend-0.1.0/gradiend/util/logging.py +91 -0
  65. gradiend-0.1.0/gradiend/util/metrics.py +191 -0
  66. gradiend-0.1.0/gradiend/util/paths.py +373 -0
  67. gradiend-0.1.0/gradiend/util/util.py +121 -0
  68. gradiend-0.1.0/gradiend/visualizer/__init__.py +30 -0
  69. gradiend-0.1.0/gradiend/visualizer/convergence.py +559 -0
  70. gradiend-0.1.0/gradiend/visualizer/encoder_distributions.py +535 -0
  71. gradiend-0.1.0/gradiend/visualizer/encoder_scatter.py +248 -0
  72. gradiend-0.1.0/gradiend/visualizer/plot_optional.py +35 -0
  73. gradiend-0.1.0/gradiend/visualizer/probability_shifts.py +373 -0
  74. gradiend-0.1.0/gradiend/visualizer/topk/__init__.py +26 -0
  75. gradiend-0.1.0/gradiend/visualizer/topk/pairwise_heatmap.py +621 -0
  76. gradiend-0.1.0/gradiend/visualizer/topk/venn_.py +298 -0
  77. gradiend-0.1.0/gradiend/visualizer/visualizer.py +99 -0
  78. gradiend-0.1.0/gradiend.egg-info/PKG-INFO +130 -0
  79. gradiend-0.1.0/gradiend.egg-info/SOURCES.txt +107 -0
  80. gradiend-0.1.0/gradiend.egg-info/dependency_links.txt +1 -0
  81. gradiend-0.1.0/gradiend.egg-info/requires.txt +33 -0
  82. gradiend-0.1.0/gradiend.egg-info/top_level.txt +1 -0
  83. gradiend-0.1.0/pyproject.toml +96 -0
  84. gradiend-0.1.0/setup.cfg +4 -0
  85. gradiend-0.1.0/tests/test_backbone_params.py +162 -0
  86. gradiend-0.1.0/tests/test_callbacks.py +315 -0
  87. gradiend-0.1.0/tests/test_data_generation.py +202 -0
  88. gradiend-0.1.0/tests/test_data_loading.py +187 -0
  89. gradiend-0.1.0/tests/test_data_module.py +303 -0
  90. gradiend-0.1.0/tests/test_datasets_common.py +506 -0
  91. gradiend-0.1.0/tests/test_datasets_text.py +634 -0
  92. gradiend-0.1.0/tests/test_decoder_plotting.py +235 -0
  93. gradiend-0.1.0/tests/test_decoder_probability_computation.py +209 -0
  94. gradiend-0.1.0/tests/test_decoder_strengthen_weaken_dataset.py +267 -0
  95. gradiend-0.1.0/tests/test_evaluator.py +517 -0
  96. gradiend-0.1.0/tests/test_examples.py +257 -0
  97. gradiend-0.1.0/tests/test_get_encoder_metrics.py +569 -0
  98. gradiend-0.1.0/tests/test_img_format.py +240 -0
  99. gradiend-0.1.0/tests/test_lightweight_metrics.py +70 -0
  100. gradiend-0.1.0/tests/test_merged_classes.py +282 -0
  101. gradiend-0.1.0/tests/test_model.py +783 -0
  102. gradiend-0.1.0/tests/test_optional_dependencies.py +532 -0
  103. gradiend-0.1.0/tests/test_trainer_data_inputs.py +337 -0
  104. gradiend-0.1.0/tests/test_trainer_model.py +510 -0
  105. gradiend-0.1.0/tests/test_training_arguments.py +183 -0
  106. gradiend-0.1.0/tests/test_training_loop.py +422 -0
  107. gradiend-0.1.0/tests/test_unified_data.py +335 -0
  108. gradiend-0.1.0/tests/test_use_cache_evaluate_decoder.py +109 -0
  109. gradiend-0.1.0/tests/test_workflow_encoder_decoder.py +107 -0
@@ -0,0 +1,20 @@
1
+ cff-version: 1.2.0
2
+ message: If you use this software, please cite both the article from preferred-citation and the software itself.
3
+ authors:
4
+ - family-names: Jonathan Drechsel
5
+ given-names: Steffen Herbold
6
+ title: 'GRADIEND: Feature Learning within Neural Networks Exemplified through Biases'
7
+ version: 1.0.0
8
+ url: https://arxiv.org/abs/2502.01406
9
+ date-released: '2025-02-04'
10
+ preferred-citation:
11
+ authors:
12
+ - family-names: Jonathan Drechsel
13
+ given-names: Steffen Herbold
14
+ title: 'GRADIEND: Feature Learning within Neural Networks Exemplified through Biases'
15
+ url: https://arxiv.org/abs/2502.01406
16
+ type: generic
17
+ year: '2025'
18
+ conference: {}
19
+ publisher: {}
20
+
gradiend-0.1.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include LICENSE
3
+ include CITATION.cff
4
+ recursive-include examples *.py *.md
5
+ recursive-exclude * __pycache__
6
+ recursive-exclude * *.py[co]
@@ -0,0 +1,130 @@
1
+ Metadata-Version: 2.4
2
+ Name: gradiend
3
+ Version: 0.1.0
4
+ Summary: GRADIEND: Gradient-based Targeted Feature Learning within Neural Networks
5
+ Author-email: Jonathan Drechsel <jonathan.drechsel@uni-passau.de>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/aieng-lab/gradiend
8
+ Project-URL: Documentation, https://aieng-lab.github.io/gradiend/
9
+ Project-URL: Repository, https://github.com/aieng-lab/gradiend
10
+ Project-URL: Bug Tracker, https://github.com/aieng-lab/gradiend/issues
11
+ Keywords: machine learning,neural networks,feature learning,interpretability
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: torch>=2.0.0
24
+ Requires-Dist: transformers<5.0.0,>=4.30.0
25
+ Requires-Dist: numpy>=1.20.0
26
+ Requires-Dist: pandas>=1.3.0
27
+ Requires-Dist: tqdm>=4.60.0
28
+ Provides-Extra: recommended
29
+ Requires-Dist: safetensors>=0.3.0; extra == "recommended"
30
+ Requires-Dist: sentencepiece>=0.1.99; extra == "recommended"
31
+ Requires-Dist: datasets>=3.0.2; extra == "recommended"
32
+ Requires-Dist: spacy>=3.5.0; extra == "recommended"
33
+ Requires-Dist: pyarrow>=14.0.0; extra == "recommended"
34
+ Requires-Dist: matplotlib>=3.5.0; extra == "recommended"
35
+ Requires-Dist: seaborn>=0.12.0; extra == "recommended"
36
+ Requires-Dist: venn>=0.1.0; extra == "recommended"
37
+ Requires-Dist: matplotlib_venn>=0.11.7; extra == "recommended"
38
+ Provides-Extra: data
39
+ Requires-Dist: datasets>=3.0.2; extra == "data"
40
+ Requires-Dist: spacy>=3.5.0; extra == "data"
41
+ Requires-Dist: pyarrow>=14.0.0; extra == "data"
42
+ Provides-Extra: plot
43
+ Requires-Dist: matplotlib>=3.5.0; extra == "plot"
44
+ Requires-Dist: seaborn>=0.12.0; extra == "plot"
45
+ Requires-Dist: venn>=0.1.0; extra == "plot"
46
+ Requires-Dist: matplotlib_venn>=0.11.7; extra == "plot"
47
+ Provides-Extra: dev
48
+ Requires-Dist: mkdocs-material>=9.0.0; extra == "dev"
49
+ Requires-Dist: mkdocstrings[python]>=0.24.0; extra == "dev"
50
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
51
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
52
+ Dynamic: license-file
53
+
54
+ # GRADIEND
55
+
56
+ **Gradient-based targeted feature learning within neural networks.** Learn where a language model encodes a feature (e.g. gender, race) and rewrite the model to strengthen or weaken it—for example debias it—while keeping other behaviour unchanged.
57
+
58
+ [![arXiv:2502.01406](https://img.shields.io/badge/arXiv-2502.01406-blue.svg)](https://arxiv.org/abs/2502.01406)
59
+ [![arXiv:2601.09313](https://img.shields.io/badge/arXiv-2601.09313-blue.svg)](https://arxiv.org/abs/2601.09313)
60
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
61
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
62
+ [![Tests](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml/badge.svg)](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml)
63
+ [![Documentation](https://img.shields.io/badge/docs-github.io-blue.svg)](https://aieng-lab.github.io/gradiend/)
64
+
65
+ Paper: [GRADIEND: Feature Learning within Neural Networks Exemplified through Biases](https://arxiv.org/abs/2502.01406)
66
+
67
+ ---
68
+
69
+ ## Install
70
+
71
+ ```bash
72
+ pip install gradiend
73
+ ```
74
+
75
+ Optional extras (install one or combine with e.g. `gradiend[data,plot]`):
76
+
77
+ | Tag | Command | Includes |
78
+ |-----|---------|----------------------------------------------|
79
+ | **recommended** | `pip install gradiend[recommended]` | full data generation, training, and plotting |
80
+ | **data** | `pip install gradiend[data]` | for advanced data generation and import |
81
+ | **plot** | `pip install gradiend[plot]` | for plotting support | |
82
+ | **dev** | `pip install gradiend[dev]` | for building docs and running tests |
83
+
84
+ ---
85
+
86
+ ## Links
87
+
88
+ | | |
89
+ |---|---|
90
+ | **Documentation** | [aieng-lab.github.io/gradiend](https://aieng-lab.github.io/gradiend/) |
91
+ | **Source & issues** | [github.com/aieng-lab/gradiend](https://github.com/aieng-lab/gradiend) |
92
+ | **Example scripts & notebooks** | [gradiend/examples](https://github.com/aieng-lab/gradiend/tree/main/gradiend/examples) — start_workflow, english_pronouns, gender_de, gender_en, race_religion, etc. |
93
+ | **Datasets (Hugging Face)** | [de-gender-case-articles](https://huggingface.co/datasets/aieng-lab/de-gender-case-articles), [gradiend_race_data](https://huggingface.co/datasets/aieng-lab/gradiend_race_data), [gradiend_religion_data](https://huggingface.co/datasets/aieng-lab/gradiend_religion_data), [biasneutral](https://huggingface.co/datasets/aieng-lab/biasneutral), [geneutral](https://huggingface.co/datasets/aieng-lab/geneutral) |
94
+ | **Pre-trained GRADIEND models** | [bert-base-cased-gradiend-gender-debiased](https://huggingface.co/aieng-lab/bert-base-cased-gradiend-gender-debiased), [gpt2-gradiend-gender-debiased](https://huggingface.co/aieng-lab/gpt2-gradiend-gender-debiased), [Llama-3.2-3B-gradiend-gender-debiased](https://huggingface.co/aieng-lab/Llama-3.2-3B-gradiend-gender-debiased) |
95
+
96
+ ---
97
+
98
+ ## Citation
99
+
100
+ ```bibtex
101
+ @misc{drechsel2025gradiend,
102
+ title={{GRADIEND}: Feature Learning within Neural Networks Exemplified through Biases},
103
+ author={Jonathan Drechsel and Steffen Herbold},
104
+ year={2025},
105
+ eprint={2502.01406},
106
+ archivePrefix={arXiv},
107
+ primaryClass={cs.LG},
108
+ url={https://arxiv.org/abs/2502.01406},
109
+ }
110
+ ```
111
+
112
+ German definite articles study:
113
+
114
+ ```bibtex
115
+ @misc{drechsel2026understanding,
116
+ title={Understanding or Memorizing? A Case Study of German Definite Articles in Language Models},
117
+ author={Jonathan Drechsel and Erisa Bytyqi and Steffen Herbold},
118
+ year={2026},
119
+ eprint={2601.09313},
120
+ archivePrefix={arXiv},
121
+ primaryClass={cs.CL},
122
+ url={https://arxiv.org/abs/2601.09313},
123
+ }
124
+ ```
125
+
126
+ ---
127
+
128
+ ## License
129
+
130
+ Apache 2.0. See [LICENSE](https://github.com/aieng-lab/gradiend/blob/main/LICENSE) on GitHub.
gradiend-0.1.0/PYPI.md ADDED
@@ -0,0 +1,77 @@
1
+ # GRADIEND
2
+
3
+ **Gradient-based targeted feature learning within neural networks.** Learn where a language model encodes a feature (e.g. gender, race) and rewrite the model to strengthen or weaken it—for example debias it—while keeping other behaviour unchanged.
4
+
5
+ [![arXiv:2502.01406](https://img.shields.io/badge/arXiv-2502.01406-blue.svg)](https://arxiv.org/abs/2502.01406)
6
+ [![arXiv:2601.09313](https://img.shields.io/badge/arXiv-2601.09313-blue.svg)](https://arxiv.org/abs/2601.09313)
7
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
8
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
9
+ [![Tests](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml/badge.svg)](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml)
10
+ [![Documentation](https://img.shields.io/badge/docs-github.io-blue.svg)](https://aieng-lab.github.io/gradiend/)
11
+
12
+ Paper: [GRADIEND: Feature Learning within Neural Networks Exemplified through Biases](https://arxiv.org/abs/2502.01406)
13
+
14
+ ---
15
+
16
+ ## Install
17
+
18
+ ```bash
19
+ pip install gradiend
20
+ ```
21
+
22
+ Optional extras (install one or combine with e.g. `gradiend[data,plot]`):
23
+
24
+ | Tag | Command | Includes |
25
+ |-----|---------|----------------------------------------------|
26
+ | **recommended** | `pip install gradiend[recommended]` | full data generation, training, and plotting |
27
+ | **data** | `pip install gradiend[data]` | for advanced data generation and import |
28
+ | **plot** | `pip install gradiend[plot]` | for plotting support | |
29
+ | **dev** | `pip install gradiend[dev]` | for building docs and running tests |
30
+
31
+ ---
32
+
33
+ ## Links
34
+
35
+ | | |
36
+ |---|---|
37
+ | **Documentation** | [aieng-lab.github.io/gradiend](https://aieng-lab.github.io/gradiend/) |
38
+ | **Source & issues** | [github.com/aieng-lab/gradiend](https://github.com/aieng-lab/gradiend) |
39
+ | **Example scripts & notebooks** | [gradiend/examples](https://github.com/aieng-lab/gradiend/tree/main/gradiend/examples) — start_workflow, english_pronouns, gender_de, gender_en, race_religion, etc. |
40
+ | **Datasets (Hugging Face)** | [de-gender-case-articles](https://huggingface.co/datasets/aieng-lab/de-gender-case-articles), [gradiend_race_data](https://huggingface.co/datasets/aieng-lab/gradiend_race_data), [gradiend_religion_data](https://huggingface.co/datasets/aieng-lab/gradiend_religion_data), [biasneutral](https://huggingface.co/datasets/aieng-lab/biasneutral), [geneutral](https://huggingface.co/datasets/aieng-lab/geneutral) |
41
+ | **Pre-trained GRADIEND models** | [bert-base-cased-gradiend-gender-debiased](https://huggingface.co/aieng-lab/bert-base-cased-gradiend-gender-debiased), [gpt2-gradiend-gender-debiased](https://huggingface.co/aieng-lab/gpt2-gradiend-gender-debiased), [Llama-3.2-3B-gradiend-gender-debiased](https://huggingface.co/aieng-lab/Llama-3.2-3B-gradiend-gender-debiased) |
42
+
43
+ ---
44
+
45
+ ## Citation
46
+
47
+ ```bibtex
48
+ @misc{drechsel2025gradiend,
49
+ title={{GRADIEND}: Feature Learning within Neural Networks Exemplified through Biases},
50
+ author={Jonathan Drechsel and Steffen Herbold},
51
+ year={2025},
52
+ eprint={2502.01406},
53
+ archivePrefix={arXiv},
54
+ primaryClass={cs.LG},
55
+ url={https://arxiv.org/abs/2502.01406},
56
+ }
57
+ ```
58
+
59
+ German definite articles study:
60
+
61
+ ```bibtex
62
+ @misc{drechsel2026understanding,
63
+ title={Understanding or Memorizing? A Case Study of German Definite Articles in Language Models},
64
+ author={Jonathan Drechsel and Erisa Bytyqi and Steffen Herbold},
65
+ year={2026},
66
+ eprint={2601.09313},
67
+ archivePrefix={arXiv},
68
+ primaryClass={cs.CL},
69
+ url={https://arxiv.org/abs/2601.09313},
70
+ }
71
+ ```
72
+
73
+ ---
74
+
75
+ ## License
76
+
77
+ Apache 2.0. See [LICENSE](https://github.com/aieng-lab/gradiend/blob/main/LICENSE) on GitHub.
@@ -0,0 +1,227 @@
1
+ # GRADIEND
2
+
3
+ [![arXiv:2502.01406](https://img.shields.io/badge/arXiv-2502.01406-blue.svg)](https://arxiv.org/abs/2502.01406)
4
+ [![arXiv:2601.09313](https://img.shields.io/badge/arXiv-2601.09313-blue.svg)](https://arxiv.org/abs/2601.09313)
5
+ [![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)
6
+ [![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+ [![Tests](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml/badge.svg)](https://github.com/aieng-lab/gradiend/actions/workflows/tests.yml)
8
+ [![Documentation](https://img.shields.io/badge/docs-github.io-blue.svg)](https://aieng-lab.github.io/gradiend/)
9
+
10
+ GRADIEND (Gradient-based targeted feature learning within neural networks) learns features inside language models by training an encoder-decoder on gradients. Find where a model encodes a feature (e.g. gender, race) and rewrite the model to strengthen or weaken it—for example debias it—while keeping other behaviour. See [GRADIEND: Feature Learning within Neural Networks Exemplified through Biases](https://arxiv.org/abs/2502.01406).
11
+
12
+ ![GRADIEND overview](docs/img/workflow-diagram.png)
13
+
14
+
15
+ ## Installation
16
+
17
+ With minimal requirements (enables full data generation, training and evaluation workflow):
18
+ ```bash
19
+ pip install gradiend
20
+ ```
21
+
22
+ With recommended dependencies (enables additional features such as Hugging Face datasets, safetensors, and plotting):
23
+
24
+ ```bash
25
+ pip install gradiend[recommended]
26
+ ```
27
+
28
+ Similar, `gradiend[data]` and `gradiend[plot]` only adds additional dependencies for data generation and plotting, respectively.
29
+
30
+ Installation from source:
31
+
32
+ ```bash
33
+ git clone https://github.com/aieng-lab/gradiend.git
34
+ cd gradiend
35
+ pip install -e ".[recommended]"
36
+ ```
37
+
38
+ ## Quick start
39
+
40
+ Self-contained example (runs with the few sentences below; for real use, add more base data):
41
+
42
+ ```python
43
+ from gradiend import TextPredictionDataCreator, TextFilterConfig, TrainingArguments, TextPredictionTrainer
44
+
45
+ base = [
46
+ "The chef tasted the soup, then he added pepper.",
47
+ "The players ran; they scored.",
48
+ "She handed the package to the courier and asked them to deliver it.",
49
+ # ... see more texts in the dropdown below (needed to run a meaningful training)
50
+ ]
51
+ creator = TextPredictionDataCreator(
52
+ base_data=base,
53
+ feature_targets=[
54
+ TextFilterConfig(targets=["he", "she", "it"], id="3SG"),
55
+ TextFilterConfig(target="they", id="3PL"),
56
+ ],
57
+ )
58
+ training = creator.generate_training_data(max_size_per_class=30)
59
+ neutral = creator.generate_neutral_data(additional_excluded_words=["i", "we", "you"], max_size=10)
60
+
61
+ args = TrainingArguments(train_batch_size=2, eval_steps=5, max_steps=20, learning_rate=1e-3)
62
+ trainer = TextPredictionTrainer(model="bert-base-cased", data=training, eval_neutral_data=neutral, args=args)
63
+ trainer.train()
64
+ trainer.plot_training_convergence()
65
+
66
+ enc_result = trainer.evaluate_encoder(plot=True)
67
+ dec = trainer.evaluate_decoder(plot=True, target_class="3SG")
68
+ changed_model = trainer.rewrite_base_model(decoder_results=dec, target_class="3SG")
69
+ ```
70
+
71
+ <details>
72
+ <summary><strong>More base texts required to run above example </strong> </summary>
73
+
74
+ ```python
75
+ base = [
76
+ "The chef tasted the soup, then he added a pinch of pepper and stirred it.",
77
+ "The pianist closed her eyes and played the final chord; she had practised it for weeks.",
78
+ "The dog ran to the door; it wanted to go outside and chase the ball.",
79
+ "The report will be ready for review by the end of the week.",
80
+ "His phone rang twice before he picked it up and left the room.",
81
+ "She handed the package to the courier and asked them to deliver it by noon.",
82
+ "The players on both teams huddled on the pitch before they ran back to their positions.",
83
+ "The author read a paragraph from his novel to the audience and then they asked him questions.",
84
+ "Breakfast is included for all guests staying at the hotel.",
85
+ "The nurse checked her clipboard and told the family members they could all visit soon.",
86
+ "The birds gathered on the wire; when the cat moved they flew away at once.",
87
+ "The mechanic wiped his hands and said the car would be ready by Friday; he had fixed it.",
88
+ "She opened the window and watched the leaves fall in the garden below.",
89
+ "The committee members met on Tuesday and they voted to postpone the decision.",
90
+ "When the referee blew the whistle he showed the player a yellow card.",
91
+ "The volunteers packed the boxes and said they would all load the van at dawn.",
92
+ "Her brother and his colleagues sent a text saying they were stuck in traffic together.",
93
+ "The dog barked at the postman and he dropped the letters when it lunged.",
94
+ "The meeting ran over time so the chair cut the last two items; the participants agreed they would meet again.",
95
+ "The weather improved by the afternoon and the streets dried quickly.",
96
+ "Rain poured all morning but the basement stayed dry; the pump had run all night and it held.",
97
+ "She found the recipe in the drawer; it calls for butter, flour, and a pinch of salt.",
98
+ "The road was closed for repairs and the local officials said they expected the detour would add twenty minutes.",
99
+ "The film ended at midnight and the group of friends all went home; they shared umbrellas in the rain.",
100
+ "The museum opens at nine and closes at six on weekdays.",
101
+ "Trains run every fifteen minutes during peak hours.",
102
+ "The recipe works best when the oven is preheated properly.",
103
+ "The meeting has been moved to the smaller room on the third floor.",
104
+ "The gardener pruned the roses and he left the cuttings by the gate.",
105
+ "The drummer lost her stick in the middle of the song but she kept the beat.",
106
+ "One of the students raised her hand and asked them to repeat the question.",
107
+ "The board members announced that they had approved the budget; the CEO signed it.",
108
+ "He left the book on the table and she noticed the door was open when it swung.",
109
+ "The car broke down on the motorway and it had to be towed away.",
110
+ "The cat jumped off the wall; it landed in the flower bed and ran off.",
111
+ "Two colleagues from his team offered him a seat but he preferred to stand; she nodded and they sat down.",
112
+ "Coffee was served in the lobby while the conference continued upstairs.",
113
+ "Keys were left on the counter by the front door.",
114
+ "Parking is available in the lot behind the building.",
115
+ "The driver signalled left and he turned into the car park.",
116
+ "She replied to the email before the other team members had a chance to follow up, and they thanked her later.",
117
+ "The laptop was slow so it needed a restart and more memory.",
118
+ "The lecture will be recorded and posted online by tomorrow.",
119
+ "His keys fell under the seat and he had to reach for them.",
120
+ "The team members celebrated after they won the final match.",
121
+ "The manager gave his approval and then the project team met; they scheduled the launch.",
122
+ "They invited her to the meeting and she accepted on the spot.",
123
+ "Lunch will be served in the canteen from twelve to two.",
124
+ "The doctor checked her notes and told the patients they could go home; they thanked her at the door.",
125
+ "The fans in the crowd cheered when they saw the result on the screen.",
126
+ "When the alarm went off he switched it off and got up.",
127
+ "The staff members finished the inventory and they reported the count.",
128
+ "Her colleague forwarded the file and the two of them opened it together; they checked every page.",
129
+ "The cat stretched and it jumped onto the sofa.",
130
+ "The council members met last night and they approved the new bylaws.",
131
+ "The coach gave his feedback and the players practised the drill again; they repeated it three times.",
132
+ "The intern made her first presentation and her colleagues in the room asked a few questions; they praised her work.",
133
+ "The dog waited by the bowl; it had not been fed yet.",
134
+ "The panel members discussed the proposal and they reached a consensus.",
135
+ "He locked the office and she set the alarm before they left the building together.",
136
+ "The van pulled up and it unloaded the delivery at the back.",
137
+ "The results are published on the intranet every Friday.",
138
+ "The neighbours across the street waved to her and she waved back as they all passed by.",
139
+ "Snow fell all day but the gritters had been out and it cleared.",
140
+ "The schedule is on the wall next to the break room.",
141
+ "The bus was late so her friends and she missed the start of the film; they had to sneak into their seats.",
142
+ "The document is in the shared folder and can be edited by anyone.",
143
+ "The waiter brought the bill and he left the tip on the table.",
144
+ "The singer forgot the words but she carried on and the audience members applauded; they gave her a standing ovation.",
145
+ "The printer ran out of paper and it stopped mid-job.",
146
+ "The deadline has been extended to the end of the month.",
147
+ "The committee will reconvene next week to finalise the report.",
148
+ "Tea and biscuits are provided in the kitchen on each floor.",
149
+ "She booked the room and the hotel staff confirmed the reservation by email; they also sent her directions.",
150
+ "The gate was left open so the horse got out and it wandered off.",
151
+ "The contract is valid for twelve months from the signing date.",
152
+ ]
153
+ ```
154
+ </details>
155
+
156
+ <p align="center">
157
+ <img src="docs/img/readme_training_convergence.png"
158
+ width="32%"
159
+ style="vertical-align: middle;" />
160
+ <img src="docs/img/readme_encoder_analysis_split_test.png"
161
+ width="32%"
162
+ style="vertical-align: middle;" />
163
+ <img src="docs/img/readme_decoder_probability_shifts_3SG.png"
164
+ width="32%"
165
+ style="vertical-align: middle;" />
166
+ </p>
167
+
168
+
169
+
170
+ More examples: [gradiend/examples](https://github.com/aieng-lab/gradiend/tree/main/gradiend/examples)
171
+
172
+ ## Documentation
173
+
174
+ - [Documentation](https://aieng-lab.github.io/gradiend/)
175
+ - [Installation details](docs/installation.md)
176
+ - [Train Your first GRADIEND Model](docs/start.md) — minimal workflow
177
+ - [Tutorials](docs/index.md#tutorials)
178
+ - [API reference](docs/api-reference.md)
179
+
180
+ ## Examples
181
+
182
+ Example scripts and notebooks: [gradiend/examples](https://github.com/aieng-lab/gradiend/tree/main/gradiend/examples) on GitHub (not in the pip package; download a file or read to get inspired).
183
+
184
+ - [start_workflow.py](gradiend/examples/start_workflow.py) — Minimal runnable example
185
+ - [english_pronouns.ipynb](gradiend/examples/english_pronouns.ipynb) — English pronouns (3SG vs 3PL): data creation from Wikipedia → training → evaluation ([script](gradiend/examples/english_pronouns.py))
186
+ - [gender_de.py](gradiend/examples/gender_de.py) — German gender (masc_nom vs fem_nom)
187
+ - [gender_en.py](gradiend/examples/gender_en.py) — English gender with name augmentation
188
+ - [gender_de_decoder_only.py](gradiend/examples/gender_de_decoder_only.py) — Decoder-only model with optional MLM head
189
+ - [race_religion.py](gradiend/examples/race_religion.py) — Race and religion bias
190
+
191
+ ## Datasets and models
192
+
193
+ **Datasets (Hugging Face):** [de-gender-case-articles](https://huggingface.co/datasets/aieng-lab/de-gender-case-articles), [gradiend_race_data](https://huggingface.co/datasets/aieng-lab/gradiend_race_data), [gradiend_religion_data](https://huggingface.co/datasets/aieng-lab/gradiend_religion_data), [biasneutral](https://huggingface.co/datasets/aieng-lab/biasneutral), [geneutral](https://huggingface.co/datasets/aieng-lab/geneutral), and more.
194
+
195
+ **Pre-trained GRADIEND models:** [bert-base-cased-gradiend-gender-debiased](https://huggingface.co/aieng-lab/bert-base-cased-gradiend-gender-debiased), [gpt2-gradiend-gender-debiased](https://huggingface.co/aieng-lab/gpt2-gradiend-gender-debiased), [Llama-3.2-3B-gradiend-gender-debiased](https://huggingface.co/aieng-lab/Llama-3.2-3B-gradiend-gender-debiased), and others.
196
+
197
+ ## Citation
198
+
199
+ ```bibtex
200
+ @misc{drechsel2025gradiend,
201
+ title={{GRADIEND}: Feature Learning within Neural Networks Exemplified through Biases},
202
+ author={Jonathan Drechsel and Steffen Herbold},
203
+ year={2025},
204
+ eprint={2502.01406},
205
+ archivePrefix={arXiv},
206
+ primaryClass={cs.LG},
207
+ url={https://arxiv.org/abs/2502.01406},
208
+ }
209
+ ```
210
+
211
+ For the German definite articles study using GRADIEND:
212
+
213
+ ```bibtex
214
+ @misc{drechsel2026understanding,
215
+ title={Understanding or Memorizing? A Case Study of German Definite Articles in Language Models},
216
+ author={Jonathan Drechsel and Erisa Bytyqi and Steffen Herbold},
217
+ year={2026},
218
+ eprint={2601.09313},
219
+ archivePrefix={arXiv},
220
+ primaryClass={cs.CL},
221
+ url={https://arxiv.org/abs/2601.09313},
222
+ }
223
+ ```
224
+
225
+ ## License
226
+
227
+ Apache 2.0. See [LICENSE](LICENSE).