60分钟吃掉三杀模型FiBiNET

ctr特征重要性建模:FiBiNet&FiBiNet++模型 **FiBiNET**(Feature Importance and Bilinear feature Interaction NETwork)为推荐系统的CTR模型提出了一些创新方向: - **引入一个SENet模块,可以动态学习特征的重要性;** - **引入一个双线性模块(Bilinear-Interaction layer),来改进特征交互方式。** 阅读详情

神经网络的结构设计有3个主流的高级技巧:

  • 1,高低融合 (将高层次特征与低层次特征融合,提升特征维度的丰富性和多样性,像人一样同时考虑整体和细节)

  • 2,权值共享 (一个权值矩阵参与多个不同的计算,降低参数规模并同时缓解样本稀疏性,像人一样一条知识多处运用)

  • 3,动态适应 (不同的输入样本使用不同的权值矩阵,动态地进行特征选择并赋予特征重要度解释性,像人一样聚焦重要信息排除干扰信息)

技巧应用范例:

  • 1,高低融合 (DeepWide,UNet,特征金字塔FPN...)

  • 2,权值共享 (CNN,RNN,FM,DeepFM,BlinearFFM...)

  • 3,动态适应 (各种Attention机制...)

新浪微博广告推荐技术团队2019年发布的CTR预估模型FiBiNET同时巧妙地运用了以上3种技巧,是神经网络结构设计的教科书级的范例。

在此介绍给大家。

参考资料:

  • FiBiNET论文:https://arxiv.org/pdf/1905.09433.pdf

  • FiBiNET-结合特征重要性和双线性特征交互进行CTR预估:https://zhuanlan.zhihu.com/p/72931811

  • 代码实现:https://github.com/xue-pai/FuxiCTR/blob/main/fuxictr/pytorch/models/FiBiNET.py

  • SENet原理:https://zhuanlan.zhihu.com/p/65459972

公众号后台回复关键词: FiBiNET 获取本文全部源码和数据集下载链接。

本文目录:

一,FiBiNET原理解析

二,FiBiNET的pytorch实现

三,Criteo数据集完整范例

1,准备数据

2,定义模型

3,训练模型

4,评估模型

5,使用模型

6,保存模型

一,FiBiNET原理解析

FiBiNET全称为Feature Importance and Bilinear Interaction Network.

顾名思义,其主要的创意有2个。

第一个是Feature Importance,通过借鉴SENet(Squeeze-and-Excitation)Attention机制实现特征选择和重要度解释。

第二个是Bilinear Interaction Network,这是应用权值共享技巧对 FFM(Field-Aware FM)结构进行改进的一种结构。

同时,FiBiNET保留了DeepWide的高低融合的网络架构。

所以它综合使用了 高低融合、权值共享、动态适应 这3种神经网络结构设计的高级技巧。一个不落,Triple kill!

我们重点介绍一下 SENet Attention 和 Bilinear Interaction.

1, SENet Attention

SENet 全称为 Squeeze-and-Excitation Network,是一种通过注意力机制计算特征重要度的网络模块。

最早是在CV领域引入,通过在ResNet结构上添加SENet Attention模块,赢得了ImageNet 2017竞赛分类任务的冠军。

如何计算各个Feature Map(通道)的特征重要度(注意力权重)呢?

SENet的思想非常简洁。

step1: 通过全局池化将各个Feature Map由一个一个的矩阵汇总成一个一个的标量。此即Squeeze操作。

step2:通过一个2层MLP将汇总成得到的一个一个的标量所构成的向量进行变换,得到注意力权重。此即Excitation操作。细节一点地说,这个2层的MLP的第1层将通道数量缩减成原来的1/3, 第2层再将通道数恢复。并且每层后面都接入了激活函数。

step3:用注意力权重乘以原始的Feature Map。这个是Re-Weight操作。

图片示意如下。

ef22ef8bfa31f6139c1960e0fce509c0.png

pytorch代码实现如下,可能比图片更加好懂。

import torch 
from torch import nn 
class SENetAttention(nn.Module):
    """
    Squeeze-and-Excitation Attention
    输入shape: [batch_size, num_fields, d_embed]   #num_fields即num_features
    输出shape: [batch_size, num_fields, d_embed]
    """
    def __init__(self, num_fields, reduction_ratio=3):
        super().__init__()
        reduced_size = max(1, int(num_fields / reduction_ratio))
        self.excitation = nn.Sequential(nn.Linear(num_fields, reduced_size, bias=False),
                                        nn.ReLU(),
                                        nn.Linear(reduced_size, num_fields, bias=False),
                                        nn.ReLU())

    def forward(self, x):
        Z = torch.mean(x, dim=-1, out=None) #1,Sequeeze
        A = self.excitation(Z) #2,Excitation
        V = x * A.unsqueeze(-1) #3,Re-Weight
        return V

2, Bilinear Interaction

Bilinear Interaction实际上是FFM在权值共享思想下的一种改进,也可以称之为Bilinear FFM。

我们先说说FFM(Field-Aware FM),再看看这个Bilinear FFM 怎么改进的。

FM用隐向量之间的点积来计算特征之间的交叉,并且一个特征用一个隐向量来表示。

FFM认为一个特征用一个隐向量来表达太粗糙了,如果这个特征和不同分组(Field)的特征来做交叉,应该用不同的隐向量。

举例来说,考虑一个广告点击预测的场景,广告类别 和 用户所在城市、用户职业之间的交叉。

在FM中 一个确定的广告类别 比如游戏广告 不论是和用户所在城市,还是用户职业交叉,都用同一个隐向量。

但是FFM认为,用户所在城市和用户职业是两类完全不同的特征(不同Field),描述它们的向量空间应该是完全不相关的,FM用一个相同的隐向量来和它们做点积不合理。

所以,FFM引入了Field(域)的概念,和不同Field的特征做交叉,要使用不同的隐向量。

实践表明,FFM这个思路是有效的, FFM的作者阮毓钦正是凭借这个方案赢得了2015年kaggle举办的Criteo比赛的冠军。

但是FFM有个很大的缺点,就是参数量太多了。

对于FM来说,每个特征只有一个隐向量,假设有n个特征,每个隐向量维度为k,全部隐向量参数矩阵的大小 size = n k.

但是对于FFM,有过有f个不同的field,每个特征都将有f-1个隐向量,全部隐向量的参数矩阵的大小增大为 size = (f-1) n k.

通常的应用场景中,Field的数量有几十几百维,而Feature的数量有数万数百万维。

很显然,FFM将隐向量的参数规模扩大了几十几百倍。

FFM的本质思想是在做特征交叉的时候要区分不同的Field,其实现方式是和不同的Field做交叉时用不同的隐向量。

有没有办法保留FFM中区分不同Field的特性,并降低参数规模呢?

BilinearFFM说,我有办法,权重共享走起来!

BilinearFFM不直接针对不同Field设计不同的隐向量,而是引入了Field变换矩阵来区分不同的Field。

每个特征还是一个隐向量,但是和不同的Field的特征做交叉时,先乘上这个特征所在Field的变换矩阵,然后再做后面的点积。

因此,同属一个Field的特征共享一个Field变换矩阵。这种bilinear_type叫做 field_each.

Field变换矩阵的大小是k^2, 这种方式下,全部隐向量的参数大小加上共享变换矩阵的参数大小一共是 size = n k + f k^2

由于k和f远小于n,这种Bilinear方式相比FM增加的参数量可以忽略不计。

除了 同属一个Field的特征共享一个Field变换矩阵外,我们还可以更加简单粗暴一点,所有特征共享一个变换矩阵.

这种bilinear_type叫做 field_all.这种方式下,size = n k + k^2

我们也可以更加精细一点,相同的Field组合之间的交互共享一个变换矩阵,这种bilinear_type叫做field_interaction.

总共有f(f-1)/2种组合,这种方式下, size = n k + k^2 f(f-1)/2

以上就是BilinearFFM的基本思想。

FiBiNET中用到的Bilinear Interaction相比BilinearFFM, 还有一处小改动,将点积改成了哈达玛积,如下图所示。

28aa4d0cf7aa8b53ffefdb36bc7c6c22.png

pytorch代码实现如下,整体不难理解。作2点说明。

1,Field概念说明

在FFM相关的文章中,引入了Field的概念,以和Feature区分,一个Field中可以包括多个Feature.

实际上Field就是我们通常理解的特征,包括数值特征和类别特征,但是Feature是数值特征或者类别特征onehot后的特征。一个类别特征对应一个Field,但是对应多个Feature。

abb1a09bb3f0d98019c4567dbbd31d8f.png

2,combinations函数说明

组合函数combinations从num_fields中任取2种作为组合,共有 num_fields*(num_fields-1)中组合方式。

所以输出的Field数量变成了 num_fields*(num_fields-1)/2。

import torch 
from torch import nn 
from itertools import combinations
class BilinearInteraction(nn.Module):
    """
    双线性FFM
    输入shape: [batch_size, num_fields, d_embed] #num_fields即num_features
    输出shape: [batch_size, num_fields*(num_fields-1)/2, d_embed]
    """
    def __init__(self, num_fields, d_embed, bilinear_type="field_interaction"):
        super().__init__()
        self.bilinear_type = bilinear_type
        if self.bilinear_type == "field_all":
            self.bilinear_layer = nn.Linear(d_embed, d_embed, bias=False)
        elif self.bilinear_type == "field_each":
            self.bilinear_layer = nn.ModuleList([nn.Linear(d_embed, d_embed, bias=False)
                                                 for i in range(num_fields)])
        elif self.bilinear_type == "field_interaction":
            self.bilinear_layer = nn.ModuleList([nn.Linear(d_embed, d_embed, bias=False)
                                                 for i, j in combinations(range(num_fields), 2)])
        else:
            raise NotImplementedError()

    def forward(self, feature_emb):
        feature_emb_list = torch.split(feature_emb, 1, dim=1)
        if self.bilinear_type == "field_all":
            bilinear_list = [self.bilinear_layer(v_i) * v_j
                             for v_i, v_j in combinations(feature_emb_list, 2)]
        elif self.bilinear_type == "field_each":
            bilinear_list = [self.bilinear_layer[i](feature_emb_list[i]) * feature_emb_list[j]
                             for i, j in combinations(range(len(feature_emb_list)), 2)]
        elif self.bilinear_type == "field_interaction":
            bilinear_list = [self.bilinear_layer[i](v[0]) * v[1]
                             for i, v in enumerate(combinations(feature_emb_list, 2))]
        return torch.cat(bilinear_list, dim=1)

二,FiBiNET的pytorch实现

下面是FiBiNET的一个pytorch实现。

核心代码是SENetAttention模块和BilinearInteraction模块的实现。

11cf45c72f43bbc98264e2a16d4f52c1.png
import torch 
from torch import nn 
from itertools import combinations

class NumEmbedding(nn.Module):
    """
    连续特征用linear层编码
    输入shape: [batch_size,num_features, d_in], # d_in 通常是1
    输出shape: [batch_size,num_features, d_out]
    """
    
    def __init__(self, n: int, d_in: int, d_out: int, bias: bool = False) -> None:
        super().__init__()
        self.weight = nn.Parameter(torch.Tensor(n, d_in, d_out))
        self.bias = nn.Parameter(torch.Tensor(n, d_out)) if bias else None
        with torch.no_grad():
            for i in range(n):
                layer = nn.Linear(d_in, d_out)
                self.weight[i] = layer.weight.T
                if self.bias is not None:
                    self.bias[i] = layer.bias

    def forward(self, x_num):
        assert x_num.ndim == 3
        #x = x_num[..., None] * self.weight[None]
        #x = x.sum(-2)
        x = torch.einsum("bfi,fij->bfj",x_num,self.weight)
        if self.bias is not None:
            x = x + self.bias[None]
        return x
    
class CatEmbedding(nn.Module):
    """
    离散特征用Embedding层编码
    输入shape: [batch_size, num_features], 
    输出shape: [batch_size, num_features, d_embed]
    """
    def __init__(self, categories, d_embed):
        super().__init__()
        self.embedding = nn.Embedding(sum(categories), d_embed)
        self.offsets = nn.Parameter(
                torch.tensor([0] + categories[:-1]).cumsum(0),requires_grad=False)
        
        nn.init.xavier_uniform_(self.embedding.weight.data)

    def forward(self, x_cat):
        """
        x_cat: Long tensor of size ``(batch_size, features_num)``
        """
        x = x_cat + self.offsets[None]
        return self.embedding(x) 
    
class CatLinear(nn.Module):
    """
    离散特征用Embedding实现线性层(等价于先F.onehot再nn.Linear())
    输入shape: [batch_size, num_features ], 
    输出shape: [batch_size, d_out]
    """
    def __init__(self, categories, d_out=1):
        super().__init__()
        self.fc = nn.Embedding(sum(categories), d_out)
        self.bias = nn.Parameter(torch.zeros((d_out,)))
        self.offsets = nn.Parameter(
                torch.tensor([0] + categories[:-1]).cumsum(0),requires_grad=False)
        nn.init.xavier_uniform_(self.fc.weight.data)

    def forward(self, x_cat):
        """
        Long tensor of size ``(batch_size, num_features)``
        """
        x = x_cat + self.offsets[None]
        return torch.sum(self.fc(x), dim=1) + self.bias 
    
class SENetAttention(nn.Module):
    """
    Squeeze-and-Excitation Attention
    输入shape: [batch_size, num_fields, d_embed]   #num_fields即num_features
    输出shape: [batch_size, num_fields, d_embed]
    """
    def __init__(self, num_fields, reduction_ratio=3):
        super().__init__()
        reduced_size = max(1, int(num_fields / reduction_ratio))
        self.excitation = nn.Sequential(nn.Linear(num_fields, reduced_size, bias=False),
                                        nn.ReLU(),
                                        nn.Linear(reduced_size, num_fields, bias=False),
                                        nn.ReLU())

    def forward(self, x):
        Z = torch.mean(x, dim=-1, out=None) #1,Sequeeze
        A = self.excitation(Z) #2,Excitation
        V = x * A.unsqueeze(-1) #3,Re-Weight
        return V
    
class BilinearInteraction(nn.Module):
    """
    双线性FFM
    输入shape: [batch_size, num_fields, d_embed] #num_fields即num_features
    输出shape: [batch_size, num_fields*(num_fields-1)/2, d_embed]
    """
    def __init__(self, num_fields, d_embed, bilinear_type="field_interaction"):
        super().__init__()
        self.bilinear_type = bilinear_type
        if self.bilinear_type == "field_all":
            self.bilinear_layer = nn.Linear(d_embed, d_embed, bias=False)
        elif self.bilinear_type == "field_each":
            self.bilinear_layer = nn.ModuleList([nn.Linear(d_embed, d_embed, bias=False)
                                                 for i in range(num_fields)])
        elif self.bilinear_type == "field_interaction":
            self.bilinear_layer = nn.ModuleList([nn.Linear(d_embed, d_embed, bias=False)
                                                 for i, j in combinations(range(num_fields), 2)])
        else:
            raise NotImplementedError()

    def forward(self, feature_emb):
        feature_emb_list = torch.split(feature_emb, 1, dim=1)
        if self.bilinear_type == "field_all":
            bilinear_list = [self.bilinear_layer(v_i) * v_j
                             for v_i, v_j in combinations(feature_emb_list, 2)]
        elif self.bilinear_type == "field_each":
            bilinear_list = [self.bilinear_layer[i](feature_emb_list[i]) * feature_emb_list[j]
                             for i, j in combinations(range(len(feature_emb_list)), 2)]
        elif self.bilinear_type == "field_interaction":
            bilinear_list = [self.bilinear_layer[i](v[0]) * v[1]
                             for i, v in enumerate(combinations(feature_emb_list, 2))]
        return torch.cat(bilinear_list, dim=1)
    

#mlp
class MultiLayerPerceptron(nn.Module):
    def __init__(self, d_in, d_layers, dropout, 
                 d_out = 1):
        super().__init__()
        layers = []
        for d in d_layers:
            layers.append(nn.Linear(d_in, d))
            layers.append(nn.BatchNorm1d(d))
            layers.append(nn.ReLU())
            layers.append(nn.Dropout(p=dropout))
            d_in = d
        layers.append(nn.Linear(d_layers[-1], d_out))
        self.mlp = nn.Sequential(*layers)

    def forward(self, x):
        """
        float tensor of size ``(batch_size, d_in)``
        """
        return self.mlp(x)
    

