-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslates.py
172 lines (139 loc) · 5.25 KB
/
slates.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import pandas as pd
import numpy as np
from vowpalwabbit import pyvw
def create_slates_example(vw, shared, action_sets, outcome=None, debug=False):
def add(container, ex_string):
if(debug):
container.append(ex_string)
else:
container.append(vw.example(
ex_string, labelType=pyvw.pylibvw.vw.lConditionalContextualBandit))
examples = []
add(examples, "ccb shared |User {}".format(shared))
counter = 0
actions = []
slots = []
slot_index = 0
slot_thresh = 0
for action_set in action_sets:
ids = []
for action in action_set:
add(actions, "ccb action |Action {}".format(action))
ids.append(str(counter))
counter += 1
if(outcome is not None):
chosen, cost, prob = outcome[slot_index]
# Transform back to original space
chosen += slot_thresh
slot_str = "ccb slot {}:{}:{} {} |Slot slot_id={} constant".format(
chosen, cost, prob, ",".join(ids), slot_index)
else:
slot_str = "ccb slot {} |Slot slot_id={} constant".format(
",".join(ids), slot_index)
add(slots, slot_str)
slot_index += 1
slot_thresh = counter
examples.extend(actions)
examples.extend(slots)
return examples
def create_native_slates_example(vw, shared, action_sets, outcome=None, debug=False):
def add(container, ex_string):
if(debug):
container.append(ex_string)
else:
container.append(vw.example(
ex_string, labelType=pyvw.pylibvw.vw.lSlates))
examples = []
global_cost = ""
if(outcome is not None):
_, global_cost, _ = outcome[0]
add(examples, "slates shared {} |User {}".format(global_cost, shared))
counter = 0
actions = []
slots = []
slot_index = 0
for action_set in action_sets:
ids = []
for action in action_set:
add(actions, "slates action {} |Action {}".format(slot_index, action))
ids.append(str(counter))
counter += 1
if(outcome is not None):
chosen, cost, prob = outcome[slot_index]
slot_str = "slates slot {}:{} |Slot slot_id={} constant".format(
chosen, prob, slot_index)
else:
slot_str = "slates slot |Slot slot_id={} constant".format(slot_index)
add(slots, slot_str)
slot_index += 1
examples.extend(actions)
examples.extend(slots)
return examples
def create_cb_example(vw, shared, actions, outcome=None, debug=False):
def add(container, ex_string):
if(debug):
container.append(ex_string)
else:
container.append(vw.example(
ex_string, labelType=pyvw.pylibvw.vw.lContextualBandit))
examples = []
add(examples, "shared |User {}".format(shared))
if(outcome is not None):
chosen, cost, prob = outcome
else:
chosen = -1
for i, action in enumerate(actions):
if i == chosen:
action_str = "{}:{}:{} |Action {}".format(
chosen, cost, prob, action)
else:
action_str = "|Action {}".format(action)
add(examples, action_str)
return examples
def combine(lst, index_labels=None, fmt_str="{}={} {}"):
return combine_re(lst, 0, index_labels, fmt_str)
def combine_re(lst, index, index_labels, fmt_str):
index_label = index_labels[index] if index_labels != None else "_{}".format(
index)
if(index == len(lst) - 1):
return ["{}={}".format(index_label, item) for item in lst[index]]
vals = []
for item in lst[index]:
next_items = combine_re(lst, index+1, index_labels, fmt_str)
vals.extend([fmt_str.format(index_label, item, nxt)
for nxt in next_items])
return vals
def combine_float_actions(x_actions, y_actions, z_actions):
all_string_actions = []
all_actions = []
for x_action in x_actions:
for y_action in y_actions:
for z_action in z_actions:
all_string_actions.append(
"x={} y={} z={}".format(x_action, y_action, z_action))
all_actions.append((x_action, y_action, z_action))
return all_string_actions, all_actions
def combine_float_actions_categorical(x_actions, y_actions, z_actions):
all_string_actions = []
all_actions = []
for x_action in x_actions:
for y_action in y_actions:
for z_action in z_actions:
all_string_actions.append(
"x={},y={},z={}".format(x_action, y_action, z_action))
all_actions.append((x_action, y_action, z_action))
return all_string_actions, all_actions
def slate_pred_conv(prediction):
size_so_far = 0
for action_score in prediction:
for i, a_s in enumerate(action_score):
a, s = a_s
action_score[i] = (a - size_so_far, s)
size_so_far += len(action_score)
return prediction
def normalize(items):
return [float(i)/sum(items) for i in items]
def sample_index(id_prob_pairs):
ids = [item[0] for item in id_prob_pairs]
probabilities = normalize([item[1] for item in id_prob_pairs])
return np.random.choice(len(ids), p=probabilities)