Skip to content

ADD: Implement Product T-norm for AND operator#135

Open
Vrinda12-tech wants to merge 1 commit into
IBM:masterfrom
Vrinda12-tech:master
Open

ADD: Implement Product T-norm for AND operator#135
Vrinda12-tech wants to merge 1 commit into
IBM:masterfrom
Vrinda12-tech:master

Conversation

@Vrinda12-tech

Copy link
Copy Markdown

Summary

Implements the Product T-norm as an alternative AND operator for Logical Neural Networks.

Changes

  • Added lnn/neural/methods/product.py – Product activation class with log-space implementation.
  • Added ProductAnd symbolic node in n_ary_neuron.py.
  • Registered ProductAnd in lnn/symbolic/logic/__init__.py and lnn/__init__.py.
  • Added unit test 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 -v passes.
  • All existing tests remain unaffected.

Trade-offs

  • Advantage: Smooth gradients everywhere, no clamping artifacts.
  • Disadvantage: Gradients scale multiplicatively (∂(x*y)/∂x = y), which can cause vanishing gradients in very deep networks. This is a known and documented trade-off.

Copilot AI review requested due to automatic review settings July 5, 2026 11:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py implementing a Product activation (and aliases for ProductAnd).
  • Added ProductAnd symbolic node to lnn/symbolic/logic/n_ary_neuron.py and exported it through lnn/symbolic/logic/__init__.py and lnn/__init__.py.
  • Added tests/reasoning/logic/propositional/test_product_and_1.py to 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants