Created
April 23, 2020 12:45
-
-
Save khanhnamle1994/3e8542ca0eb6a1b4a9e5e3d7d9be56da to your computer and use it in GitHub Desktop.
Neural Factorization Machine model
This file contains hidden or 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
| import torch | |
| from layer import FactorizationMachine, FeaturesEmbedding, MultiLayerPerceptron, FeaturesLinear | |
| class NeuralFactorizationMachineModel(torch.nn.Module): | |
| """ | |
| A Pytorch implementation of Neural Factorization Machine. | |
| Reference: | |
| X He and TS Chua, Neural Factorization Machines for Sparse Predictive Analytics, 2017. | |
| """ | |
| def __init__(self, field_dims, embed_dim, mlp_dims, dropouts): | |
| super().__init__() | |
| self.embedding = FeaturesEmbedding(field_dims, embed_dim) | |
| self.linear = FeaturesLinear(field_dims) | |
| self.fm = torch.nn.Sequential( | |
| FactorizationMachine(reduce_sum=False), | |
| torch.nn.BatchNorm1d(embed_dim), | |
| torch.nn.Dropout(dropouts[0]) | |
| ) | |
| self.mlp = MultiLayerPerceptron(embed_dim, mlp_dims, dropouts[1]) | |
| def forward(self, x): | |
| """ | |
| :param x: Long tensor of size ``(batch_size, num_fields)`` | |
| """ | |
| cross_term = self.fm(self.embedding(x)) | |
| x = self.linear(x) + self.mlp(cross_term) | |
| return torch.sigmoid(x.squeeze(1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment