def count_alpha(self, context, aspect, output, ctx_bitmap, alpha_adj=None): ''' Count the attention weights. alpha = softmax(tanh(wa*asp + wb*ctx)) Args: :type context: tensor, shape = [batch_size, time_steps, edim] :param context: the input context. :type aspect: tensor, shape = [batch_size, edim] :param aspect: the input aspect. :type ctx_bitmap: tensorflow, shape like context. :param ctx_bitmap: the context's bitmap, use to remove the influence of padding. \ Returns: A tensor. The attention weights of the context. ''' time_steps = tf.shape(context)[1] aspect_3dim = tf.reshape( tf.tile(aspect, [1, time_steps]), [-1, time_steps, self.edim] ) output_3dim = tf.reshape( tf.tile(output, [1, time_steps]), [-1, time_steps, self.edim] ) res_asp = self.line_layer_asp.forward(aspect_3dim) res_ctx = self.line_layer_ctx.forward(context) res_output = self.line_layer_output.forward(output_3dim) res_sum = res_asp + res_ctx + res_output res_act = activer(res_sum, self.active) batch_size = tf.shape(context)[0] w_shp0 = tf.shape(self.wline_ca)[0] w_shp1 = tf.shape(self.wline_ca)[1] w_line_3dim = tf.reshape( tf.tile(self.wline_ca, [batch_size, 1]), [batch_size, w_shp0, w_shp1] ) res_act = tf.reshape( tf.matmul(res_act, w_line_3dim), [-1, time_steps] ) alpha = normalizer(self.norm_type ,res_act, ctx_bitmap, 1) if alpha_adj is not None: alpha += alpha_adj return alpha
def count_alpha(self, context, aspect, ctx_bitmap): ''' count the content attention (weight) ''' mem_size = tf.shape(context)[1] context = context aspect = aspect # adjust attention asp_3dim = tf.reshape(aspect, [-1, self.edim, 1]) # gout = tf.matmul(context, asp_3dim) res_act = self.line_layer.forward(context) res_act = tf.reshape( tf.matmul(res_act, asp_3dim), [-1, mem_size] ) # alpha = tf.nn.softmax(tf.reshape(gout, [-1, mem_size])) alpha = normalizer(self.norm_type, res_act, ctx_bitmap, 1) return alpha