congrads 0.1.0__py3-none-any.whl → 0.3.0__py3-none-any.whl
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.
- congrads/__init__.py +10 -20
- congrads/callbacks/base.py +357 -0
- congrads/callbacks/registry.py +106 -0
- congrads/checkpoints.py +178 -0
- congrads/constraints/base.py +242 -0
- congrads/constraints/registry.py +1255 -0
- congrads/core/batch_runner.py +200 -0
- congrads/core/congradscore.py +271 -0
- congrads/core/constraint_engine.py +209 -0
- congrads/core/epoch_runner.py +119 -0
- congrads/datasets/registry.py +799 -0
- congrads/descriptor.py +147 -43
- congrads/metrics.py +116 -41
- congrads/networks/registry.py +68 -0
- congrads/py.typed +0 -0
- congrads/transformations/base.py +37 -0
- congrads/transformations/registry.py +86 -0
- congrads/utils/preprocessors.py +439 -0
- congrads/utils/utility.py +506 -0
- congrads/utils/validation.py +182 -0
- congrads-0.3.0.dist-info/METADATA +234 -0
- congrads-0.3.0.dist-info/RECORD +23 -0
- congrads-0.3.0.dist-info/WHEEL +4 -0
- congrads/constraints.py +0 -507
- congrads/core.py +0 -211
- congrads/datasets.py +0 -742
- congrads/learners.py +0 -233
- congrads/networks.py +0 -91
- congrads-0.1.0.dist-info/LICENSE +0 -34
- congrads-0.1.0.dist-info/METADATA +0 -196
- congrads-0.1.0.dist-info/RECORD +0 -13
- congrads-0.1.0.dist-info/WHEEL +0 -5
- congrads-0.1.0.dist-info/top_level.txt +0 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: congrads
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A toolbox for using Constraint Guided Gradient Descent when training neural networks.
|
|
5
|
+
Author: Wout Rombouts, Quinten Van Baelen, Peter Karsmakers
|
|
6
|
+
Author-email: Wout Rombouts <wout.rombouts@kuleuven.be>, Quinten Van Baelen <quinten.vanbaelen@kuleuven.be>, Peter Karsmakers <peter.karsmakers@kuleuven.be>
|
|
7
|
+
License: Copyright 2024 DTAI - KU Leuven
|
|
8
|
+
|
|
9
|
+
Redistribution and use in source and binary forms, with or without modification,
|
|
10
|
+
are permitted provided that the following conditions are met:
|
|
11
|
+
|
|
12
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer.
|
|
14
|
+
|
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
16
|
+
this list of conditions and the following disclaimer in the documentation
|
|
17
|
+
and/or other materials provided with the distribution.
|
|
18
|
+
|
|
19
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
20
|
+
contributors may be used to endorse or promote products derived from
|
|
21
|
+
this software without specific prior written permission.
|
|
22
|
+
|
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
|
|
24
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
25
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
27
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
29
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
30
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
31
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
32
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
33
|
+
Requires-Dist: numpy>=1.24.0
|
|
34
|
+
Requires-Dist: pandas>=1.5.0
|
|
35
|
+
Requires-Dist: torch>=2.0.0
|
|
36
|
+
Requires-Dist: torchvision>=0.15.1
|
|
37
|
+
Requires-Dist: tqdm>=4.65.0
|
|
38
|
+
Requires-Dist: matplotlib>=3.7.0 ; extra == 'examples'
|
|
39
|
+
Requires-Dist: tensorboard>=2.18.0 ; extra == 'examples'
|
|
40
|
+
Requires-Python: >=3.11
|
|
41
|
+
Provides-Extra: examples
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
|
|
44
|
+
<div align="center">
|
|
45
|
+
<img src="https://github.com/ML-KULeuven/congrads/blob/main/docs/_static/congrads_export.png?raw=true" height="200">
|
|
46
|
+
<p>
|
|
47
|
+
<b>Incorporate constraints into neural network training for more reliable and robust models.</b>
|
|
48
|
+
</p>
|
|
49
|
+
<br/>
|
|
50
|
+
|
|
51
|
+
[](https://pypi.org/project/congrads)
|
|
52
|
+
[](https://congrads.readthedocs.io)
|
|
53
|
+
[](https://pypi.org/project/congrads)
|
|
54
|
+
[](https://pypistats.org/packages/congrads)
|
|
55
|
+
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
56
|
+
|
|
57
|
+
<br/>
|
|
58
|
+
<br/>
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
**Congrads** is a Python toolbox that brings **constraint-guided gradient descent** capabilities to your machine learning projects. Built with seamless integration into PyTorch, Congrads empowers you to enhance the training and optimization process by incorporating constraints into your training pipeline.
|
|
62
|
+
|
|
63
|
+
Whether you're working with simple inequality constraints, combinations of input-output relations, or custom constraint formulations, Congrads provides the tools and flexibility needed to build more robust and generalized models.
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
> **Notice:** All previous `v1.x` releases are **yanked**.
|
|
67
|
+
> The library is still in active development, and backwards compatibility is not guaranteed.
|
|
68
|
+
> Please use the new `v0.x` series for ongoing updates.
|
|
69
|
+
|
|
70
|
+
## Key Features
|
|
71
|
+
|
|
72
|
+
- **Constraint-Guided Training**: Add constraints to guide the optimization process, ensuring that your model generalizes better by trying to satisfy the constraints.
|
|
73
|
+
- **Flexible Constraint Definition**: Define constraints on inputs, outputs, or combinations thereof, using an intuitive and extendable interface. Make use of pre-programmed constraint classes or write your own.
|
|
74
|
+
- **Seamless PyTorch Integration**: Use Congrads within your existing PyTorch workflows with minimal setup.
|
|
75
|
+
- **Flexible and extendible**: Write your own custom networks, constraints and dataset classes to easily extend the functionality of the toolbox.
|
|
76
|
+
|
|
77
|
+
## Getting Started
|
|
78
|
+
|
|
79
|
+
### 1. **Installation**
|
|
80
|
+
|
|
81
|
+
First, make sure to install PyTorch since Congrads heavily relies on its deep learning framework. Please refer to the [PyTorch's getting started guide](https://pytorch.org/get-started/locally/). Make sure to install with CUDA support for GPU training.
|
|
82
|
+
|
|
83
|
+
Next, install the Congrads toolbox. The recommended way to install it is to use pip:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install congrads
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
You can also install Congrads together with extra packages required to run the examples:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pip install congrads[examples]
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This should automatically install all required dependencies for you. If you would like to install dependencies manually, Congrads depends on the following:
|
|
96
|
+
|
|
97
|
+
- Python 3.11 - 3.13
|
|
98
|
+
- **PyTorch** (install with CUDA support for GPU training, refer to [PyTorch's getting started guide](https://pytorch.org/get-started/locally/))
|
|
99
|
+
- **NumPy** (install with `pip install numpy`, or refer to [NumPy's install guide](https://numpy.org/install/).)
|
|
100
|
+
- **Pandas** (install with `pip install pandas`, or refer to [Panda's install guide](https://pandas.pydata.org/docs/getting_started/install.html).)
|
|
101
|
+
- **Tqdm** (install with `pip install tqdm`)
|
|
102
|
+
- **Torchvision** (install with `pip install torchvision`)
|
|
103
|
+
- Optional: **Tensorboard** (install with `pip install tensorboard`)
|
|
104
|
+
|
|
105
|
+
### 2. **Core concepts**
|
|
106
|
+
|
|
107
|
+
Before diving into the toolbox, it is recommended to familiarize yourself with Congrads's core concept and topics.
|
|
108
|
+
Please read the documentation at https://congrads.readthedocs.io/en/latest/ to get up-to-date.
|
|
109
|
+
|
|
110
|
+
### 3. **Basic Usage**
|
|
111
|
+
|
|
112
|
+
Below, a basic example can be found that illustrates how to work with the Congrads toolbox.
|
|
113
|
+
For additional examples, refer to the [examples](https://github.com/ML-KULeuven/congrads/tree/main/examples) and [notebooks](https://github.com/ML-KULeuven/congrads/tree/main/notebooks) folders in the repository.
|
|
114
|
+
|
|
115
|
+
#### 1. First, select the device to run your code on with.
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
use_cuda = torch.cuda.is_available()
|
|
119
|
+
device = torch.device("cuda:0" if use_cuda else "cpu")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### 2. Next, load your data and split it into training, validation and testing subsets.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
data = BiasCorrection(
|
|
126
|
+
"./datasets", preprocess_BiasCorrection, download=True
|
|
127
|
+
)
|
|
128
|
+
loaders = split_data_loaders(
|
|
129
|
+
data,
|
|
130
|
+
loader_args={"batch_size": 100, "shuffle": True},
|
|
131
|
+
valid_loader_args={"shuffle": False},
|
|
132
|
+
test_loader_args={"shuffle": False},
|
|
133
|
+
)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
#### 3. Instantiate your neural network, make sure the dimensions match up with your data.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
network = MLPNetwork(25, 2, n_hidden_layers=3, hidden_dim=35)
|
|
140
|
+
network = network.to(device)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### 4. Choose your loss function and optimizer.
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
criterion = MSELoss()
|
|
147
|
+
optimizer = Adam(network.parameters(), lr=0.001)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### 5. Then, setup the descriptor, that will attach names to specific parts of your network.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
descriptor = Descriptor()
|
|
154
|
+
descriptor.add("output", 0, "Tmax")
|
|
155
|
+
descriptor.add("output", 1, "Tmin")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 6. Define your constraints on the network.
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
Constraint.descriptor = descriptor
|
|
162
|
+
Constraint.device = device
|
|
163
|
+
constraints = [
|
|
164
|
+
ScalarConstraint("Tmin", ge, 0),
|
|
165
|
+
ScalarConstraint("Tmin", le, 1),
|
|
166
|
+
ScalarConstraint("Tmax", ge, 0),
|
|
167
|
+
ScalarConstraint("Tmax", le, 1),
|
|
168
|
+
BinaryConstraint("Tmax", gt, "Tmin"),
|
|
169
|
+
]
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
#### 7. Instantiate metric manager and core, and start the training.
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
metric_manager = MetricManager()
|
|
176
|
+
core = CongradsCore(
|
|
177
|
+
descriptor,
|
|
178
|
+
constraints,
|
|
179
|
+
loaders,
|
|
180
|
+
network,
|
|
181
|
+
criterion,
|
|
182
|
+
optimizer,
|
|
183
|
+
metric_manager,
|
|
184
|
+
device,
|
|
185
|
+
checkpoint_manager,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
core.fit(max_epochs=50)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Example Use Cases
|
|
192
|
+
|
|
193
|
+
- **Optimization with Domain Knowledge**: Ensure outputs meet real-world restrictions or safety standards.
|
|
194
|
+
- **Improve Training Process**: Inject domain knowledge in the training stage, increasing learning efficiency.
|
|
195
|
+
- **Physics-Informed Neural Networks (PINNs)**: Coming soon, Enforce physical laws as constraints in your models.
|
|
196
|
+
|
|
197
|
+
## Planned changes / Roadmap
|
|
198
|
+
|
|
199
|
+
- [ ] Add ODE/PDE constraints to support PINNs
|
|
200
|
+
- [x] Rework callback system
|
|
201
|
+
- [ ] Add support for constraint parser that can interpret equations
|
|
202
|
+
|
|
203
|
+
## Research
|
|
204
|
+
|
|
205
|
+
If you make use of this package or it's concepts in your research, please consider citing the following papers.
|
|
206
|
+
|
|
207
|
+
- Van Baelen, Q., & Karsmakers, P. (2023). **Constraint guided gradient descent: Training with inequality constraints with applications in regression and semantic segmentation.**
|
|
208
|
+
Neurocomputing, 556, 126636. doi:10.1016/j.neucom.2023.126636 <br/>[ [pdf](https://www.sciencedirect.com/science/article/abs/pii/S0925231223007592) | [bibtex](https://raw.githubusercontent.com/ML-KULeuven/congrads/main/docs/_static/VanBaelen2023.bib) ]
|
|
209
|
+
|
|
210
|
+
## Contributing
|
|
211
|
+
|
|
212
|
+
We welcome contributions to Congrads! Whether you want to report issues, suggest features, or contribute code via issues and pull requests.
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
Congrads is licensed under the [The 3-Clause BSD License](LICENSE). We encourage companies that are interested in a collaboration for a specific topic to contact the authors for more information or to set up joint research projects.
|
|
217
|
+
|
|
218
|
+
## Contacts
|
|
219
|
+
|
|
220
|
+
Feel free to contact any of the below contact persons for more information or details about the project. Companies interested in a collaboration, or to set up joint research projects are also encouraged to get in touch with us.
|
|
221
|
+
|
|
222
|
+
- Peter Karsmakers [ [email](mailto:peter.karsmakers@kuleuven.be) | [website](https://www.kuleuven.be/wieiswie/en/person/00047893) ]
|
|
223
|
+
- Quinten Van Baelen [ [email](mailto:quinten.vanbaelen@kuleuven.be) | [website](https://www.kuleuven.be/wieiswie/en/person/00125540) ]
|
|
224
|
+
|
|
225
|
+
## Contributors
|
|
226
|
+
|
|
227
|
+
Below you find a list of people who contributed in making the toolbox. Feel free to contact them for any repository- or code-specific questions, suggestions or remarks.
|
|
228
|
+
|
|
229
|
+
- Wout Rombouts [ [email](mailto:wout.rombouts@kuleuven.be) | [github profile](https://github.com/rombie18) ]
|
|
230
|
+
- Quinten Van Baelen [ [email](mailto:quinten.vanbaelen@kuleuven.be) | [github profile](https://github.com/quinten-vb) ]
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
Elevate your neural networks with Congrads! 🚀
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
congrads/__init__.py,sha256=XJKWRteSvmTYawgS1Pon8kWhhd3haKo4RGAsyjEGS8Q,383
|
|
2
|
+
congrads/callbacks/base.py,sha256=OChXls-tndgJQOXNfqavnPywHZHn3N87yLKD4kbHDHk,13714
|
|
3
|
+
congrads/callbacks/registry.py,sha256=KkzjDqMS3CkE__PpGrmAEYwRngqGSdQNE8NVWl7ogeA,3898
|
|
4
|
+
congrads/checkpoints.py,sha256=V79n3mqjB48nbNkBELqKDg9iou0b1vc5eRrlcu8aIA4,7228
|
|
5
|
+
congrads/constraints/base.py,sha256=k9OyPS2A4bP3fSEAEANGuw7zofiWlGIxqb5ows1LQWs,10105
|
|
6
|
+
congrads/constraints/registry.py,sha256=k__RfcXle-qDL9OJ-nfwgL9zeM6-ISwgQyIzmx-lsgc,52302
|
|
7
|
+
congrads/core/batch_runner.py,sha256=emc7smJLDHq0J8_J9t9X0RtqrXaYwOP9mmhlX_M78e4,6967
|
|
8
|
+
congrads/core/congradscore.py,sha256=9ZKUVMB9RbmudtuC-MQcNColBYUjb6XLqb0eISzfrGk,12070
|
|
9
|
+
congrads/core/constraint_engine.py,sha256=UEt-tmtJeJX0Wu3ol17Z0A9hacL0F8oouJUwHgIIoDE,8994
|
|
10
|
+
congrads/core/epoch_runner.py,sha256=l0x3uLXQ5I5o1C63wXgL4_QkhFmXxW-jeejNJK6sf18,4093
|
|
11
|
+
congrads/datasets/registry.py,sha256=RfffRiA7Qijc69cJTBJhItTZ8x9B-p1kXMjvcfEC_nA,31102
|
|
12
|
+
congrads/descriptor.py,sha256=tUHF4vvyNzJP5vpq1xn0uhKnOlAkElwG2R9gG4glHvQ,6914
|
|
13
|
+
congrads/metrics.py,sha256=e52QC8yNKsxAndjC3U4WMUnQ_0GmiSlExKtxRRShHao,4625
|
|
14
|
+
congrads/networks/registry.py,sha256=UPzPDU0wI2zoOEvi697QBSDOtaa3Rc0rgCb-tCxbjak,2252
|
|
15
|
+
congrads/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
|
+
congrads/transformations/base.py,sha256=KZQkloaDGcqAp8EhlUnHL8VfZqSq8OCqp_Iy_a2Nfns,900
|
|
17
|
+
congrads/transformations/registry.py,sha256=p2cLnt3X1bspEPfR7IVd31qPXQimVe_bRu2VhUOIZj0,2607
|
|
18
|
+
congrads/utils/preprocessors.py,sha256=oqW3hV_yoUd-6I-NSoE61e_JDNEPnBJvvvdsuKd9Ekg,18190
|
|
19
|
+
congrads/utils/utility.py,sha256=zvOAVjQjtmsvyuJm0rF0cy_jApR6qluQsFxk9ItalzE,18893
|
|
20
|
+
congrads/utils/validation.py,sha256=Jj8ZJGJrrH9B02cIaScsQpne3zjyarkPldDdT1pejVA,6208
|
|
21
|
+
congrads-0.3.0.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
|
|
22
|
+
congrads-0.3.0.dist-info/METADATA,sha256=UCutFzaiD6CaeSr9BqtfiyMS-LsDs6Iwpw8QcXSS2jc,10748
|
|
23
|
+
congrads-0.3.0.dist-info/RECORD,,
|