1
- # Bregman divergence
1
+ # Bregman divergence
2
2
3
3
"""
4
4
Implements the Bregman divergence, a friendly introduction to which can be found
5
- [here](http://mark.reid.name/blog/meet-the-bregman-divergences.html).
6
- Bregman divergences are a minimal implementation of the "mean-minimizer" property.
5
+ [here](http://mark.reid.name/blog/meet-the-bregman-divergences.html).
6
+ Bregman divergences are a minimal implementation of the "mean-minimizer" property.
7
7
8
- It is assumed that the (convex differentiable) function F maps vectors (of any type or size) to real numbers.
9
- The inner product used is `Base.dot`, but one can be passed in either by defining `inner` or by
10
- passing in a keyword argument. If an analytic gradient isn't available, Julia offers a suite
11
- of good automatic differentiation packages.
8
+ It is assumed that the (convex differentiable) function F maps vectors (of any type or size) to real numbers.
9
+ The inner product used is `Base.dot`, but one can be passed in either by defining `inner` or by
10
+ passing in a keyword argument. If an analytic gradient isn't available, Julia offers a suite
11
+ of good automatic differentiation packages.
12
12
13
13
function evaluate(dist::Bregman, p::AbstractVector, q::AbstractVector)
14
14
"""
@@ -18,31 +18,31 @@ struct Bregman{T1 <: Function, T2 <: Function, T3 <: Function} <: PreMetric
18
18
inner:: T3
19
19
end
20
20
21
- # Default costructor.
21
+ # Default costructor.
22
22
Bregman (F, ∇) = Bregman (F, ∇, LinearAlgebra. dot)
23
23
24
- # Evaluation fuction
24
+ # Evaluation fuction
25
25
function (dist:: Bregman )(p, q)
26
26
# Create cache vals.
27
27
FP_val = dist. F (p)
28
- FQ_val = dist. F (q)
28
+ FQ_val = dist. F (q)
29
29
DQ_val = dist.∇ (q)
30
30
p_size = length (p)
31
- # Check F codomain.
31
+ # Check F codomain.
32
32
if ! (isa (FP_val, Real) && isa (FQ_val, Real))
33
33
throw (ArgumentError (" F Codomain Error: F doesn't map the vectors to real numbers" ))
34
- end
35
- # Check vector size.
34
+ end
35
+ # Check vector size.
36
36
if p_size != length (q)
37
37
throw (DimensionMismatch (" The vector p ($(size (p)) ) and q ($(size (q)) ) are different sizes." ))
38
38
end
39
- # Check gradient size.
39
+ # Check gradient size.
40
40
if length (DQ_val) != p_size
41
41
throw (DimensionMismatch (" The gradient result is not the same size as p and q" ))
42
- end
43
- # Return the Bregman divergence.
42
+ end
43
+ # Return the Bregman divergence.
44
44
return FP_val - FQ_val - dist. inner (DQ_val, p .- q)
45
- end
45
+ end
46
46
47
- # Convenience function.
47
+ # Convenience function.
48
48
bregman (F, ∇, x, y; inner = LinearAlgebra. dot) = Bregman (F, ∇, inner)(x, y)
0 commit comments