def lstmp_encoder(input_seq, gate_size, h_0, c_0, para_name, proj_size, args): # A lstm encoder implementation with projection. # Linear transformation part for input gate, output gate, forget gate # and cell activation vectors need be done outside of dynamic_lstm. # So the output size is 4 times of gate_size. init = None init_b = None input_proj = layers.fc(input=input_seq, param_attr=fluid.ParamAttr(name=para_name + '_gate_w', initializer=init), size=gate_size * 4, act=None, bias_attr=False) hidden, cell = layers.dynamic_lstmp( input=input_proj, size=gate_size * 4, proj_size=proj_size, h_0=h_0, c_0=c_0, use_peepholes=False, proj_clip=proj_clip, cell_clip=cell_clip, proj_activation="identity", param_attr=fluid.ParamAttr(initializer=init), bias_attr=fluid.ParamAttr(initializer=init_b)) return hidden, cell, input_proj
def test_dynamic_lstmp(self): program = Program() with program_guard(program): hidden_dim, proj_dim = 16, 8 seq_data = layers.data( name='seq_data', shape=[10, 10], dtype='float32', lod_level=1) fc_out = layers.fc(input=seq_data, size=4 * hidden_dim) self.assertIsNotNone( layers.dynamic_lstmp( input=fc_out, size=4 * hidden_dim, proj_size=proj_dim)) print(str(program))
def test_dynamic_lstmp(self): program = Program() with program_guard(program): hidden_dim, proj_dim = 16, 8 seq_data = layers.data( name='seq_data', shape=[10, 10], dtype='float32', lod_level=1) fc_out = layers.fc(input=seq_data, size=4 * hidden_dim) self.assertIsNotNone( layers.dynamic_lstmp( input=fc_out, size=4 * hidden_dim, proj_size=proj_dim)) print(str(program))
def __call__(self, input): return layers.dynamic_lstmp( input=input, size=size, proj_size=proj_size, param_attr=self.attr_holder.param_attr, bias_attr=self.attr_holder.bias_attr, use_peepholes=use_peepholes, is_reverse=is_reverse, gate_activation=gate_activation, cell_activation=cell_activation, candidate_activation=candidate_activation, proj_activation=proj_activation, dtype=dtype, cell_clip=cell_clip, proj_clip=proj_clip)
def lstmp_encoder(input_seq, gate_size, h_0, c_0, para_name, proj_size, test_mode, args): # A lstm encoder implementation with projection. # Linear transformation part for input gate, output gate, forget gate # and cell activation vectors need be done outside of dynamic_lstm. # So the output size is 4 times of gate_size. if args.para_init: init = fluid.initializer.Constant(args.init1) init_b = fluid.initializer.Constant(0.0) else: init = None init_b = None input_seq = dropout(input_seq, test_mode, args) input_proj = layers.fc(input=input_seq, param_attr=fluid.ParamAttr( name=para_name + '_gate_w', initializer=init), size=gate_size * 4, act=None, bias_attr=False) if args.debug: layers.Print(input_seq, message='input_seq', summarize=10) layers.Print(input_proj, message='input_proj', summarize=10) hidden, cell = layers.dynamic_lstmp( input=input_proj, size=gate_size * 4, proj_size=proj_size, h_0=h_0, c_0=c_0, use_peepholes=False, proj_clip=args.proj_clip, cell_clip=args.cell_clip, proj_activation="identity", param_attr=fluid.ParamAttr(initializer=init), bias_attr=fluid.ParamAttr(initializer=init_b)) return hidden, cell, input_proj