Coverage for hopwise/model/context_aware_recommender/fwfm.py: 87%

70 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-09-30 13:25 +0000

1# @Time : 2020/10/06 

2# @Author : Xinyan Fan 

3# @Email : xinyan.fan@ruc.edu.cn 

4# @File : fwfm.py 

5 

6r"""FwFM 

7##################################################### 

8Reference: 

9 Junwei Pan et al. "Field-weighted Factorization Machines for Click-Through Rate Prediction in Display Advertising." 

10 in WWW 2018. 

11""" 

12 

13import torch 

14from torch import nn 

15from torch.nn.init import constant_, xavier_normal_ 

16 

17from hopwise.model.abstract_recommender import ContextRecommender 

18 

19 

20class FwFM(ContextRecommender): 

21 r"""FwFM is a context-based recommendation model. It aims to model the different feature interactions 

22 between different fields in a much more memory-efficient way. It proposes a field pair weight matrix 

23 :math:`r_{F(i),F(j)}`, to capture the heterogeneity of field pair interactions. 

24 

25 The model defines as follows: 

26 

27 .. math:: 

28 y = w_0 + \sum_{i=1}^{m}x_{i}w_{i} + \sum_{i=1}^{m}\sum_{j=i+1}^{m}x_{i}x_{j}<v_{i}, v_{j}>r_{F(i),F(j)} 

29 """ 

30 

31 def __init__(self, config, dataset): 

32 super().__init__(config, dataset) 

33 

34 # load parameters info 

35 self.dropout_prob = config["dropout_prob"] 

36 self.fields = config["fields"] # a dict; key: field_id; value: feature_list 

37 

38 self.num_features = self.num_feature_field 

39 

40 self.dropout_layer = nn.Dropout(p=self.dropout_prob) 

41 self.sigmoid = nn.Sigmoid() 

42 

43 self.feature2id = {} 

44 self.feature2field = {} 

45 

46 self.feature_names = ( 

47 self.token_field_names, 

48 self.token_seq_field_names, 

49 self.float_field_names, 

50 ) 

51 self.feature_dims = ( 

52 self.token_field_dims, 

53 self.token_seq_field_dims, 

54 self.float_field_dims, 

55 ) 

56 self._get_feature2field() 

57 self.num_fields = len(set(self.feature2field.values())) # the number of fields 

58 self.num_pair = self.num_fields * self.num_fields 

59 self.weight = torch.randn(self.num_fields, self.num_fields, 1, requires_grad=True, device=self.device) 

60 self.loss = nn.BCEWithLogitsLoss() 

61 

62 # parameters initialization 

63 self.apply(self._init_weights) 

64 

65 def _init_weights(self, module): 

66 if isinstance(module, nn.Embedding): 

67 xavier_normal_(module.weight.data) 

68 elif isinstance(module, nn.Linear): 

69 xavier_normal_(module.weight.data) 

70 if module.bias is not None: 

71 constant_(module.bias.data, 0) 

72 

73 def _get_feature2field(self): 

74 r"""Create a mapping between features and fields.""" 

75 fea_id = 0 

76 for names in self.feature_names: 

77 if names is not None: 

78 for name in names: 

79 self.feature2id[name] = fea_id 

80 fea_id += 1 

81 

82 if self.fields is None: 

83 field_id = 0 

84 for key, value in self.feature2id.items(): 

85 self.feature2field[self.feature2id[key]] = field_id 

86 field_id += 1 

87 else: 

88 for key, value in self.fields.items(): 

89 for v in value: 

90 try: 

91 self.feature2field[self.feature2id[v]] = key 

92 except IndexError: 

93 pass 

94 

95 def fwfm_layer(self, infeature): 

96 r"""Get the field pair weight matrix r_{F(i),F(j)}, and model the different interaction strengths of 

97 different field pairs :math:`\sum_{i=1}^{m}\sum_{j=i+1}^{m}x_{i}x_{j}<v_{i}, v_{j}>r_{F(i),F(j)}`. 

98 

99 Args: 

100 infeature (torch.cuda.FloatTensor): [batch_size, field_size, embed_dim] 

101 

102 Returns: 

103 torch.cuda.FloatTensor: [batch_size, 1] 

104 """ 

105 # get r(Fi, Fj) 

106 batch_size = infeature.shape[0] 

107 weight = self.weight.expand(batch_size, -1, -1, -1) 

108 

109 fwfm_inter = list() # [batch_size, num_fields, emb_dim] 

110 for i in range(self.num_features - 1): 

111 for j in range(i + 1, self.num_features): 

112 Fi, Fj = self.feature2field[i], self.feature2field[j] 

113 fwfm_inter.append(infeature[:, i] * infeature[:, j] * weight[:, Fi, Fj]) 

114 fwfm_inter = torch.stack(fwfm_inter, dim=1) 

115 fwfm_inter = torch.sum(fwfm_inter, dim=1) # [batch_size, emb_dim] 

116 fwfm_inter = self.dropout_layer(fwfm_inter) 

117 

118 fwfm_output = torch.sum(fwfm_inter, dim=1, keepdim=True) # [batch_size, 1] 

119 

120 return fwfm_output 

121 

122 def forward(self, interaction): 

123 fwfm_all_embeddings = self.concat_embed_input_fields(interaction) # [batch_size, num_field, embed_dim] 

124 

125 output = self.first_order_linear(interaction) + self.fwfm_layer(fwfm_all_embeddings) 

126 

127 return output.squeeze(-1) 

128 

129 def calculate_loss(self, interaction): 

130 label = interaction[self.LABEL] 

131 

132 output = self.forward(interaction) 

133 return self.loss(output, label) 

134 

135 def predict(self, interaction): 

136 return self.sigmoid(self.forward(interaction))