• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alan-turing-institute / MLJTuning.jl / 445

4 Nov 2020 - 14:02 coverage: 81.224% (-8.3%) from 89.485%
445

Pull #83

travis-ci-com

web-flow
Merge 95ae99102 into c2b8a4e85
Pull Request #83: WIP Latin hypercube

0 of 40 new or added lines in 1 file covered. (0.0%)

398 of 490 relevant lines covered (81.22%)

987.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/strategies/latin_hypercube.jl
1
"""
2
LatinHypercube(nGenerations = 1, popSize = 100, nTournament = 2,
3
                pTournament = 0.8. interSampleWeight = 1.0,
4
                ae_power = 2, periodic_ae = false)
5

6
Instantiate  grid-based hyperparameter tuning strategy using the library
7
LatinHypercubeSampling.jl. The optimised Latin Hypercube sampling plan is
8
created using a genetic based optimization algorithm based on the inverse of the
9
Audze-Eglais function.
10
The optimization is run for nGenerations. The population size, number of samples
11
selected, probability of selection, the inter sample weight of sampling and the
12
norm can be choosen. There is also the possibility of using a periodic version
13
of the Audze-Eglais which reduces clustering along the boundaries of the
14
sampling plan. To enable this feature set `periodic_ae = true`.
15

16
### Supported ranges:
17

18
A single one-dimensional range or vector of one-dimensioinal ranges
19
can be specified. Specifically, in `LatinHypercubeSampling` search,
20
the `range` field of a `TunedModel` instance can be:
21

22
- A single one-dimensional range - ie, `ParamRange` object - `r`, constructed
23
using the `range` method.
24

25
- Any vector of objects of the above form
26

27
"""
28
mutable struct LatinHypercube <: TuningStrategy
29
    nGenerations::Int
30
    popSize::Int
31
    nTournament::Int
32
    pTournament::Number
33
    interSampleWeight::Number
34
    ae_power::Number
35
    periodic_ae::Bool
36
    rng::Random.AbstractRNG
37
end
38

39
function LatinHypercube(; nGenerations = 1, popSize = 100, nTournament = 2,
40
                        pTournament = 0.8, interSampleWeight = 1.0,
41
                        ae_power = 2, periodic_ae = false,
42
                        rng=Random.GLOBAL_RNG)
43

NEW
44
               _rng = rng isa Integer ? Random.MersenneTwister(rng) : rng
!
45

NEW
46
              return LatinHypercube(nGenerations,popSize,nTournament,
!
47
                                    pTournament, interSampleWeight, ae_power,
48
                                    periodic_ae, _rng)
49

50
end
51

52
function _create_bounds_and_dims(d,r)
NEW
53
    bounds = []
!
NEW
54
    dims = []
!
NEW
55
    for i = 1:d
!
NEW
56
        if r[i] isa NumericRange
!
NEW
57
            if !(r[i].scale isa Symbol)
!
NEW
58
                error("Callable scale not supported in LatinHyperCube tuning.")
!
59
            end
NEW
60
            push!(dims,Continuous())
!
NEW
61
            if isfinite(r[i].lower) && isfinite(r[i].upper)
!
NEW
62
                push!(bounds,(transform(MLJBase.Scale,scale(r[i].scale),r[i].lower),
!
63
                 transform(MLJBase.Scale,scale(r[i].scale),r[i].upper)))
NEW
64
            elseif !isfinite(r.lower) && isfinite(r.upper)
!
NEW
65
                push!(bounds,(transform(MLJBase.Scale,scale(r[i].scale),r[i].upper - 2*r[i].unit),
!
66
                 transform(MLJBase.Scale,scale(r[i].scale),r[i].upper)))
NEW
67
            elseif isfinite(r.lower) && !isfinite(r.upper)
!
NEW
68
                push!(bounds,(transform(MLJBase.Scale,scale(r[i].scale),r[i].lower),
!
69
                 transform(MLJBase.Scale,scale(r[i].scale),r[i].lower + 2*r[i].unit)))
70
            else
NEW
71
                push!(bounds,(transform(MLJBase.Scale,scale(r[i].scale),r[i].origin - r[i].unit),
!
72
                 transform(MLJBase.Scale,scale(r[i].scale),r[i].origin + r[i].unit)))
73
            end
74
        else
NEW
75
            push!(dims, Categorical(length(r[i].values), 1.0))
!
NEW
76
            push!(bounds,(0,length(r[i].values)))
!
77
        end
78
    end
NEW
79
    return bounds, dims
!
80
end
81

82
function setup(tuning::LatinHypercube, model, r, verbosity)
NEW
83
    d = length(r)
!
NEW
84
    bounds, dims = _create_bounds_and_dims(d, r)
!
NEW
85
    initial_plan = randomLHC(n,dims,nGenerations,
!
86
                              popsize = tuning.popSize,
87
                              ntour = tuning.nTournament,
88
                              ptour = tuning.pTournment,
89
                              interSampleWeight = tuning.interSampleWeight,
90
                              periodic_ae = tuning.periodic_ae,
91
                              ae_power = tuning.ae_power,
92
                              rng = tuning.rng)
NEW
93
    scaled_plan = scaleLHC(initial_plan, bounds)
!
NEW
94
    @inbounds for i = 1:size(scaled_plan,1)
!
NEW
95
        for j = 1:size(scaled_plan,2)
!
NEW
96
            if dims[j] isa LatinHypercubeSampling.Continuous
!
NEW
97
                scaled_plan[i][j] = inverse_transform(MLJBase.Scale,
!
98
                                                      scale(r[j].scale),
99
                                                      scaled_plan[i][j])
100
            else
NEW
101
                scaled_plan[i][j] = r[j].values[scaled_plan[i][j]]
!
102
            end
103
        end
104
    end
NEW
105
    ranges = r
!
NEW
106
    fields = map(r -> r.field, ranges)
!
NEW
107
    models = makeLatinHypercube(model, fields, scaled_plan)
!
NEW
108
    state = (models = models,
!
109
             fields = fields)
NEW
110
    return state
!
111
end
112

113
function MLJTuning.models(tuning::LatinHypercube,
114
                          model,
115
                          hystory,
116
                          state,
117
                          n_remaining,
118
                          verbosity)
NEW
119
     return state.models[_length(history) + 1:end], state
!
120
end
121

122
function makeLatinHypercube(prototype::Model,fields,plan)
NEW
123
    N = size(plan,1)
!
NEW
124
    map(1:N) do i
!
NEW
125
        clone = deepcopy(prototype)
!
NEW
126
        for k in eachindex(fields)
!
NEW
127
            recursive_setproperty(clone,fields[k],plan[i,k])
!
128
        end
NEW
129
        clone
!
130
    end
131
end
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc