-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentralityencoding.py
More file actions
37 lines (32 loc) · 1.23 KB
/
Copy pathcentralityencoding.py
File metadata and controls
37 lines (32 loc) · 1.23 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
import torch.nn as nn
import torch
class CentralityEncoder(nn.Module):
"""
Centrality Encoder for encoding node centrality features.
Args:
cfg (object): Configuration object containing model parameters.
"""
def __init__(self, cfg):
super().__init__()
self.in_degree_embedding_table = nn.Embedding(
cfg.max_degrees + 1, cfg.d_model, padding_idx=0, device=cfg.device,
)
self.out_degree_embedding_table = nn.Embedding(
cfg.max_degrees + 1, cfg.d_model, padding_idx=0, device=cfg.device,
)
self.cfg = cfg
def forward(self, in_degrees, out_degrees):
"""
Forward pass for the centrality encoder.
Args:
in_degrees (Tensor): In-degree tensor.
out_degrees (Tensor): Out-degree tensor.
Returns:
Tensor: Centrality encoding tensor.
"""
in_degrees = torch.clamp(in_degrees, min=0, max=self.cfg.max_degrees)
out_degrees = torch.clamp(out_degrees, min=0, max=self.cfg.max_degrees)
z_in_degree = self.in_degree_embedding_table(in_degrees)
z_out_degree = self.out_degree_embedding_table(out_degrees)
z = z_in_degree + z_out_degree
return z