src/DifferentiableFN.jl

Wed, 22 Dec 2021 11:14:38 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 22 Dec 2021 11:14:38 +0200
changeset 35
d881275c6564
parent 27
62c62f451a41
child 37
f8be66557e0f
permissions
-rw-r--r--

Add metaprogramming tools and fast multidimensional loops.

27
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
1 ########################################################
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
2 # Abstract class for differentiable functions that have
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
3 # abstract LinOps as differentials. Also included are
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
4 # sum composition and squaring.
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
5 ########################################################
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
6
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 module DifferentiableFN
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 using ..LinOps
27
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
10 using ..Util
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
25
90f92ee9cb81 Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents: 21
diff changeset
12 export Func,
90f92ee9cb81 Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents: 21
diff changeset
13 DiffF,
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 value,
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 differential,
27
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
16 adjoint_differential,
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
17 Squared,
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
18 Summed,
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
19 SummedVOnly
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
25
90f92ee9cb81 Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents: 21
diff changeset
21 abstract type Func{X,Y} end
90f92ee9cb81 Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents: 21
diff changeset
22 abstract type DiffF{X,Y,T} <: Func{X, Y} end
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23
25
90f92ee9cb81 Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents: 21
diff changeset
24 function value(f :: F, x :: X) :: Y where {X, Y, F <: Func{X,Y}}
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 @error "`value` unimplemented"
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 end
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 # function (f :: D)(x::X) where {X, Y, T <: LinOp{X, Y}, D <: DiffF{X, Y, T}}
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 # return value(x)
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 # end
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 function differential(f :: DiffF{X,Y,T}, x :: X) :: T where {X, Y, T <: LinOp{X, Y}}
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 @error "`differential` unimplemented"
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 end
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 function adjoint_differential(f :: DiffF{X,Y,T}, x :: X) :: T where {X, Y, T <: LinOp{X, Y}}
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 @error "`adjoint_differential` unimplemented"
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 end
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 function adjoint_differential(f :: DiffF{X,Y, T}, x :: X) :: T where {X, Y, T <: AdjointableOp{X, Y}}
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 return AdjointOp(differential(f, x))
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 end
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43
27
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
44 # Sum of differentiable functions
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
45
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
46 struct Summed{X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}} <: DiffF{X, Y, T}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
47 a :: D₁
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
48 b :: D₂
21
3b7fcc651585 Add DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 end
27
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
50
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
51 function value(summed :: Summed{X, Y, T, D₁, D₂}, x :: X) where {X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
52 return value(summed.a, x)+value(summed.b, x)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
53 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
54
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
55 function differential(summed :: Summed{X, Y, T, D₁, D₂}, x :: X) where {X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
56 return differential(summed.a, x) + differential(summed.b, x)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
57 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
58
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
59 # Sum of (non-differentiable) fucntions
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
60
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
61 struct SummedVOnly{X, Y, D₁ <: Func{X, Y}, D₂ <: Func{X, Y}} <: Func{X, Y}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
62 a :: D₁
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
63 b :: D₂
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
64 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
65
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
66 function value(summed :: SummedVOnly{X, Y, D₁, D₂}, x :: X) where {X, Y, D₁ <: Func{X, Y}, D₂ <: Func{X, Y}}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
67 return value(summed.a, x)+value(summed.b, x)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
68 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
69
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
70 # Squared norm of a differentiable function as a differentiable function
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
71
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
72 ℝ = Float64
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
73 ℝⁿ = Vector{Float64}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
74
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
75 struct Squared <: DiffF{ℝⁿ, ℝ, MatrixOp{ℝ}}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
76 r :: DiffF{ℝⁿ, ℝⁿ, MatrixOp{ℝ}}
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
77 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
78
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
79 function value(sq :: Squared, x :: ℝⁿ)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
80 return norm₂²(value(sq.r, x))/2
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
81 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
82
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
83 function differential(sq :: Squared, x :: ℝⁿ)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
84 v = value(sq.r, x)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
85 d = differential(sq.r, x)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
86 return MatrixOp(v'*d.m)
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
87 end
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
88
62c62f451a41 DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents: 25
diff changeset
89 end

mercurial