Passed
Push — master ( 626f23...be3b1e )
by Simon
01:48
created

gradient_free_optimizers.optimizers.smb_opt.sampling   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A InitialSampler.__init__() 0 4 1
A InitialSampler.random_choices() 0 14 3
B InitialSampler.get_pos_space() 0 19 6
A InitialSampler.get_n_samples_dims() 0 22 3
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import math
6
import random
7
import numpy as np
8
9
10
class InitialSampler:
11
    def __init__(self, conv, max_sample_size, dim_max_sample_size=1000000):
12
        self.conv = conv
13
        self.max_sample_size = max_sample_size
14
        self.dim_max_sample_size = dim_max_sample_size
15
16
    def get_pos_space(self):
17
        if self.max_sample_size < self.conv.search_space_size:
18
            n_samples_array = self.get_n_samples_dims()
19
            return self.random_choices(n_samples_array)
20
        else:
21
            if self.conv.max_dim < 255:
22
                _dtype = np.uint8
23
            elif self.conv.max_dim < 65535:
24
                _dtype = np.uint16
25
            elif self.conv.max_dim < 4294967295:
26
                _dtype = np.uint32
27
            else:
28
                _dtype = np.uint64
29
30
            pos_space = []
31
            for dim_ in self.conv.dim_sizes:
32
                pos_space.append(np.arange(dim_, dtype=_dtype))
33
34
            return pos_space
35
36
    def get_n_samples_dims(self):
37
        # TODO of search space is > 33 dims termination criterion must be:
38
        # "search_space_size < self.max_sample_size"
39
40
        dim_sizes_temp = self.conv.dim_sizes
41
        dim_sizes_temp = np.clip(
42
            dim_sizes_temp, a_min=1, a_max=self.dim_max_sample_size
43
        )
44
        search_space_size = self.conv.dim_sizes.prod()
45
46
        while abs(search_space_size - self.max_sample_size) > self.max_sample_size / 10:
47
            n_samples_array = []
48
            for idx, dim_size in enumerate(np.nditer(dim_sizes_temp)):
49
                array_diff_ = random.randint(1, dim_size)
50
                n_samples_array.append(array_diff_)
51
52
                sub = int((dim_size / 1000) ** 1.5)
53
                dim_sizes_temp[idx] = np.maximum(1, dim_size - sub)
54
55
            search_space_size = np.array(n_samples_array).prod()
56
57
        return n_samples_array
0 ignored issues
show
introduced by
The variable n_samples_array does not seem to be defined in case the while loop on line 46 is not entered. Are you sure this can never be the case?
Loading history...
58
59
    def random_choices(self, n_samples_array):
60
        pos_space = []
61
        for n_samples, dim_size in zip(n_samples_array, self.conv.dim_sizes):
62
63
            if dim_size > self.dim_max_sample_size:
64
                pos_space.append(
65
                    np.random.randint(low=1, high=dim_size, size=n_samples)
66
                )
67
            else:
68
                pos_space.append(
69
                    np.random.choice(dim_size, size=n_samples, replace=False)
70
                )
71
72
        return pos_space
73