def build(self,input_shapes): input_shape=input_shapes[0] assert len(input_shape)==3 input_dim=input_shape[2] self.input_batch=input_shape[0] self.input_num=input_shape[1] self.W_c=self.init((input_dim,self.output_dim),name='{}_W_c'.format(self.name)) self.b_c=K.zeros((self.output_dim,),name='{}_b'.format(self.name)) self.W_m=self.init((input_dim,self.mem_vector_dim),name='{}_W_c'.format(self.name)) self.b_m=K.zeros((self.mem_vector_dim,),name='{}_b'.format(self.name)) #可训练参数 self.trainable_weights=[self.W_c,self.W_m,self.b_c,self.b_m]
def build(self, input_shapes): input_shape = input_shapes[0] assert len(input_shape) == 3 input_dim = input_shape[2] self.input_batch = input_shape[0] self.input_num = input_shape[1] self.W_c = self.init((input_dim, self.output_dim), name='{}_W_c'.format(self.name)) self.b_c = K.zeros((self.output_dim, ), name='{}_b'.format(self.name)) self.W_m = self.init((input_dim, self.mem_vector_dim), name='{}_W_c'.format(self.name)) self.b_m = K.zeros((self.mem_vector_dim, ), name='{}_b'.format(self.name)) #可训练参数 self.trainable_weights = [self.W_c, self.W_m, self.b_c, self.b_m]
def build(self, input_shapes): ''' build方法初始化权重矩阵 U_a: x到attention输出的权值矩阵 U_m: attention_vec到attention输出的取值矩阵 U_s: attention输出到softmax输出的权重矩阵 ''' input_shape = input_shapes[0] super(AttentionLSTM, self).build(input_shape) self.input_spec = [ InputSpec(shape=input_shapes[0]), InputSpec(shape=input_shapes[1]) ] #attention_dim=self.input_spec[1].shape[1] attention_dim = self.att_dim input_dim = input_shape[2] #attention参数 self.U_a = self.inner_init((input_dim, self.output_dim), name='{}_U_a'.format(self.name)) self.b_a = K.zeros((self.output_dim, ), name='{}_b_a'.format(self.name)) self.U_m = self.inner_init((attention_dim, self.output_dim), name='{}_U_m'.format(self.name)) self.b_m = K.zeros((self.output_dim, ), name='{}_b_m'.format(self.name)) if self.single_attention_param: self.U_s = self.inner_init((self.output_dim, 1), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((1, ), name='{}_b_s'.format(self.name)) else: self.U_s = self.inner_init((self.output_dim, self.output_dim), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((self.output_dim, ), name='{}_b_s'.format(self.name)) self.trainable_weights += [ self.U_a, self.U_m, self.U_s, self.b_a, self.b_m, self.b_s ] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights
def build(self, input_shape): ''' build方法初始化权重矩阵 U_a: LSTM层输出到attention输出的权值矩阵 U_m: attention_vec到attention输出的取值矩阵 U_s: attention输出到softmax输出的权重矩阵 ''' super(AttentionLSTM, self).build(input_shape) if hasattr(self.attention_vec, '_keras_shape'): attention_dim = self.attention_vec._keras_shape[1] else: raise Exception( 'Layer could not be build: No information about expected input shape.' ) attention_dim = self.attention_vec._keras_shape[1] #attention参数 self.U_a = self.inner_init((self.output_dim, self.output_dim), name='{}_U_a'.format(self.name)) self.b_a = K.zeros((self.output_dim, ), name='{}_b_a'.format(self.name)) self.U_m = self.inner_init((attention_dim, self.output_dim), name='{}_U_m'.format(self.name)) self.b_m = K.zeros((self.output_dim, ), name='{}_b_m'.format(self.name)) if self.single_attention_param: self.U_s = self.inner_init((self.output_dim, 1), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((1, ), name='{}_b_s'.format(self.name)) else: self.U_s = self.inner_init((self.output_dim, self.output_dim), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((self.output_dim, ), name='{}_b_s'.format(self.name)) self.trainable_weights += [ self.U_a, self.U_m, self.U_s, self.b_a, self.b_m, self.b_s ] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights
def build(self,input_shapes): ''' build方法初始化权重矩阵 U_a: x到attention输出的权值矩阵 U_m: attention_vec到attention输出的取值矩阵 U_s: attention输出到softmax输出的权重矩阵 ''' input_shape=input_shapes[0] super(AttentionLSTM,self).build(input_shape) self.input_spec = [InputSpec(shape=input_shapes[0]),InputSpec(shape=input_shapes[1])] #attention_dim=self.input_spec[1].shape[1] attention_dim=self.att_dim input_dim = input_shape[2] #attention参数 self.U_a=self.inner_init((input_dim,self.output_dim), name='{}_U_a'.format(self.name)) self.b_a=K.zeros((self.output_dim,),name='{}_b_a'.format(self.name)) self.U_m=self.inner_init((attention_dim,self.output_dim), name='{}_U_m'.format(self.name)) self.b_m=K.zeros((self.output_dim,),name='{}_b_m'.format(self.name)) if self.single_attention_param: self.U_s = self.inner_init((self.output_dim, 1), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((1,), name='{}_b_s'.format(self.name)) else: self.U_s = self.inner_init((self.output_dim, self.output_dim), name='{}_U_s'.format(self.name)) self.b_s = K.zeros((self.output_dim,), name='{}_b_s'.format(self.name)) self.trainable_weights+=[self.U_a,self.U_m,self.U_s, self.b_a,self.b_m,self.b_s] if self.initial_weights is not None: self.set_weights(self.initial_weights) del self.initial_weights