neumf.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2018, deepakn94, robieta. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # -----------------------------------------------------------------------
  16. #
  17. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  18. #
  19. # Licensed under the Apache License, Version 2.0 (the "License");
  20. # you may not use this file except in compliance with the License.
  21. # You may obtain a copy of the License at
  22. #
  23. # http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. # Unless required by applicable law or agreed to in writing, software
  26. # distributed under the License is distributed on an "AS IS" BASIS,
  27. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. # See the License for the specific language governing permissions and
  29. # limitations under the License.
  30. import numpy as np
  31. import torch
  32. import torch.nn as nn
  33. import sys
  34. from os.path import abspath, join, dirname
  35. class NeuMF(nn.Module):
  36. def __init__(self, nb_users, nb_items,
  37. mf_dim, mlp_layer_sizes, dropout=0):
  38. if mlp_layer_sizes[0] % 2 != 0:
  39. raise RuntimeError('u dummy, mlp_layer_sizes[0] % 2 != 0')
  40. super(NeuMF, self).__init__()
  41. nb_mlp_layers = len(mlp_layer_sizes)
  42. self.mf_user_embed = nn.Embedding(nb_users, mf_dim)
  43. self.mf_item_embed = nn.Embedding(nb_items, mf_dim)
  44. self.mlp_user_embed = nn.Embedding(nb_users, mlp_layer_sizes[0] // 2)
  45. self.mlp_item_embed = nn.Embedding(nb_items, mlp_layer_sizes[0] // 2)
  46. self.dropout = dropout
  47. self.mlp = nn.ModuleList()
  48. for i in range(1, nb_mlp_layers):
  49. self.mlp.extend([nn.Linear(mlp_layer_sizes[i - 1], mlp_layer_sizes[i])]) # noqa: E501
  50. self.final = nn.Linear(mlp_layer_sizes[-1] + mf_dim, 1)
  51. self.mf_user_embed.weight.data.normal_(0., 0.01)
  52. self.mf_item_embed.weight.data.normal_(0., 0.01)
  53. self.mlp_user_embed.weight.data.normal_(0., 0.01)
  54. self.mlp_item_embed.weight.data.normal_(0., 0.01)
  55. def glorot_uniform(layer):
  56. fan_in, fan_out = layer.in_features, layer.out_features
  57. limit = np.sqrt(6. / (fan_in + fan_out))
  58. layer.weight.data.uniform_(-limit, limit)
  59. def lecunn_uniform(layer):
  60. fan_in, fan_out = layer.in_features, layer.out_features # noqa: F841, E501
  61. limit = np.sqrt(3. / fan_in)
  62. layer.weight.data.uniform_(-limit, limit)
  63. for layer in self.mlp:
  64. if type(layer) != nn.Linear:
  65. continue
  66. glorot_uniform(layer)
  67. lecunn_uniform(self.final)
  68. def forward(self, user, item, sigmoid=False):
  69. xmfu = self.mf_user_embed(user)
  70. xmfi = self.mf_item_embed(item)
  71. xmf = xmfu * xmfi
  72. xmlpu = self.mlp_user_embed(user)
  73. xmlpi = self.mlp_item_embed(item)
  74. xmlp = torch.cat((xmlpu, xmlpi), dim=1)
  75. for i, layer in enumerate(self.mlp):
  76. xmlp = layer(xmlp)
  77. xmlp = nn.functional.relu(xmlp)
  78. if self.dropout != 0:
  79. xmlp = nn.functional.dropout(xmlp, p=self.dropout, training=self.training)
  80. x = torch.cat((xmf, xmlp), dim=1)
  81. x = self.final(x)
  82. if sigmoid:
  83. x = torch.sigmoid(x)
  84. return x