forked from gabmoreira/subspaces
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampler.py
More file actions
50 lines (43 loc) · 1.58 KB
/
Copy pathsampler.py
File metadata and controls
50 lines (43 loc) · 1.58 KB
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
import numpy as np
from typing import List, Optional
class MintermSampler():
def __init__(
self,
targets: List[List[int]],
batch_size: int,
n_literals: int,
n_minterms_per_batch: Optional[int] = None
) -> None:
self._batch_size = batch_size
self._n_samples = len(targets)
self._n_literals = n_literals
self._targets = np.array(targets)
self._n_batches = self._n_samples // self._batch_size
self._minterms, self._minterm_labels = np.unique(
self._targets,
axis=0,
return_inverse=True,
)
self._n_minterms = self._minterm_labels.max() + 1
self._minterm2idx = []
for m in range(self._n_minterms):
self._minterm2idx.append(np.where(self._minterm_labels == m)[0])
if n_minterms_per_batch is None:
self._n_minterms_per_batch = self._n_literals
else:
self._n_minterms_per_batch = n_minterms_per_batch
def __iter__(self):
for _ in range(self._n_batches):
sel = np.random.choice(
np.arange(self._n_minterms),
self._n_minterms_per_batch,
replace=False
)
batch = []
for minterm_label in sel:
batch.append(np.random.choice(self._minterm2idx[minterm_label],
self._batch_size // self._n_minterms_per_batch))
batch = np.concatenate(batch)
yield (batch)
def __len__(self) -> int:
return self._n_batches