ADD: Implement Product T-norm for AND operator#135
Open
Vrinda12-tech wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new Product T-norm–based AND connective to LNN, including a new neural activation implementation, a corresponding symbolic node (ProductAnd), API exports/registration, and a propositional unit test.
Changes:
- Added
lnn/neural/methods/product.pyimplementing aProductactivation (and aliases forProductAnd). - Added
ProductAndsymbolic node tolnn/symbolic/logic/n_ary_neuron.pyand exported it throughlnn/symbolic/logic/__init__.pyandlnn/__init__.py. - Added
tests/reasoning/logic/propositional/test_product_and_1.pyto validate product behavior on a grid of inputs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/reasoning/logic/propositional/test_product_and_1.py | Adds a unit test for ProductAnd behavior across sampled inputs. |
| lnn/symbolic/logic/n_ary_neuron.py | Introduces a ProductAnd symbolic connective selecting the Product activation. |
| lnn/symbolic/logic/init.py | Registers/exports ProductAnd in the symbolic logic API. |
| lnn/neural/methods/product.py | Implements the Product T-norm activation (AND/OR/IMPLIES) and aliases for ProductAnd. |
| lnn/init.py | Exposes ProductAnd via the top-level lnn package API. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+24
to
+27
| def __init__(self, num_inputs=2, **kwds): | ||
| super().__init__(**kwds) | ||
| self.weights = torch.nn.Parameter(torch.ones(num_inputs)) | ||
| self.bias = torch.nn.Parameter(torch.tensor(0.0)) |
Comment on lines
+40
to
+52
| # Determine input shape: [batch, num_inputs, 2] or [batch, num_inputs] or ... | ||
| if operand_bounds.dim() == 3 and operand_bounds.shape[-1] == 2: | ||
| # Interval inputs: separate lower and upper | ||
| lower = operand_bounds[..., 0] # [batch, num_inputs] | ||
| upper = operand_bounds[..., 1] # [batch, num_inputs] | ||
| else: | ||
| # Point inputs: treat as degenerate interval (lower == upper) | ||
| # Ensure we have at least 2D: (batch, num_inputs) | ||
| if operand_bounds.dim() == 1: | ||
| operand_bounds = operand_bounds.unsqueeze(0) | ||
| lower = operand_bounds | ||
| upper = operand_bounds # same values | ||
|
|
Comment on lines
+129
to
+136
| if operand_bounds.dim() == 3 and operand_bounds.shape[-1] == 2: | ||
| neg = 1 - operand_bounds # [batch, num_inputs, 2] | ||
| else: | ||
| # assume point values, treat as intervals | ||
| if operand_bounds.dim() == 1: | ||
| operand_bounds = operand_bounds.unsqueeze(0) | ||
| neg = torch.stack([1 - operand_bounds, 1 - operand_bounds], dim=-1) | ||
|
|
Comment on lines
+151
to
+154
| neg_operator = _not(operator_bounds) | ||
| neg_operand = _not(operand_bounds, dim=-1) | ||
| and_result = self._and_downward(neg_operator, neg_operand) | ||
| return _not(and_result, dim=-1) |
Comment on lines
+163
to
+178
| # operand_bounds shape: (batch, 2, 2) or (batch, 2) for point values | ||
| if operand_bounds.dim() == 3 and operand_bounds.shape[-1] == 2: | ||
| # intervals: separate lower/upper for lhs and rhs | ||
| lhs_lower = operand_bounds[..., 0, 0] | ||
| lhs_upper = operand_bounds[..., 0, 1] | ||
| rhs_lower = operand_bounds[..., 1, 0] | ||
| rhs_upper = operand_bounds[..., 1, 1] | ||
| else: | ||
| # point values: treat as equal bounds | ||
| if operand_bounds.dim() == 1: | ||
| operand_bounds = operand_bounds.unsqueeze(0) | ||
| lhs_val = operand_bounds[..., 0] | ||
| rhs_val = operand_bounds[..., 1] | ||
| lhs_lower = lhs_upper = lhs_val | ||
| rhs_lower = rhs_upper = rhs_val | ||
|
|
| from .formula import Formula | ||
| from .leaf_formula import _LeafFormula, Predicate, Predicates, Proposition, Propositions | ||
| from .n_ary_neuron import And, Or, XOr | ||
| from .n_ary_neuron import And, Or, XOr,ProductAnd |
Comment on lines
+1
to
+2
| import pytest | ||
| import torch |
Comment on lines
+16
to
+26
| # ============================================================ | ||
| # CRITICAL: Reset the model state to prevent stale bounds | ||
| # ============================================================ | ||
| if hasattr(model, 'clear_data'): | ||
| model.clear_data() | ||
| elif hasattr(model, 'reset_bounds'): | ||
| model.reset_bounds() | ||
| else: | ||
| # Fallback: manually reset propositions to Unknown | ||
| model.add_data({A: (0.0, 1.0), B: (0.0, 1.0)}) | ||
|
|
Comment on lines
+32
to
+49
| # Retrieve bounds | ||
| bounds = my_and.get_data() | ||
| if bounds is None: | ||
| bounds = my_and.neuron.bounds_table | ||
|
|
||
| # Extract lower bound | ||
| if isinstance(bounds, torch.Tensor): | ||
| # bounds shape is [batch, 2] or [2] | ||
| if bounds.dim() == 2: | ||
| val = bounds[0, 0].item() | ||
| else: | ||
| val = bounds[0].item() | ||
| elif isinstance(bounds, tuple): | ||
| val = bounds[0] | ||
| else: | ||
| val = bounds | ||
|
|
||
| val = float(val) |
Comment on lines
+75
to
+80
| def _and_downward( | ||
| self, operator_bounds: torch.Tensor, operand_bounds: torch.Tensor | ||
| ): | ||
| """Exact algebraic inverse of Product AND. No clamping.""" | ||
| eps = 1e-8 | ||
|
|
Signed-off-by: Vrinda <vrinda.02096211624@adgips.ac.in>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the Product T-norm as an alternative AND operator for Logical Neural Networks.
Changes
lnn/neural/methods/product.py– Product activation class with log-space implementation.ProductAndsymbolic node inn_ary_neuron.py.ProductAndinlnn/symbolic/logic/__init__.pyandlnn/__init__.py.tests/reasoning/logic/propositional/test_product_and_1.py.Why this matters
The Product T-norm provides a smooth, C∞-differentiable alternative to the clamped Łukasiewicz T-norm. It eliminates the zero-gradient plateaus caused by clamping, enabling more stable gradient flow in deep logical graphs.
As shown in the LNN paper (Riegel et al., 2020, Appendix B) and empirically validated in "Evaluating Relaxations of Logic for Neural Networks" (Grespan et al., IJCAI 2021), the Product T-norm often achieves superior predictive performance in practice.
Testing
pytest tests/reasoning/logic/propositional/test_product_and_1.py -vpasses.Trade-offs
∂(x*y)/∂x = y), which can cause vanishing gradients in very deep networks. This is a known and documented trade-off.