關注公眾號 長歌大腿,發送“機器學習”關鍵字,可獲取包含機器學習(包含深度學習),統計概率,優化算法等系列文本與視頻經典資料,如《ESL》《PRML》《MLAPP》等。
《現代推薦算法》神經協同過濾之MLP算法
神經協同過濾簡介
前面的文章介紹了協同過濾算法,主要分為基于用戶的協同過濾算法與基于物品的協同過濾算法,同時指出,矩陣分解也屬于廣義的協同過濾算法。
那么之前的文章介紹的SVD,SVD++等等矩陣分解算法都是在傳統矩陣分解上面進行的改進。隨著神經網絡的興起,神經網絡應用到協同過濾算法上,有研究者(何教授)提出了神經協同過濾算法,并將其分為GMF,MLP,NeuMF三種具體的網絡結構。
這篇文章為神經協同過濾系列的第二篇文章,我們在本篇文章中介紹其中的MLP模型,在下篇短文中我們再介紹借鑒Wide-Deep結構由GMF與MLP融合而成的NeuMF網絡結構。
多層感知機分解(MLP)
MLP是多層感知器分解的簡寫(Multi-Layer Perceptron).MLP的設計初衷是為了得到關于用戶隱空間向量與物品隱空間向量更多的交互特征,故采用多層感知器的結構來進行特征提取。具體而言,MLP使用大量的非線性網絡層來提取用戶隱向量 與物品隱向量
的交互特征。如下所示
其中 ,
, 與
是權重矩陣,偏執向量,激活函數。
代碼實現
我們采用pytorch計算框架來示例MLP的網絡結構部分。MLP網絡類如下所示,
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)
先根據嵌入層維度等信息初始化網絡,同時初始化權重。
網絡傳播層如代碼所示,輸入為用戶與物品的ID,然后經過嵌入編碼,再進行橫向拼接,然后經過幾個全連接層進行特征提取,最后通過一個全連接線性層加權輸出。