Documentation

CslibFrontier.MachineLearning.LossMinimization.Basic

True and empirical risk of a loss #

A learning problem in the sense of [ShalevShwartzBenDavid2014, Section 3.2.2] consists of a set W of hypotheses (or models), a domain Z of examples and a loss ℓ : W → Z → ℝ, where ℓ w z is the loss of the hypothesis w on the example z. Classification is the case Z = X × Y with the 0-1 loss, but the examples need not be labelled points. Given a probability distribution D on Z, the true risk of w is its expected loss L_D(w) = ∫ ℓ w z ∂D [ShalevShwartzBenDavid2014, (3.3)], and on a sample S = (z₁, …, z_m) its empirical risk is the average loss L_S(w) = (1 / m) ∑ i, ℓ w (S i) [ShalevShwartzBenDavid2014, (3.4)].

The source requires nonnegative losses; nothing here does. The true risk is a Bochner integral, so it takes the junk value 0 when ℓ w is not integrable, and the results that integrate assume integrability. The empirical risk on the empty sample is 0.

Convexity enters through the curried losses fun w => ℓ w z: if each is convex on a convex set K, so is L_S and, for integrable losses, L_D. These are the objectives of regularized loss minimization and of stochastic gradient descent.

Main definitions #

Main results #

References #

Definitions #

noncomputable def Cslib.MachineLearning.trueRisk {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) (D : MeasureTheory.Measure Z) (w : W) :

The true risk L_D(w) = ∫ ℓ w z ∂D of a hypothesis w under a distribution D on examples: the expected loss [ShalevShwartzBenDavid2014, (3.3)]. It is 0 when ℓ w is not D-integrable.

theorem Cslib.MachineLearning.trueRisk_def {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) (D : MeasureTheory.Measure Z) (w : W) :
trueRisk ℓ D w = ∫ (z : Z), ℓ w z ∂D
noncomputable def Cslib.MachineLearning.empiricalRisk {W : Type u_1} {Z : Type u_2} (ℓ : W → Z → ℝ) {m : ℕ} (S : Fin m → Z) (w : W) :

The empirical risk L_S(w) = (1 / m) ∑ i, ℓ w (S i) of a hypothesis w on a sample S of m examples: the average loss [ShalevShwartzBenDavid2014, (3.4)]. It is 0 on the empty sample.

theorem Cslib.MachineLearning.empiricalRisk_def {W : Type u_1} {Z : Type u_2} (ℓ : W → Z → ℝ) {m : ℕ} (S : Fin m → Z) (w : W) :
empiricalRisk ℓ S w = Finset.univ.expect fun (i : Fin m) => ℓ w (S i)

The empirical risk #

theorem Cslib.MachineLearning.empiricalRisk_eq_sum_div {W : Type u_1} {Z : Type u_2} (ℓ : W → Z → ℝ) {m : ℕ} (S : Fin m → Z) (w : W) :
empiricalRisk ℓ S w = (∑ i : Fin m, ℓ w (S i)) / ↑m

The empirical risk is the total loss divided by the sample size.

theorem Cslib.MachineLearning.empiricalRisk_update {W : Type u_1} {Z : Type u_2} (ℓ : W → Z → ℝ) {m : ℕ} (S : Fin m → Z) (i : Fin m) (z : Z) (w : W) :
empiricalRisk ℓ (Function.update S i z) w = empiricalRisk ℓ S w + (ℓ w z - ℓ w (S i)) / ↑m

Replacing the i-th example of S by z changes the empirical risk by (ℓ w z - ℓ w (S i)) / m.

theorem Cslib.MachineLearning.convexOn_empiricalRisk {W : Type u_1} {Z : Type u_2} [AddCommGroup W] [Module ℝ W] {K : Set W} {ℓ : W → Z → ℝ} {m : ℕ} {S : Fin m → Z} (hK : Convex ℝ K) (hℓ : ∀ (i : Fin m), ConvexOn ℝ K fun (w : W) => ℓ w (S i)) :

If every loss fun w => ℓ w (S i) is convex on K, the empirical risk on S is convex on K.

