/
builtin_optimization.jl
120 lines (96 loc) · 4.15 KB
/
builtin_optimization.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
set_param!(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, value)
Set the value of a trainable parameter of the generative function.
NOTE: Does not update the gradient accumulator value.
"""
function set_param!(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, value)
gf.params[name] = value
end
"""
value = get_param(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
Get the current value of a trainable parameter of the generative function.
"""
function get_param(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
gf.params[name]
end
"""
value = get_param_grad(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
Get the current value of the gradient accumulator for a trainable parameter of the generative function.
"""
function get_param_grad(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
gf.params_grad[name]
end
"""
zero_param_grad!(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
Reset the gradient accumlator for a trainable parameter of the generative function to all zeros.
"""
function zero_param_grad!(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol)
gf.params_grad[name] = zero(gf.params[name])
end
"""
set_param_grad!(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, grad_value)
Set the gradient accumlator for a trainable parameter of the generative function.
"""
function set_param_grad!(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, grad_value)
gf.params_grad[name] = grad_value
end
"""
init_param!(gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, value)
Initialize the the value of a named trainable parameter of a generative function.
Also generates the gradient accumulator for that parameter to `zero(value)`.
Example:
```julia
init_param!(foo, :theta, 0.6)
```
"""
function init_param!(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, name::Symbol, value)
set_param!(gf, name, value)
zero_param_grad!(gf, name)
end
get_params(gf::Union{DynamicDSLFunction,StaticIRGenerativeFunction}) = keys(gf.params)
export set_param!, get_param, get_param_grad, zero_param_grad!, set_param_grad!, init_param!
#########################################
# gradient descent with fixed step size #
#########################################
mutable struct FixedStepGradientDescentBuiltinDSLState
step_size::Float64
gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}
param_list::Vector
end
function init_update_state(conf::FixedStepGradientDescent,
gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, param_list::Vector)
FixedStepGradientDescentBuiltinDSLState(conf.step_size, gen_fn, param_list)
end
function apply_update!(state::FixedStepGradientDescentBuiltinDSLState)
for param_name in state.param_list
value = get_param(state.gen_fn, param_name)
grad = get_param_grad(state.gen_fn, param_name)
set_param!(state.gen_fn, param_name, value + grad * state.step_size)
zero_param_grad!(state.gen_fn, param_name)
end
end
####################
# gradient descent #
####################
mutable struct GradientDescentBuiltinDSLState
step_size_init::Float64
step_size_beta::Float64
gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}
param_list::Vector
t::Int
end
function init_update_state(conf::GradientDescent,
gen_fn::Union{DynamicDSLFunction,StaticIRGenerativeFunction}, param_list::Vector)
GradientDescentBuiltinDSLState(conf.step_size_init, conf.step_size_beta,
gen_fn, param_list, 1)
end
function apply_update!(state::GradientDescentBuiltinDSLState)
step_size = state.step_size_init * (state.step_size_beta + 1) / (state.step_size_beta + state.t)
for param_name in state.param_list
value = get_param(state.gen_fn, param_name)
grad = get_param_grad(state.gen_fn, param_name)
set_param!(state.gen_fn, param_name, value + grad * step_size)
zero_param_grad!(state.gen_fn, param_name)
end
state.t += 1
end