-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
390 lines (344 loc) · 16.1 KB
/
main.py
File metadata and controls
390 lines (344 loc) · 16.1 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
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
import math
import random
import time
import numpy as np
import torch
from torch import nn
import torch.optim as optim
# Gradient Clipping Utility
class GradientClipping:
def __init__(self, clip_value):
self.epoch_grads = []
self.total_grads = []
self.clip = clip_value
def track_grads(self, module, grad_input, grad_output):
# Track norm of the first gradient in the tuple (if available)
if grad_input[0] is not None:
grad_norm = grad_input[0].detach().norm().item()
self.epoch_grads.append(grad_norm)
def register_hook(self, module):
# Use full backward hook (available in recent PyTorch versions)
module.register_full_backward_hook(self.track_grads)
def gradient_mean(self):
return np.mean(self.epoch_grads) if self.epoch_grads else 0.0
def gradient_std(self):
return np.std(self.epoch_grads) if self.epoch_grads else 0.0
def reset_gradients(self):
self.total_grads.extend(self.epoch_grads)
self.epoch_grads = []
def update_clip_value(self):
self.clip = self.gradient_mean() + self.gradient_std()
def update_clip_value_total(self):
all_grads = self.total_grads + self.epoch_grads
self.clip = np.mean(all_grads) if all_grads else self.clip
# Transformer Components and Layers
class FF(nn.Module):
"""Point-wise Feed-Forward Network used in transformer layers."""
def __init__(self, input_size, hidden_size, dropout=0.1):
super().__init__()
self.linear1 = nn.Linear(input_size, hidden_size)
self.linear2 = nn.Linear(hidden_size, input_size)
self.activation = nn.LeakyReLU()
self.dropout = nn.Dropout(dropout)
def forward(self, x):
return self.linear2(self.dropout(self.activation(self.linear1(x))))
class MultiHeadAttention(nn.Module):
"""Multi-head self-attention mechanism."""
def __init__(self, d_model, n_heads, dropout=0.1):
super().__init__()
assert d_model % n_heads == 0, "d_model must be divisible by n_heads"
self.n_heads = n_heads
self.d_head = d_model // n_heads
self.W_q = nn.Linear(d_model, d_model, bias=False)
self.W_k = nn.Linear(d_model, d_model, bias=False)
self.W_v = nn.Linear(d_model, d_model, bias=False)
self.fc = nn.Linear(d_model, d_model, bias=False)
self.dropout = nn.Dropout(dropout)
self.scale = math.sqrt(self.d_head)
def forward(self, q, k, v, mask=None):
batch_size = q.size(0)
# Linear projections
Q = self.W_q(q)
K = self.W_k(k)
V = self.W_v(v)
# Split into heads: shape -> (batch_size, n_heads, seq_len, d_head)
Q = Q.view(batch_size, -1, self.n_heads, self.d_head).transpose(1, 2)
K = K.view(batch_size, -1, self.n_heads, self.d_head).transpose(1, 2)
V = V.view(batch_size, -1, self.n_heads, self.d_head).transpose(1, 2)
# Scaled dot-product attention
scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale
if mask is not None:
# Expand mask for all heads and set masked positions to a very negative value
scores = scores.masked_fill(mask.unsqueeze(1) == 0, -1e9)
attn = torch.softmax(scores, dim=-1)
attn = self.dropout(attn)
context = torch.matmul(attn, V)
# Concatenate heads
context = context.transpose(1, 2).contiguous().view(batch_size, -1, self.n_heads * self.d_head)
out = self.fc(context)
return out
class EncoderCell(nn.Module):
"""A single transformer encoder cell with self-attention and feed-forward network."""
def __init__(self, d_model, hidden_size, n_heads, dropout=0.1):
super().__init__()
self.self_attn = MultiHeadAttention(d_model, n_heads, dropout)
self.lnorm1 = nn.LayerNorm(d_model)
self.ff = FF(d_model, hidden_size, dropout)
self.lnorm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x, mask=None):
# Self-attention sub-layer with residual connection and layer normalization
attn_out = self.self_attn(x, x, x, mask)
x = self.lnorm1(x + self.dropout(attn_out))
# Feed-forward sub-layer with residual connection and layer normalization
ff_out = self.ff(x)
x = self.lnorm2(x + self.dropout(ff_out))
return x
class Encoder(nn.Module):
"""Stack of transformer encoder cells."""
def __init__(self, d_model, hidden_size, n_layers, n_heads, dropout=0.1):
super().__init__()
self.layers = nn.ModuleList([
EncoderCell(d_model, hidden_size, n_heads, dropout) for _ in range(n_layers)
])
def forward(self, x, mask=None):
for layer in self.layers:
x = layer(x, mask)
return x
# BSTransformer Model
class BSTransformer(nn.Module):
"""
Behavior Sequence Transformer model that:
- Embeds user behavior sequences and other features
- Uses a transformer encoder to capture sequential relations
- Concatenates aggregated transformer output with context embeddings
- Applies an MLP to output logits for all items (for CTR prediction)
"""
def __init__(self, config):
super().__init__()
self.config = config
# Embedding layer for sequence items (e.g. item_id)
self.item_embed = nn.Embedding(
num_embeddings=config['item_embed']['num_embeddings'],
embedding_dim=config['item_embed']['embedding_dim'],
padding_idx=config['item_embed']['padding_idx']
)
# Positional embedding (using sinusoidal encoding here)
if config.get('use_sinusoidal_pos', True):
pos_emb = self.sinusoidal_pos_embedding(
config['max_seq_len'], config['item_embed']['embedding_dim']
)
# Register as a buffer so it’s part of the model but not updated
self.register_buffer('pos_embedding_buffer', pos_emb)
else:
self.pos_embed = nn.Embedding(config['max_seq_len'], config['item_embed']['embedding_dim'])
# Embeddings for "other" features (e.g. user profile, context)
self.context_embeddings = nn.ModuleList([
nn.Embedding(
num_embeddings=feat['num_embeddings'],
embedding_dim=feat['embedding_dim'],
padding_idx=feat['padding_idx']
) for feat in config['context_features']
])
# Transformer encoder (self-attention layer)
self.encoder = Encoder(
d_model=config['trans']['input_size'],
hidden_size=config['trans']['hidden_size'],
n_layers=config['trans']['n_layers'],
n_heads=config['trans']['n_heads'],
dropout=config.get('dropout', 0.1)
)
# MLP layers: concatenates the transformer output with context embeddings,
# then applies several fully-connected layers.
mlp_input_size = config['trans']['input_size'] + sum(
[feat['embedding_dim'] for feat in config['context_features']]
)
self.mlp = nn.Sequential(
nn.Linear(mlp_input_size, 1024),
nn.LeakyReLU(),
nn.Dropout(config.get('dropout', 0.1)),
nn.Linear(1024, 512),
nn.LeakyReLU(),
nn.Dropout(config.get('dropout', 0.1)),
nn.Linear(512, config['item_embed']['num_embeddings'])
)
# Parameter initialization
for param in self.parameters():
if param.dim() > 1:
if config.get('init_method', 'xavier') == 'xavier':
nn.init.xavier_uniform_(param)
elif config.get('init_method') == 'kaiming':
nn.init.kaiming_uniform_(param)
print(f"Parameters initialized using {config.get('init_method', 'xavier')} initialization!")
def sinusoidal_pos_embedding(self, max_seq_len, embedding_dim):
"""Creates a sinusoidal positional embedding matrix."""
pe = torch.zeros(max_seq_len, embedding_dim)
position = torch.arange(0, max_seq_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, embedding_dim, 2).float() * (-math.log(10000.0) / embedding_dim))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
return pe # shape: (max_seq_len, embedding_dim)
def forward(self, x, context):
"""
Args:
x: LongTensor of shape (batch_size, seq_len) – the user behavior sequence (item IDs).
context: List of LongTensors, each of shape (batch_size,) – additional context features.
Returns:
logits: Tensor of shape (batch_size, num_items), the unnormalized scores.
targets: Tensor of shape (batch_size,) assumed to be the target (last item in sequence).
"""
batch_size, seq_len = x.size()
# Get item embeddings and scale by sqrt(embedding_dim)
item_emb = self.item_embed(x) * math.sqrt(self.config['item_embed']['embedding_dim'])
# Add positional encoding
if self.config.get('use_sinusoidal_pos', True):
pos_emb = self.pos_embedding_buffer[:seq_len, :].unsqueeze(0).expand(batch_size, -1, -1)
item_emb = item_emb + pos_emb
else:
positions = torch.arange(seq_len, device=x.device).unsqueeze(0).expand(batch_size, -1)
pos_emb = self.pos_embed(positions)
item_emb = item_emb + pos_emb
# Create mask (assumes padding index is 0)
mask = (x != self.item_embed.padding_idx) # shape: (batch_size, seq_len)
# Pass through transformer encoder
enc_out = self.encoder(item_emb, mask=mask)
# Aggregate encoder outputs (mean pooling over valid positions)
mask_unsq = mask.unsqueeze(-1).float()
sum_enc = torch.sum(enc_out * mask_unsq, dim=1)
len_enc = torch.clamp(mask_unsq.sum(dim=1), min=1e-9)
agg_encoding = sum_enc / len_enc # shape: (batch_size, d_model)
# Process context features: embed each and concatenate
context_embs = []
for emb, feat in zip(self.context_embeddings, context):
feat_tensor = feat.long().to(x.device)
context_embs.append(emb(feat_tensor))
context_cat = torch.cat(context_embs, dim=1) if context_embs else torch.empty(batch_size, 0, device=x.device)
# Concatenate aggregated transformer output with context embeddings
mlp_input = torch.cat([agg_encoding, context_cat], dim=1)
logits = self.mlp(mlp_input)
# Here we assume that the target item is the last item in the sequence
targets = x[:, -1]
return logits, targets
# Trainer and Data Preparation
class Trainer:
def __init__(self, model, config, device):
self.model = model.to(device)
self.device = device
self.config = config
self.loss_fn = nn.CrossEntropyLoss()
self.optimizer = optim.AdamW(self.model.parameters(), lr=config['lr'])
self.grad_clipping = config.get('grad_clipping', False)
if self.grad_clipping:
self.clipper = GradientClipping(config['clip_value'])
# Register hook on all modules that have a weight parameter
self.model.apply(lambda module: self.clipper.register_hook(module) if hasattr(module, 'weight') else None)
self.scheduler = None
def set_lr_scheduler(self, milestones, gamma, last_epoch=-1):
self.scheduler = optim.lr_scheduler.MultiStepLR(
self.optimizer, milestones=milestones, gamma=gamma, last_epoch=last_epoch
)
def train_epoch(self, train_loader):
self.model.train()
total_loss = 0.0
num_batches = 0
start_time = time.time()
for x_batch, context_batch in train_loader:
x_batch = x_batch.to(self.device)
context_batch = [c.to(self.device) for c in context_batch]
logits, targets = self.model(x_batch, context_batch)
loss = self.loss_fn(logits, targets)
self.optimizer.zero_grad()
loss.backward()
if self.grad_clipping:
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.clipper.clip)
self.optimizer.step()
if self.scheduler:
self.scheduler.step()
total_loss += loss.item()
num_batches += 1
avg_loss = total_loss / num_batches if num_batches > 0 else 0.0
elapsed = time.time() - start_time
print(f"Training loss: {avg_loss:.4f}, Time: {elapsed:.2f}s")
return avg_loss
def pad_sequence(seq, max_seq_len, pad_value=0):
"""Pads (or truncates) a sequence to a fixed maximum length."""
seq = list(seq)
if len(seq) >= max_seq_len:
return seq[-max_seq_len:]
else:
return [pad_value] * (max_seq_len - len(seq)) + seq
def batch_fn(user_sequences, context_features, batch_size, max_seq_len, shuffle=True):
"""
Prepares batches from user behavior sequences and corresponding context features.
Args:
user_sequences: list of lists (each inner list is a sequence of item IDs)
context_features: list of lists, each corresponding to one additional feature
batch_size: number of samples per batch
max_seq_len: fixed sequence length for the transformer input
shuffle: whether to shuffle the data before batching
Yields:
A tuple (x_batch, context_batch) where:
- x_batch is a LongTensor of shape (batch_size, max_seq_len)
- context_batch is a list of LongTensors (one per feature) of shape (batch_size,)
"""
data = list(zip(user_sequences, *context_features))
if shuffle:
random.shuffle(data)
for i in range(0, len(data), batch_size):
batch = data[i:i+batch_size]
if len(batch) < batch_size:
continue # skip incomplete batch for simplicity
seqs = [pad_sequence(item[0], max_seq_len) for item in batch]
# For each context feature (starting from index 1)
context_batches = []
num_context = len(batch[0]) - 1
for j in range(num_context):
feat_batch = [item[j+1] for item in batch]
context_batches.append(torch.LongTensor(feat_batch))
yield torch.LongTensor(seqs), context_batches
# main
if __name__ == "__main__":
config = {
'item_embed': {
'num_embeddings': 10000, # total number of items in the catalog
'embedding_dim': 64,
'padding_idx': 0
},
'context_features': [
# Example: user age (vocabulary size 100, embedding dimension 8)
{'num_embeddings': 100, 'embedding_dim': 8, 'padding_idx': 0},
# Example: user gender (vocabulary size 3, embedding dimension 4)
{'num_embeddings': 3, 'embedding_dim': 4, 'padding_idx': 0}
],
'max_seq_len': 20,
'trans': {
'input_size': 64, # should match item_embed.embedding_dim
'hidden_size': 128,
'n_layers': 1,
'n_heads': 8
},
'lr': 0.001,
'clip_value': 1.0,
'grad_clipping': True,
'init_method': 'xavier',
'use_sinusoidal_pos': True,
'dropout': 0.1
}
# Instantiate model and trainer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = BSTransformer(config)
trainer = Trainer(model, config, device)
num_samples = 1000
user_sequences = []
context_age = []
context_gender = []
for _ in range(num_samples):
seq_length = random.randint(5, config['max_seq_len'])
# Generate a sequence of item IDs (non-zero values)
seq = [random.randint(1, config['item_embed']['num_embeddings'] - 1) for _ in range(seq_length)]
user_sequences.append(seq)
context_age.append(random.randint(1, 99))
context_gender.append(random.randint(0, 2))
batch_size = 32
train_loader = list(batch_fn(user_sequences, [context_age, context_gender], batch_size, config['max_seq_len']))
trainer.train_epoch(train_loader)