#fibinet 
class FiBiNET(nn.Module):
    
    def __init__(self,
                 d_numerical, 
                 categories, 
                 d_embed,
                 mlp_layers, 
                 mlp_dropout,
                 reduction_ratio = 3,
                 bilinear_type = "field_interaction",
                 n_classes = 1):
        
        super().__init__()
        
        if d_numerical is None:
            d_numerical = 0
        if categories is None:
            categories = []
            
        self.categories = categories
        self.n_classes = n_classes
        
        self.num_linear = nn.Linear(d_numerical,n_classes) if d_numerical else None
        self.cat_linear = CatLinear(categories,n_classes) if categories else None
        
        self.num_embedding = NumEmbedding(d_numerical,1,d_embed) if d_numerical else None
        self.cat_embedding = CatEmbedding(categories, d_embed) if categories else None
        
        num_fields = d_numerical+len(categories)
        
        self.se_attention = SENetAttention(num_fields, reduction_ratio)
        self.bilinear = BilinearInteraction(num_fields, d_embed, bilinear_type)
        
        mlp_in = num_fields * (num_fields - 1) * d_embed
        self.mlp = MultiLayerPerceptron(
            d_in= mlp_in,
            d_layers = mlp_layers,
            dropout = mlp_dropout,
            d_out = n_classes
        )
        
        
    def forward(self, x):
        """
        x_num: numerical features
        x_cat: category features
        """
        x_num,x_cat = x
        
        #一,wide部分
        x_linear = 0.0
        if self.num_linear:
            x_linear = x_linear + self.num_linear(x_num) 
        if self.cat_linear:
            x_linear = x_linear + self.cat_linear(x_cat)
            
        #二,deep部分 
        
        #1,embedding
        x_embedding = []
        if self.num_embedding:
            x_embedding.append(self.num_embedding(x_num[...,None]))
        if self.cat_embedding:
            x_embedding.append(self.cat_embedding(x_cat))
        x_embedding = torch.cat(x_embedding,dim=1)
        
        #2,interaction
        se_embedding = self.se_attention(x_embedding)
        ffm_out = self.bilinear(x_embedding)
        se_ffm_out = self.bilinear(se_embedding)
        x_interaction = torch.flatten(torch.cat([ffm_out, se_ffm_out], dim=1), start_dim=1)
        
        #3,mlp
        x_deep = self.mlp(x_interaction)
        
        #三,高低融合
        x_out = x_linear+x_deep
        if self.n_classes==1:
            x_out = x_out.squeeze(-1)
        return x_out
##测试 FiBiNET

model = FiBiNET(d_numerical = 3, categories = [4,3,2],
        d_embed = 4, mlp_layers = [20,20], mlp_dropout=0.25,
        reduction_ratio = 3,
        bilinear_type = "field_interaction",
        n_classes = 1)

x_num = torch.randn(2,3)
x_cat = torch.randint(0,2,(2,3))
print(model((x_num,x_cat)))
tensor([-0.8621,  0.6743], grad_fn=<SqueezeBackward1>)

三,criteo数据集完整范例

Criteo数据集是一个经典的广告点击率CTR预测数据集。

这个数据集的目标是通过用户特征和广告特征来预测某条广告是否会为用户点击。

数据集有13维数值特征(I1-I13)和26维类别特征(C14-C39), 共39维特征, 特征中包含着许多缺失值。

训练集4000万个样本,测试集600万个样本。数据集大小超过100G.

此处使用的是采样100万个样本后的cretio_small数据集。

!pip install -U torchkeras -i https://pypi.org/simple/
import numpy as np 
import pandas as pd 
import datetime 

from sklearn.model_selection import train_test_split 

import torch 
from torch import nn 
from torch.utils.data import Dataset,DataLoader  
import torch.nn.functional as F 
import torchkeras 

def printlog(info):
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print("\n"+"=========="*8 + "%s"%nowtime)
    print(info+'...\n\n')

1,准备数据

from sklearn.preprocessing import LabelEncoder,QuantileTransformer
from sklearn.pipeline import Pipeline 
from sklearn.impute import SimpleImputer 

dfdata = pd.read_csv("../input/criteo-small/train_1m.txt",sep="\t",header=None)
dfdata.columns = ["label"] + ["I"+str(x) for x in range(1,14)] + [
    "C"+str(x) for x in range(14,40)]

cat_cols = [x for x in dfdata.columns if x.startswith('C')]
num_cols = [x for x in dfdata.columns if x.startswith('I')]
num_pipe = Pipeline(steps = [('impute',SimpleImputer()),('quantile',QuantileTransformer())])

for col in cat_cols:
    dfdata[col]  = LabelEncoder().fit_transform(dfdata[col])

dfdata[num_cols] = num_pipe.fit_transform(dfdata[num_cols])

categories = [dfdata[col].max()+1 for col in cat_cols]
import torch 
from torch.utils.data import Dataset,DataLoader 

#DataFrame转换成torch数据集Dataset, 特征分割成X_num,X_cat方式
class DfDataset(Dataset):
    def __init__(self,df,
                 label_col,
                 num_features,
                 cat_features,
                 categories,
                 is_training=True):
        
        self.X_num = torch.tensor(df[num_features].values).float() if num_features else None
        self.X_cat = torch.tensor(df[cat_features].values).long() if cat_features else None
        self.Y = torch.tensor(df[label_col].values).float() 
        self.categories = categories
        self.is_training = is_training
    
    def __len__(self):
        return len(self.Y)
    
    def __getitem__(self,index):
        if self.is_training:
            return ((self.X_num[index],self.X_cat[index]),self.Y[index])
        else:
            return (self.X_num[index],self.X_cat[index])
    
    def get_categories(self):
        return self.categories
dftrain_val,dftest = train_test_split(dfdata,test_size=0.2)
dftrain,dfval = train_test_split(dftrain_val,test_size=0.2)

