congrads 0.1.0__py3-none-any.whl → 0.2.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.
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.1
2
+ Name: congrads
3
+ Version: 0.2.0
4
+ Summary: A toolbox for using Constraint Guided Gradient Descent when training neural networks.
5
+ Author-email: Wout Rombouts <wout.rombouts@kuleuven.be>, Quinten Van Baelen <quinten.vanbaelen@kuleuven.be>, Peter Karsmakers <peter.karsmakers@kuleuven.be>
6
+ License: Copyright 2024 DTAI - KU Leuven
7
+
8
+ Redistribution and use in source and binary forms, with or without modification,
9
+ are permitted provided that the following conditions are met:
10
+
11
+ 1. Redistributions of source code must retain the above copyright notice,
12
+ this list of conditions and the following disclaimer.
13
+
14
+ 2. Redistributions in binary form must reproduce the above copyright notice,
15
+ this list of conditions and the following disclaimer in the documentation
16
+ and/or other materials provided with the distribution.
17
+
18
+ 3. Neither the name of the copyright holder nor the names of its
19
+ contributors may be used to endorse or promote products derived from
20
+ this software without specific prior written permission.
21
+
22
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
23
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+
33
+ Requires-Python: >=3.9
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Requires-Dist: torch>=1.12.0
37
+ Requires-Dist: pandas>=2.2.2
38
+ Requires-Dist: numpy>=1.26.4
39
+
40
+ # Congrads
41
+
42
+ **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.
43
+
44
+ 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.
45
+
46
+ > **Note:** The Congrads toolbox is **currently in alpha phase**. Expect significant changes, potential bugs, and incomplete features as we continue to develop and improve the functionality. Feedback is highly appreciated during this phase to help us refine the toolbox and ensure its reliability in later stages.
47
+
48
+ ## Key Features
49
+
50
+ - **Constraint-Guided Training**: Add constraints to guide the optimization process, ensuring that your model generalizes better by trying to satisfy the constraints.
51
+ - **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.
52
+ - **Seamless PyTorch Integration**: Use Congrads within your existing PyTorch workflows with minimal setup.
53
+ - **Flexible and extendible**: Write your own custom networks, constraints and dataset classes to easily extend the functionality of the toolbox.
54
+
55
+ ## Installation
56
+
57
+ Currently, the **Congrads** toolbox can only be installed using pip. We will later expand to other package managers such as conda.
58
+
59
+ ```bash
60
+ pip install congrads
61
+ ```
62
+
63
+ ## Getting Started
64
+
65
+ ### 1. **Prerequisites**
66
+
67
+ Before you can use **Congrads**, make sure you have the following installed:
68
+
69
+ - Python 3.6+ (preffered version 3.11)
70
+ - **PyTorch** (install with CUDA support for GPU training, refer to [PyTorch's getting started guide](https://pytorch.org/get-started/locally/))
71
+ - **NumPy** (install with ```pip install numpy```, or refer to [NumPy's install guide](https://numpy.org/install/).)
72
+ - **Pandas** (install with ```pip install pandas```, or refer to [Panda's install guide](https://pandas.pydata.org/docs/getting_started/install.html).)
73
+
74
+ ### 2. **Installation**
75
+
76
+ Please install **Congrads** via pip:
77
+
78
+ ```bash
79
+ pip install congrads
80
+ ```
81
+
82
+ ### 3. **Basic Usage**
83
+
84
+ #### 1. Import necessary classes and functions from the toolbox
85
+
86
+ To start using the toolbox, import the required modules and functions. This includes classes for defining constraints, data processing, network setup, and training utilities.
87
+
88
+ ```python
89
+ from congrads.constraints import BinaryConstraint, ScalarConstraint, Constraint
90
+ from congrads.core import CongradsCore
91
+ from congrads.datasets import BiasCorrection
92
+ from congrads.descriptor import Descriptor
93
+ from congrads.metrics import MetricManager
94
+ from congrads.networks import MLPNetwork
95
+ from congrads.utils import preprocess_BiasCorrection, splitDataLoaders
96
+
97
+ ```
98
+
99
+ #### 2. Set up data and preprocessing
100
+
101
+ The toolbox works with various datasets, and for this example, we are using the **BiasCorrection** dataset. After loading the dataset, it is preprocessed using a utility function and split into train, validation, and test sets using DataLoader instances.
102
+
103
+ ```python
104
+ # Load and preprocess data
105
+ data = BiasCorrection("./datasets", preprocess_BiasCorrection)
106
+ loaders = splitDataLoaders(
107
+ data, loader_args={"batch_size": 100, "shuffle": True, "num_workers": 6}
108
+ )
109
+ ```
110
+
111
+ #### 3. Configure the network
112
+
113
+ The model architecture used here is a Multi-Layer Perceptron (MLP) with 25 input features, 2 output features, and 3 hidden layers, each containing 35 neurons. The network outputs are later mapped to meaningful names using the descriptor.
114
+
115
+ ```python
116
+ # Instantiate network and push to correct device
117
+ network = MLPNetwork(25, 2, n_hidden_layers=3, hidden_dim=35)
118
+ network = network.to(device)
119
+ ```
120
+
121
+ #### 4. Instantiate loss and optimizer
122
+
123
+ Define the loss function and optimizer, which are critical for training the model. In this example, we use the Mean Squared Error (MSE) loss function and the Adam optimizer with a learning rate of 0.001.
124
+
125
+ ```python
126
+ # Instantiate loss and optimizer
127
+ criterion = MSELoss()
128
+ optimizer = Adam(network.parameters(), lr=0.001)
129
+ ```
130
+
131
+ #### 5. Set up the descriptor
132
+
133
+ The descriptor serves as a mapping between network layers and their semantic meanings. For this example, the network's two outputs are named ```Tmax``` (maximum temperature) and ```Tmin``` (minimum temperature), which correspond to specific columns in the dataset.
134
+
135
+ ```python
136
+ # Descriptor setup
137
+ descriptor = Descriptor()
138
+ descriptor.add("output", 0, "Tmax", output=True)
139
+ descriptor.add("output", 1, "Tmin", output=True)
140
+ ```
141
+
142
+ #### 6. Define constraints on your network
143
+
144
+ Constraints are rules applied to the network's behavior, ensuring its outputs meet specific criteria. Using the descriptor, constraints can be defined for named outputs. In this case, constraints enforce bounds (e.g., ```0 <= Tmin <= 1```) and relationships (```Tmax > Tmin```) on the outputs.
145
+
146
+ ```python
147
+ # Constraints definition
148
+ Constraint.descriptor = descriptor
149
+ constraints = [
150
+ ScalarConstraint("Tmin", ge, 0), # Tmin >= 0
151
+ ScalarConstraint("Tmin", le, 1), # Tmin <= 1
152
+ ScalarConstraint("Tmax", ge, 0), # Tmax >= 0
153
+ ScalarConstraint("Tmax", le, 1), # Tmax <= 1
154
+ BinaryConstraint("Tmax", gt, "Tmin"), # Tmax > Tmin
155
+ ]
156
+ ```
157
+
158
+ #### 7. Set up trainer
159
+
160
+ Metrics are used to evaluate and track the model's performance during training. A ```MetricManager``` is instantiated with a TensorBoard writer to log metrics and visualize training progress.
161
+
162
+ ```python
163
+ # Initialize metrics
164
+ writer = SummaryWriter()
165
+ metric_manager = MetricManager(writer, device)
166
+ ```
167
+
168
+ #### 8. Initialize and configure the core learner
169
+
170
+ The core of the toolbox is the ```CongradsCore``` class, which integrates the descriptor, constraints, data loaders, network, loss function, optimizer, and metrics to manage the learning process.
171
+
172
+ ```python
173
+ # Instantiate core
174
+ core = CongradsCore(
175
+ descriptor,
176
+ constraints,
177
+ loaders,
178
+ network,
179
+ criterion,
180
+ optimizer,
181
+ metric_manager,
182
+ device,
183
+ )
184
+ ```
185
+
186
+ #### 9. Start training
187
+
188
+ The ```fit``` method of the core class starts the training loop for the specified number of epochs. At the end of training, the TensorBoard writer is closed to finalize the logs.
189
+
190
+ ```python
191
+ # Start training
192
+ core.fit(max_epochs=150)
193
+
194
+ # Close writer
195
+ writer.close()
196
+ ```
197
+
198
+ ## Example Use Cases
199
+
200
+ - **Optimization with Domain Knowledge**: Ensure outputs meet real-world restrictions or safety standards.
201
+ - **Physics-Informed Neural Networks (PINNs)**: Enforce physical laws as constraints in your models.
202
+ - **Improve Training Process**: Inject domain knowledge in the training stage, increasing learning efficiency.
203
+
204
+ ## Roadmap
205
+
206
+ - [ ] Documentation and Notebook examples
207
+ - [ ] Add support for constraint parser that can interpret equations
208
+ - [x] Add better handling of metric logging and visualization
209
+ - [x] Revise if Pytorch Lightning is preferable over plain Pytorch
210
+ - [ ] Determine if it is feasible to add unit and or functional tests
211
+
212
+ ## Contributing
213
+
214
+ We welcome contributions to Congrads! Whether you want to report issues, suggest features, or contribute code via issues and pull requests.
215
+
216
+ ## License
217
+
218
+ 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.
219
+
220
+ ---
221
+
222
+ Elevate your neural networks with Congrads! 🚀
@@ -0,0 +1,13 @@
1
+ congrads/__init__.py,sha256=XnRKk4VTheZJj6Z1f8x5Iq2YPtd2fycUgQazOqiqOEw,458
2
+ congrads/constraints.py,sha256=JgO8SUhSKHoBH-WvFdwYnhkVl_jO-RqwgHCVOR1_F-8,13488
3
+ congrads/core.py,sha256=egFph4MhKncwOi6pcTzDFGqh9bIxb7-z68899TxcEQM,7696
4
+ congrads/datasets.py,sha256=uTDwnjwA52wwT6Hv4Kw0WqKi2dMDJE3nP-xKB6AjCNw,5470
5
+ congrads/descriptor.py,sha256=FzmFZBHZ3nhd8NS951EJqcM97C-XsoRIA9qK6rmeBU4,1520
6
+ congrads/metrics.py,sha256=ct4wj8q-GL3lYXxBeNCsCvCLn0TPBbs_8ybiMe-Fw5w,1471
7
+ congrads/networks.py,sha256=QpuEgHmkXDCrTbonHoXbbRblZIdpYsopqMST--Ki9i4,3256
8
+ congrads/utils.py,sha256=Z4ElFFreacRN7qPXh7Gv5lIzdAs5gtvVloJnHag2E9g,13890
9
+ congrads-0.2.0.dist-info/LICENSE,sha256=hDkSuSj1L5IpO9uhrag5zd29HicibbYX8tUbY3RXF40,1480
10
+ congrads-0.2.0.dist-info/METADATA,sha256=jaWNoJ4AeWB4aCL47Tw4OsXwOb-q5w4rwYKxSE76CKM,9804
11
+ congrads-0.2.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
12
+ congrads-0.2.0.dist-info/top_level.txt,sha256=B8M9NmtHbmzp-3APHe4C0oo7aRIWRHWoba9FIy9XeYM,9
13
+ congrads-0.2.0.dist-info/RECORD,,
congrads/learners.py DELETED
@@ -1,233 +0,0 @@
1
- import logging
2
- from typing import Union
3
- from torch import Tensor
4
- from torch.nn import Module
5
- from torch.nn.modules.loss import _Loss
6
- from torch.optim import Optimizer
7
-
8
- from .core import CGGDModule
9
- from .constraints import Constraint
10
- from .descriptor import Descriptor
11
-
12
-
13
- class Learner(CGGDModule):
14
- def __init__(
15
- self,
16
- network: Module,
17
- descriptor: Descriptor,
18
- constraints: list[Constraint],
19
- loss_function: Union[_Loss, dict[str, _Loss]],
20
- optimizer: Optimizer,
21
- ):
22
- """
23
- A class that integrates a neural network with a training and validation loop,
24
- supporting single or multi-output loss functions. The class manages the forward pass,
25
- training step, and validation step while also configuring the optimizer.
26
-
27
- Args:
28
- network (Module): The neural network model to be trained.
29
- descriptor (Descriptor): An object that defines the structure of the network,
30
- including the output layers.
31
- constraints (list[Constraint]): A list of constraints that can be applied during training.
32
- loss_function (Union[_Loss, dict[str, _Loss]]): A loss function or a dictionary of loss functions
33
- for each output layer.
34
- optimizer (Optimizer): The optimizer used for training the model.
35
-
36
- Raises:
37
- ValueError: If the descriptor does not contain any output layers or if the number of loss functions
38
- does not match the number of output layers when using a dictionary of loss functions.
39
- """
40
-
41
- # Init parent class
42
- super().__init__(descriptor, constraints)
43
-
44
- # Init object variables
45
- self.network = network
46
- self.descriptor = descriptor
47
- self.loss_function = loss_function
48
- self.optimizer = optimizer
49
-
50
- # Perform checks
51
- if len(self.descriptor.output_layers) == 0:
52
- raise ValueError(
53
- 'The descriptor class must contain one or more output layers. Mark a layer as output by setting descriptor.add("layer", ..., output=True).'
54
- )
55
-
56
- if isinstance(loss_function, _Loss):
57
- if len(self.descriptor.output_layers) > 1:
58
- logging.warning(
59
- f"Multiple layers were marked as output, but only one loss function is defined. Only the loss of layer {list(self.descriptor.output_layers)[0]} will be calculated and used. To use the same loss function for all output layers, please specify then explicitly."
60
- )
61
-
62
- if isinstance(loss_function, dict):
63
- if len(self.descriptor.output_layers) != len(loss_function):
64
- raise ValueError(
65
- f"The number of marked output layers does not match the number of provided loss functions."
66
- )
67
-
68
- # Assign proper step function based on if one or multiple loss functions are assigned
69
- if isinstance(loss_function, _Loss):
70
- self.training_step = self.training_step_single
71
- self.validation_step = self.validation_step_single
72
-
73
- if isinstance(loss_function, dict):
74
- self.training_step = self.training_step_multi
75
- self.validation_step = self.validation_step_multi
76
-
77
- def forward(self, x):
78
- """
79
- Perform a forward pass through the network.
80
-
81
- Args:
82
- x (Tensor): The input tensor to pass through the network.
83
-
84
- Returns:
85
- Tensor: The model's output for the given input.
86
- """
87
-
88
- return self.network(x)
89
-
90
- def training_step_single(self, batch, batch_idx):
91
- """
92
- Perform a single training step using a single loss function.
93
-
94
- Args:
95
- batch (tuple): A tuple containing the input and target output tensors.
96
- batch_idx (int): The index of the batch in the current epoch.
97
-
98
- Returns:
99
- Tensor: The loss value for the batch.
100
- """
101
-
102
- self.train()
103
-
104
- inputs, outputs = batch
105
- prediction: dict[str, Tensor] = self(inputs)
106
-
107
- layer = list(self.descriptor.output_layers)[0]
108
- loss = self.loss_function(prediction[layer], outputs)
109
-
110
- self.log(
111
- "train_loss",
112
- loss,
113
- on_step=False,
114
- on_epoch=True,
115
- )
116
-
117
- return super().training_step(prediction, loss)
118
-
119
- def training_step_multi(self, batch, batch_idx):
120
- """
121
- Perform a training step using multiple loss functions, one for each output layer.
122
-
123
- Args:
124
- batch (tuple): A tuple containing the input and target output tensors.
125
- batch_idx (int): The index of the batch in the current epoch.
126
-
127
- Returns:
128
- Tensor: The total loss value for the batch, combining the losses from all output layers.
129
- """
130
-
131
- self.train()
132
-
133
- inputs, outputs = batch
134
- prediction: dict[str, Tensor] = self(inputs)
135
-
136
- # TODO add hyperparameter to scale loss per function
137
- loss = 0
138
- for layer in self.descriptor.output_layers:
139
- layer_loss = self.loss_function[layer](prediction[layer], outputs)
140
- loss += layer_loss
141
-
142
- self.log(
143
- f"train_loss_{layer}",
144
- layer_loss,
145
- on_step=False,
146
- on_epoch=True,
147
- )
148
-
149
- self.log(
150
- "train_loss",
151
- loss,
152
- on_step=False,
153
- on_epoch=True,
154
- )
155
-
156
- return super().training_step(prediction, loss)
157
-
158
- def validation_step_single(self, batch, batch_idx):
159
- """
160
- Perform a single validation step using a single loss function.
161
-
162
- Args:
163
- batch (tuple): A tuple containing the input and target output tensors.
164
- batch_idx (int): The index of the batch in the current epoch.
165
-
166
- Returns:
167
- Tensor: The validation loss for the batch.
168
- """
169
-
170
- self.eval()
171
-
172
- inputs, outputs = batch
173
- prediction: dict[str, Tensor] = self(inputs)
174
-
175
- layer = list(self.descriptor.output_layers)[0]
176
- loss = self.loss_function(prediction[layer], outputs)
177
-
178
- self.log(
179
- "valid_loss",
180
- loss,
181
- on_step=False,
182
- on_epoch=True,
183
- )
184
-
185
- return super().validation_step(prediction, loss)
186
-
187
- def validation_step_multi(self, batch, batch_idx):
188
- """
189
- Perform a validation step using multiple loss functions, one for each output layer.
190
-
191
- Args:
192
- batch (tuple): A tuple containing the input and target output tensors.
193
- batch_idx (int): The index of the batch in the current epoch.
194
-
195
- Returns:
196
- Tensor: The total validation loss for the batch, combining the losses from all output layers.
197
- """
198
-
199
- self.eval()
200
-
201
- inputs, outputs = batch
202
- prediction: dict[str, Tensor] = self(inputs)
203
-
204
- loss = 0
205
- for layer in self.descriptor.output_layers:
206
- layer_loss = self.loss_function[layer](prediction[layer], outputs)
207
- loss += layer_loss
208
-
209
- self.log(
210
- f"valid_loss_{layer}",
211
- layer_loss,
212
- on_step=False,
213
- on_epoch=True,
214
- )
215
-
216
- self.log(
217
- "valid_loss",
218
- loss,
219
- on_step=False,
220
- on_epoch=True,
221
- )
222
-
223
- return super().validation_step(prediction, loss)
224
-
225
- def configure_optimizers(self):
226
- """
227
- Configure the optimizer for training.
228
-
229
- Returns:
230
- Optimizer: The optimizer used to update the model's parameters during training.
231
- """
232
-
233
- return self.optimizer
@@ -1,34 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 DTAI - KU Leuven
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-
23
-
24
- "Commons Clause" License Condition v1.0
25
-
26
- The Software is provided to you by the Licensor under the License, as defined below, subject to the following condition.
27
-
28
- Without limiting other conditions in the License, the grant of rights under the License will not include, and the License does not grant to you, the right to Sell the Software.
29
-
30
- For purposes of the foregoing, "Sell" means practicing any or all of the rights granted to you under the License to provide to third parties, for a fee or other consideration (including without limitation fees for hosting or consulting/ support services related to the Software), a product or service whose value derives, entirely or substantially, from the functionality of the Software. Any license notice or attribution required by the License must also include this Commons Clause License Condition notice.
31
-
32
- Software: All CGGD-Toolbox associated files.
33
- License: MIT
34
- Licensor: DTAI - KU Leuven