Skip to content

Instantly share code, notes, and snippets.

@khanhnamle1994
Created April 23, 2020 12:45
Show Gist options
  • Select an option

  • Save khanhnamle1994/3e8542ca0eb6a1b4a9e5e3d7d9be56da to your computer and use it in GitHub Desktop.

Select an option

Save khanhnamle1994/3e8542ca0eb6a1b4a9e5e3d7d9be56da to your computer and use it in GitHub Desktop.
Neural Factorization Machine model
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