ds_train = DfDataset(dftrain,label_col = "label",num_features = num_cols,cat_features = cat_cols,
                    categories = categories, is_training=True)

ds_val = DfDataset(dfval,label_col = "label",num_features = num_cols,cat_features = cat_cols,
                    categories = categories, is_training=True)

ds_test = DfDataset(dftest,label_col = "label",num_features = num_cols,cat_features = cat_cols,
                    categories = categories, is_training=True)
dl_train = DataLoader(ds_train,batch_size = 2048,shuffle=True)
dl_val = DataLoader(ds_val,batch_size = 2048,shuffle=False)
dl_test = DataLoader(ds_test,batch_size = 2048,shuffle=False)

for features,labels in dl_train:
    break

2,定义模型

def create_net():
    net = FiBiNET(
        d_numerical= ds_train.X_num.shape[1],
        categories= ds_train.get_categories(),
        d_embed = 8, mlp_layers = [128,64,32], mlp_dropout=0.25,
        reduction_ratio = 3,
        bilinear_type = "field_all",
        n_classes = 1
        
    )
    return net 

from torchkeras import summary

net = create_net()

3,训练模型

import os,sys,time
import numpy as np
import pandas as pd
import datetime 
from tqdm import tqdm 

import torch
from torch import nn 
from accelerate import Accelerator
from copy import deepcopy


def printlog(info):
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print("\n"+"=========="*8 + "%s"%nowtime)
    print(str(info)+"\n")
    
class StepRunner:
    def __init__(self, net, loss_fn,stage = "train", metrics_dict = None, 
                 optimizer = None, lr_scheduler = None,
                 accelerator = None
                 ):
        self.net,self.loss_fn,self.metrics_dict,self.stage = net,loss_fn,metrics_dict,stage
        self.optimizer,self.lr_scheduler = optimizer,lr_scheduler
        self.accelerator = accelerator
    
    def __call__(self, features, labels):
        #loss
        preds = self.net(features)
        loss = self.loss_fn(preds,labels)

        #backward()
        if self.optimizer is not None and self.stage=="train":
            if self.accelerator is  None:
                loss.backward()
            else:
                self.accelerator.backward(loss)
            self.optimizer.step()
            if self.lr_scheduler is not None:
                self.lr_scheduler.step()
            self.optimizer.zero_grad()
            
        #metrics
        step_metrics = {self.stage+"_"+name:metric_fn(preds, labels).item() 
                        for name,metric_fn in self.metrics_dict.items()}
        return loss.item(),step_metrics
    
    
class EpochRunner:
    def __init__(self,steprunner):
        self.steprunner = steprunner
        self.stage = steprunner.stage
        self.steprunner.net.train() if self.stage=="train" else self.steprunner.net.eval()
        
    def __call__(self,dataloader):
        total_loss,step = 0,0
        loop = tqdm(enumerate(dataloader), total =len(dataloader))
        for i, batch in loop:
            features,labels = batch
            if self.stage=="train":
                loss, step_metrics = self.steprunner(features,labels)
            else:
                with torch.no_grad():
                    loss, step_metrics = self.steprunner(features,labels)

            step_log = dict({self.stage+"_loss":loss},**step_metrics)

            total_loss += loss
            step+=1
            if i!=len(dataloader)-1:
                loop.set_postfix(**step_log)
            else:
                epoch_loss = total_loss/step
                epoch_metrics = {self.stage+"_"+name:metric_fn.compute().item() 
                                 for name,metric_fn in self.steprunner.metrics_dict.items()}
                epoch_log = dict({self.stage+"_loss":epoch_loss},**epoch_metrics)
                loop.set_postfix(**epoch_log)

                for name,metric_fn in self.steprunner.metrics_dict.items():
                    metric_fn.reset()
        return epoch_log