theorem Cslib.MachineLearning.continuousOn_empiricalRisk {W : Type u_1} {Z : Type u_2} [TopologicalSpace W] {K : Set W} {ℓ : W → Z → ℝ} {m : ℕ} {S : Fin m → Z} (hℓ : ∀ (i : Fin m), ContinuousOn (fun (w : W) => ℓ w (S i)) K) :

If every loss fun w => ℓ w (S i) is continuous on K, so is the empirical risk on S.

theorem Cslib.MachineLearning.empiricalRisk_mem_Icc {W : Type u_1} {Z : Type u_2} (ℓ : W → Z → ℝ) {m : ℕ} (S : Fin m → Z) {w : W} (hℓ : ∀ (z : Z), ℓ w z ∈ Set.Icc 0 1) :

A loss with values in [0, 1] has empirical risk in [0, 1] on every sample.

Expected risks #

theorem Cslib.MachineLearning.integrable_empiricalRisk_comp {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) {m : ℕ} (μ : MeasureTheory.Measure (Fin m → Z)) (A : (Fin m → Z) → W) (hint : ∀ (i : Fin m), MeasureTheory.Integrable (fun (S : Fin m → Z) => ℓ (A S) (S i)) μ) :
MeasureTheory.Integrable (fun (S : Fin m → Z) => empiricalRisk ℓ S (A S)) μ

For a hypothesis A S chosen from the sample, the empirical risk of A S is integrable when its loss on each example is.

theorem Cslib.MachineLearning.integral_empiricalRisk_comp {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) {m : ℕ} (μ : MeasureTheory.Measure (Fin m → Z)) (A : (Fin m → Z) → W) (hint : ∀ (i : Fin m), MeasureTheory.Integrable (fun (S : Fin m → Z) => ℓ (A S) (S i)) μ) :
∫ (S : Fin m → Z), empiricalRisk ℓ S (A S) ∂μ = Finset.univ.expect fun (i : Fin m) => ∫ (S : Fin m → Z), ℓ (A S) (S i) ∂μ

For a hypothesis A S chosen from the sample S (under any sample distribution μ), the expected empirical risk of A S is the average over i of the expected loss of A S on the i-th example.

theorem Cslib.MachineLearning.integral_empiricalRisk {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) (D : MeasureTheory.Measure Z) [MeasureTheory.IsProbabilityMeasure D] {m : ℕ} (hm : 0 < m) (w : W) (hint : MeasureTheory.Integrable (ℓ w) D) :
(∫ (S : Fin m → Z), empiricalRisk ℓ S w ∂MeasureTheory.Measure.pi fun (x : Fin m) => D) = trueRisk ℓ D w

Unbiasedness of the empirical risk: for a fixed hypothesis w with D-integrable loss, the expected empirical risk on m > 0 independent examples from a probability distribution D is the true risk.

theorem Cslib.MachineLearning.convexOn_trueRisk {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] [AddCommGroup W] [Module ℝ W] {K : Set W} {ℓ : W → Z → ℝ} {D : MeasureTheory.Measure Z} (hK : Convex ℝ K) (hℓ : ∀ (z : Z), ConvexOn ℝ K fun (w : W) => ℓ w z) (hint : ∀ w ∈ K, MeasureTheory.Integrable (ℓ w) D) :

If every loss fun w => ℓ w z is convex on K and ℓ w is D-integrable for every w ∈ K, the true risk is convex on K.

theorem Cslib.MachineLearning.measurable_empiricalRisk {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] {ℓ : W → Z → ℝ} (m : ℕ) {w : W} (hℓ : Measurable (ℓ w)) :
Measurable fun (S : Fin m → Z) => empiricalRisk ℓ S w

The empirical risk of w is measurable in the sample when the loss ℓ w is measurable.

theorem Cslib.MachineLearning.trueRisk_mem_Icc {W : Type u_1} {Z : Type u_2} [MeasurableSpace Z] (ℓ : W → Z → ℝ) (D : MeasureTheory.Measure Z) [MeasureTheory.IsProbabilityMeasure D] {w : W} (hℓ : ∀ (z : Z), ℓ w z ∈ Set.Icc 0 1) :
trueRisk ℓ D w ∈ Set.Icc 0 1

A loss with values in [0, 1] has true risk in [0, 1] under a probability distribution.