Real Tensor Proof Toolkit #
This file is the ℝ-specialized proof-facing companion to the spec tensor layer.
The tensor proof folder has two layers:
NN.Proofs.Tensor.Algebrais backend-generic and proves semiring facts about recursive tensor dot products and executable folds.- this file works in
Specoverℝ, where calculus, norms, Frobenius products, and model-analysis lemmas live.
The statements are intentionally PyTorch-shaped where that helps readers:
flattenR/unflattenRgive aFin (Shape.size s) → ℝview ofTensor ℝ s.- lemmas relate
toVecviews toadd_spec,scale_spec, etc.
We re-export selected generic helpers from NN.Proofs.Tensor.Algebra into the Spec.* namespace so
downstream proof files can use one consistent tensor vocabulary (Spec.toVec, Spec.ofVec,
Spec.finRange_foldl_add_eq_finset_sum) through shared fold and vector lemmas.
PyTorch correspondence / citations #
- Flatten / reshape:
torch.flatten,torch.reshape, andTensor.view. https://pytorch.org/docs/stable/generated/torch.flatten.html https://pytorch.org/docs/stable/generated/torch.reshape.html https://pytorch.org/docs/stable/generated/torch.Tensor.view.html - “numel”:
tensor.numel()corresponds toShape.size. https://pytorch.org/docs/stable/generated/torch.Tensor.numel.html
Algebraic instances for small tensor shapes #
Additive commutative monoid structure on scalar-shaped real tensors, transported by equivalence.
Additive commutative monoid structure on 1D real tensors (transported via an equiv).
Scalar tensors inherit an ℝ-module structure when their entries do, transported by equivalence.
Tensor α (dim n scalar) inherits an ℝ-module structure when α is an ℝ-module (via an
equiv).
Noncomputable ℝ-module instance on scalar real tensors (for calculus proofs).
Noncomputable ℝ-module instance on 1D real tensors (for calculus proofs).
1D helpers #
Flatten a tensor of shape s into a 1D view Fin (Shape.size s) → ℝ.
This is the proof-facing counterpart of Spec.Tensor.flatten_spec specialized to ℝ. In PyTorch
terms it is the functional analogue of flattening a tensor and then indexing it linearly
(torch.flatten, tensor.view(-1)). See the spec file NN/Spec/Core/TensorReductionShape.lean
for the definitional flatten/unflatten interface.
Citations: https://pytorch.org/docs/stable/generated/torch.flatten.html https://pytorch.org/docs/stable/generated/torch.Tensor.view.html
Instances For
Unflatten a 1D view Fin (Shape.size s) → ℝ back into a tensor of shape s.
This is the proof-facing counterpart of Spec.Tensor.unflatten_spec specialized to ℝ, and is
intended to round-trip with flattenR under the spec lemmas in
NN/Spec/Core/TensorReductionShape.lean.
Instances For
Pointwise tensor algebra #
Real dot product and fold bridges #
Real dot product for same-shape tensors, defined as the sum of elementwise products.
This matches the common PyTorch idiom (a * b).sum() for same-shape tensors.
Citations:
https://pytorch.org/docs/stable/generated/torch.sum.html
This is the Spec-namespace dot product used by real-analysis proofs. The backend-generic
recursive dot product is Proofs.TensorAlgebra.dot.
Instances For
One-step unfolding of the internal tail-recursive helper tensor_foldl_spec.go when the loop
condition holds (k < n).
This lemma is a proof tool: it lets proofs peel one loop step without using unfold directly.
One-step unfolding of the internal tail-recursive helper tensor_foldl_spec.go when the loop
condition fails (¬ k < n), i.e. the loop terminates and returns the accumulator.
Accumulator lemma for tensor_foldl_spec specialized to addition.
Informally: folding with (+) over a tensor adds sum_spec t to the initial accumulator.
This is frequently used to move between “fold-style” specs and “sum-style” algebra.
Unfolding lemma for get2 (2D tensor indexing).
Coordinate formula for mat_vec_mul_spec, converted from the spec's List.finRange fold to a
Finset.univ.sum.
This is the “PyTorch-looking” statement of matvec: each output entry is a dot product of the corresponding row with the input vector.
Coordinate formula for mat_mul_spec (matrix-matrix multiplication).
This is the standard triple-sum identity: (A @ B)[i,j] = ∑ k, A[i,k] * B[k,j], matching the
textbook/PyTorch view of matrix multiplication.
Citations: https://pytorch.org/docs/stable/generated/torch.matmul.html
Coordinate formula for vec_mat_mul_spec as a Finset sum: (v @ A)[j] = ∑ i, v[i] * A[i,j].
Adjointness of matrix-vector and vector-matrix multiplication under the dot product:
⟪y, W x⟫ = ⟪y W, x⟫ (a.k.a. ⟪y, W x⟫ = ⟪Wᵀ y, x⟫ depending on conventions).
This is the algebraic heart of the linear-layer gradient rule.
Map and elementwise operation laws #
Functor identity law for map_spec: mapping id is a no-op.
Matrix and vector algebra #
Associativity of matrix-vector multiplication: A (B x) = (A B) x.
Matrix transpose is an involution.
Coordinate rule for matrix_transpose_spec: (Aᵀ)[i,j] = A[j,i].
Transpose of a product: (A ⬝ B)ᵀ = Bᵀ ⬝ Aᵀ.
Right-adjointness of matrix multiplication under the Frobenius dot-product.
Informally: ⟪A ⬝ B, C⟫ = ⟪A, C ⬝ Bᵀ⟫.
Transpose invariance of the Frobenius dot-product: ⟪Aᵀ, Bᵀ⟫ = ⟪A, B⟫.
Left-adjointness of matrix multiplication under the Frobenius dot-product.
Informally: ⟪A ⬝ B, C⟫ = ⟪B, Aᵀ ⬝ C⟫.
Outer product properties. Essential for proving weight gradient correctness.
Reductions and aggregation #
Flatten / unflatten round-trips #
The round-trip lemmas for the spec definitions live with the definitions themselves:
NN/Spec/Core/TensorReductionShape.lean provides
so downstream proof files do not need to re-prove the index arithmetic here.
Existence of a uniform bound for safediv_spec, expressed via an idempotent min-clamp.
tensor_norm_squared t is nonnegative, since it is a sum of squares.
Convenience orientation of tensor_norm_squared_nonneg.
Keep this untagged as a simp lemma: the canonical theorem above uses the existing >= spelling,
while downstream analysis proofs often need the 0 ≤ ... spelling for Real.sq_sqrt.
tensor_norm_squared t = 0 iff t is the all-zero tensor.
Extensionality and structural algebra #
Linearity of matrix-vector multiplication in the vector argument (addition).
Linearity of matrix-vector multiplication in the vector argument (scaling).
Full linearity of matrix-vector multiplication in the vector argument.
Simplification lemmas for common patterns. Useful for automated proof tactics.
toScalar is the identity on scalar tensors.
Mapping 0 + · over an Option is the identity.