-
Notifications
You must be signed in to change notification settings - Fork 1
/
neumf.py
121 lines (103 loc) · 4.46 KB
/
neumf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (c) 2018, deepakn94, robieta. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----------------------------------------------------------------------
#
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import torch
import torch.nn as nn
from utils import DefaultArgs
import utils
class NeuMF(nn.Module):
def __init__(self, nb_users, nb_items,
mf_dim, mf_reg,
mlp_layer_sizes, mlp_layer_regs,
dropout=0):
if len(mlp_layer_sizes) != len(mlp_layer_regs):
raise RuntimeError('u dummy, layer_sizes != layer_regs!')
if mlp_layer_sizes[0] % 2 != 0:
raise RuntimeError('u dummy, mlp_layer_sizes[0] % 2 != 0')
super(NeuMF, self).__init__()
nb_mlp_layers = len(mlp_layer_sizes)
self.mf_user_embed = nn.Embedding(nb_users, mf_dim)
self.mf_item_embed = nn.Embedding(nb_items, mf_dim)
self.mlp_user_embed = nn.Embedding(nb_users, mlp_layer_sizes[0] // 2)
self.mlp_item_embed = nn.Embedding(nb_items, mlp_layer_sizes[0] // 2)
self.dropout = dropout
self.mlp = nn.ModuleList()
for i in range(1, nb_mlp_layers):
self.mlp.extend([nn.Linear(mlp_layer_sizes[i - 1], mlp_layer_sizes[i])]) # noqa: E501
self.final = nn.Linear(mlp_layer_sizes[-1] + mf_dim, 1)
self.mf_user_embed.weight.data.normal_(0., 0.01)
self.mf_item_embed.weight.data.normal_(0., 0.01)
self.mlp_user_embed.weight.data.normal_(0., 0.01)
self.mlp_item_embed.weight.data.normal_(0., 0.01)
def glorot_uniform(layer):
fan_in, fan_out = layer.in_features, layer.out_features
limit = np.sqrt(6. / (fan_in + fan_out))
layer.weight.data.uniform_(-limit, limit)
def lecunn_uniform(layer):
fan_in, fan_out = layer.in_features, layer.out_features # noqa: F841, E501
limit = np.sqrt(3. / fan_in)
layer.weight.data.uniform_(-limit, limit)
for layer in self.mlp:
if type(layer) != nn.Linear:
continue
glorot_uniform(layer)
lecunn_uniform(self.final)
def forward(self, user, item, sigmoid=False):
xmfu = self.mf_user_embed(user)
xmfi = self.mf_item_embed(item)
xmf = xmfu * xmfi
xmlpu = self.mlp_user_embed(user)
xmlpi = self.mlp_item_embed(item)
xmlp = torch.cat((xmlpu, xmlpi), dim=1)
for i, layer in enumerate(self.mlp):
xmlp = layer(xmlp)
xmlp = nn.functional.relu(xmlp)
if self.dropout != 0:
xmlp = nn.functional.dropout(xmlp, p=self.dropout, training=self.training)
x = torch.cat((xmf, xmlp), dim=1)
x = self.final(x)
if sigmoid:
x = torch.sigmoid(x)
return x
# Create model
def initialize_model(nb_users, nb_items):
# make pytorch memory behavior more consistent later
torch.cuda.empty_cache()
if DefaultArgs.seed is not None:
torch.manual_seed(DefaultArgs.seed)
model = NeuMF(nb_users, nb_items,
mf_dim=DefaultArgs.factors, mf_reg=0.,
mlp_layer_sizes=DefaultArgs.layers,
mlp_layer_regs=[0. for i in DefaultArgs.layers],
dropout=DefaultArgs.dropout)
model = model.cuda()
print(model)
print("{} parameters".format(utils.count_parameters(model)))
return model