-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit.jl
144 lines (128 loc) · 4.06 KB
/
fit.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function init(config::EvoLinearRegressor{L,T}, x, y; w = nothing) where {L,T}
cache = init_cache(config, x, y; w)
m = EvoLinearModel(L; coef = zeros(T, size(x, 2)), bias = zero(T))
return m, cache
end
function init_cache(::EvoLinearRegressor{L,T}, x, y; w = nothing) where {L,T}
∇¹, ∇² = zeros(T, size(x, 2)), zeros(T, size(x, 2))
∇b = zeros(T, 2)
w = isnothing(w) ? ones(T, size(y)) : convert(Vector{T}, w)
∑w = sum(w)
cache = (
∇¹ = ∇¹,
∇² = ∇²,
∇b = ∇b,
x = convert(Matrix{T}, x),
y = convert(Vector{T}, y),
w = w,
∑w = ∑w,
info = Dict(:nrounds => 0),
)
return cache
end
"""
fit(config::EvoLinearRegressor;
x, y, w=nothing,
x_eval=nothing, y_eval=nothing, w_eval=nothing,
metric=:none,
print_every_n=1)
Provided a `config`, `EvoLinear.fit` takes `x` and `y` as features and target inputs, plus optionally `w` as weights and train a Linear boosted model.
# Arguments
- `config::EvoLinearRegressor`:
# Keyword arguments
- `x::AbstractMatrix`: Features matrix. Dimensions are `[nobs, num_features]`.
- `y::AbstractVector`: Vector of observed targets.
- `w=nothing`: Vector of weights. Can be be either a `Vector` or `nothing`. If `nothing`, assumes a vector of 1s.
- `metric=nothing`: Evaluation metric to be tracked through each iteration. Default to `nothing`. Can be one of:
- `:mse`
- `:logistic`
- `:poisson_deviance`
- `:gamma_deviance`
- `:tweedie_deviance`
"""
function fit(
config::EvoLinearRegressor{L,T};
x_train,
y_train,
w_train = nothing,
x_eval = nothing,
y_eval = nothing,
w_eval = nothing,
metric = nothing,
print_every_n = 9999,
early_stopping_rounds = 9999,
verbosity = 1,
fnames = nothing,
return_logger = false,
) where {L,T}
m, cache = init(config, x_train, y_train; w = w_train)
logger = nothing
if !isnothing(metric) && !isnothing(x_eval) && !isnothing(y_eval)
cb = CallBackLinear(config; metric, x_eval, y_eval, w_eval)
logger = init_logger(;
T,
metric,
maximise = is_maximise(cb.feval),
early_stopping_rounds,
)
cb(logger, 0, m)
(verbosity > 0) && @info "initialization" metric = logger[:metrics][end]
end
for iter = 1:config.nrounds
fit!(m, cache, config)
if !isnothing(logger)
cb(logger, iter, m)
if iter % print_every_n == 0 && verbosity > 0
@info "iter $iter" metric = logger[:metrics][end]
end
(logger[:iter_since_best] >= logger[:early_stopping_rounds]) && break
end
end
if return_logger
return (m, logger)
else
return m
end
end
function fit!(m::EvoLinearModel{L}, cache, config::EvoLinearRegressor{L,T}) where {L,T}
∇¹, ∇², ∇b = cache.∇¹ .* 0, cache.∇² .* 0, cache.∇b .* 0
x, y, w = cache.x, cache.y, cache.w
∑w = cache.∑w
if config.updater == :all
# update all coefs then bias
p = m(x; proj = true)
update_∇_bias!(L, ∇b, x, y, p, w)
update_bias!(m, ∇b)
p = m(x; proj = true)
update_∇!(L, ∇¹, ∇², x, y, p, w)
update_coef!(m, ∇¹, ∇², ∑w, config)
else
@error "invalid updater"
end
cache[:info][:nrounds] += 1
return nothing
end
function update_coef!(m, ∇¹, ∇², ∑w, config)
update = -∇¹ ./ (∇² .+ config.L2 * ∑w)
update[abs.(update).<config.L1] .= 0
m.coef .+= update .* config.eta
return nothing
end
function update_bias!(m, ∇b)
m.bias += -∇b[1] / ∇b[2]
return nothing
end
function CallBackLinear(
::EvoLinearRegressor{L,T};
metric,
x_eval,
y_eval,
w_eval = nothing,
) where {L,T}
feval = metric_dict[metric]
x = convert(Matrix{T}, x_eval)
p = zeros(T, length(y_eval))
y = convert(Vector{T}, y_eval)
w = isnothing(w_eval) ? ones(T, size(y)) : convert(Vector{T}, w_eval)
return CallBackLinear(feval, x, p, y, w)
end