TorchLean API

NN.Proofs.Tensor.Basic.Core

Real Tensor Proof Toolkit #

This file is the -specialized proof-facing companion to the spec tensor layer.

The tensor proof folder has two layers:

The statements use PyTorch-shaped names where that helps readers:

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 #

Algebraic instances for small tensor shapes #

@[implicit_reducible]

Additive commutative monoid structure on scalar-shaped real tensors, transported by equivalence.

@[implicit_reducible]

Additive commutative monoid structure on 1D real tensors (transported via an equiv).

@[implicit_reducible]

Scalar tensors inherit an -module structure when their entries do, transported by equivalence.

@[implicit_reducible]

Tensor α (dim n scalar) inherits an -module structure when α is an -module (via an equiv).

@[implicit_reducible]

Noncomputable -module instance on scalar real tensors (for calculus proofs).

@[implicit_reducible]

Noncomputable -module instance on 1D real tensors (for calculus proofs).

1D helpers #

theorem Spec.toVec_add_spec {n : } (x y : Tensor (Shape.dim n Shape.scalar)) :
toVec (x.addSpec y) = fun (i : Fin n) => toVec x i + toVec y i

toVec distributes over pointwise addition (add_spec).

theorem Spec.toVec_scale_spec {n : } (x : Tensor (Shape.dim n Shape.scalar)) (c : ) :
toVec (x.scaleSpec c) = fun (i : Fin n) => toVec x i * c

toVec distributes over pointwise scaling (scale_spec).

def Spec.flattenR {s : Shape} (x : Tensor s) :
Fin s.size

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
    def Spec.unflattenR {s : Shape} (v : Fin s.size) :

    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 #