class KerasModel(torch.nn.Module):
    def __init__(self,net,loss_fn,metrics_dict=None,optimizer=None,lr_scheduler = None):
        super().__init__()
        self.accelerator = Accelerator()
        self.history = {}
        
        self.net = net
        self.loss_fn = loss_fn
        self.metrics_dict = nn.ModuleDict(metrics_dict) 
        
        self.optimizer = optimizer if optimizer is not None else torch.optim.Adam(
            self.parameters(), lr=1e-2)
        self.lr_scheduler = lr_scheduler
        
        self.net,self.loss_fn,self.metrics_dict,self.optimizer = self.accelerator.prepare(
            self.net,self.loss_fn,self.metrics_dict,self.optimizer)

    def forward(self, x):
        if self.net:
            return self.net.forward(x)
        else:
            raise NotImplementedError


    def fit(self, train_data, val_data=None, epochs=10, ckpt_path='checkpoint.pt', 
            patience=5, monitor="val_loss", mode="min"):
        
        train_data = self.accelerator.prepare(train_data)
        val_data = self.accelerator.prepare(val_data) if val_data else []

        for epoch in range(1, epochs+1):
            printlog("Epoch {0} / {1}".format(epoch, epochs))
            
            # 1,train -------------------------------------------------  
            train_step_runner = StepRunner(net = self.net,stage="train",
                    loss_fn = self.loss_fn,metrics_dict=deepcopy(self.metrics_dict),
                    optimizer = self.optimizer, lr_scheduler = self.lr_scheduler,
                    accelerator = self.accelerator)
            train_epoch_runner = EpochRunner(train_step_runner)
            train_metrics = train_epoch_runner(train_data)
            
            for name, metric in train_metrics.items():
                self.history[name] = self.history.get(name, []) + [metric]

            # 2,validate -------------------------------------------------
            if val_data:
                val_step_runner = StepRunner(net = self.net,stage="val",
                    loss_fn = self.loss_fn,metrics_dict=deepcopy(self.metrics_dict),
                    accelerator = self.accelerator)
                val_epoch_runner = EpochRunner(val_step_runner)
                with torch.no_grad():
                    val_metrics = val_epoch_runner(val_data)
                val_metrics["epoch"] = epoch
                for name, metric in val_metrics.items():
                    self.history[name] = self.history.get(name, []) + [metric]
            
            # 3,early-stopping -------------------------------------------------
            arr_scores = self.history[monitor]
            best_score_idx = np.argmax(arr_scores) if mode=="max" else np.argmin(arr_scores)
            if best_score_idx==len(arr_scores)-1:
                torch.save(self.net.state_dict(),ckpt_path)
                print("<<<<<< reach best {0} : {1} >>>>>>".format(monitor,
                     arr_scores[best_score_idx]),file=sys.stderr)
            if len(arr_scores)-best_score_idx>patience:
                print("<<<<<< {} without improvement in {} epoch, early stopping >>>>>>".format(
                    monitor,patience),file=sys.stderr)
                self.net.load_state_dict(torch.load(ckpt_path))
                break 
            
        return pd.DataFrame(self.history)

    @torch.no_grad()
    def evaluate(self, val_data):
        val_data = self.accelerator.prepare(val_data)
        val_step_runner = StepRunner(net = self.net,stage="val",
                    loss_fn = self.loss_fn,metrics_dict=deepcopy(self.metrics_dict),
                    accelerator = self.accelerator)
        val_epoch_runner = EpochRunner(val_step_runner)
        val_metrics = val_epoch_runner(val_data)
        return val_metrics
        
       
    @torch.no_grad()
    def predict(self, dataloader):
        dataloader = self.accelerator.prepare(dataloader)
        result = torch.cat([self.forward(t[0]) for t in dataloader])
        return result.data
from torchkeras.metrics import AUC

loss_fn = nn.BCEWithLogitsLoss()

metrics_dict = {"auc":AUC()}

optimizer = torch.optim.Adam(net.parameters(), lr=0.002, weight_decay=0.001) 

model = KerasModel(net,
                   loss_fn = loss_fn,
                   metrics_dict= metrics_dict,
                   optimizer = optimizer
                  )
dfhistory = model.fit(train_data=dl_train,val_data=dl_val,epochs=100, patience=5,
                      monitor = "val_auc",mode="max",ckpt_path='checkpoint.pt')
a083040c2d25347ae2073b5aba796c6f.png

4,评估模型

%matplotlib inline
%config InlineBackend.figure_format = 'svg'

import matplotlib.pyplot as plt

def plot_metric(dfhistory, metric):
    train_metrics = dfhistory["train_"+metric]
    val_metrics = dfhistory['val_'+metric]
    epochs = range(1, len(train_metrics) + 1)
    plt.plot(epochs, train_metrics, 'bo--')
    plt.plot(epochs, val_metrics, 'ro-')
    plt.title('Training and validation '+ metric)
    plt.xlabel("Epochs")
    plt.ylabel(metric)
    plt.legend(["train_"+metric, 'val_'+metric])
    plt.show()
plot_metric(dfhistory,"loss")
4a73e21659f7d67225874e4a06ea4a70.png
plot_metric(dfhistory,"auc")
a4c5079851ca0ebd95f7096cb89f0f3d.png

5,使用模型

from sklearn.metrics import roc_auc_score
preds = torch.sigmoid(model.predict(dl_val))
labels = torch.cat([x[-1] for x in dl_val])

val_auc = roc_auc_score(labels.cpu().numpy(),preds.cpu().numpy())
print(val_auc)

0.7806176567186112

6,保存模型

torch.save(model.net.state_dict(),"best_fibinet.pt")
net_clone = create_net()
net_clone.load_state_dict(torch.load("best_fibinet.pt"))
from sklearn.metrics import roc_auc_score
net_clone.eval()
preds = torch.cat([torch.sigmoid(net_clone(x[0])).data for x in dl_val]) 
labels = torch.cat([x[-1] for x in dl_val])

val_auc = roc_auc_score(labels.cpu().numpy(),preds.cpu().numpy())
print(val_auc)

0.7806176567186112

可以看到FiBiNET在验证集的AUC得分为0.7806,相比之下DeepFM的验证集AUC为0.7803。

不能说纹丝不动, 只能说了涨了个蚊子腿大小肉的点。

并且这是以较大地牺牲模型训练预测效率为代价的。

DeepFM训练一个Epoch大约需要20s, 而FiBiNET训练一个Epoch需要大约2min.

尽管如此, FiBiNET的结构设计依然是值得我们学习和借鉴的, 集神经网络结构设计三大主流高级技巧于一体, 闪烁着穿越时空的才华与智慧光芒。

以上。

万水千山总是情,点个在看行不行?😋😋 

公众号后台回复关键词: FiBiNET 获取本文全部源码和数据集下载链接。

9c9c2591bba282310f87edc86cfdaed1.png

AI上推荐 之 FiBiNET模型(特征重要性选择与双线性特征交叉) 1. 写在前面 从五月份的xdeepFM到现在,已经有一个多月的时间没有与推荐模型相关的paper了,说好的"小步快跑"呢? 来到公司实习之后, 更一直抽不出时间来更新这个系列。不过,最近这段时间, 由于开始接触实际场景下的推荐小项目, 而实习生的话一般要从模型上手,所以这一周终于又有了时间开始玩推荐模型,并进行相应的魔改。 当然,来实习之后, 从前辈们的以及周围伙伴的交流中悟出了一个真理,在推荐系统甚至其他领域(CV,NLP)等,模型都只是工具而已,遇到问题解决问题的方法和策略才是王道, 那么我为啥还要坚 阅读详情

