NFM推荐算法pytorch实现
时间: 2023-10-23 22:16:06 浏览: 134
以下是基于PyTorch实现的NFM推荐算法代码,供参考:
```
import torch
import torch.nn as nn
import torch.nn.functional as F
class NFM(nn.Module):
def __init__(self, feature_dim, embedding_dim, hidden_dim, output_dim, dropout_prob):
super().__init__()
self.feature_dim = feature_dim
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.dropout_prob = dropout_prob
self.embedding = nn.Embedding(feature_dim, embedding_dim)
self.linear1 = nn.Linear(embedding_dim, hidden_dim)
self.linear2 = nn.Linear(hidden_dim, output_dim)
self.linear3 = nn.Linear(embedding_dim, output_dim)
self.dropout = nn.Dropout(dropout_prob)
def forward(self, x):
# Embedding layer
x_emb = self.embedding(x)
# Bi-Interaction layer
sum_square = torch.sum(x_emb, dim=1) ** 2
square_sum = torch.sum(x_emb ** 2, dim=1)
bi_interaction = 0.5 * (sum_square - square_sum)
# MLP layer
mlp_output = F.relu(self.linear1(self.dropout(bi_interaction)))
mlp_output = self.linear2(self.dropout(mlp_output))
# Output layer
output = self.linear3(x_emb.mean(dim=1)) + mlp_output
return output
```
其中,`feature_dim`为特征维度,`embedding_dim`为嵌入维度,`hidden_dim`为隐层维度,`output_dim`为输出维度,`dropout_prob`为dropout概率。
在模型的前向传播过程中,首先通过`nn.Embedding`层将输入的离散特征转化为嵌入向量,然后通过Bi-Interaction Layer计算二阶交叉特征,再通过MLP Layer进行非线性变换,最后将MLP输出和原始嵌入向量的平均值相加得到最终输出。
使用时,可以像以下代码一样初始化并训练模型:
```
# 初始化模型
model = NFM(feature_dim, embedding_dim, hidden_dim, output_dim, dropout_prob)
# 定义损失函数和优化器
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
for inputs, targets in dataloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
```
其中,`feature_dim`、`embedding_dim`、`hidden_dim`、`output_dim`、`dropout_prob`、`learning_rate`、`num_epochs`等参数需要根据具体情况进行设置,`dataloader`是一个PyTorch的`DataLoader`对象,用于加载训练数据。
阅读全文