-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
940967a
commit 789bb88
Showing
6 changed files
with
235 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# ========================================================================= | ||
# Copyright (C) 2024. The FuxiCTR Library. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ========================================================================= | ||
|
||
import torch | ||
from torch import nn | ||
from fuxictr.pytorch.models import BaseModel | ||
from fuxictr.pytorch.layers import FeatureEmbedding | ||
|
||
|
||
class ECN(BaseModel): | ||
def __init__(self, | ||
feature_map, | ||
model_id="ECN", | ||
gpu=-1, | ||
learning_rate=1e-3, | ||
embedding_dim=16, | ||
num_cross_layers=3, | ||
net_dropout=0, | ||
layer_norm=True, | ||
batch_norm=False, | ||
num_heads=1, | ||
embedding_regularizer=None, | ||
net_regularizer=None, | ||
**kwargs): | ||
super(ECN, self).__init__(feature_map, | ||
model_id=model_id, | ||
gpu=gpu, | ||
embedding_regularizer=embedding_regularizer, | ||
net_regularizer=net_regularizer, | ||
**kwargs) | ||
self.embedding_layer = MultiHeadFeatureEmbedding(feature_map, embedding_dim * num_heads, num_heads) | ||
input_dim = feature_map.sum_emb_out_dim() | ||
self.ECN = ExponentialCrossNetwork(input_dim=input_dim, | ||
num_cross_layers=num_cross_layers, | ||
net_dropout=net_dropout, | ||
layer_norm=layer_norm, | ||
batch_norm=batch_norm, | ||
num_heads=num_heads) | ||
self.compile(kwargs["optimizer"], kwargs["loss"], learning_rate) | ||
self.reset_parameters() | ||
self.model_to_device() | ||
|
||
def forward(self, inputs): | ||
X = self.get_inputs(inputs) | ||
feature_emb = self.embedding_layer(X) # B × H × FD/H | ||
y_pred = self.ECN(feature_emb).mean(dim=1) | ||
y_pred = self.output_activation(y_pred) | ||
return_dict = {"y_pred": y_pred} | ||
return return_dict | ||
|
||
|
||
class MultiHeadFeatureEmbedding(nn.Module): | ||
def __init__(self, feature_map, embedding_dim, num_heads=2): | ||
super(MultiHeadFeatureEmbedding, self).__init__() | ||
self.num_heads = num_heads | ||
self.embedding_layer = FeatureEmbedding(feature_map, embedding_dim) | ||
|
||
def forward(self, X): # H = num_heads | ||
feature_emb = self.embedding_layer(X) # B × F × D | ||
multihead_feature_emb = torch.tensor_split(feature_emb, self.num_heads, dim=-1) | ||
multihead_feature_emb = torch.stack(multihead_feature_emb, dim=1) # B × H × F × D/H | ||
multihead_feature_emb1, multihead_feature_emb2 = torch.tensor_split(multihead_feature_emb, 2, | ||
dim=-1) # B × H × F × D/2H | ||
multihead_feature_emb1, multihead_feature_emb2 = multihead_feature_emb1.flatten(start_dim=2), \ | ||
multihead_feature_emb2.flatten( | ||
start_dim=2) # B × H × FD/2H; B × H × FD/2H | ||
multihead_feature_emb = torch.cat([multihead_feature_emb1, multihead_feature_emb2], dim=-1) | ||
return multihead_feature_emb # B × H × FD/H | ||
|
||
|
||
class ExponentialCrossNetwork(nn.Module): | ||
def __init__(self, | ||
input_dim, | ||
num_cross_layers=3, | ||
layer_norm=True, | ||
batch_norm=True, | ||
net_dropout=0.1, | ||
num_heads=1): | ||
super(ExponentialCrossNetwork, self).__init__() | ||
self.num_cross_layers = num_cross_layers | ||
self.layer_norm = nn.ModuleList() | ||
self.batch_norm = nn.ModuleList() | ||
self.dropout = nn.ModuleList() | ||
self.w = nn.ModuleList() | ||
self.b = nn.ParameterList() | ||
for i in range(num_cross_layers): | ||
self.w.append(nn.Linear(input_dim, input_dim // 2, bias=False)) | ||
self.b.append(nn.Parameter(torch.empty((input_dim,)))) | ||
if layer_norm: | ||
self.layer_norm.append(nn.LayerNorm(input_dim // 2)) | ||
if batch_norm: | ||
self.batch_norm.append(nn.BatchNorm1d(num_heads)) | ||
if net_dropout > 0: | ||
self.dropout.append(nn.Dropout(net_dropout)) | ||
nn.init.uniform_(self.b[i].data) | ||
self.masker = nn.ReLU() | ||
self.fc = nn.Linear(input_dim, 1) | ||
|
||
def forward(self, x): | ||
for i in range(self.num_cross_layers): | ||
H = self.w[i](x) | ||
if len(self.batch_norm) > i: | ||
H = self.batch_norm[i](H) | ||
if len(self.layer_norm) > i: | ||
norm_H = self.layer_norm[i](H) | ||
mask = self.masker(norm_H) | ||
else: | ||
mask = self.masker(H) | ||
H = torch.cat([H, H * mask], dim=-1) | ||
x = x * (H + self.b[i]) + x | ||
if len(self.dropout) > i: | ||
x = self.dropout[i](x) | ||
logit = self.fc(x) | ||
return logit |
Oops, something went wrong.