|
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, init_sample_size, dim_max_sample_size=1000000): |
|
12
|
|
|
self.conv = conv |
|
13
|
|
|
self.init_sample_size = init_sample_size |
|
14
|
|
|
self.dim_max_sample_size = dim_max_sample_size |
|
15
|
|
|
|
|
16
|
|
|
def get_pos_space(self): |
|
17
|
|
|
if self.init_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.init_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.init_sample_size) > 10000: |
|
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 |
|
|
|
|
|
|
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
|
|
|
|