-
Notifications
You must be signed in to change notification settings - Fork 6
/
selection_policy.py
131 lines (110 loc) · 3.51 KB
/
selection_policy.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
def softmax(x):
"""Stable softmax"""
x -= np.max(x, axis=0)
e_x = np.exp(x)
return e_x / np.sum(e_x, axis=0)
def get_idx_aug_baseline(LOO_influences):
"""Returns points randomly"""
idxs = np.random.choice(
len(LOO_influences),
len(LOO_influences),
p=None,
replace=False,
)
for idx in idxs:
yield [idx]
def get_idx_aug_influence(LOO_influences):
"""Returns points with probability proportional to magnitude of LOO"""
p = np.abs(LOO_influences, dtype=float)
p[p == 0] = min(np.min(p[p > 0]), 1e-20)
p /= np.sum(p)
idxs = np.random.choice(
len(LOO_influences),
len(LOO_influences),
p=p,
replace=False,
)
for idx in idxs:
yield [idx]
def get_idx_aug_k_dpp(LOO_influences, k):
"""Returns points with probability proportional to L matrix using DPP"""
import sample_dpp
L = LOO_influences.T.dot(LOO_influences)
assert len(L) == len(LOO_influences)
idxs = sample_dpp.oct_sample_k_dpp(
L,
k=k,
one_hot=False)
for idx in idxs:
yield [idx]
def get_idx_aug_influence_reverse(LOO_influences):
"""Returns points with probability proportional to magnitude of LOO"""
p = np.abs(LOO_influences)
p[p == 0] = min(np.min(p[p > 0]), 1e-20)
p = 1 / p
p /= np.sum(p)
p[p == 0] = 1e-20
p /= np.sum(p)
idxs = np.random.choice(
len(LOO_influences),
len(LOO_influences),
p=p,
replace=False,
)
for idx in idxs:
yield [idx]
def get_idx_aug_softmax_influence(LOO_influences):
"""Returns points with probability proportional to softmax of magnitude
of LOO"""
p = np.abs(LOO_influences)
p[p == 0] = min(np.min(p[p > 0]), 1e-20)
p = math_util.softmax(p)
idxs = np.random.choice(
len(LOO_influences),
len(LOO_influences),
p=p,
replace=False,
)
for idx in idxs:
yield [idx]
def get_idx_aug_softmax_influence_reverse(LOO_influences):
"""Returns points with probability proportional to softmax of magnitude
of LOO"""
p = np.abs(LOO_influences)
p[p == 0] = min(np.min(p[p > 0]), 1e-20)
p = 1 / p
p = math_util.softmax(p)
p[p == 0] = 1e-20
p /= np.sum(p)
idxs = np.random.choice(
len(LOO_influences),
len(LOO_influences),
p=p,
replace=False,
)
for idx in idxs:
yield [idx]
def get_idx_aug_deterministic_influence(LOO_influences):
"""Returns points in deterministic order ranked by LOO magnitude"""
idxs = np.argsort(-np.abs(LOO_influences))
for idx in idxs:
yield [idx]
def get_idx_aug_deterministic_influence_reverse(LOO_influences):
"""Returns points in deterministic order ranked by LOO magnitude"""
idxs = np.argsort(np.abs(LOO_influences))
for idx in idxs:
yield [idx]
name_to_policy = {
"baseline": get_idx_aug_baseline,
"random_proportional": get_idx_aug_influence,
"random_inverse_proportional": get_idx_aug_influence_reverse,
"random_softmax_proportional": get_idx_aug_softmax_influence,
"random_inverse_softmax_proportional":
get_idx_aug_softmax_influence_reverse,
"deterministic_proportional": get_idx_aug_deterministic_influence,
"deterministic_inverse_proportional":
get_idx_aug_deterministic_influence_reverse,
}
def get_policy_by_name(name):
return name_to_policy[name]