Skip to content

Commit

Permalink
fix bug in #128
Browse files Browse the repository at this point in the history
  • Loading branch information
zanshuxun committed Oct 23, 2020
1 parent 7996548 commit 61b5775
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion deepctr_torch/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,6 @@ def get_dense_input(X, features, feature_columns):

def maxlen_lookup(X, sparse_input_dict, maxlen_column):
if maxlen_column is None or len(maxlen_column)==0:
raise ValueError('please add max length column for VarLenSparseFeat of DIEN input')
raise ValueError('please add max length column for VarLenSparseFeat of DIN/DIEN input')
lookup_idx = np.array(sparse_input_dict[maxlen_column[0]])
return X[:, lookup_idx[0]:lookup_idx[1]].long()
18 changes: 10 additions & 8 deletions deepctr_torch/models/din.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ def __init__(self, dnn_feature_columns, history_feature_list, dnn_use_bn=False,


def forward(self, X):
sparse_embedding_list, dense_value_list = self.input_from_feature_columns(X, self.dnn_feature_columns,
self.embedding_dict)
_, dense_value_list = self.input_from_feature_columns(X, self.dnn_feature_columns, self.embedding_dict)

# sequence pooling part
query_emb_list = embedding_lookup(X, self.embedding_dict, self.feature_index, self.sparse_feature_columns,
self.history_feature_list, self.history_feature_list, to_list=True)
return_feat_list=self.history_feature_list, to_list=True)
keys_emb_list = embedding_lookup(X, self.embedding_dict, self.feature_index, self.history_feature_columns,
self.history_fc_names, self.history_fc_names, to_list=True)
dnn_input_emb_list = embedding_lookup(X, self.embedding_dict, self.feature_index, self.sparse_feature_columns, mask_feat_list=self.history_feature_list, to_list=True)

return_feat_list=self.history_fc_names, to_list=True)
dnn_input_emb_list = embedding_lookup(X, self.embedding_dict, self.feature_index, self.sparse_feature_columns,
to_list=True)

sequence_embed_dict = varlen_embedding_lookup(X, self.embedding_dict, self.feature_index,
self.sparse_varlen_feature_columns)
Expand All @@ -97,12 +96,15 @@ def forward(self, X):
self.sparse_varlen_feature_columns, self.device)

dnn_input_emb_list += sequence_embed_list
deep_input_emb = torch.cat(dnn_input_emb_list, dim=-1)

# concatenate
query_emb = torch.cat(query_emb_list, dim=-1) # [B, 1, E]
keys_emb = torch.cat(keys_emb_list, dim=-1) # [B, T, E]
keys_length = torch.ones((query_emb.size(0), 1)).to(self.device) # [B, 1]
deep_input_emb = torch.cat(dnn_input_emb_list, dim=-1)

keys_length_feature_name = [feat.length_name for feat in self.varlen_sparse_feature_columns if
feat.length_name is not None]
keys_length = torch.squeeze(maxlen_lookup(X, self.feature_index, keys_length_feature_name), 1) # [B, 1]

hist = self.attention(query_emb, keys_emb, keys_length) # [B, 1, E]

Expand Down
11 changes: 6 additions & 5 deletions examples/run_din.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ def get_xy_fd():
SparseFeat('item', 3 + 1, embedding_dim=8), SparseFeat('item_gender', 2 + 1, embedding_dim=8),
DenseFeat('score', 1)]

feature_columns += [VarLenSparseFeat(SparseFeat('hist_item', 3 + 1, embedding_dim=8), 4),
VarLenSparseFeat(SparseFeat('hist_item_gender', 2 + 1, embedding_dim=8), 4)]

feature_columns += [VarLenSparseFeat(SparseFeat('hist_item', 3 + 1, embedding_dim=8), 4, length_name="seq_length"),
VarLenSparseFeat(SparseFeat('hist_item_gender', 2 + 1, embedding_dim=8), 4, length_name="seq_length")]
behavior_feature_list = ["item", "item_gender"]
uid = np.array([0, 1, 2])
ugender = np.array([0, 1, 0])
Expand All @@ -26,9 +25,11 @@ def get_xy_fd():

hist_iid = np.array([[1, 2, 3, 0], [1, 2, 3, 0], [1, 2, 0, 0]])
hist_igender = np.array([[1, 1, 2, 0], [2, 1, 1, 0], [2, 1, 0, 0]])
behavior_length = np.array([3, 3, 2])

feature_dict = {'user': uid, 'gender': ugender, 'item': iid, 'item_gender': igender,
'hist_item': hist_iid, 'hist_item_gender': hist_igender, 'score': score}
'hist_item': hist_iid, 'hist_item_gender': hist_igender, 'score': score,
"seq_length": behavior_length}
x = {name: feature_dict[name] for name in get_feature_names(feature_columns)}
y = np.array([1, 0, 1])

Expand All @@ -43,7 +44,7 @@ def get_xy_fd():
print('cuda ready...')
device = 'cuda:0'

model = DIN(feature_columns, behavior_feature_list, device=device)
model = DIN(feature_columns, behavior_feature_list, device=device, att_weight_normalization=True)
model.compile('adagrad', 'binary_crossentropy',
metrics=['binary_crossentropy'])
history = model.fit(x, y, batch_size=3, epochs=10, verbose=2, validation_split=0.0)

0 comments on commit 61b5775

Please sign in to comment.