congrads 0.1.0__tar.gz → 0.2.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.
congrads-0.2.0/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright 2024 DTAI - KU Leuven
2
+
3
+ Redistribution and use in source and binary forms, with or without modification,
4
+ are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice,
7
+ this list of conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ 3. Neither the name of the copyright holder nor the names of its
14
+ contributors may be used to endorse or promote products derived from
15
+ this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
18
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -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,183 @@
1
+ # Congrads
2
+
3
+ **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.
4
+
5
+ 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.
6
+
7
+ > **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.
8
+
9
+ ## Key Features
10
+
11
+ - **Constraint-Guided Training**: Add constraints to guide the optimization process, ensuring that your model generalizes better by trying to satisfy the constraints.
12
+ - **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.
13
+ - **Seamless PyTorch Integration**: Use Congrads within your existing PyTorch workflows with minimal setup.
14
+ - **Flexible and extendible**: Write your own custom networks, constraints and dataset classes to easily extend the functionality of the toolbox.
15
+
16
+ ## Installation
17
+
18
+ Currently, the **Congrads** toolbox can only be installed using pip. We will later expand to other package managers such as conda.
19
+
20
+ ```bash
21
+ pip install congrads
22
+ ```
23
+
24
+ ## Getting Started
25
+
26
+ ### 1. **Prerequisites**
27
+
28
+ Before you can use **Congrads**, make sure you have the following installed:
29
+
30
+ - Python 3.6+ (preffered version 3.11)
31
+ - **PyTorch** (install with CUDA support for GPU training, refer to [PyTorch's getting started guide](https://pytorch.org/get-started/locally/))
32
+ - **NumPy** (install with ```pip install numpy```, or refer to [NumPy's install guide](https://numpy.org/install/).)
33
+ - **Pandas** (install with ```pip install pandas```, or refer to [Panda's install guide](https://pandas.pydata.org/docs/getting_started/install.html).)
34
+
35
+ ### 2. **Installation**
36
+
37
+ Please install **Congrads** via pip:
38
+
39
+ ```bash
40
+ pip install congrads
41
+ ```
42
+
43
+ ### 3. **Basic Usage**
44
+
45
+ #### 1. Import necessary classes and functions from the toolbox
46
+
47
+ To start using the toolbox, import the required modules and functions. This includes classes for defining constraints, data processing, network setup, and training utilities.
48
+
49
+ ```python
50
+ from congrads.constraints import BinaryConstraint, ScalarConstraint, Constraint
51
+ from congrads.core import CongradsCore
52
+ from congrads.datasets import BiasCorrection
53
+ from congrads.descriptor import Descriptor
54
+ from congrads.metrics import MetricManager
55
+ from congrads.networks import MLPNetwork
56
+ from congrads.utils import preprocess_BiasCorrection, splitDataLoaders
57
+
58
+ ```
59
+
60
+ #### 2. Set up data and preprocessing
61
+
62
+ 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.
63
+
64
+ ```python
65
+ # Load and preprocess data
66
+ data = BiasCorrection("./datasets", preprocess_BiasCorrection)
67
+ loaders = splitDataLoaders(
68
+ data, loader_args={"batch_size": 100, "shuffle": True, "num_workers": 6}
69
+ )
70
+ ```
71
+
72
+ #### 3. Configure the network
73
+
74
+ 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.
75
+
76
+ ```python
77
+ # Instantiate network and push to correct device
78
+ network = MLPNetwork(25, 2, n_hidden_layers=3, hidden_dim=35)
79
+ network = network.to(device)
80
+ ```
81
+
82
+ #### 4. Instantiate loss and optimizer
83
+
84
+ 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.
85
+
86
+ ```python
87
+ # Instantiate loss and optimizer
88
+ criterion = MSELoss()
89
+ optimizer = Adam(network.parameters(), lr=0.001)
90
+ ```
91
+
92
+ #### 5. Set up the descriptor
93
+
94
+ 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.
95
+
96
+ ```python
97
+ # Descriptor setup
98
+ descriptor = Descriptor()
99
+ descriptor.add("output", 0, "Tmax", output=True)
100
+ descriptor.add("output", 1, "Tmin", output=True)
101
+ ```
102
+
103
+ #### 6. Define constraints on your network
104
+
105
+ 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.
106
+
107
+ ```python
108
+ # Constraints definition
109
+ Constraint.descriptor = descriptor
110
+ constraints = [
111
+ ScalarConstraint("Tmin", ge, 0), # Tmin >= 0
112
+ ScalarConstraint("Tmin", le, 1), # Tmin <= 1
113
+ ScalarConstraint("Tmax", ge, 0), # Tmax >= 0
114
+ ScalarConstraint("Tmax", le, 1), # Tmax <= 1
115
+ BinaryConstraint("Tmax", gt, "Tmin"), # Tmax > Tmin
116
+ ]
117
+ ```
118
+
119
+ #### 7. Set up trainer
120
+
121
+ 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.
122
+
123
+ ```python
124
+ # Initialize metrics
125
+ writer = SummaryWriter()
126
+ metric_manager = MetricManager(writer, device)
127
+ ```
128
+
129
+ #### 8. Initialize and configure the core learner
130
+
131
+ 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.
132
+
133
+ ```python
134
+ # Instantiate core
135
+ core = CongradsCore(
136
+ descriptor,
137
+ constraints,
138
+ loaders,
139
+ network,
140
+ criterion,
141
+ optimizer,
142
+ metric_manager,
143
+ device,
144
+ )
145
+ ```
146
+
147
+ #### 9. Start training
148
+
149
+ 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.
150
+
151
+ ```python
152
+ # Start training
153
+ core.fit(max_epochs=150)
154
+
155
+ # Close writer
156
+ writer.close()
157
+ ```
158
+
159
+ ## Example Use Cases
160
+
161
+ - **Optimization with Domain Knowledge**: Ensure outputs meet real-world restrictions or safety standards.
162
+ - **Physics-Informed Neural Networks (PINNs)**: Enforce physical laws as constraints in your models.
163
+ - **Improve Training Process**: Inject domain knowledge in the training stage, increasing learning efficiency.
164
+
165
+ ## Roadmap
166
+
167
+ - [ ] Documentation and Notebook examples
168
+ - [ ] Add support for constraint parser that can interpret equations
169
+ - [x] Add better handling of metric logging and visualization
170
+ - [x] Revise if Pytorch Lightning is preferable over plain Pytorch
171
+ - [ ] Determine if it is feasible to add unit and or functional tests
172
+
173
+ ## Contributing
174
+
175
+ We welcome contributions to Congrads! Whether you want to report issues, suggest features, or contribute code via issues and pull requests.
176
+
177
+ ## License
178
+
179
+ 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.
180
+
181
+ ---
182
+
183
+ Elevate your neural networks with Congrads! 🚀
@@ -1,21 +1,22 @@
1
1
  # __init__.py
2
+ version = "0.2.0"
2
3
 
3
4
  # Only expose the submodules, not individual classes
4
- from . import core
5
5
  from . import constraints
6
+ from . import core
6
7
  from . import datasets
7
8
  from . import descriptor
8
- from . import learners
9
9
  from . import metrics
10
10
  from . import networks
11
+ from . import utils
11
12
 
12
13
  # Define __all__ to specify that the submodules are accessible, but not classes directly.
13
14
  __all__ = [
14
- "core",
15
15
  "constraints",
16
+ "core",
16
17
  "datasets",
17
18
  "descriptor",
18
- "learners",
19
19
  "metrics",
20
- "networks"
21
- ]
20
+ "networks",
21
+ "utils",
22
+ ]