相关推荐

FiBiNet分享1

FiBiNet分享1

单样本微调给ChatGLM2注入知识~

前方干货预警:这可能也是一篇会改变你对LLM微调范式,以及对LLM原理理解的文章。同时这也是一篇非常有趣好玩,具有强大实操性的ChatGLM2微调喂饭级教程。我们演示了使用AdaLoRA算法,使用1条样本对ChatGLM2-6b实施微调。几分钟就成功注入了"梦中情炉"有关的知识。公众号算法美食屋后台回复关键词:torchkeras,获取本文notebook源码。summary:(1) 只需要1条样...

Python_Ai_Road的博客 1414

SpringBoot实战——仿b站项目(一)

热部署就是当应用程序这种运行的时候升级冉家或者修改某一部分代码、配置文件时,无需手动启动应用,即可修改的部分生效工具 + IDEA配置。

weixin_52880572的博客 3466

FiBiNET:结合特征重要性和双线性特征交互进行CTR预估

FiBiNET全称FiBiNET: Combining Feature Importance and Bilinear feature Interaction for Click-Through Rate Prediction,是新浪微博提出的一种基于深度学习的广告推荐/点击率预测算法。可以认为FiBiNET是在wide & deep模型的基础上对它的wide部分进行了一些创新的改进,或者...

Tak-Wah Blog 4371

RS485/CAN总线防护电路设计:TVS选型与ESD防护实战解析

本文深入解析了RS485与CAN总线防护电路设计中的TVS选型与ESD防护实战技巧。针对工业现场常见的静电放电、浪涌等干扰,文章详细阐述了TVS管关键参数的选择、多级防护体系的构建,并特别强调了在隔离电路设计中地回路处理的重要性,帮助工程师设计出更可靠的通信接口。

metaverse5vr的博客 407

『与善仁』Appium基础 — 19、元素定位工具之UI Automator Viewer

文章目录(一)uiautomatorviewer定位工具1、uiautomatorviewer介绍2、uiautomatorviewer工具打开方式3、uiautomatorviewer布局介绍4、uiautomatorviewer工具的使用 之前我们在做web页面自动化的时候就说过,元素定位是页面自动化的第一步。 同理App自动化测试的第一步,也是元素的定位。 在APP中定位元素的话,需要借助一些元素定位工具。 APP中的元素定位工具大致有三种: uiautomatorviewer Appium Ins

繁华似锦Fighting 1523

用BERT做命名实体识别任务

命名实体识别NER任务是NLP的一个常见任务,它是Named Entity Recognization的简称。简单地说,就是识别一个句子中的各种 名称实体。诸如:人名,地名,机构 等。例如对于下面这句话:小明对小红说:"你听说过安利吗?"它的NER抽取结果如下:[{'entity':'person', 'word':'小明', 'start':0, 'end':2}, {'e...

Python_Ai_Road的博客 3446

机器学习kaggle案例:风控评分卡模型(Give_Me_Some_Credit)

kaggle链接:https://www.kaggle.com/c/GiveMeSomeCredit ipynb文件:https://github.com/824024445/KaggleCases/blob/master/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0kaggle%E6%A1%88%E4%BE%8B%EF%BC%9A%E9%A3%8E%E6%8E%A7%...

weixin_42662126的博客 1万+

Fibinet排序模型

实现了“特征嵌入 → 动态加权 → 深度交互 → 概率预测”的完整流程,是工业级推荐系统中经典的 “深度特征工程 + 注意力机制” 模型,适合需要精准捕捉特征关联的场景。挖掘特征间复杂关联,最终用 MLP 输出预测结果(如用户点击概率)。,主要用于推荐系统场景。对特征重要性动态加权,结合。

qq_39084101的博客 923

解析vCard 3.0:提取电子名片中的姓名

N:LastName;FirstName;;;END:VCARD和END:VCARD标记数据块的开始和结束。N字段存储结构化姓名(分号分隔)。FN字段为直接可读的显示名称。其他字段如TELEMAIL等保存联系信息。

qq_45797625的博客 2797

MUJOCO+AUBO机械臂+强化学习+路径规划

最近在学习机械臂强化学习路径规划,参考了知乎教程。由于原教程较老(使用stable baselines3+panda-gym),新版本变化了,我略做调整,记录写在。上面这个教程用的是官方,环境封装较死(类似),最终基于重新开发。代码已开源至github:https://github.com/YellowThree-HS/mujoco-Aubo-RL-PathPlanning。

qq_63308550的博客 2051

Accclerate使用教程

1.特点 Accelerate提供简单的API使得脚本可以按混合精度运行,同时可在任何分布式设置上运行,相同的代码可以在本地电脑上进行debug也可以在训练环境执行。同时, Accelerate提供一个CLI工具,允许快速配置并测试训练环境,然后运行脚本 2.易于集成 Pytorch中传统的训练Loop my_model.to(device) for batch in my_training_dataloader: my_optimizer.zero_grad() ...

celialhl的博客 1万+

sap资产post记录查询时候 报错 如图

SAP资产过账查询报错AA653解决方案摘要 错误原因:系统无法计算会计年度期间的最后一天,导致资产折旧/过账失败。常见原因包括:1)未创建2025年期间版本;2)期间日期映射不完整;3)依赖日历但未生成2025年日历。 解决方法: 通过OB37维护2025年12/13个完整期间 补充缺失期间或修正结束日期 使用SCAL生成2025年工厂日历 验证步骤: 检查T009R表2025年记录完整性 测试AFAB折旧运行 重新查询资产过账 提示:建议财务团队统一维护跨年度期间数据,并在测试系统验证变更。

