Skip to content

Commit fedbb79

Browse files
Merge pull request #135 from Marco-Congedo/dev
dev
2 parents bbb9e02 + ca39c84 commit fedbb79

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PosDefManifoldML"
22
uuid = "a07f4532-e2c9-11e9-2ea2-6d98fe4a1f21"
33
authors = ["Marco-Congedo <marco.congedo@gmail.com>"]
4-
version = "0.5.15"
4+
version = "0.5.16"
55

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

src/conditioners.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# ? CONTENTS :
99
# This unit implements (pre) conditioners for fast Riemannian
10-
# machine learning classifier using package PosDefManifold.
10+
# machine learning classifier using package PosDefManifoldML
1111

1212
include("conditioners_lowlevel.jl")
1313

@@ -30,14 +30,14 @@ This is because the learnt parameters will be needed for the transformation of t
3030
abstract type Equalizing <: Conditioner end # Equalizing (individual scaling)
3131
abstract type Shrinking <: Conditioner end # Geodesic Shrinking
3232
```
33-
Abstract types for **Conditioners**. Those are the elemntary pipes
33+
Abstract types for **Conditioners**. Those are the elementary pipes
3434
to build a [Pipeline](@ref). The available conditioners are
3535
3636
- Tikhonov regularization (diagonal loading)
3737
- Recentering by whitening with or w/o dimensionality reduction
3838
- Compressing by global scaling
3939
- Equalizing by individual scaling
40-
- Shrinking by moving along geodesics towards the identity matrix
40+
- Shrinking by moving along geodesics with the identity matrix
4141
"""
4242
=#
4343

@@ -59,9 +59,9 @@ transform the set such as
5959
6060
``P_j+αI, \\ j=1,...,k``,
6161
62-
where ``I`` is the identity matrix and ``α`` is a non-negative number.
62+
where ``I`` is the identity matrix and ``α`` is a non-negative scalar.
6363
64-
This conditoner structure has two fields:
64+
This conditioner structure has two fields:
6565
6666
- `.α`, which is written in the structure when it is fitted to some data.
6767
@@ -136,7 +136,7 @@ the barycenter ``G``, like [`tsWeights`](@ref)
136136
does for computing the barycenter used for tangent space mapping.
137137
If the classes are balanced, the weighting has no effect.
138138
139-
This conditoner structure has the following fields:
139+
This conditioner structure has the following fields:
140140
141141
- `.metric`, of type [Metric](https://marco-congedo.github.io/PosDefManifold.jl/dev/MainModule/#Metric::Enumerated-type-1), is to be specified by the user. It is the metric that will be adopted to compute the class means and the distances to the mean. default: `PosDefManifold.Euclidean`.
142142
@@ -354,7 +354,7 @@ mutable struct Shrink <: Conditioner
354354
Mutable structure of the **geodesic shrinking** conditioner.
355355
356356
Given a set of points ``𝐏`` in the manifold of positive-definite matrices,
357-
this conditioner moves all points towards the identity matrix ``I`` along geodesics
357+
this conditioner moves all points towards or away from the identity matrix ``I`` along geodesics
358358
on the manifold defined in accordance to the specified `metric`. This effectively defines a ball
359359
centered at ``I``.
360360
@@ -460,6 +460,7 @@ A pipeline holds a sequence of conditioners learned
460460
and (optionally) applied using [`fit!`](@ref). It can be
461461
subsequently applied on other data as it has been learnt
462462
using the [`transform!`](@ref) function.
463+
463464
All `fit!` methods return a pipeline.
464465
465466
Pipelines comprising a single conditioner are allowed.
@@ -491,7 +492,7 @@ macro pipeline(args...)
491492
492493
Create a [`Pipeline`](@ref) chaining the provided expressions.
493494
494-
As an example, the sintax is:
495+
As an example, the syntax is:
495496
496497
```julia
497498
p = @pipeline Recenter() → Compress → Shrink(Fisher; threaded=false)
@@ -956,7 +957,7 @@ includes(pipeline, Shrink) # true
956957
957958
includes(pipeline, Shrink()) # true
958959
959-
# same type, althoug a different instance
960+
# same type, although a different instance
960961
includes(pipeline, Shrink(Fisher; radius=0.1)) # true
961962
962963
includes(pipeline, Compress) # false
@@ -975,6 +976,7 @@ function dim(pipeline::Pipeline)
975976
```
976977
Return the dimension determined by a fitted [`Recenter`](@ref) pre-conditioner
977978
if the `pipeline` comprises such a pre-conditioner, `nothing` otherwise.
979+
978980
This is used to adapt pipelines - see the documentation of the [`fit!`](@ref)
979981
function for ENLR machine learning models for an example.
980982
@@ -983,7 +985,7 @@ function for ENLR machine learning models for an example.
983985
using PosDefManifoldML, PosDefManifold
984986
985987
pipeline = @→ Recenter(; eVar=0.9) → Shrink()
986-
dim(pipeline) # return false, as it is not fitted
988+
dim(pipeline) # return `nothing`, as it is not fitted
987989
988990
P = randP(10, 5)
989991
p = fit!(P, pipeline)

src/conditioners_lowlevel.jl

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,15 @@ end
282282

283283
# TRAINING:
284284
# Return 2-tuple (𝐏, γ), where 𝐏 is the set of input matrices 𝐏 shrinked in an open ball with given `radius` r
285-
# according to the given `metric`, and γ ∈ (0, 1] is the the shrinking parameter.
285+
# according to the given `metric`, and γ ∈ (0, 1] is the shrinking parameter.
286286
# The radius r englobes the most distant point of set 𝐏 if `refpoint`=:max, the mean eccentricity
287287
# of the points in 𝐏 if `refpoint`=:mean. This letter option means that the points are shrinked so that r
288288
# is equal to sqrt(1/n * mean(norm(P) for P in 𝐏).
289-
# The shrinking is done moving, for each matric P_k in the set, along the gedesic relying I to P_k,
290-
# acording to the given metric. The distance according to the given metric is evaluated for all matrices
289+
# The shrinking is done moving, for each matrix P_k in the set, along the gedesic relying I to P_k,
290+
# according to the given metric. The distance according to the given metric is evaluated for all matrices
291291
# in 𝐏 and γ is computed so as to set the maximum or mean eccentricity equal to r + ϵ,
292292
# where ϵ is the `epsilon` kwarg.
293-
# If the maximum or mean eccentricity is already in the ball of radius r, no shrinking is carried out
294-
# and return 2-tuple (𝐏, 1).
293+
# The ball containing 𝐏 may also be increased (the opposite of shrinking).
295294
# For all metrics γ = (r*√n) / (δ(P, I) + ϵ). The defaults are r = 0.02 and ϵ = 0
296295

297296
# Proof (for three metrics, see PosDefManifold.jl for available metrics supporting a geodesic equation):
@@ -363,10 +362,10 @@ function shrink!(metric::PosDefManifold.Metric, 𝐏::PosDefManifold.ℍVector,
363362
d = threaded ? Folds.sum(funcLC, 𝐋)/length(𝐋) : sum(funcLC, 𝐋)/length(𝐋)
364363
end
365364

366-
if d radius # d is the maximal or mean logCholesky distance squared to I
365+
# if d ≥ radius # d is the maximal or mean logCholesky distance squared to I
367366
γ = (radius * sqrt(n)) / (sqrt(d) + epsilon)
368367
0 γ || throw(ArgumentError("Shrink conditioner with $(metric) metric; shrinkage parameter γ≤0"))
369-
γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
368+
# γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
370369

371370
# move on the logCholesky geodesic relying I to 𝐏[i] with step-size γ
372371
if transform
@@ -380,28 +379,28 @@ function shrink!(metric::PosDefManifold.Metric, 𝐏::PosDefManifold.ℍVector,
380379
end
381380
end
382381
end # if transform
383-
else
384-
verbose && @warn "Shrink conditioner with method $(metric): no shrinking is necessary" d radius
385-
end
382+
# else
383+
# verbose && @warn "Shrink conditioner with method $(metric): no shrinking is necessary" d radius
384+
# end
386385

387386
elseif metric == PosDefManifold.Fisher
388387
#eltype(𝐏[1]) <: Complex && throw(ArgumentError("The metric Fisher for function shrink! and shrink is defined only for real matrices"))
389388
𝛌, 𝐔 = evds(𝐏; threaded)
390389

391-
# here the eigenvalues could be normalized to costant variance
390+
# here the eigenvalues could be normalized to constant variance
392391

393392
funcF(λ) = sqrt(sum(x -> real(log(x))^2, λ)) # norm
394393
if refpoint==:max
395394
d = threaded ? Folds.maximum(funcF, 𝛌) : maximum(funcF, 𝛌)
396395
else
397-
d = threaded ? Folds.sum(funcF, 𝛌)/length(𝛌) : sum(funcF, 𝛌)/length(𝛌) # average eccentricity
396+
d = threaded ? Folds.sum(funcF, 𝛌)/length(𝛌) : sum(funcF, 𝛌)/length(𝛌) # average norm
398397
end
399398

400-
if d radius # γ is the Fisher distance to I
401-
γ = (radius * sqrt(n)) / (d + epsilon)
399+
# if d ≥ radius
400+
γ = (radius * sqrt(n)) / (d + epsilon) # γ is the Fisher distance to I
402401
# println("γ, d ", γ, " ", d)
403402
0 γ || throw(ArgumentError("Shrink conditioner with $(metric) metric; computed shrinkage parameter γ≤0"))
404-
γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
403+
# γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
405404

406405
# move on the Fisher geodesic relying I to 𝐏[i] with step-size γ
407406
# and re-recenter the eigenvalues if recenter=true
@@ -442,9 +441,9 @@ function shrink!(metric::PosDefManifold.Metric, 𝐏::PosDefManifold.ℍVector,
442441
end
443442
end
444443
end # if transform
445-
else
446-
verbose && @warn "Shrink conditioner with method $(metric): no shrinking was necessary" d radius
447-
end
444+
# else
445+
# verbose && @warn "Shrink conditioner with method $(metric): no shrinking was necessary" d radius
446+
# end
448447

449448
else # all other supported metrics
450449
func(P) = distance(metric, P) # distance
@@ -454,10 +453,10 @@ function shrink!(metric::PosDefManifold.Metric, 𝐏::PosDefManifold.ℍVector,
454453
d = threaded ? Folds.sum(func, 𝐏)/length(𝐏) : sum(func, 𝐏)/length(𝐏)
455454
end
456455

457-
if d radius # γ is the metric distance to I
456+
# if d ≥ radius # γ is the metric distance to I
458457
γ = (radius * sqrt(n)) / (d + epsilon)
459458
0 γ || throw(ArgumentError("Shrink conditioner with $(metric) metric; computed shrinkage parameter γ≤0"))
460-
γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
459+
#γ < 1 || @warn "Shrink conditioner with $(metric) metric; shrinkage parameter γ≥1" γ radius
461460

462461
if transform
463462
#0 < γ ≤ 1 || throw(ArgumentError("shrink! or shrink function with $(metric) metric; shrinkage parameter γ ∉(0, 1]"))
@@ -473,9 +472,9 @@ function shrink!(metric::PosDefManifold.Metric, 𝐏::PosDefManifold.ℍVector,
473472
end
474473
end
475474
end # if transform
476-
else
477-
verbose && @warn "Shrink conditioner with metric $(metric): no shrinking was necessary" d radius
478-
end
475+
# else
476+
# verbose && @warn "Shrink conditioner with metric $(metric): no shrinking was necessary" d radius
477+
# end
479478
end
480479

481480
return (𝐏, γ, m, sd)

0 commit comments

Comments
 (0)