math / data /lean /OAI /Computability /PerfectCompleteness /Algebra /TensorBucketEvaluationLemmas.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
17.2 kB
import Mathlib.LinearAlgebra.TensorProduct.Associator
import OAI.Computability.PerfectCompleteness.Algebra.BilinearGramCompressionLemmas
import OAI.Computability.PerfectCompleteness.Algebra.EvaluationMatrix
import OAI.Computability.PerfectCompleteness.Construction.HiddenBucketBias
import OAI.Computability.PerfectCompleteness.Construction.NodeEmbedding
import OAI.Computability.PerfectCompleteness.Foundations.PartitionsLemmas
namespace OAI
namespace PerfectCompleteness.TensorBucketEvaluation
open scoped BigOperators TensorProduct Classical
noncomputable section
abbrev F2 := BucketSampler.F2
variable {β„“ : Nat} {Ξ© H : Type*} [AddCommGroup H] [Module F2 H]
def tensorOutputLinear (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) : (W βŠ—[F2] H) β†’β‚—[F2] (Fin β„“ β†’ F2) :=
W.subtype.comp
((TensorProduct.rid F2 W).toLinearMap.comp
(TensorProduct.map (LinearMap.id : W β†’β‚—[F2] W) q))
def tensorOutput (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (X : W βŠ—[F2] H) : Fin β„“ β†’ F2 :=
tensorOutputLinear W q X
@[simp] theorem tensorOutput_tmul (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (w : W) (h : H) :
tensorOutput W q (w βŠ—β‚œ[F2] h) = q h β€’ w.val := by
simp [tensorOutput, tensorOutputLinear]
@[simp] theorem tensorOutput_zero (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) : tensorOutput W q 0 = 0 :=
(tensorOutputLinear W q).map_zero
@[simp] theorem tensorOutput_add (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (X Y : W βŠ—[F2] H) :
tensorOutput W q (X + Y) = tensorOutput W q X + tensorOutput W q Y :=
(tensorOutputLinear W q).map_add X Y
theorem tensorOutput_hiddenSum (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (eval : Ξ© β†’ H)
(Ο‰ : HiddenBucketBias.HiddenTape W Ξ©) :
tensorOutput W q (HiddenBucketBias.hiddenSum W eval Ο‰) =
βˆ‘ v : HiddenBucketBias.HiddenDirection W, q (eval (Ο‰ v)) β€’ v.val.val := by
change tensorOutputLinear W q
(βˆ‘ v : HiddenBucketBias.HiddenDirection W,
HiddenBucketBias.directionIn W v βŠ—β‚œ[F2] eval (Ο‰ v)) = _
rw [map_sum]
apply Finset.sum_congr rfl
intro v _
exact tensorOutput_tmul W q (HiddenBucketBias.directionIn W v) (eval (Ο‰ v))
def visibleOutput (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (eval : Ξ© β†’ H)
(Ο‰ : HiddenBucketBias.VisibleTape W Ξ©) : Fin β„“ β†’ F2 :=
βˆ‘ v : HiddenBucketBias.VisibleDirection W, q (eval (Ο‰ v)) β€’ v.val.val
theorem evaluate_eq_direction_sum (q : Module.Dual F2 H)
(eval : Ξ© β†’ H) (t : BucketSampler.Tape β„“ Ξ©) :
(fun i => q (BucketSampler.evaluate β„“ eval t i)) =
βˆ‘ v : BucketSampler.Direction β„“, q (eval (t v)) β€’ v.val := by
funext i
rw [BucketSampler.linearMap_evaluate]
simp only [Finset.sum_apply, Pi.smul_apply, smul_eq_mul]
apply Finset.sum_congr rfl
intro v _
exact mul_comm _ _
theorem evaluate_split (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (eval : Ξ© β†’ H) (t : BucketSampler.Tape β„“ Ξ©) :
(fun i => q (BucketSampler.evaluate β„“ eval t i)) =
visibleOutput W q eval (fun v => t v.val) +
tensorOutput W q
(HiddenBucketBias.hiddenSum W eval (fun v => t v.val)) := by
rw [evaluate_eq_direction_sum, tensorOutput_hiddenSum]
exact (Fintype.sum_subtype_add_sum_subtype
(fun v : BucketSampler.Direction β„“ => v.val ∈ W)
(fun v => q (eval (t v)) β€’ v.val)).symm.trans (add_comm _ _)
theorem evaluate_splitTape (W : Submodule F2 (Fin β„“ β†’ F2))
(q : Module.Dual F2 H) (eval : Ξ© β†’ H) (t : BucketSampler.Tape β„“ Ξ©) :
(fun i => q (BucketSampler.evaluate β„“ eval t i)) =
visibleOutput W q eval (HiddenBucketBias.splitTape W Ξ© t).2 +
tensorOutput W q
(HiddenBucketBias.hiddenSum W eval (HiddenBucketBias.splitTape W Ξ© t).1) :=
evaluate_split W q eval t
theorem evaluate_apply_split {A : Type*}
(W : Submodule F2 (Fin β„“ β†’ F2)) (V : Submodule F2 (A β†’ F2))
(eval : Ξ© β†’ V) (t : BucketSampler.Tape β„“ Ξ©) (a : A) :
(fun i => (BucketSampler.evaluate β„“ eval t i).val a) =
visibleOutput W (EvaluationMatrix.evaluation V a) eval
(HiddenBucketBias.splitTape W Ξ© t).2 +
tensorOutput W (EvaluationMatrix.evaluation V a)
(HiddenBucketBias.hiddenSum W eval (HiddenBucketBias.splitTape W Ξ© t).1) :=
evaluate_splitTape W (EvaluationMatrix.evaluation V a) eval t
end
end PerfectCompleteness.TensorBucketEvaluation
namespace PerfectCompleteness.TensorProjectedSlice
noncomputable section
open scoped TensorProduct
open UniqueGamesTheorem.Integration.BinaryLinear (F2)
variable {K H H' : Type*} [AddCommGroup K] [Module F2 K]
[AddCommGroup H] [Module F2 H] [AddCommGroup H'] [Module F2 H']
[FiniteDimensional F2 H']
(i : H' β†’β‚—[F2] H) (W : Submodule F2 K)
def totalMatrix (Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (T : W βŠ—[F2] H') :
Module.Dual F2 H β†’β‚—[F2] K :=
Xβ‚€ + W.subtype.comp ((TensorRestriction.tensorEquivHom T).comp i.dualMap)
@[simp] theorem totalMatrix_apply (Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K)
(T : W βŠ—[F2] H') (z : Module.Dual F2 H) :
totalMatrix i W Xβ‚€ T z =
Xβ‚€ z + (TensorRestriction.tensorEquivHom T (i.dualMap z) : K) := rfl
variable (Z : Submodule F2 (Module.Dual F2 H))
def Compatible (Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(T : W βŠ—[F2] H') : Prop :=
(totalMatrix i W Xβ‚€ T).domRestrict Z = v
def targetAt (Tβ‚€ : W βŠ—[F2] H') :
ProjectedColumnSlice.projectedColumns i Z β†’β‚—[F2] W :=
TensorRestriction.restrictionMap (ProjectedColumnSlice.projectedColumns i Z) Tβ‚€
theorem compatible_iff_restrictionMap_eq
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(Tβ‚€ : W βŠ—[F2] H') (hβ‚€ : Compatible i W Z Xβ‚€ v Tβ‚€)
(T : W βŠ—[F2] H') :
Compatible i W Z Xβ‚€ v T ↔
TensorRestriction.restrictionMap (ProjectedColumnSlice.projectedColumns i Z) T =
targetAt i W Z Tβ‚€ := by
constructor
Β· intro hT
apply LinearMap.ext
intro q
obtain ⟨z, rfl⟩ := ProjectedColumnSlice.restrictionZ_surjective i Z q
apply Subtype.ext
have hTz := LinearMap.congr_fun hT z
have hβ‚€z := LinearMap.congr_fun hβ‚€ z
change Xβ‚€ z.val + (TensorRestriction.tensorEquivHom T (i.dualMap z.val) : K) =
v z at hTz
change Xβ‚€ z.val + (TensorRestriction.tensorEquivHom Tβ‚€ (i.dualMap z.val) : K) =
v z at hβ‚€z
exact add_left_cancel (hTz.trans hβ‚€z.symm)
Β· intro hT
apply LinearMap.ext
intro z
have hz := LinearMap.congr_fun hT (ProjectedColumnSlice.restrictionZ i Z z)
change TensorRestriction.tensorEquivHom T (i.dualMap z.val) =
TensorRestriction.tensorEquivHom Tβ‚€ (i.dualMap z.val) at hz
change Xβ‚€ z.val + (TensorRestriction.tensorEquivHom T (i.dualMap z.val) : K) = v z
rw [hz]
exact LinearMap.congr_fun hβ‚€ z
theorem targetAt_comp_restrictionZ
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(Tβ‚€ : W βŠ—[F2] H') (hβ‚€ : Compatible i W Z Xβ‚€ v Tβ‚€) :
W.subtype.comp ((targetAt i W Z Tβ‚€).comp
(ProjectedColumnSlice.restrictionZ i Z)) = v - Xβ‚€.domRestrict Z := by
apply LinearMap.ext
intro z
have hβ‚€z := LinearMap.congr_fun hβ‚€ z
change Xβ‚€ z.val + (TensorRestriction.tensorEquivHom Tβ‚€ (i.dualMap z.val) : K) =
v z at hβ‚€z
change (TensorRestriction.tensorEquivHom Tβ‚€ (i.dualMap z.val) : K) = v z - Xβ‚€ z.val
rw [← hβ‚€z, add_sub_cancel_left]
theorem targetAt_independent
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(Tβ‚€ T₁ : W βŠ—[F2] H')
(hβ‚€ : Compatible i W Z Xβ‚€ v Tβ‚€) (h₁ : Compatible i W Z Xβ‚€ v T₁) :
targetAt i W Z Tβ‚€ = targetAt i W Z T₁ :=
(compatible_iff_restrictionMap_eq i W Z Xβ‚€ v T₁ h₁ Tβ‚€).mp hβ‚€
def inducedTarget (Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(hne : βˆƒ T : W βŠ—[F2] H', Compatible i W Z Xβ‚€ v T) :
ProjectedColumnSlice.projectedColumns i Z β†’β‚—[F2] W :=
targetAt i W Z (Classical.choose hne)
theorem inducedTarget_eq_targetAt
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(hne : βˆƒ T : W βŠ—[F2] H', Compatible i W Z Xβ‚€ v T)
(Tβ‚€ : W βŠ—[F2] H') (hβ‚€ : Compatible i W Z Xβ‚€ v Tβ‚€) :
inducedTarget i W Z Xβ‚€ v hne = targetAt i W Z Tβ‚€ :=
targetAt_independent i W Z Xβ‚€ v (Classical.choose hne) Tβ‚€
(Classical.choose_spec hne) hβ‚€
theorem inducedTarget_comp_restrictionZ
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(hne : βˆƒ T : W βŠ—[F2] H', Compatible i W Z Xβ‚€ v T) :
W.subtype.comp ((inducedTarget i W Z Xβ‚€ v hne).comp
(ProjectedColumnSlice.restrictionZ i Z)) = v - Xβ‚€.domRestrict Z :=
targetAt_comp_restrictionZ i W Z Xβ‚€ v (Classical.choose hne) (Classical.choose_spec hne)
theorem columnEquations_iff_inducedTarget
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(hne : βˆƒ T : W βŠ—[F2] H', Compatible i W Z Xβ‚€ v T)
(T : W βŠ—[F2] H') :
(totalMatrix i W Xβ‚€ T).domRestrict Z = v ↔
TensorRestriction.restrictionMap (ProjectedColumnSlice.projectedColumns i Z) T =
inducedTarget i W Z Xβ‚€ v hne :=
compatible_iff_restrictionMap_eq i W Z Xβ‚€ v
(Classical.choose hne) (Classical.choose_spec hne) T
def sliceEquiv (Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] K) (v : Z β†’β‚—[F2] K)
(hne : βˆƒ T : W βŠ—[F2] H', Compatible i W Z Xβ‚€ v T) :
{T : W βŠ—[F2] H' // (totalMatrix i W Xβ‚€ T).domRestrict Z = v} ≃
{T : W βŠ—[F2] H' //
TensorRestriction.restrictionMap (ProjectedColumnSlice.projectedColumns i Z) T =
inducedTarget i W Z Xβ‚€ v hne} where
toFun T := ⟨T.val, (columnEquations_iff_inducedTarget i W Z Xβ‚€ v hne T.val).mp T.property⟩
invFun T := ⟨T.val, (columnEquations_iff_inducedTarget i W Z Xβ‚€ v hne T.val).mpr T.property⟩
left_inv _ := rfl
right_inv _ := rfl
omit [FiniteDimensional F2 H'] in
theorem finrank_projectedColumns_le [FiniteDimensional F2 Z] :
Module.finrank F2 (ProjectedColumnSlice.projectedColumns i Z) ≀ Module.finrank F2 Z :=
ProjectedColumnSlice.finrank_projectedColumns_le i Z
variable {β„“ : Nat} (R : Submodule F2 (Fin β„“ β†’ F2))
theorem includedEvaluation_eq_tensorOutputLinear (q : Module.Dual F2 H') :
R.subtype.comp ((LinearMap.applyβ‚— (R := F2) q).comp
(TensorRestriction.tensorEquivHom (W := R) (H := H')).toLinearMap) =
TensorBucketEvaluation.tensorOutputLinear R q := by
apply TensorProduct.ext'
intro w h
change (TensorRestriction.tensorEquivHom (w βŠ—β‚œ[F2] h) q : Fin β„“ β†’ F2) =
TensorBucketEvaluation.tensorOutput R q (w βŠ—β‚œ[F2] h)
rw [TensorRestriction.tensorEquivHom_tmul, TensorBucketEvaluation.tensorOutput_tmul]
rfl
theorem includedEvaluation_eq_tensorOutput (q : Module.Dual F2 H')
(T : R βŠ—[F2] H') :
(TensorRestriction.tensorEquivHom T q : Fin β„“ β†’ F2) =
TensorBucketEvaluation.tensorOutput R q T :=
LinearMap.congr_fun (includedEvaluation_eq_tensorOutputLinear R q) T
theorem prediction_eq_visible_add_tensorOutput
(Xβ‚€ : Module.Dual F2 H β†’β‚—[F2] (Fin β„“ β†’ F2))
(T : R βŠ—[F2] H') (z : Module.Dual F2 H) (u : Fin β„“ β†’ F2) :
totalMatrix i R Xβ‚€ T z + u =
(Xβ‚€ z + u) + TensorBucketEvaluation.tensorOutput R (i.dualMap z) T := by
rw [totalMatrix_apply, includedEvaluation_eq_tensorOutput]
exact add_right_comm _ _ _
end
end PerfectCompleteness.TensorProjectedSlice
namespace PerfectCompleteness.HierarchicalTensorMatrix
noncomputable section
open scoped BigOperators TensorProduct Classical
open TreeSourceSpaces HierarchicalArrays
variable {branch rows : Nat β†’ Nat} {n t : Nat}
(slots : RecursiveSpaces.Slots branch n β†’ Fin t β†’ MixedSupport.Slot)
(upper : Nodes branch n) (W : Submodule F2 (Block rows upper))
def visibleMatrix
(known : HiddenBucketBias.VisibleDirection W β†’ NodeEmbedding.NodeH slots upper) :
Module.Dual F2 (NodeEmbedding.RowSpace slots upper) β†’β‚—[F2] Block rows upper :=
βˆ‘ v : HiddenBucketBias.VisibleDirection W,
(Module.Dual.eval F2 (NodeEmbedding.RowSpace slots upper)
(NodeEmbedding.embed slots upper (known v))).smulRight v.val.val
theorem visibleMatrix_apply
(known : HiddenBucketBias.VisibleDirection W β†’ NodeEmbedding.NodeH slots upper)
(q : Module.Dual F2 (NodeEmbedding.RowSpace slots upper)) :
visibleMatrix slots upper W known q =
βˆ‘ v : HiddenBucketBias.VisibleDirection W,
(q.comp (NodeEmbedding.embed slots upper)) (known v) β€’ v.val.val := by
simp only [visibleMatrix, LinearMap.sum_apply, LinearMap.smulRight_apply]
rfl
variable {Ξ© : Type*} (eval : Ξ© β†’ NodeEmbedding.NodeH slots upper)
(tape : BucketSampler.Tape (rows (Nodes.height upper)) Ξ©)
theorem matrix_eq_totalMatrix (arrays : Arrays slots rows)
(hrows : BucketSampler.evaluate (rows (Nodes.height upper)) eval tape = arrays upper) :
NodeEmbedding.matrix arrays upper =
TensorProjectedSlice.totalMatrix (NodeEmbedding.embed slots upper) W
(visibleMatrix slots upper W
(fun v => eval ((HiddenBucketBias.splitTape W Ξ© tape).2 v)))
(HiddenBucketBias.hiddenSum W eval (HiddenBucketBias.splitTape W Ξ© tape).1) := by
apply LinearMap.ext
intro q
calc
NodeEmbedding.matrix arrays upper q =
(fun i => (q.comp (NodeEmbedding.embed slots upper)) (arrays upper i)) := rfl
_ = (fun i => (q.comp (NodeEmbedding.embed slots upper))
(BucketSampler.evaluate (rows (Nodes.height upper)) eval tape i)) := by rw [hrows]
_ = TensorBucketEvaluation.visibleOutput W (q.comp (NodeEmbedding.embed slots upper))
eval (HiddenBucketBias.splitTape W Ξ© tape).2 +
TensorBucketEvaluation.tensorOutput W (q.comp (NodeEmbedding.embed slots upper))
(HiddenBucketBias.hiddenSum W eval (HiddenBucketBias.splitTape W Ξ© tape).1) :=
TensorBucketEvaluation.evaluate_splitTape W (q.comp (NodeEmbedding.embed slots upper)) eval tape
_ = _ := by
rw [TensorProjectedSlice.totalMatrix_apply,
TensorProjectedSlice.includedEvaluation_eq_tensorOutput, visibleMatrix_apply]
rfl
theorem prediction_eq_visible_add_tensorOutput (arrays : Arrays slots rows)
(hrows : BucketSampler.evaluate (rows (Nodes.height upper)) eval tape = arrays upper)
(z : Module.Dual F2 (NodeEmbedding.RowSpace slots upper)) (u : Block rows upper) :
NodeEmbedding.matrix arrays upper z + u =
(visibleMatrix slots upper W
(fun v => eval ((HiddenBucketBias.splitTape W Ξ© tape).2 v)) z + u) +
TensorBucketEvaluation.tensorOutput W
(z.comp (NodeEmbedding.embed slots upper))
(HiddenBucketBias.hiddenSum W eval (HiddenBucketBias.splitTape W Ξ© tape).1) := by
rw [matrix_eq_totalMatrix slots upper W eval tape arrays hrows]
exact TensorProjectedSlice.prediction_eq_visible_add_tensorOutput
(NodeEmbedding.embed slots upper) W _ _ z u
end
end PerfectCompleteness.HierarchicalTensorMatrix
namespace PerfectCompleteness.TensorTargetCharacter
noncomputable section
open scoped TensorProduct
open UniqueGamesTheorem.Integration.BinaryLinear (F2)
open TensorCosetEvaluation (pureFrequency)
variable {β„“ : Nat} {H : Type*} [AddCommGroup H] [Module F2 H]
theorem comp_tensorOutputLinear (W : Submodule F2 (Fin β„“ β†’ F2))
(Οƒ : Module.Dual F2 (Fin β„“ β†’ F2)) (z : Module.Dual F2 H) :
Οƒ.comp (TensorBucketEvaluation.tensorOutputLinear W z) =
pureFrequency (Οƒ.comp W.subtype) z := by
apply TensorProduct.ext'
intro w h
change Οƒ (TensorBucketEvaluation.tensorOutput W z (w βŠ—β‚œ[F2] h)) =
pureFrequency (Οƒ.comp W.subtype) z (w βŠ—β‚œ[F2] h)
rw [TensorBucketEvaluation.tensorOutput_tmul,
TensorCosetEvaluation.pureFrequency_tmul, map_smul]
change z h * Οƒ w.val = Οƒ w.val * z h
exact mul_comm _ _
theorem apply_tensorOutput (W : Submodule F2 (Fin β„“ β†’ F2))
(Οƒ : Module.Dual F2 (Fin β„“ β†’ F2)) (z : Module.Dual F2 H)
(X : W βŠ—[F2] H) :
Οƒ (TensorBucketEvaluation.tensorOutput W z X) =
pureFrequency (Οƒ.comp W.subtype) z X :=
LinearMap.congr_fun (comp_tensorOutputLinear W Οƒ z) X
theorem character_affine_tensorOutput (W : Submodule F2 (Fin β„“ β†’ F2))
(Ο‡ : AddChar F2 ℝ) (Οƒ : Module.Dual F2 (Fin β„“ β†’ F2))
(z : Module.Dual F2 H) (offset : Fin β„“ β†’ F2) (X : W βŠ—[F2] H) :
Ο‡ (Οƒ (offset + TensorBucketEvaluation.tensorOutput W z X)) =
Ο‡ (Οƒ offset) * Ο‡ (pureFrequency (Οƒ.comp W.subtype) z X) := by
rw [map_add, apply_tensorOutput, Ο‡.map_add_eq_mul]
theorem sign_affine_tensorOutput (W : Submodule F2 (Fin β„“ β†’ F2))
(Οƒ : Module.Dual F2 (Fin β„“ β†’ F2)) (z : Module.Dual F2 H)
(offset : Fin β„“ β†’ F2) (X : W βŠ—[F2] H) :
HiddenBucketBias.signCharacter
(Οƒ (offset + TensorBucketEvaluation.tensorOutput W z X)) =
HiddenBucketBias.signCharacter (Οƒ offset) *
HiddenBucketBias.signCharacter (pureFrequency (Οƒ.comp W.subtype) z X) :=
character_affine_tensorOutput W HiddenBucketBias.signCharacter Οƒ z offset X
end
end PerfectCompleteness.TensorTargetCharacter
end OAI