def _build_graph(self): with tf.name_scope('inputs'): sentence1, sentence1_lengths, sentence2, \ sentence2_lengths, labels, is_training = self._build_inputs() self._word_embedding = self.make_word_embedding() sentence1_embed, sentence2_embed, sentence1_rnned, sentence2_rnned = \ self._build_rnn_encoder( sentence1, sentence2, sentence1_lengths, sentence2_lengths ) with tf.name_scope('attend'): sentence1_attend, sentence2_attend, att_weights1, att_weights2 = \ decom_ops.attend( sentence1_rnned, sentence2_rnned, sentence1_lengths, sentence2_lengths, is_training=is_training ) with tf.name_scope('compare'): compare1, compare2 = decom_ops.compare(sentence1_embed, sentence2_embed, sentence1_attend, sentence2_attend, is_training=is_training) compare_dim = self.config['rnn']['state_size'] * 2 num_category = self.config['data']['num_category'] with tf.name_scope('aggregate'): result = decom_ops.aggregate( compare1, compare2, sentence1_lengths, sentence2_lengths, mapper_num_layers=[compare_dim // 2, num_category], is_training=is_training) with tf.name_scope('summary_viz'): attentions = [ (att_weights1, 'attention_weight1', sentence2_lengths), (att_weights2, 'attention_weight2', sentence1_lengths) ] for attention_info in attentions: self._build_attention_viz(*attention_info) self.loss = self._build_loss(result, labels) self.inference_probs = tf.nn.softmax(result) self.inference = tf.argmax(self.inference_probs, axis=-1) self.train_step, self.train_op = self._build_train_step(self.loss) self.summary_op = tf.summary.merge_all()
def _build_graph(self): with tf.name_scope('inputs'): sentence1, sentence1_lengths, \ sentence2_pos, sentence2_pos_lengths,\ sentence2_neg, sentence2_neg_lengths,\ is_training = self._build_inputs() self._word_embedding = self.make_word_embedding() with tf.name_scope('rnn_encode'): sentence1_embed, sentence2_pos_embed, sentence2_neg_embed, \ sentence1_rnned, sentence2_pos_rnned, sentence2_neg_rnned = \ self._build_rnn_encoder( sentence1, sentence2_pos, sentence2_neg, sentence1_lengths, sentence2_pos_lengths, sentence2_neg_lengths) with tf.name_scope('attend'): sentence1_pos_attend, sentence2_pos_attend, \ pos_att_weights1, pos_att_weights2 = \ decom_ops.attend( sentence1_rnned, sentence2_pos_rnned, sentence1_lengths, sentence2_pos_lengths, is_training=is_training) sentence1_neg_attend, sentence2_neg_attend, \ neg_att_weights1, neg_att_weights2 = \ decom_ops.attend( sentence1_rnned, sentence2_neg_rnned, sentence1_lengths, sentence2_neg_lengths, is_training=is_training, reuse=True) with tf.name_scope('compare'): pos_compare1, pos_compare2 = decom_ops.compare( sentence1_embed, sentence2_pos_embed, sentence1_pos_attend, sentence2_pos_attend, is_training=is_training) neg_compare1, neg_compare2 = decom_ops.compare( sentence1_embed, sentence2_neg_embed, sentence1_neg_attend, sentence2_neg_attend, is_training=is_training, reuse=True) compare_dim = self.config['rnn']['state_size'] + \ self.config['word']['embedding_dim'] // 2 with tf.name_scope('aggregate'): pos_result = decom_ops.aggregate( pos_compare1, pos_compare2, sentence1_lengths, sentence2_pos_lengths, mapper_num_layers=[int(compare_dim * 0.7), 1], is_training=is_training) neg_result = decom_ops.aggregate( neg_compare1, neg_compare2, sentence1_lengths, sentence2_neg_lengths, mapper_num_layers=[int(compare_dim * 0.7), 1], is_training=is_training, reuse=True) pos_result = tf.squeeze(tf.tanh(pos_result), axis=[-1]) neg_result = tf.squeeze(tf.tanh(neg_result), axis=[-1]) with tf.name_scope('loss'): self.loss = self._build_loss(pos_result, neg_result) with tf.name_scope('summary'): tf.summary.histogram('pos_result', pos_result) tf.summary.histogram('neg_result', neg_result) attentions = [ (pos_att_weights1, 'pos_att_weight1', sentence2_pos_lengths), (pos_att_weights2, 'pos_att_weight2', sentence1_lengths), (neg_att_weights1, 'neg_att_weight1', sentence2_neg_lengths), (neg_att_weights2, 'neg_att_weight2', sentence1_lengths) ] for attention_info in attentions: self._build_attention_viz(*attention_info) self.pos_inference = pos_result self.neg_inference = neg_result self.train_step, self.train_op = self._build_train_step(self.loss) self.summary_op = tf.summary.merge_all()