Coverage for hopwise/model/context_aware_recommender/eulernet.py: 98%
90 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-30 13:25 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-09-30 13:25 +0000
1# @Time : 2023/4/21 12:00
2# @Author : Zhen Tian
3# @Email : chenyuwuxinn@gmail.com
4# @File : eulernet.py
6r"""EulerNet
7################################################
8Reference:
9 Zhen Tian et al. "EulerNet: Adaptive Feature Interaction Learning via Euler's Formula for CTR Prediction." in SIGIR 2023.
11Reference code:
12 https://github.com/chenyuwuxin/EulerNet
14""" # noqa: E501
16import torch
17from torch import nn
18from torch.nn.init import constant_, xavier_normal_
20from hopwise.model.abstract_recommender import ContextRecommender
21from hopwise.model.loss import RegLoss
24class EulerNet(ContextRecommender):
25 r"""EulerNet is a context-based recommendation model.
26 It can adaptively learn the arbitrary-order feature interactions in a complex vector space
27 by conducting space mapping according to Euler's formula. Meanwhile, it can jointly capture
28 the explicit and implicit feature interactions in a unified model architecture.
29 """
31 def __init__(self, config, dataset):
32 super().__init__(config, dataset)
33 field_num = self.field_num = self.num_feature_field
34 shape_list = [config.embedding_size * field_num] + [
35 num_neurons * config.embedding_size for num_neurons in config.order_list
36 ]
38 interaction_shapes = []
39 for inshape, outshape in zip(shape_list[:-1], shape_list[1:]):
40 interaction_shapes.append(EulerInteractionLayer(config, inshape, outshape))
42 self.Euler_interaction_layers = nn.Sequential(*interaction_shapes)
43 self.mu = nn.Parameter(torch.ones(1, field_num, 1))
44 self.reg = nn.Linear(shape_list[-1], 1)
45 self.reg_weight = config.reg_weight
46 nn.init.normal_(self.reg.weight, mean=0, std=0.01)
47 self.sigmoid = nn.Sigmoid()
48 self.reg_loss = RegLoss()
49 self.loss = nn.BCEWithLogitsLoss()
50 self.apply(self._init_other_weights)
52 def _init_other_weights(self, module):
53 if isinstance(module, nn.Embedding):
54 xavier_normal_(module.weight.data)
55 elif isinstance(module, nn.Linear):
56 if module.bias is not None:
57 constant_(module.bias.data, 0)
59 def forward(self, interaction):
60 fm_all_embeddings = self.concat_embed_input_fields(interaction) # [batch_size, num_field, embed_dim]
61 r, p = (
62 self.mu * torch.cos(fm_all_embeddings),
63 self.mu * torch.sin(fm_all_embeddings),
64 )
65 o_r, o_p = self.Euler_interaction_layers((r, p))
66 o_r, o_p = o_r.reshape(o_r.shape[0], -1), o_p.reshape(o_p.shape[0], -1)
67 re, im = self.reg(o_r), self.reg(o_p)
68 logits = re + im
69 return logits.squeeze(-1)
71 def calculate_loss(self, interaction):
72 label = interaction[self.LABEL]
73 output = self.forward(interaction)
74 return self.loss(output, label) + self.RegularLoss(self.reg_weight)
76 def predict(self, interaction):
77 return self.sigmoid(self.forward(interaction))
79 def RegularLoss(self, weight):
80 if weight == 0:
81 return 0
82 loss = 0
83 for _ in ["Euler_interaction_layers", "mu", "reg"]:
84 comp = getattr(self, _)
85 if isinstance(comp, nn.Parameter):
86 loss += torch.norm(comp, p=2)
87 continue
88 for params in comp.parameters():
89 loss += torch.norm(params, p=2)
90 return loss * weight
93class EulerInteractionLayer(nn.Module):
94 r"""Euler interaction layer is the core component of EulerNet,
95 which enables the adaptive learning of explicit feature interactions. An Euler
96 interaction layer performs the feature interaction under the complex space one time,
97 taking as input a complex representation and outputting a transformed complex representation.
98 """
100 def __init__(self, config, inshape, outshape):
101 super().__init__()
102 self.feature_dim = config.embedding_size
103 self.apply_norm = config.apply_norm
105 init_orders = torch.softmax(
106 torch.randn(inshape // self.feature_dim, outshape // self.feature_dim) / 0.01,
107 dim=0,
108 )
109 self.inter_orders = nn.Parameter(init_orders)
110 self.im = nn.Linear(inshape, outshape)
112 self.bias_lam = nn.Parameter(torch.randn(1, self.feature_dim, outshape // self.feature_dim) * 0.01)
113 self.bias_theta = nn.Parameter(torch.randn(1, self.feature_dim, outshape // self.feature_dim) * 0.01)
114 nn.init.normal_(self.im.weight, mean=0, std=0.1)
116 self.drop_ex = nn.Dropout(p=config.drop_ex)
117 self.drop_im = nn.Dropout(p=config.drop_im)
118 self.norm_r = nn.LayerNorm([self.feature_dim])
119 self.norm_p = nn.LayerNorm([self.feature_dim])
121 def forward(self, complex_features):
122 r, p = complex_features
124 lam = r**2 + p**2 + 1e-8
125 theta = torch.atan2(p, r)
126 lam, theta = (
127 lam.reshape(lam.shape[0], -1, self.feature_dim),
128 theta.reshape(theta.shape[0], -1, self.feature_dim),
129 )
130 r, p = self.drop_im(r), self.drop_im(p)
132 lam = 0.5 * torch.log(lam)
133 lam, theta = torch.transpose(lam, -2, -1), torch.transpose(theta, -2, -1)
134 lam, theta = self.drop_ex(lam), self.drop_ex(theta)
135 lam, theta = (
136 lam @ (self.inter_orders) + self.bias_lam,
137 theta @ (self.inter_orders) + self.bias_theta,
138 )
139 lam = torch.exp(lam)
140 lam, theta = torch.transpose(lam, -2, -1), torch.transpose(theta, -2, -1)
142 r, p = r.reshape(r.shape[0], -1), p.reshape(p.shape[0], -1)
143 r, p = self.im(r), self.im(p)
144 r, p = torch.relu(r), torch.relu(p)
145 r, p = (
146 r.reshape(r.shape[0], -1, self.feature_dim),
147 p.reshape(p.shape[0], -1, self.feature_dim),
148 )
150 o_r, o_p = r + lam * torch.cos(theta), p + lam * torch.sin(theta)
151 o_r, o_p = (
152 o_r.reshape(o_r.shape[0], -1, self.feature_dim),
153 o_p.reshape(o_p.shape[0], -1, self.feature_dim),
154 )
155 if self.apply_norm:
156 o_r, o_p = self.norm_r(o_r), self.norm_p(o_p)
157 return o_r, o_p