1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import random |
6
|
|
|
import numpy as np |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class InitialSampler: |
10
|
|
|
def __init__(self, conv, max_sample_size, dim_max_sample_size=1000000): |
11
|
|
|
self.conv = conv |
12
|
|
|
self.max_sample_size = max_sample_size |
13
|
|
|
self.dim_max_sample_size = dim_max_sample_size |
14
|
|
|
|
15
|
|
|
if self.conv.n_dimensions > 31: |
16
|
|
|
msg = "maximum supported dimension for a search-space in sequence-model-based optimizers is 31" |
17
|
|
|
raise ValueError(msg) |
18
|
|
|
|
19
|
|
|
def get_pos_space(self): |
20
|
|
|
if self.max_sample_size < self.conv.search_space_size: |
21
|
|
|
n_samples_array = self.get_n_samples_dims() |
22
|
|
|
return self.random_choices(n_samples_array) |
23
|
|
|
else: |
24
|
|
|
if self.conv.max_dim < 255: |
25
|
|
|
_dtype = np.uint8 |
26
|
|
|
elif self.conv.max_dim < 65535: |
27
|
|
|
_dtype = np.uint16 |
28
|
|
|
elif self.conv.max_dim < 4294967295: |
29
|
|
|
_dtype = np.uint32 |
30
|
|
|
else: |
31
|
|
|
_dtype = np.uint64 |
32
|
|
|
|
33
|
|
|
pos_space = [] |
34
|
|
|
for dim_ in self.conv.dim_sizes: |
35
|
|
|
pos_space.append(np.arange(dim_, dtype=_dtype)) |
36
|
|
|
|
37
|
|
|
return pos_space |
38
|
|
|
|
39
|
|
|
def get_n_samples_dims(self): |
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
|
|
|
if search_space_size == 0: |
46
|
|
|
search_space_size = np.inf |
47
|
|
|
|
48
|
|
|
while abs(search_space_size) > self.max_sample_size: |
49
|
|
|
n_samples_array = [] |
50
|
|
|
for idx, dim_size in enumerate(np.nditer(dim_sizes_temp)): |
51
|
|
|
array_diff_ = random.randint(1, dim_size) |
52
|
|
|
n_samples_array.append(array_diff_) |
53
|
|
|
|
54
|
|
|
sub = max(int(dim_size - (dim_size**0.999)), 1) |
55
|
|
|
dim_sizes_temp[idx] = np.maximum(1, dim_size - sub) |
56
|
|
|
|
57
|
|
|
search_space_size = np.array(n_samples_array).prod() |
58
|
|
|
if search_space_size == 0: |
59
|
|
|
search_space_size = np.inf |
60
|
|
|
|
61
|
|
|
return n_samples_array |
62
|
|
|
|
63
|
|
|
def random_choices(self, n_samples_array): |
64
|
|
|
pos_space = [] |
65
|
|
|
for n_samples, dim_size in zip(n_samples_array, self.conv.dim_sizes): |
66
|
|
|
if dim_size > self.dim_max_sample_size: |
67
|
|
|
pos_space.append( |
68
|
|
|
np.random.randint(low=1, high=dim_size, size=n_samples) |
69
|
|
|
) |
70
|
|
|
else: |
71
|
|
|
pos_space.append( |
72
|
|
|
np.random.choice(dim_size, size=n_samples, replace=False) |
73
|
|
|
) |
74
|
|
|
|
75
|
|
|
return pos_space |
76
|
|
|
|