def build_network(self): # Define weights weights = { 'wordEmbedding': tf.Variable(self.qa.word_embedding, trainable=True, name="word_embeddding"), 'crf_W': tf.get_variable("crf_W", initializer=tf.random_uniform( shape=[2 * self.n_hidden, self.n_tags], minval=-0.08, maxval=0.08)), 'crf_b': tf.get_variable("crf_b", initializer=tf.random_uniform(shape=[self.n_tags], minval=-0.08, maxval=0.08)) } self.is_training = tf.placeholder(tf.bool, name="is_training") self.question_ids = tf.placeholder(tf.int32, [None, self.n_steps]) self.x_lens = tf.placeholder(tf.int32, [None]) self.y = tf.placeholder(tf.int32, [None, self.n_steps]) x = tf.nn.embedding_lookup(weights['wordEmbedding'], ids=self.question_ids) query4ner, _ = NN.bi_gru(x, self.x_lens, self.n_hidden, "query4ner", self.keep_prob, self.is_training) crf_x = tf.concat(query4ner, 2) matricized_x_t = tf.reshape( crf_x, [-1, 2 * self.n_hidden]) # maybe it is reasonable but pass it now! matricized_unary_scores = tf.matmul( matricized_x_t, weights['crf_W']) + weights['crf_b'] self.unary_scores = tf.reshape(matricized_unary_scores, [-1, self.n_steps, self.n_tags]) log_likelihood, self.transition_params = tf.contrib.crf.crf_log_likelihood( self.unary_scores, self.y, self.x_lens) self.loss = tf.reduce_mean(-log_likelihood) all_var = tf.global_variables() grads = tf.gradients(self.loss, all_var) grads = [tf.clip_by_value(grad, -10, 10) for grad in grads] self.optimizer = tf.train.MomentumOptimizer( learning_rate=self.learning_rate, momentum=0.9).apply_gradients(zip(grads, all_var)) self.saver = tf.train.Saver()
def subject_network(self, x): ''' :param x: query embedding after look-up :return:subject_score:[batch_size,cand_sub_size] loss_subject:[1] ''' with tf.variable_scope("subject_part"): _, query4subject = NN.bi_gru(x, self.x_lens, self.n_hidden, "bi_gru4subject_query", self.keep_prob, self.is_training) query4subject = tf.concat(query4subject, -1) # [batch,type_len] query4subject = tf.sigmoid( NN.linear(query4subject, self.qa.type_len, name="query_trans_subject")) with tf.variable_scope(tf.get_variable_scope(), reuse=True): self.type_emb_w_martix = tf.get_variable( "query_trans_subjectkernel") self.type_emb_b_martix = tf.get_variable( "query_trans_subjectbias") # [batch_size,emb_size] gold_subject = self.subject_emb[:, 0, :] self.loss_subject = gold_subject * tf.log( tf.clip_by_value( query4subject, 1e-10, 1.0)) + (1 - gold_subject) * tf.log( tf.clip_by_value(1 - query4subject, 1e-10, 1.0)) # [1] loss_subject = tf.reduce_mean( -tf.reduce_sum(self.loss_subject, axis=1), axis=0) with tf.variable_scope("test"): # [batch,1,cand_sub] subject_score = tf.matmul( tf.expand_dims(query4subject, 1), tf.transpose(self.subject_emb, perm=[0, 2, 1])) # [batch,cand_sub,1] subject_score = tf.squeeze(subject_score, 1) # subject_score = tf.transpose(subject_score, [0, 2, 1]) return subject_score, loss_subject
def relation_network(self, x): ''' :param x: query embedding after look-up :return: rel_score:[batch_size,relation_voc_size], loss:[1] ''' with tf.variable_scope("relation_network"): _, query4relation = NN.bi_gru(x, self.x_lens, self.n_hidden, "bi_gru4relation_query", self.keep_prob, self.is_training) # [batch_size,n_hidden*2] query4relation = tf.concat(query4relation, -1) # [batch,relation_voc_size] rel_score = self.matmul_query_relation( query4relation, self.weight['relationEmbedding'], "rel_score") self.only_rel_predict = tf.argmax(rel_score, 1) # [1] loss_relation = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( labels=self.relation_index, logits=rel_score), axis=0) return rel_score, loss_relation