-
Notifications
You must be signed in to change notification settings - Fork 9
/
baller2vec.py
392 lines (339 loc) · 13.9 KB
/
baller2vec.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Adapted from: https://pytorch.org/tutorials/beginner/transformer_tutorial.html.
import math
import torch
from torch import nn
class Baller2Vec(nn.Module):
def __init__(
self,
n_player_ids,
embedding_dim,
sigmoid,
seq_len,
mlp_layers,
n_players,
n_player_labels,
n_ball_labels,
n_seq_labels,
nhead,
dim_feedforward,
num_layers,
dropout,
use_cls,
embed_before_mlp,
):
super().__init__()
self.sigmoid = sigmoid
self.seq_len = seq_len
self.use_cls = use_cls
self.n_players = n_players
self.embed_before_mlp = embed_before_mlp
# Initialize players, ball, and CLS (if used) embeddings.
initrange = 0.1
self.player_embedding = nn.Embedding(n_player_ids, embedding_dim)
self.player_embedding.weight.data.uniform_(-initrange, initrange)
self.ball_embedding = nn.Parameter(torch.Tensor(embedding_dim))
nn.init.uniform_(self.ball_embedding, -initrange, initrange)
if use_cls:
self.cls_embedding = nn.Parameter(torch.Tensor(mlp_layers[-1]))
nn.init.uniform_(self.cls_embedding, -initrange, initrange)
# Initialize preprocessing MLPs.
player_mlp = nn.Sequential()
ball_mlp = nn.Sequential()
# Extra dimensions for (x, y) coordinates and hoop side (for players) or z
# coordinate (for ball).
in_feats = embedding_dim + 3 if embed_before_mlp else 3
for (layer_idx, out_feats) in enumerate(mlp_layers):
if (not embed_before_mlp) and (layer_idx == len(mlp_layers) - 1):
out_feats = out_feats - embedding_dim
player_mlp.add_module(f"layer{layer_idx}", nn.Linear(in_feats, out_feats))
ball_mlp.add_module(f"layer{layer_idx}", nn.Linear(in_feats, out_feats))
if layer_idx < len(mlp_layers) - 1:
player_mlp.add_module(f"relu{layer_idx}", nn.ReLU())
ball_mlp.add_module(f"relu{layer_idx}", nn.ReLU())
in_feats = out_feats
self.player_mlp = player_mlp
self.ball_mlp = ball_mlp
# Initialize Transformer.
d_model = mlp_layers[-1]
self.d_model = d_model
encoder_layer = nn.TransformerEncoderLayer(
d_model, nhead, dim_feedforward, dropout
)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers)
# Initialize classification layers.
self.player_classifier = nn.Linear(d_model, n_player_labels)
self.player_classifier.weight.data.uniform_(-initrange, initrange)
self.player_classifier.bias.data.zero_()
self.ball_classifier = nn.Linear(d_model, n_ball_labels)
self.ball_classifier.weight.data.uniform_(-initrange, initrange)
self.ball_classifier.bias.data.zero_()
if use_cls:
self.event_classifier = nn.Linear(d_model, n_seq_labels)
self.event_classifier.weight.data.uniform_(-initrange, initrange)
self.event_classifier.bias.data.zero_()
# Initialize mask.
self.register_buffer("mask", self.generate_self_attn_mask())
def generate_self_attn_mask(self):
# n players plus the ball and the CLS entity (if used).
if self.use_cls:
sz = (self.n_players + 2) * self.seq_len
else:
sz = (self.n_players + 1) * self.seq_len
mask = torch.zeros(sz, sz)
ball_start = self.n_players * self.seq_len
if self.use_cls:
cls_start = ball_start + self.seq_len
for step in range(self.seq_len):
start = self.n_players * step
stop = start + self.n_players
ball_stop = ball_start + step + 1
# The players can look at the players.
mask[start:stop, :stop] = 1
# The players can look at the ball.
mask[start:stop, ball_start:ball_stop] = 1
# The ball can look at the players.
mask[ball_start + step, :stop] = 1
# The ball can look at the ball.
mask[ball_start + step, ball_start:ball_stop] = 1
if self.use_cls:
cls_stop = cls_start + step + 1
# The players can look at the CLS.
mask[start:stop, cls_start:cls_stop] = 1
# The ball can look at the CLS.
mask[ball_start + step, cls_start:cls_stop] = 1
# The CLS can look at the players.
mask[cls_start + step, :stop] = 1
# The CLS can look at the ball.
mask[cls_start + step, ball_start:ball_stop] = 1
# The CLS can look at the CLS.
mask[cls_start + step, cls_start:cls_stop] = 1
mask = mask.masked_fill(mask == 0, float("-inf"))
mask = mask.masked_fill(mask == 1, float(0.0))
return mask
def forward(self, tensors):
device = list(self.player_mlp.parameters())[0].device
# Get player features.
player_embeddings = self.player_embedding(
tensors["player_idxs"].flatten().to(device)
)
if self.sigmoid == "logistic":
player_embeddings = torch.sigmoid(player_embeddings)
elif self.sigmoid == "tanh":
player_embeddings = torch.tanh(player_embeddings)
player_xs = tensors["player_xs"].flatten().unsqueeze(1).to(device)
player_ys = tensors["player_ys"].flatten().unsqueeze(1).to(device)
player_hoop_sides = (
tensors["player_hoop_sides"].flatten().unsqueeze(1).to(device)
)
if self.embed_before_mlp:
player_pos = torch.cat(
[
player_embeddings,
player_xs,
player_ys,
player_hoop_sides,
],
dim=1,
)
player_feats = self.player_mlp(player_pos) * math.sqrt(self.d_model)
else:
player_pos = torch.cat(
[
player_xs,
player_ys,
player_hoop_sides,
],
dim=1,
)
pos_feats = self.player_mlp(player_pos) * math.sqrt(self.d_model)
player_feats = torch.cat([player_embeddings, pos_feats], dim=1)
# Get ball features.
ball_embeddings = self.ball_embedding.repeat(self.seq_len, 1)
ball_xs = tensors["ball_xs"].unsqueeze(1).to(device)
ball_ys = tensors["ball_ys"].unsqueeze(1).to(device)
ball_zs = tensors["ball_zs"].unsqueeze(1).to(device)
if self.embed_before_mlp:
ball_pos = torch.cat(
[
ball_embeddings,
ball_xs,
ball_ys,
ball_zs,
],
dim=1,
)
ball_feats = self.ball_mlp(ball_pos) * math.sqrt(self.d_model)
else:
ball_pos = torch.cat(
[
ball_xs,
ball_ys,
ball_zs,
],
dim=1,
)
pos_feats = self.player_mlp(ball_pos) * math.sqrt(self.d_model)
ball_feats = torch.cat([ball_embeddings, pos_feats], dim=1)
# Combine players and ball features.
combined = torch.cat([player_feats, ball_feats], dim=0)
if self.use_cls:
# Get CLS features.
cls_feats = self.cls_embedding.repeat(self.seq_len, 1)
# Combine with CLS features.
combined = torch.cat([combined, cls_feats], dim=0)
output = self.transformer(combined.unsqueeze(1), self.mask)
preds = {
"player": self.player_classifier(output).squeeze(1),
"ball": self.ball_classifier(output).squeeze(1),
}
if self.use_cls:
preds["seq_label"] = self.event_classifier(output).squeeze(1)
return preds
class Baller2VecSeq2Seq(nn.Module):
def __init__(
self,
n_player_ids,
embedding_dim,
sigmoid,
seq_len,
mlp_layers,
n_player_labels,
nhead,
dim_feedforward,
num_layers,
dropout,
):
super().__init__()
self.sigmoid = sigmoid
self.seq_len = seq_len
# Initialize players and ball embeddings.
initrange = 0.1
self.player_embedding = nn.Embedding(n_player_ids, embedding_dim)
self.player_embedding.weight.data.uniform_(-initrange, initrange)
self.ball_embedding = nn.Parameter(torch.Tensor(embedding_dim))
nn.init.uniform_(self.ball_embedding, -initrange, initrange)
d_model = mlp_layers[-1]
self.d_model = d_model
model = {}
for enc_dec in ["enc", "dec"]:
# Initialize preprocessing MLPs.
player_mlp = nn.Sequential()
ball_mlp = nn.Sequential()
# Extra dimensions for (x, y) coordinates and hoop side (for players) or z
# coordinate (for ball).
in_feats = embedding_dim + 3
for (layer_idx, out_feats) in enumerate(mlp_layers):
player_mlp.add_module(
f"layer{layer_idx}", nn.Linear(in_feats, out_feats)
)
ball_mlp.add_module(f"layer{layer_idx}", nn.Linear(in_feats, out_feats))
if layer_idx < len(mlp_layers) - 1:
player_mlp.add_module(f"relu{layer_idx}", nn.ReLU())
ball_mlp.add_module(f"relu{layer_idx}", nn.ReLU())
in_feats = out_feats
# Initialize Transformer.
if enc_dec == "enc":
encoder_layer = nn.TransformerEncoderLayer(
d_model, nhead, dim_feedforward, dropout
)
transformer = nn.TransformerEncoder(encoder_layer, num_layers)
else:
decoder_layer = nn.TransformerDecoderLayer(
d_model, nhead, dim_feedforward, dropout
)
transformer = nn.TransformerDecoder(decoder_layer, num_layers)
model[enc_dec] = nn.ModuleDict(
{
"player_mlp": player_mlp,
"ball_mlp": ball_mlp,
"transformer": transformer,
}
)
self.model = nn.ModuleDict(model)
# Initialize classification layer.
self.player_classifier = nn.Linear(d_model, n_player_labels)
self.player_classifier.weight.data.uniform_(-initrange, initrange)
self.player_classifier.bias.data.zero_()
# Initialize mask.
self.register_buffer("mask", self.generate_self_attn_mask())
def generate_self_attn_mask(self):
# Five players plus the ball.
sz = 6 * self.seq_len
mask = torch.zeros(sz, sz)
ball_start = 5 * self.seq_len
for step in range(self.seq_len):
start = 5 * step
stop = start + 5
ball_stop = ball_start + step + 1
# The players can look at the players.
mask[start:stop, :stop] = 1
# The players can look at the ball.
mask[start:stop, ball_start:ball_stop] = 1
# The ball can look at the players.
mask[ball_start + step, :stop] = 1
# The ball can look at the ball.
mask[ball_start + step, ball_start:ball_stop] = 1
mask = mask.masked_fill(mask == 0, float("-inf"))
mask = mask.masked_fill(mask == 1, float(0.0))
return mask
def forward(self, tensors, start_stops):
device = list(self.player_embedding.parameters())[0].device
for enc_dec in ["enc", "dec"]:
(start, stop) = start_stops[enc_dec]
# Get player position features.
player_embeddings = self.player_embedding(
tensors["player_idxs"][:, start:stop].flatten().to(device)
)
if self.sigmoid == "logistic":
player_embeddings = torch.sigmoid(player_embeddings)
elif self.sigmoid == "tanh":
player_embeddings = torch.tanh(player_embeddings)
player_xs = (
tensors["player_xs"][:, start:stop].flatten().unsqueeze(1).to(device)
)
player_ys = (
tensors["player_ys"][:, start:stop].flatten().unsqueeze(1).to(device)
)
player_hoop_sides = (
tensors["player_hoop_sides"][:, start:stop]
.flatten()
.unsqueeze(1)
.to(device)
)
player_pos = torch.cat(
[
player_embeddings,
player_xs,
player_ys,
player_hoop_sides,
],
dim=1,
)
player_feats = self.model[enc_dec]["player_mlp"](player_pos) * math.sqrt(
self.d_model
)
# Get ball position features.
ball_embeddings = self.ball_embedding.repeat(self.seq_len, 1)
ball_xs = tensors["ball_xs"].unsqueeze(1).to(device)
ball_ys = tensors["ball_ys"].unsqueeze(1).to(device)
ball_zs = tensors["ball_zs"].unsqueeze(1).to(device)
ball_pos = torch.cat(
[
ball_embeddings,
ball_xs,
ball_ys,
ball_zs,
],
dim=1,
)
ball_feats = self.model[enc_dec]["ball_mlp"](ball_pos) * math.sqrt(
self.d_model
)
# Combine players and ball features.
combined = torch.cat([player_feats, ball_feats], dim=0).unsqueeze(1)
if enc_dec == "enc":
output = self.model[enc_dec]["transformer"](combined)
else:
output = self.model[enc_dec]["transformer"](combined, output, self.mask)
output = self.player_classifier(output).squeeze(1)
return output