Skip to content

Commit 82b83b8

Browse files
author
shixiaowen03
committed
Deep Knowledge-aware network
1 parent 3892bd7 commit 82b83b8

3 files changed

Lines changed: 88 additions & 48 deletions

File tree

.idea/workspace.xml

Lines changed: 52 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

recommendation/Basic-DKN-Demo/data_loader.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ def load_data(args):
1717
return train_data,test_data
1818

1919

20-
21-
22-
2320
def read(file):
2421
df = pd.read_table(file,sep='\t',header=None,names=['user_id','news_words','news_entities','label'])
2522
df['news_words'] = df['news_words'].map(lambda x:[int(i) for i in x.split(",")])

recommendation/Basic-DKN-Demo/dkn.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,51 @@ def _build_model(self,args):
5252

5353

5454

55-
def _attention(self,args):
56-
clicked_words = tf.reshape(self.clicked_words,shape=[-1,args.max_title_length])
57-
clicked_entities = tf.reshape(self.clicked_entities
58-
,shape=[-1,args.max_title_length])
59-
with tf.variable_scope('kcnn',reuse=tf.AUTO_REUSE):
60-
clicked_embeddings = self._kcnn(clicked_words,clicked_entities,args)
61-
news_embeddings = self._kcnn(self.news_words,self.news_entities,args)
55+
def _attention(self, args):
56+
# (batch_size * max_click_history, max_title_length)
57+
clicked_words = tf.reshape(self.clicked_words, shape=[-1, args.max_title_length])
58+
clicked_entities = tf.reshape(self.clicked_entities, shape=[-1, args.max_title_length])
6259

63-
clicked_embeddings = tf.reshape(clicked_embeddings,shape=[-1,args.max_click_history,args.n_filters * len(args.filter_sizes)])
60+
with tf.variable_scope('kcnn', reuse=tf.AUTO_REUSE): # reuse the variables of KCNN
61+
# (batch_size * max_click_history, title_embedding_length)
62+
# title_embedding_length = n_filters_for_each_size * n_filter_sizes
63+
clicked_embeddings = self._kcnn(clicked_words, clicked_entities, args)
6464

65+
# (batch_size, title_embedding_length)
66+
news_embeddings = self._kcnn(self.news_words, self.news_entities, args)
67+
68+
# (batch_size, max_click_history, title_embedding_length)
69+
clicked_embeddings = tf.reshape(
70+
clicked_embeddings, shape=[-1, args.max_click_history, args.n_filters * len(args.filter_sizes)])
71+
72+
# (batch_size, 1, title_embedding_length)
6573
news_embeddings_expanded = tf.expand_dims(news_embeddings, 1)
6674

67-
attention_weights = tf.reduce_sum(clicked_embeddings * news_embeddings_expanded,axis=-1)
68-
attnetion_weights = tf.nn.softmax(attention_weights,dim=-1)
69-
attention_weights_expanded = tf.expand_dims(attention_weights,axis=-1)
70-
user_embeddings = tf.reduce_sum(clicked_embeddings * attention_weights_expanded,axis=1)
75+
# (batch_size, max_click_history)
76+
attention_weights = tf.reduce_sum(clicked_embeddings * news_embeddings_expanded, axis=-1)
77+
78+
# (batch_size, max_click_history)
79+
attention_weights = tf.nn.softmax(attention_weights, dim=-1)
80+
81+
# (batch_size, max_click_history, 1)
82+
attention_weights_expanded = tf.expand_dims(attention_weights, axis=-1)
83+
84+
# (batch_size, title_embedding_length)
85+
user_embeddings = tf.reduce_sum(clicked_embeddings * attention_weights_expanded, axis=1)
7186

72-
return user_embeddings,news_embeddings
87+
return user_embeddings, news_embeddings
7388

7489

7590

7691

7792
def _kcnn(self,words,entities,args):
93+
# (batch_size * max_click_history, max_title_length, word_dim) for users
94+
# (batch_size, max_title_length, word_dim) for news
7895
embedded_words = tf.nn.embedding_lookup(self.word_embeddings,words)
7996
embedded_entities = tf.nn.embedding_lookup(self.entity_embeddings,entities)
8097

98+
# (batch_size * max_click_history, max_title_length, full_dim) for users
99+
# (batch_size, max_title_length, full_dim) for news
81100
if args.use_context:
82101
embedded_contexts = tf.nn.embedding_lookup(self.context_embeddings,entities)
83102
concat_input = tf.concat([embedded_words,embedded_entities,embedded_contexts],axis=-1)
@@ -86,6 +105,8 @@ def _kcnn(self,words,entities,args):
86105
concat_input = tf.concat([embedded_words,embedded_entities],axis=-1)
87106
full_dim = args.word_dim + args.entity_dim
88107

108+
# (batch_size * max_click_history, max_title_length, full_dim, 1) for users
109+
# (batch_size, max_title_length, full_dim, 1) for news
89110
concat_input = tf.expand_dims(concat_input,-1)
90111

91112
outputs = []
@@ -107,8 +128,8 @@ def _kcnn(self,words,entities,args):
107128
strides=[1, 1, 1, 1], padding='VALID', name='pool')
108129
outputs.append(pool)
109130

110-
# (batch_size * max_click_history, 1, 1, n_filters_for_each_size * n_filter_sizes) for users
111-
# (batch_size, 1, 1, n_filters_for_each_size * n_filter_sizes) for news
131+
# (batch_size * max_click_history, 1, 1, n_filters_for_each_size * n_filter_sizes) for users
132+
# (batch_size, 1, 1, n_filters_for_each_size * n_filter_sizes) for news
112133
output = tf.concat(outputs, axis=-1)
113134

114135
# (batch_size * max_click_history, n_filters_for_each_size * n_filter_sizes) for users

0 commit comments

Comments
 (0)