注冊過什么網(wǎng)站經(jīng)典軟文案例和扶貧農(nóng)產(chǎn)品軟文
對于CTR問題,被證明的最有效的提升任務(wù)表現(xiàn)的策略是特征組合(Feature Interaction);
兩個(gè)問題:
如何更好地學(xué)習(xí)特征組合,進(jìn)而更加精確地描述數(shù)據(jù)的特點(diǎn);
如何更高效的學(xué)習(xí)特征組合。
DNN局限?:當(dāng)我們使用DNN網(wǎng)絡(luò)解決推薦問題的時(shí)候存在網(wǎng)絡(luò)參數(shù)過于龐大的問題,這是因?yàn)樵谶M(jìn)行特征處理的時(shí)候我們需要使用one-hot編碼來處理離散特征,這會導(dǎo)致輸入的維度猛增。
為了解決DNN參數(shù)量過大的局限性,可以采用非常經(jīng)典的Field思想,將OneHot特征轉(zhuǎn)換為Dense Vector,通過增加全連接層就可以實(shí)現(xiàn)高階的特征組合。
黑色的線 和 紅色的線 進(jìn)行concat
self定義?
deep_features = deep_features
fm_features = fm_features ?#稀疏的特征
deep_dims = sum([fea.embed_dim for fea in deep_features]) ?#8
fm_dims = sum([fea.embed_dim for fea in fm_features]) ?#368 ? = 23*16 ? ? ? ? ? #稀疏的特征embedding化
linear = LR(fm_dims) ?# 1-odrder interaction ? 低階信息 ? (fc): Linear(in_features=368, out_features=1, bias=True)
fm = FM(reduce_sum=True) ?# 2-odrder interaction ? ?#FM將一階特征和二階特征cancat
embedding = EmbeddingLayer(deep_features + fm_features)
mlp = MLP(deep_dims, **mlp_params)
?forward
input_deep = embedding(x, deep_features, squeeze_dim=True) ?#[batch_size, deep_dims] ? ?torch.Size([10, 8])
input_fm = embedding(x, fm_features, squeeze_dim=False) ?#[batch_size, num_fields, embed_dim] ? torch.Size([10, 23, 16])
y_linear = linear(input_fm.flatten(start_dim=1)) ?#torch.Size([10, 1]) ?對應(yīng)的稀疏特征 經(jīng)過線性層變?yōu)?
y_fm = fm(input_fm) ?#torch.Size([10, 1]) ? ?#對稀疏特征做一階 二階處理?
y_deep = mlp(input_deep) ?#[batch_size, 1] ?#torch.Size([10, 1])
y = y_linear + y_fm + y_deep? ? ? ? ??
# return torch.sigmoid(y.squeeze(1))
定義的一些函數(shù):?
import torch.nn as nn
class LR(nn.Module):
? ? """Logistic Regression Module. It is the one Non-linear?
? ? transformation for input feature.? ? Args:
? ? ? ? input_dim (int): input size of Linear module.
? ? ? ? sigmoid (bool): whether to add sigmoid function before output.? ? Shape:
? ? ? ? - Input: `(batch_size, input_dim)`
? ? ? ? - Output: `(batch_size, 1)`
? ? """? ? def __init__(self, input_dim, sigmoid=False):
? ? ? ? super().__init__()
? ? ? ? self.sigmoid = sigmoid
? ? ? ? self.fc = nn.Linear(input_dim, 1, bias=True)? ? def forward(self, x):
? ? ? ? if self.sigmoid:
? ? ? ? ? ? return torch.sigmoid(self.fc(x))
? ? ? ? else:
? ? ? ? ? ? return self.fc(x)
? ? ? ??class FM(nn.Module):
? ? """The Factorization Machine module, mentioned in the `DeepFM paper
? ? <https://arxiv.org/pdf/1703.04247.pdf>`. It is used to learn 2nd-order?
? ? feature interactions.? ? Args:
? ? ? ? reduce_sum (bool): whether to sum in embed_dim (default = `True`).? ? Shape:
? ? ? ? - Input: `(batch_size, num_features, embed_dim)`
? ? ? ? - Output: `(batch_size, 1)`` or ``(batch_size, embed_dim)`
? ? """? ? def __init__(self, reduce_sum=True):
? ? ? ? super().__init__()
? ? ? ? self.reduce_sum = reduce_sum? ? def forward(self, x):
? ? ? ? square_of_sum = torch.sum(x, dim=1)**2
? ? ? ? sum_of_square = torch.sum(x**2, dim=1)
? ? ? ? ix = square_of_sum - sum_of_square
? ? ? ? if self.reduce_sum:
? ? ? ? ? ? ix = torch.sum(ix, dim=1, keepdim=True)
? ? ? ? return 0.5 * ix
參考資料:
推薦系統(tǒng)遇上深度學(xué)習(xí)(三)--DeepFM模型理論和實(shí)踐 - 簡書 (jianshu.com)
DeepFM (datawhalechina.github.io)