A neural network ends in a distribution. A logical constraint says which distributions are acceptable. The loss interface is the simplest way to put the two together: turn the constraint into a scalar penalty, add it to the training objective, and let the gradient push the distribution toward the constraint. This lecture is about that move — why it works, why it is not a guarantee, and where it breaks.
00 / The hookTwo digits, one sum.
A classifier reads two handwritten digits from an image. It outputs a distribution over 0–9 for each digit. The task is not “read digit A” and “read digit B”; it is “the sum is 8”. A purely data-driven model can learn this from enough labeled pairs, but the constraint is free information: it rules out most of the 100 combinations. The loss interface uses it as a penalty:
The data term fits the observations. The logic term measures how far the predicted distribution is from satisfying the constraint. The weight λ decides which one wins when they disagree. That is the whole idea. The rest is the engineering of “how far” and “which distributions”.
A loss does not enforce a constraint; it makes violating it expensive. At the end of training, the constraint may still be violated. If you need a guarantee, the loss interface is the wrong tool; use an architecture that executes the constraint or a verifier that checks the answer.
01 / Hard to softA truth value between 0 and 1.
Classical logic has two truth values. A neural network outputs a probability, or a logit, or a softmax. To connect them, we need a semantics that maps a formula and a probability assignment to a value in [0,1]. The standard construction is fuzzy logic: atoms get values in [0,1]; connectives are t-norms and their duals.
| Family | AND (t-norm) | OR (t-conorm) | Gradient behaviour |
|---|---|---|---|
| Product | a · b | a + b − ab | Smooth; gradients shrink near 0. |
| Łukasiewicz | max(0, a + b − 1) | min(1, a + b) | Piecewise linear; saturates at the bounds. |
| Gödel | min(a, b) | max(a, b) | Gradient flows to one operand; hard to train. |
Negation is usually 1 − a; implication is defined from the t-norm, e.g. a → b = 1 − a + a·b for the product t-norm. The choice is not innocent. It changes which formulas are “almost true”, and it changes which parameters receive gradient. A constraint that is easy to satisfy under one t-norm may be a flat plateau under another.
Fuzzy truth is not probability. A value of 0.8 for “the digit is 3” can mean “80% of the mass is on 3” in a probabilistic reading, or “the degree of truth is 0.8” in a fuzzy reading. The two readings coincide for a single atom and diverge for conjunctions. The semantic loss takes the probabilistic reading seriously; LTN and DL2 are closer to the fuzzy reading. Knowing which one a paper uses is part of reading it.
02 / Semantic lossThe probability that the constraint holds.
Xu et al. (2018) define the semantic loss of a constraint α over Boolean variables X with predicted probabilities p as the negative log-probability that α is satisfied:
The sum is over all assignments that satisfy the constraint. Each assignment contributes the probability that the model assigns to it. The loss is small when the model puts its mass on satisfying assignments, and large when it puts its mass on violations.
For the “exactly one of n variables is true” constraint, the sum simplifies:
If the constraint involves only one variable, the semantic loss reduces to the cross-entropy of that variable. That is a useful sanity check: the semantic loss generalizes cross-entropy from “one variable” to “a set of assignments”.
The gradient has a clean interpretation. It increases the probability of satisfying assignments and decreases the probability of violations, weighted by how much mass each assignment currently has. In the exactly-one case, it pushes the model toward a one-hot distribution over the variables.
Computing the sum in (7.2) is #P-hard in general. Practical systems compile the constraint into a tractable circuit (d-DNNF, SDD) and evaluate the sum on the circuit. If the constraint has no structure, the semantic loss is as expensive as the reasoning it was meant to replace.
03 / LTNFirst-order logic in real values.
Logic Tensor Networks (Badreddine et al.) generalize the idea from Boolean variables to first-order logic over real-valued terms. A ground atom is a value in [0,1], produced by a predicate network. Connectives use a t-norm; quantifiers use an aggregator over the domain: ∀x φ(x) can be the minimum (or a soft minimum) of φ(x), and ∃x φ(x) the maximum (or a soft maximum).
A knowledge base is a set of formulas. Each formula has a satisfaction value; the logic loss aggregates them (often a weighted mean). Training minimizes data loss plus a weighted logic loss, with the predicates and the task network trained jointly. The appeal is that the knowledge base can contain variables, quantifiers, and relations, not just a fixed Boolean constraint.
The engineering choices are the semantics. The minimum is a harsh aggregator: one bad ground instance makes the whole formula false, and its gradient goes to one instance. A soft minimum (pMean, log-sum-exp) spreads the gradient. The choice of aggregator is as important as the choice of t-norm, and papers often report only one.
04 / DL2 and fuzzy logicA family, not a formula.
DL2 (Fischer et al., 2019) is a framework for training and querying neural networks with logic. It makes the t-norm explicit, supports learning from noisy labels and constraints, and can answer queries by optimizing the truth value of a formula. The contribution is not a single loss; it is a systematic way to choose a differentiable semantics and use it for training and inference.
The family has a common shape:
- Grounding: map the symbols and the network outputs to values in [0,1].
- Semantics: choose t-norms for connectives and aggregators for quantifiers.
- Loss: aggregate the satisfaction of the knowledge base, weighted against the data loss.
- Inference: for a query, optimize the truth value over the free variables, or score candidates.
Two warnings. First, fuzzy truth is not calibrated probability; a satisfaction of 0.9 does not mean “90% chance the constraint holds”. Second, the semantics is a hyperparameter: changing the t-norm can change which solution the model finds. A result that depends on the product t-norm may not survive under Łukasiewicz.
05 / What it buys & costsThe honest ledger.
| What it buys | What it costs |
|---|---|
| End-to-end training with a single optimizer | No hard guarantee; the constraint can still be violated |
| Soft guidance when the data is noisy or sparse | Sensitivity to λ; the balance is task-specific |
| Works with any differentiable model | Semantic loss is #P-hard without compilation |
| No external solver at training time | Mis-specified constraints teach the wrong thing |
| Can encode background knowledge as a prior | The model can satisfy the letter and violate the intent |
The failure modes are worth naming. Constraint gaming: the model finds a distribution that minimizes the penalty without doing the task, e.g. putting all mass on one satisfying assignment regardless of the input. Weight collapse: a large λ makes the data term irrelevant, and the model satisfies the constraint on the training set while losing accuracy. Semantic mismatch: the constraint is a proxy for the real requirement, and the proxy is wrong. None of these is fixed by a better optimizer.
06 / Four casesRead them along the tuple.
Semantic loss on MNIST-Add
Two digit classifiers, a sum constraint, and a penalty on the probability that the sum is wrong. The constraint is a Boolean formula over the predicted labels.
LTN for knowledge completion
Predicates are networks; formulas quantify over entities; the logic loss aggregates satisfaction. The knowledge base is a set of first-order formulas.
DL2 for learning and querying
A t-norm semantics used both to train with constraints and to answer queries by optimizing a formula's truth value.
Semantic-based regularization
Constraints as a penalty on the output distribution, used to inject background knowledge into a classifier.
All four have the same anatomy: a neural part that produces a distribution, a symbolic part that says which distributions are acceptable, and a loss that couples them. The differences are the logic (Boolean vs first-order), the semantics (probabilistic vs fuzzy), and the guarantee (none).
07 / The design checklistWhen a loss is the right interface.
- Is the constraint soft or hard? If a violation is unacceptable, a loss is not enough. Use a verifier or an architecture that enforces it.
- Can you compute the satisfaction probability? If the constraint has no tractable structure, semantic loss may be too expensive; a fuzzy penalty may be cheaper but less principled.
- What is the semantics? Choose the t-norm and aggregator deliberately; report them. Do not compare two systems with different semantics as if they were the same constraint.
- How will you tune λ? On a validation set, with a metric that includes constraint satisfaction, not only accuracy.
- What is the failure mode? Constraint gaming, weight collapse, semantic mismatch. Write a test for each.
- What does the interface guarantee? A loss guarantees nothing at inference. Say that in the paper, and say it in the product.
A logic loss is a prior, not a proof: it changes which solutions are likely, and it cannot tell you that the answer satisfies the constraint.
08 / RecapWhat you should be able to do now.
Explain how a hard constraint becomes a differentiable penalty, and why the result is not a guarantee.
Write the semantic loss for a Boolean constraint and evaluate it on a small example.
Explain how a t-norm and a quantifier aggregator change the semantics and the gradients.
Distinguish fuzzy truth from probability, and say why the distinction matters.
Name the three failure modes: constraint gaming, weight collapse, semantic mismatch.
Decide when to use a loss instead of an architecture or a verifier.
Exercises
- Exactly-one by hand. For three Boolean variables with probabilities 0.5, 0.5, 0.5, compute the semantic loss of exactly-one. Then compute it for 0.9, 0.05, 0.05. Which distribution is better, and why?
- Two t-norms. Evaluate
a ∧ bunder product and Łukasiewicz for (0.9, 0.9) and (0.5, 0.5). Compute the gradient with respect toain each case. What changes? - Constraint gaming. Construct a distribution that satisfies “exactly one” with probability 1 but is useless for the task. What data term would prevent it?
- Weight sweep. In the lab, run λ = 0, 0.2, 1, 5. Record the constraint probability and the data loss. Where does the trade-off break?
- Choose an interface. For a medical coding task with a hard billing constraint, argue for a loss, an architecture, or a verifier. What does each guarantee?