金牌架构师 837

60分钟吃掉FM算法

FM算法全称为因子分解机 (FactorizationMachine)。它是广告和推荐领域非常著名的算法,在线性回归模型上考虑了特征的二阶交互。适合捕捉大规模稀疏特征(类别特征)当中的特征交互。FM及其衍生的一些较有名的算法的简要介绍如下:FM(FactorizationMachine):在LR基础上用隐向量点积实现自动化特征二阶交叉,且交互项的计算复杂度是O(n),效果...

Python_Ai_Road的博客 646

YOLOv11改进 | 集成FEM、FFM与SCAM即插即用模块:特征增强、融合与空间上下文建模新范式

FEM解决特征表达不足问题,增强关键特征响应FFM优化多尺度融合,提升小目标检测精度SCAM建模空间上下文,改善目标交互预测在保持YOLOv11实时性优势的同时,实现mAP+8.1%的显著提升,特别在复杂光照、密集目标、交互预测场景表现突出。其模块化设计使开发者可根据需求灵活组合,为各种复杂场景提供定制化解决方案。未来,随着跨模态扩展与神经架构搜索的深入,YOLOv11-FSC将进一步推动目标检测技术向更智能、更高效的方向发展。

走向CTO的路上... 993

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

特征交叉-FiBiNet特征交叉原理与代码学习

两个field做交叉得到新的特征,比方把物品和用户地点做embedding,然后做交叉, 如果用inner product和hadamard乘积,必须维度一致,不一致的情况下,Bilinear引入了一个W矩阵,保证两者可以做交叉。SENet 虽然不是特征交叉方法,但是是在FiBiNet这个文章里一起出现的,就说明下,是一种field-wise加权,一共m个field。1)field维度的平均池化:每一个field做average-pooling,3)还原:再加一个linear+sigmoid还原成m维度。

清风徐来 615

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

特征建模之FiBiNet

推荐领经典CTR模型

努力的小菜鸟的博客 713

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

torch.nn.Bilinear函数计算原理

双线性层的 W 的维度为 (out_features,in1_features, in2_features);的维度为 (batch_size, in1_features);的维度为 (batch_size, in2_features);双线性层的 B 的维度为 (out_features);实例化 torch.nn.Bilinear 类时,通过。

qq_49323609的博客 1150

推荐系统系列:新浪搜索团队FiBiNET理论和代码实现

一、前言 (1)代码参考了阿里巴巴浅梦大神的实现,非常牛逼的一位大佬,非常值得大家学习; (2)微博提出的fibinet点击预估模型,论文地址为:https://arxiv.org/pdf/1905.09433.pdf; 二、算法原理 (1)整体框架如图所示: (2)SENET Layer部分的主要作用是学习不同特征的一个重要程度,对重要特征加权,对蕴含信息量不多的特征进行削弱;参考...

wwwsctvcom的博客 1051

细粒度图像分类(FGVC)--- 综述

目录 一、概述什么是细粒度图像分类意义细粒度图像分类的挑战细粒度分类常用方法 二、基于定位-识别的方法2.1 强监督2.1.1 Part-based R-CNN2.1.2Pose Normalized CNN2.1.3 基于多候选区集成的部件定位(Part loca...

7365

深度学习 - 43.SeNET、Bilinear Interaction 实现特征交叉 By Keras

kears 实现 FiBiNet 论文中的 SENET Layer 与 Bilinear Interaction Layer。

BITDDD小栈 1595

论文笔记: (compact) Bilinear Pooling, Confusion

Learn by attention, learn with confusion.先引用几段魏秀参大神的论述「见微知著」——细粒度图像分析进展综述 细粒度物体的差异仅体现在细微之处。如何有效地对前景对象进行检测,并从中发现重要的局部区域信息,成为了细粒度图像分类算法要解决的关键问题。对细粒度分类模型,可以按照其使用的监督信息的多少,分为“基于强监督信息的分类模型”和“基于弱监督信息的分类模型”两

Wayne2019的博客 1万+

FiBiNet 网络介绍与源码浅析

FiBiNet 网络介绍与源码浅析 前言 (与主题无关, 可以忽略) 我知道这有点不太厚道, 文章不写全就发出来, 但最近真的很忙, 同时给自己立了 9 月再写一篇博客的 Flag~ 可是这个月只写了一篇 ???????????? 今晚是 9 月 30 日, 月色很美 … (猜测的, 毕竟明日中秋和国庆一起过; 走路忘了抬头看看夜空, 忧桑~). 虽然下班较早, 但心事重重, 不到最后一刻不动笔. 因此现在先扯一点前言, 后续一定会以全副精力来完成 Flag! 我最近可是看了很多 paper 的, 可以乘着

珍妮的选择的博客 1189

深度推荐模型——FiBiNet[RecSys 19][Weibo]

2、传统的特征交叉方式广泛采用了内积和哈达玛积,而这两种方式在稀疏数据上很难有效对特征交叉进行建模。作者认为模型需要学习不同特征的一个重要程度,对重要特征加权,对蕴含信息量不多的特征进行削弱。

774

FiBiNET总结

Combining Feature Importance and Bilinear feature Interaction for Click-Through Rate Prediction 目录 本文主要工作 相关工作 本文的模型 实验 本文主要工作 (1)Our main contributions are listed as follows: • Inspired by th...

ShangYT的专栏 1461
上一篇: 40分钟吃掉DeepFM算法
下一篇: 60分钟吃掉嘎嘣脆的DeepCross模型
算法美食屋
博客等级 码龄9年 2万+粉丝 154原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址