【推荐算法 学习与复现】-- 深度学习系列 -- DeepFM

 整体内容和前面相似,FM部分二阶交叉部分数学原理参考如下:

简单理解FM公式 - 知乎 (zhihu.com)icon-default.png?t=M3K6https://zhuanlan.zhihu.com/p/354994307

class FM(nn.Module):
    def __init__(self, num_embeddings, embedding_dim):
        super().__init__()
        self.embedding_dim = embedding_dim

        self.w0 = nn.Parameter(torch.zeros([1,]))
        self.w1 = nn.Parameter(torch.rand([num_embeddings, 1]))
        self.w2 = nn.Parameter(torch.rand([num_embeddings, embedding_dim]))
    
    def forward(self, x):
        first_order = torch.mm(x, self.w1)
        second_order = 0.5 * torch.sum(
            torch.pow(torch.mm(x, self.w2), 2) - torch.mm(torch.pow(x, 2), torch.pow(self.w2, 2)),
            dim=1,
            keepdim=True
        )

        return self.w0 + first_order + second_order
        

class DNN(nn.Module):
    def __init__(self, hidden_units):
        super().__init__()
        self.layers = nn.ModuleList([
            nn.Linear(in_features, out_features, bias=True) for in_features, out_features in zip(hidden_units[:-1], hidden_units[1:])
        ])

    def forward(self, x):
        for layer in self.layers:
            x = F.relu(layer(x))
        return x


class DeepFM(nn.Module):
    def __init__(self, features_info, hidden_units, embedding_dim):
        super().__init__()

        # 解析特征信息
        self.dense_features, self.sparse_features, self.sparse_features_nunique = features_info

        # 解析拿到所有 数值型 和 稀疏型特征信息
        self.__dense_features_nums = len(self.dense_features)
        self.__sparse_features_nums = len(self.sparse_features)

        # embedding 
        self.embeddings = nn.ModuleDict({
            "embed_" + key : nn.Embedding(num_embeds, embedding_dim)
                for key, num_embeds in self.sparse_features_nunique.items()
        })

        stack_dim = self.__dense_features_nums + self.__sparse_features_nums * embedding_dim
        hidden_units.insert(0, stack_dim)

        self.fm = FM(stack_dim, embedding_dim)

        self.dnn = DNN(hidden_units)

        self.dnn_last_linear = nn.Linear(hidden_units[-1], 1, bias=False)

    def forward(self, x):

        # 从输入x中单独拿出 sparse_input 和 dense_input 
        dense_inputs, sparse_inputs = x[:, :self.__dense_features_nums], x[:, self.__dense_features_nums:]
        sparse_inputs = sparse_inputs.long()

        embedding_feas = [self.embeddings["embed_" + key](sparse_inputs[:, idx]) for idx, key in enumerate(self.sparse_features)]
        embedding_feas = torch.cat(embedding_feas, dim=-1)

        input_feas = torch.cat([embedding_feas, dense_inputs], dim=-1)

        fm = self.fm(input_feas)
        dnn = self.dnn_last_linear(self.dnn(input_feas))

        return F.sigmoid(fm + dnn)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值