Course / Act III · The five interfaces / Lectures Lecture 07

Logic as a loss.

A constraint can be a penalty. Compile the logic into a differentiable function of the network's output, and the gradient does the reasoning — softly, approximately, and with no guarantee.

~45 min read Lecture 02 first 4 systems 1 soft-constraint lab Updated 2026-11-17

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:

L(θ) = Ldata(θ) + λ · Llogic(θ)
(7.1)

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”.

Caveat · this is not a solver

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.

Table 1.1 · Three t-norms and their implications for gradients.
FamilyAND (t-norm)OR (t-conorm)Gradient behaviour
Producta · ba + b − abSmooth; gradients shrink near 0.
Łukasiewiczmax(0, a + b − 1)min(1, a + b)Piecewise linear; saturates at the bounds.
Gödelmin(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:

Ls(α, p) = −log Σx ⊨ α Πi pixi (1 − pi)1 − xi
(7.2)

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:

Ls = −log Σi pi Πj ≠ i (1 − pj)
(7.3)

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.

The computational catch

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.

Table 5.1 · The loss interface, in one table.
What it buysWhat it costs
End-to-end training with a single optimizerNo hard guarantee; the constraint can still be violated
Soft guidance when the data is noisy or sparseSensitivity to λ; the balance is task-specific
Works with any differentiable modelSemantic loss is #P-hard without compilation
No external solver at training timeMis-specified constraints teach the wrong thing
Can encode background knowledge as a priorThe 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.

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.

  1. 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.
  2. 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.
  3. 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.
  4. How will you tune λ? On a validation set, with a metric that includes constraint satisfaction, not only accuracy.
  5. What is the failure mode? Constraint gaming, weight collapse, semantic mismatch. Write a test for each.
  6. What does the interface guarantee? A loss guarantees nothing at inference. Say that in the paper, and say it in the product.
The one-sentence version

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.

01

Explain how a hard constraint becomes a differentiable penalty, and why the result is not a guarantee.

02

Write the semantic loss for a Boolean constraint and evaluate it on a small example.

03

Explain how a t-norm and a quantifier aggregator change the semantics and the gradients.

04

Distinguish fuzzy truth from probability, and say why the distinction matters.

05

Name the three failure modes: constraint gaming, weight collapse, semantic mismatch.

06

Decide when to use a loss instead of an architecture or a verifier.

Exercises

  1. 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?
  2. Two t-norms. Evaluate a ∧ b under product and Łukasiewicz for (0.9, 0.9) and (0.5, 0.5). Compute the gradient with respect to a in each case. What changes?
  3. Constraint gaming. Construct a distribution that satisfies “exactly one” with probability 1 but is useless for the task. What data term would prevent it?
  4. 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?
  5. 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?

Check your understanding

Reading list

Xu et al., “A Semantic Loss Function for Deep Learning with Symbolic Knowledge” (ICML 2018).
The cleanest formulation: the probability that the constraint holds.
Badreddine et al., “Logic Tensor Networks” (Artificial Intelligence, 2022).
First-order logic with real-valued semantics; read the aggregators carefully.
Fischer et al., “DL2: Training and Querying Neural Networks with Logic” (ICML 2019).
A systematic t-norm framework for training and querying.
Diligenti et al., “Semantic-based regularization for learning and inference” (2017).
The precursor: constraints as a penalty on the output.
Marra et al., “From Statistical Relational to Neuro-Symbolic Artificial Intelligence” · arXiv:2003.08316.
Where the loss interface sits in the larger landscape.
Garcez et al., “Neural-Symbolic Computing” · arXiv:1905.06088.
The integration agenda; useful for the vocabulary.