-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
62 lines (47 loc) · 1.8 KB
/
Copy pathutil.py
File metadata and controls
62 lines (47 loc) · 1.8 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
51
52
53
54
55
56
57
58
59
60
61
62
"""
Massachusetts Institute of Technology
Izzy Brand, 2020
"""
from itertools import combinations_with_replacement
import numpy as np
import torch
def class_combinations(c, n, m=np.inf):
""" Generates an array of n-element combinations where each element is one of
the c classes (an integer). If m is provided and m < n^c, then instead of all
n^c combinations, m combinations are randomly sampled.
Arguments:
c {int} -- the number of classes
n {int} -- the number of elements in each combination
Keyword Arguments:
m {int} -- the number of desired combinations (default: {np.inf})
Returns:
np.ndarry -- An [m x n] or [n^c x n] array of integers in [0, c)
"""
if m < c**n:
# randomly sample combinations
return np.random.randint(c, size=(int(m), n))
else:
p_c = combinations_with_replacement(np.arange(c), n)
return np.array(list(iter(p_c)), dtype=int)
def H(x, eps=1e-6):
""" Compute the element-wise entropy of x
Arguments:
x {torch.Tensor} -- array of probabilities in (0,1)
Keyword Arguments:
eps {float} -- prevent failure on x == 0
Returns:
torch.Tensor -- H(x)
"""
return -(x+eps)*torch.log(x+eps)
def hasnan(x):
return torch.isnan(x).any()
def remove_occurrences_from_list(l, items):
# print(items)
return list(np.setdiff1d(np.array(l, dtype=int),
np.array(items, dtype=int), assume_unique=True))
def move_data(indices, from_subset, to_subset):
from_subset.indices = remove_occurrences_from_list(from_subset.indices, indices)
if isinstance(to_subset.indices, list):
to_subset.indices.extend(indices)
elif isinstance(to_subset.indices, np.ndarray):
to_subset.indices = np.concatenate([to_subset.indices, np.array(indices)])