def __call__(self, annotion_list, back_word_list, p): """ Calculate the annotion and back word value :param annotion_list: :param back_word_list: :param p: hidden value :return: """ batch_size = p.data.shape[0] exponential_list = [] sum_exponential = XP.fzeros((batch_size, 1)) # Calculate the total value list and total value # Prepare the Convoluation for annotion, back_word in zip(annotion_list, back_word_list): weight = functions.tanh( self.annotion_weight(annotion) + self.back_weight(back_word) + self.pw(p)) exponential = functions.exp(self.weight_exponential(weight)) exponential_list.append(exponential) sum_exponential += exponential ZEROS = XP.fzeros((batch_size, self.hidden_size)) annotion_value = ZEROS back_word_value = ZEROS # Calculate the Convolution Value each annotion and back word for annotion, back_word, exponential in zip(annotion_list, back_word_list, exponential_list): exponential /= sum_exponential annotion_value += functions.reshape( functions.batch_matmul(annotion, exponential), (batch_size, self.hidden_size)) back_word_value += functions.reshape( functions.batch_matmul(back_word, exponential), (batch_size, self.hidden_size)) return annotion_value, back_word_value
def __call__(self, annotion_list, back_word_list, p): """ Calculate the annotion and back word value :param annotion_list: :param back_word_list: :param p: hidden value :return: """ batch_size = p.data.shape[0] exponential_list = [] sum_exponential = XP.fzeros((batch_size, 1)) # Calculate the total value list and total value # Prepare the Convoluation for annotion, back_word in zip(annotion_list, back_word_list): weight = functions.tanh(self.annotion_weight(annotion) + self.back_weight(back_word) + self.pw(p)) exponential = functions.exp(self.weight_exponential(weight)) exponential_list.append(exponential) sum_exponential += exponential ZEROS = XP.fzeros((batch_size, self.hidden_size)) annotion_value = ZEROS back_word_value = ZEROS # Calculate the Convolution Value each annotion and back word for annotion, back_word, exponential in zip(annotion_list, back_word_list, exponential_list): exponential /= sum_exponential annotion_value += functions.reshape(functions.batch_matmul(annotion, exponential), (batch_size, self.hidden_size)) back_word_value += functions.reshape(functions.batch_matmul(back_word, exponential), (batch_size, self.hidden_size)) return annotion_value, back_word_value