def doc_embbedding(self, sent_topic_emb, seqLen, scope=None): with tf.variable_scope(scope or "sent_topic_embedding"): if self.cfg.doc_encode == "bigru": birnn_sent = biGRU(sent_topic_emb, self.cfg.sent_hidden, seqLen) elif self.cfg.doc_encode == 'bilstm': birnn_sent = biLSTM(sent_topic_emb, self.cfg.sent_hidden, seqLen) else: raise ValueError("no such encoder %s" %(self.cfg.doc_encode)) doc_emb = mask_attention(birnn_sent, self.cfg.atten_size, seqLen) # (b_sz, sent_hidden * 2) return doc_emb
def sent_encode(self, user_word_emb, scope=None): with tf.variable_scope(scope or "sent_encode"): if self.cfg.lstm_sent_encode == 'bigru': sent_emb = biGRU(user_word_emb, self.cfg.word_hidden, self.ph_sNum) sent_emb = avg_pooling(sent_emb, self.cfg.maxseq, self.cfg.word_hidden * 2) elif self.cfg.lstm_sent_encode == 'bilstm': sent_emb = biLSTM(user_word_emb, self.cfg.word_hidden, self.ph_sNum) sent_emb = avg_pooling(sent_emb, self.cfg.maxseq, self.cfg.word_hidden * 2) else: raise ValueError("no such sent encode %s" % (self.cfg.lstm_sent_encode)) return sent_emb
def topic_embedding(self, topic_word_emb, scope=None): # topic_word_emb = tf.expand_dims(topic_word_emb, axis=0) with tf.variable_scope(scope or "topic_encode"): if self.cfg.topic_encode == "bigru": topic_seqLen = get_length(topic_word_emb) topic_seqLen = tf.reshape(topic_seqLen, [-1, ]) birnn_x = biGRU(topic_word_emb, self.cfg.word_hidden, topic_seqLen) elif self.cfg.topic_encode == 'bilstm': topic_seqLen = get_length(topic_word_emb) topic_seqLen = tf.reshape(topic_seqLen, [-1, ]) birnn_x = biLSTM(topic_word_emb, self.cfg.word_hidden, topic_seqLen) else: raise ValueError("no such encoder %s" %(self.cfg.rnn_encode)) topic_emb = avg_pooling(birnn_x, self.cfg.topic_num, self.cfg.word_hidden * 2) # shape: (1, topic, word_hidden) # topic_emb = mask_attention(birnn_x, self.cfg.topic_num, self.cfg.word_hidden * 2, self.cfg.atten_size, topic_seqLen) # sequence_length, dim, attn_size, seqLen topic_emb = tf.reshape(topic_emb, [self.cfg.topic_num, self.cfg.word_hidden * 2]) # shape: (topic_num, word_hidden) return topic_emb
def sent_encode(self, user_word_emb, scope=None): # b_sz, ststp, wtstp, emb_sz = tf.unstack(tf.shape(user_word_emb)) with tf.variable_scope(scope or "sent_encode"): sent_word = tf.reshape(user_word_emb, [-1, self.cfg.maxword, self.cfg.emb_size]) sent_wNUm = tf.reshape(self.ph_wNum, [-1, ]) if self.cfg.ute_sent_encode == "cnn": sent_emb = cnn_layer(sent_word, self.cfg.emb_size, filter_sizes=self.cfg.filter_sizes, num_filters=self.cfg.num_filters) elif self.cfg.ute_sent_encode == 'bigru': sent_emb = biGRU(sent_word,self.cfg.word_hidden, sent_wNUm) sent_emb = mask_attention(sent_emb, self.cfg.maxword, self.cfg.word_hidden * 2, self.cfg.atten_size, sent_wNUm) elif self.cfg.ute_sent_encode == 'bilstm': sent_emb = biLSTM(sent_word, self.cfg.word_hidden, sent_wNUm) sent_emb = mask_attention(sent_emb, self.cfg.maxword, self.cfg.word_hidden * 2, self.cfg.atten_size, sent_wNUm) else: raise ValueError("no such sent encode %s" % (self.cfg.ute_sent_encode)) return sent_emb
def sent_encode(self, user_word_emb, scope=None): with tf.variable_scope(scope or "sent_encode"): sent_word = tf.reshape(user_word_emb, [-1, self.cfg.maxword, self.cfg.emb_size]) sent_wNUm = tf.reshape(self.ph_wNum, [ -1, ]) if self.cfg.han_sent_encode == 'bigru': sent_emb = biGRU(sent_word, self.cfg.word_hidden, sent_wNUm) elif self.cfg.han_sent_encode == 'bilstm': sent_emb = biLSTM(sent_word, self.cfg.word_hidden, sent_wNUm) else: raise ValueError("no such sent encode %s" % (self.cfg.han_sent_encode)) sent_emb = mask_attention(sent_emb, self.cfg.maxword, self.cfg.word_hidden * 2, self.cfg.atten_size, sent_wNUm) # attention mechanism return sent_emb