關(guān)注公眾號 長歌大腿,發(fā)送“機(jī)器學(xué)習(xí)”關(guān)鍵字,可獲取包含機(jī)器學(xué)習(xí)(包含深度學(xué)習(xí)),統(tǒng)計(jì)概率,優(yōu)化算法等系列文本與視頻經(jīng)典資料,如《ESL》《PRML》《MLAPP》等。
《現(xiàn)代推薦算法》神經(jīng)協(xié)同過濾之MLP算法
神經(jīng)協(xié)同過濾簡介
前面的文章介紹了協(xié)同過濾算法,主要分為基于用戶的協(xié)同過濾算法與基于物品的協(xié)同過濾算法,同時(shí)指出,矩陣分解也屬于廣義的協(xié)同過濾算法。
那么之前的文章介紹的SVD,SVD++等等矩陣分解算法都是在傳統(tǒng)矩陣分解上面進(jìn)行的改進(jìn)。隨著神經(jīng)網(wǎng)絡(luò)的興起,神經(jīng)網(wǎng)絡(luò)應(yīng)用到協(xié)同過濾算法上,有研究者(何教授)提出了神經(jīng)協(xié)同過濾算法,并將其分為GMF,MLP,NeuMF三種具體的網(wǎng)絡(luò)結(jié)構(gòu)。
這篇文章為神經(jīng)協(xié)同過濾系列的第二篇文章,我們在本篇文章中介紹其中的MLP模型,在下篇短文中我們再介紹借鑒Wide-Deep結(jié)構(gòu)由GMF與MLP融合而成的NeuMF網(wǎng)絡(luò)結(jié)構(gòu)。
多層感知機(jī)分解(MLP)
MLP是多層感知器分解的簡寫(Multi-Layer Perceptron).MLP的設(shè)計(jì)初衷是為了得到關(guān)于用戶隱空間向量與物品隱空間向量更多的交互特征,故采用多層感知器的結(jié)構(gòu)來進(jìn)行特征提取。具體而言,MLP使用大量的非線性網(wǎng)絡(luò)層來提取用戶隱向量 與物品隱向量
的交互特征。如下所示
其中 ,
, 與
是權(quán)重矩陣,偏執(zhí)向量,激活函數(shù)。
代碼實(shí)現(xiàn)
我們采用pytorch計(jì)算框架來示例MLP的網(wǎng)絡(luò)結(jié)構(gòu)部分。MLP網(wǎng)絡(luò)類如下所示,
class MLP(nn.Module):
def __init__(self, user_num, item_num, factor_num, num_layers, dropout):
super(MLP, self).__init__()
self.embed_user_MLP = nn.Embedding(user_num, factor_num * (2 ** (num_layers - 1)))
self.embed_item_MLP = nn.Embedding(item_num, factor_num * (2 ** (num_layers - 1)))
MLP_modules = []
for i in range(num_layers):
input_size = factor_num * (2 ** (num_layers - i))
MLP_modules.append(nn.Dropout(p=dropout))
MLP_modules.append(nn.Linear(input_size, input_size // 2))
MLP_modules.append(nn.ReLU())
self.MLP_layers = nn.Sequential(*MLP_modules)
self.predict_layer = nn.Linear(factor_num, 1)
self._init_weight_()
def _init_weight_(self):
nn.init.normal_(self.embed_user_MLP.weight, std=0.01)
nn.init.normal_(self.embed_item_MLP.weight, std=0.01)
for m in self.MLP_layers:
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
nn.init.kaiming_uniform_(self.predict_layer.weight, a=1, nonlinearity='sigmoid')
def forward(self, user, item):
embed_user_MLP = self.embed_user_MLP(user)
embed_item_MLP = self.embed_item_MLP(item)
interaction = torch.cat((embed_user_MLP, embed_item_MLP), -1)
output_MLP = self.MLP_layers(interaction)
prediction = self.predict_layer(output_MLP)
return prediction.view(-1)
先根據(jù)嵌入層維度等信息初始化網(wǎng)絡(luò),同時(shí)初始化權(quán)重。
網(wǎng)絡(luò)傳播層如代碼所示,輸入為用戶與物品的ID,然后經(jīng)過嵌入編碼,再進(jìn)行橫向拼接,然后經(jīng)過幾個(gè)全連接層進(jìn)行特征提取,最后通過一個(gè)全連接線性層加權(quán)輸出。