math / data /lean /OAI /Probability /InvariantIsing /Arrays /NSpinTensorBackwardPaths.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
15.2 kB
import OAI.Probability.InvariantIsing.Arrays.NSpinTensorBackwardDerivative
namespace OAI
/-! Collapse the two independent residual paths to actual backward gradients. -/
noncomputable section
open MeasureTheory ProbabilityTheory IsingPerceptron
open scoped BigOperators NNReal ENNReal
namespace InvariantIsing
def tensorPairCoordinatePayoff {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(q : β„•) (z₁ zβ‚‚ : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree)
(w : Fin q β†’ (SpinTensorIndex I degree β†’ ℝ) Γ— (SpinTensorIndex I degree β†’ ℝ)) : ℝ :=
tensorTerminalCoordinateMean eig U c I degree amplitude (z₁ + βˆ‘ i, (w i).1) j₁ *
tensorTerminalCoordinateMean eig U c I degree amplitude (zβ‚‚ + βˆ‘ i, (w i).2) jβ‚‚
lemma tensorPairCoordinatePayoff_cons {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(q : β„•) (z₁ zβ‚‚ a₁ aβ‚‚ : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree)
(w : Fin q β†’ (SpinTensorIndex I degree β†’ ℝ) Γ— (SpinTensorIndex I degree β†’ ℝ)) :
tensorPairCoordinatePayoff eig U c I degree amplitude (q + 1) z₁ zβ‚‚ j₁ jβ‚‚ (Fin.cons (a₁, aβ‚‚) w) =
tensorPairCoordinatePayoff eig U c I degree amplitude q (z₁ + a₁) (zβ‚‚ + aβ‚‚) j₁ jβ‚‚ w := by
simp only [tensorPairCoordinatePayoff, Fin.sum_univ_succ, Fin.cons_zero, Fin.cons_succ, add_assoc]
lemma tensorPairCoordinatePayoff_abs_le {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(q : β„•) (z₁ zβ‚‚ : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree)
(w : Fin q β†’ (SpinTensorIndex I degree β†’ ℝ) Γ— (SpinTensorIndex I degree β†’ ℝ)) :
|tensorPairCoordinatePayoff eig U c I degree amplitude q z₁ zβ‚‚ j₁ jβ‚‚ w| ≀
tensorFeatureCoordinateCap U I degree amplitude j₁ * tensorFeatureCoordinateCap U I degree amplitude jβ‚‚ := by
rw [tensorPairCoordinatePayoff, abs_mul]
exact mul_le_mul (tensorTerminalCoordinateMean_abs_le eig U c I degree amplitude _ j₁)
(tensorTerminalCoordinateMean_abs_le eig U c I degree amplitude _ jβ‚‚) (abs_nonneg _)
(Finset.sum_nonneg fun _ _ => abs_nonneg _)
/-- Once the two paths have branched, each terminal gradient averages
independently to its actual backward gradient. -/
theorem tensorAncestorPairPathMean_independent_coordinates {N m k : β„•} (hN : 0 < N)
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(hb : CascadeExponents n b) (d q i : β„•) (hqi : i + q = n) (hdi : d ≀ i)
(z₁ zβ‚‚ : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree) :
tensorAncestorPairPathMean eig U c I degree amplitude n b v d q i z₁ zβ‚‚
(tensorPairCoordinatePayoff eig U c I degree amplitude q z₁ zβ‚‚ j₁ jβ‚‚) =
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i z₁ j₁ *
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i zβ‚‚ jβ‚‚ := by
induction q generalizing i z₁ zβ‚‚ with
| zero =>
have hi : i = n := by omega
subst i
simp only [tensorAncestorPairPathMean, tensorPairCoordinatePayoff, Finset.univ_eq_empty,
Finset.sum_empty, add_zero, tensorBackwardCoordinateMean, Nat.sub_self, tensorCascadeCoordinateMean]
| succ q ih =>
have hi : i < n := by omega
have hqi' : i + 1 + q = n := by omega
have hdi' : d ≀ i + 1 := by omega
dsimp only [tensorAncestorPairPathMean]
rw [ite_eq_right (by omega : Β¬i < d)]
calc
_ = ∫ a₁, ∫ aβ‚‚,
tensorBackwardCoordinateMean eig U c I degree amplitude n b v (i + 1) (z₁ + a₁) j₁ *
tensorBackwardCoordinateMean eig U c I degree amplitude n b v (i + 1) (zβ‚‚ + aβ‚‚) jβ‚‚
βˆ‚tensorAncestorMarkKernel eig U c I degree amplitude n b v i zβ‚‚
βˆ‚tensorAncestorMarkKernel eig U c I degree amplitude n b v i z₁ := by
apply integral_congr_ae
apply ae_of_all
intro a₁
apply integral_congr_ae
apply ae_of_all
intro aβ‚‚
dsimp only
have hF : (fun w => tensorPairCoordinatePayoff eig U c I degree amplitude (q + 1)
z₁ zβ‚‚ j₁ jβ‚‚ (Fin.cons (a₁, aβ‚‚) w)) =
tensorPairCoordinatePayoff eig U c I degree amplitude q (z₁ + a₁) (zβ‚‚ + aβ‚‚) j₁ jβ‚‚ :=
funext (tensorPairCoordinatePayoff_cons eig U c I degree amplitude q z₁ zβ‚‚ a₁ aβ‚‚ j₁ jβ‚‚)
rw [hF]
exact ih (i + 1) hqi' hdi' (z₁ + a₁) (zβ‚‚ + aβ‚‚)
_ = (∫ a₁, tensorBackwardCoordinateMean eig U c I degree amplitude n b v (i + 1) (z₁ + a₁) j₁
βˆ‚tensorAncestorMarkKernel eig U c I degree amplitude n b v i z₁) *
(∫ aβ‚‚, tensorBackwardCoordinateMean eig U c I degree amplitude n b v (i + 1) (zβ‚‚ + aβ‚‚) jβ‚‚
βˆ‚tensorAncestorMarkKernel eig U c I degree amplitude n b v i zβ‚‚) := by
simp_rw [integral_const_mul]
rw [integral_mul_const]
_ = _ := by
rw [← tensorBackwardCoordinateMean_step hN eig U c I degree amplitude n b v hb i hi z₁ j₁,
← tensorBackwardCoordinateMean_step hN eig U c I degree amplitude n b v hb i hi zβ‚‚ jβ‚‚]
/-- Only the shared ancestor increments remain after the independent
suffixes have been integrated. At the branching level the integrand is
the product of actual backward gradients. -/
def tensorSharedCoordinateProduct {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0) (d : β„•)
(j₁ jβ‚‚ : SpinTensorIndex I degree) :
(q : β„•) β†’ β„• β†’ (SpinTensorIndex I degree β†’ ℝ) β†’ (SpinTensorIndex I degree β†’ ℝ) β†’ ℝ
| 0, i, z₁, zβ‚‚ =>
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i z₁ j₁ *
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i zβ‚‚ jβ‚‚
| q + 1, i, z₁, zβ‚‚ =>
if i < d then
∫ a, tensorSharedCoordinateProduct eig U c I degree amplitude n b v d j₁ jβ‚‚ q
(i + 1) (z₁ + a) (zβ‚‚ + a) βˆ‚tensorAncestorMarkKernel eig U c I degree amplitude n b v i z₁
else
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i z₁ j₁ *
tensorBackwardCoordinateMean eig U c I degree amplitude n b v i zβ‚‚ jβ‚‚
lemma tensorSharedCoordinateProduct_nonneg {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(d : β„•) (j : SpinTensorIndex I degree) (q i : β„•) (z : SpinTensorIndex I degree β†’ ℝ) :
0 ≀ tensorSharedCoordinateProduct eig U c I degree amplitude n b v d j j q i z z := by
induction q generalizing i z with
| zero => exact mul_self_nonneg _
| succ q ih =>
dsimp only [tensorSharedCoordinateProduct]
split_ifs
Β· exact integral_nonneg fun a => ih (i + 1) (z + a)
Β· exact mul_self_nonneg _
/-- The finite pair-path expectation is an expectation over the common
ancestor path of the backward gradient product at branching. -/
theorem tensorAncestorPairPathMean_coordinates {N m k : β„•} (hN : 0 < N)
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(hb : CascadeExponents n b) (d q i : β„•) (hqi : i + q = n)
(z₁ zβ‚‚ : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree) :
tensorAncestorPairPathMean eig U c I degree amplitude n b v d q i z₁ zβ‚‚
(tensorPairCoordinatePayoff eig U c I degree amplitude q z₁ zβ‚‚ j₁ jβ‚‚) =
tensorSharedCoordinateProduct eig U c I degree amplitude n b v d j₁ jβ‚‚ q i z₁ zβ‚‚ := by
induction q generalizing i z₁ zβ‚‚ with
| zero =>
have hi : i = n := by omega
subst i
simp only [tensorAncestorPairPathMean, tensorPairCoordinatePayoff, Finset.univ_eq_empty,
Finset.sum_empty, add_zero, tensorSharedCoordinateProduct, tensorBackwardCoordinateMean,
Nat.sub_self, tensorCascadeCoordinateMean]
| succ q ih =>
by_cases hid : i < d
Β· dsimp only [tensorAncestorPairPathMean, tensorSharedCoordinateProduct]
rw [ite_eq_left hid, ite_eq_left hid]
apply integral_congr_ae
apply ae_of_all
intro a
dsimp only
have hF : (fun w => tensorPairCoordinatePayoff eig U c I degree amplitude (q + 1)
z₁ zβ‚‚ j₁ jβ‚‚ (Fin.cons (a, a) w)) =
tensorPairCoordinatePayoff eig U c I degree amplitude q (z₁ + a) (zβ‚‚ + a) j₁ jβ‚‚ :=
funext (tensorPairCoordinatePayoff_cons eig U c I degree amplitude q z₁ zβ‚‚ a a j₁ jβ‚‚)
rw [hF]
exact ih (i + 1) (by omega) (z₁ + a) (zβ‚‚ + a)
Β· rw [tensorSharedCoordinateProduct, ite_eq_right hid]
exact tensorAncestorPairPathMean_independent_coordinates hN eig U c I degree amplitude
n b v hb d (q + 1) i hqi (by omega) z₁ zβ‚‚ j₁ jβ‚‚
lemma tensorAncestorPairPathMean_const_mul {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(d q i : β„•) (z₁ zβ‚‚ : SpinTensorIndex I degree β†’ ℝ)
(F : (Fin q β†’ (SpinTensorIndex I degree β†’ ℝ) Γ— (SpinTensorIndex I degree β†’ ℝ)) β†’ ℝ) (C : ℝ) :
tensorAncestorPairPathMean eig U c I degree amplitude n b v d q i z₁ zβ‚‚ (fun w => C * F w) =
C * tensorAncestorPairPathMean eig U c I degree amplitude n b v d q i z₁ zβ‚‚ F := by
induction q generalizing i z₁ zβ‚‚ with
| zero => rfl
| succ q ih =>
dsimp only [tensorAncestorPairPathMean]
split_ifs <;> simp_rw [ih, integral_const_mul]
lemma tensorPairPathSpinMean_features {N m k : β„•}
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (z : SpinTensorIndex I degree β†’ ℝ) (j₁ jβ‚‚ : SpinTensorIndex I degree)
(w : Fin n β†’ (SpinTensorIndex I degree β†’ ℝ) Γ— (SpinTensorIndex I degree β†’ ℝ)) :
tensorPairPathSpinMean eig U c I degree amplitude n (z, w)
(fun Οƒ => spinTensorFeature U I degree amplitude (Οƒ 0) j₁ *
spinTensorFeature U I degree amplitude (Οƒ 1) jβ‚‚) =
tensorPairCoordinatePayoff eig U c I degree amplitude n z z j₁ jβ‚‚ w := by
let ΞΌ := fun j : Fin 2 => gibbsProbability (uniformSpinPrior N : Measure (Spin N))
(tensorSpinBaseEnergy eig U c I degree amplitude (tensorPairPathState I degree n (z, w) j))
change (∫ Οƒ : Fin 2 β†’ Spin N, spinTensorFeature U I degree amplitude (Οƒ 0) j₁ *
spinTensorFeature U I degree amplitude (Οƒ 1) jβ‚‚ βˆ‚Measure.pi ΞΌ) = _
have h := integral_fin_nat_prod_eq_prod (ΞΌ := ΞΌ)
(fun j Οƒ => spinTensorFeature U I degree amplitude Οƒ (if j = 0 then j₁ else jβ‚‚))
have h10 : (1 : Fin 2) β‰  0 := by decide
simpa only [Fin.prod_univ_two, ite_eq_left rfl, ite_true, ite_eq_right h10, ΞΌ,
integral_uniformSpinGibbs_eq, tensorTerminalCoordinateMean, tensorPairCoordinatePayoff,
tensorPairPathState] using h
/-- Actual finite tensor feature pair tests are expectations of backward
gradient products along the shared ancestor path. -/
theorem tensorFeaturePair_backward_identity (hpub : PanchenkoTalagrandTensorPairInput)
{N m k : β„•} (hN : 0 < N)
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(hb : CascadeExponents n b) (z : SpinTensorIndex I degree β†’ ℝ)
(ψ : β„• β†’ ℝ) {B : ℝ} (hB : 0 ≀ B) (hψ : βˆ€ d, |ψ d| ≀ B)
(j₁ jβ‚‚ : SpinTensorIndex I degree) :
tensorSpinPairAverage eig U c I degree amplitude n b v z ψ
(fun Οƒ => spinTensorFeature U I degree amplitude (Οƒ 0) j₁ *
spinTensorFeature U I degree amplitude (Οƒ 1) jβ‚‚) =
∫ Ξ± : β„• β†’ LabeledLeaf n, ψ (labeledCommonDepth n (Ξ± 0) (Ξ± 1)) *
tensorSharedCoordinateProduct eig U c I degree amplitude n b v
(labeledCommonDepth n (Ξ± 0) (Ξ± 1)) j₁ jβ‚‚ n 0 z z βˆ‚cascadeReplicaLaw n b := by
have hf (Οƒ : Spin N) (j : SpinTensorIndex I degree) :
|spinTensorFeature U I degree amplitude Οƒ j| ≀ tensorFeatureCoordinateCap U I degree amplitude j := by
exact Finset.single_le_sum (fun Ο„ _ => abs_nonneg (spinTensorFeature U I degree amplitude Ο„ j))
(Finset.mem_univ Οƒ)
rw [tensorSpinPair_markov_identity hpub hN eig U c I degree amplitude n b v hb z ψ _ hB hψ
(C := tensorFeatureCoordinateCap U I degree amplitude j₁ * tensorFeatureCoordinateCap U I degree amplitude jβ‚‚)
(fun Οƒ => by
rw [abs_mul]
exact mul_le_mul (hf (Οƒ 0) j₁) (hf (Οƒ 1) jβ‚‚) (abs_nonneg _)
(Finset.sum_nonneg fun _ _ => abs_nonneg _))]
apply integral_congr_ae
apply ae_of_all
intro Ξ±
simp only [tensorPairSpinPathPayoff, tensorPairPathSpinMean_features]
rw [tensorAncestorPairPathMean_const_mul,
tensorAncestorPairPathMean_coordinates hN eig U c I degree amplitude n b v hb _ n 0 (by omega)]
theorem tensorFeaturePair_nonneg (hpub : PanchenkoTalagrandTensorPairInput)
{N m k : β„•} (hN : 0 < N)
(eig : Fin N β†’ ℝ) (U : Rotation N) (c : Fin N β†’ ℝ)
(I : Fin m β†’ Finset (Fin N)) (degree : Fin k β†’ Fin m β†’ β„•) (amplitude : Fin k β†’ ℝ)
(n : β„•) (b : β„• β†’ ℝ) (v : β„• β†’ SpinTensorIndex I degree β†’ ℝβ‰₯0)
(hb : CascadeExponents n b) (z : SpinTensorIndex I degree β†’ ℝ)
(ψ : β„• β†’ ℝ) {B : ℝ} (hB : 0 ≀ B) (hψ : βˆ€ d, |ψ d| ≀ B) (hψ0 : βˆ€ d, 0 ≀ ψ d)
(j : SpinTensorIndex I degree) :
0 ≀ tensorSpinPairAverage eig U c I degree amplitude n b v z ψ
(fun Οƒ => spinTensorFeature U I degree amplitude (Οƒ 0) j *
spinTensorFeature U I degree amplitude (Οƒ 1) j) := by
rw [tensorFeaturePair_backward_identity hpub hN eig U c I degree amplitude n b v hb z ψ hB hψ j j]
exact integral_nonneg fun α => mul_nonneg (hψ0 _)
(tensorSharedCoordinateProduct_nonneg eig U c I degree amplitude n b v _ j n 0 z)
end InvariantIsing
end
end OAI