def call(self, inputs, mask=None): #w_c=K.repeat(self.W_c,self.input_num) #w_m=K.repeat(self.W_m,self.input_num) x = inputs[0] mem_vector = inputs[1] c = K.dot(x, self.W_c) + self.b_c #context向量 m = K.dot(x, self.W_m) + self.b_m #memory向量 mem_vec = K.repeat(mem_vector, self.input_num) #与问题进行内积 m = K.sum(m * mem_vec, axis=2, keepdims=False) s = K.softmax(m) #softmax s = K.reshape(s, (-1, self.input_num, 1)) ctx = self.activation(c * s) return ctx #self.activation(ctx)
def call(self,inputs,mask=None): #w_c=K.repeat(self.W_c,self.input_num) #w_m=K.repeat(self.W_m,self.input_num) x=inputs[0] mem_vector=inputs[1] c=K.dot(x,self.W_c)+self.b_c #context向量 m=K.dot(x,self.W_m)+self.b_m #memory向量 mem_vec=K.repeat(mem_vector,self.input_num) #与问题进行内积 m=K.sum(m*mem_vec,axis=2,keepdims=False) s=K.softmax(m) #softmax s=K.reshape(s,(-1,self.input_num,1)) ctx=self.activation(c*s) return ctx#self.activation(ctx)
def cosine(x1,x2): return K.sum(x1*x2,axis=-1)/(K.sqrt(K.sum(x1*x1,axis=-1)*K.sum(x2*x2,axis=-1))+0.0000001) #cos