def predict(self, left, right): """ Forward network """ # embedding layer emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb") left_emb = emb_layer.ops(left) right_emb = emb_layer.ops(right) # Presentation context lstm_layer = layers.DynamicLSTMLayer(self.lstm_dim, "lstm") left_lstm = lstm_layer.ops(left_emb) right_lstm = lstm_layer.ops(right_emb) last_layer = layers.SequenceLastStepLayer() left_last = last_layer.ops(left_lstm) right_last = last_layer.ops(right_lstm) # matching layer if self.task_mode == "pairwise": fc_layer = layers.FCLayer(self.hidden_dim, None, "fc") left_fc = fc_layer.ops(left_last) right_fc = fc_layer.ops(right_last) cos_sim_layer = layers.CosSimLayer() pred = cos_sim_layer.ops(left_fc, right_fc) return left_fc, pred else: concat_layer = layers.ConcatLayer(1) concat = concat_layer.ops([left_last, right_last]) fc_layer = layers.FCLayer(self.hidden_dim, None, "fc") concat_fc = fc_layer.ops(concat) softmax_layer = layers.FCLayer(2, "softmax", "cos_sim") pred = softmax_layer.ops(concat_fc) return left_last, pred
def bi_dynamic_lstm(self, input, hidden_size): """ bi_lstm layer """ fw_in_proj = Linear( input_dim=self.emb_size, output_dim=4 * hidden_size, param_attr=fluid.ParamAttr(name="fw_fc.w"), bias_attr=False) fw_in_proj = fw_in_proj(input) forward = pd_layers.DynamicLSTMLayer( size=4 * hidden_size, is_reverse=False, param_attr=fluid.ParamAttr(name="forward_lstm.w"), bias_attr=fluid.ParamAttr(name="forward_lstm.b")).ops() forward = forward(fw_in_proj) rv_in_proj = Linear( input_dim=self.emb_size, output_dim=4 * hidden_size, param_attr=fluid.ParamAttr(name="rv_fc.w"), bias_attr=False) rv_in_proj = rv_in_proj(input) reverse = pd_layers.DynamicLSTMLayer( 4 * hidden_size, 'lstm' is_reverse=True, param_attr=fluid.ParamAttr(name="reverse_lstm.w"), bias_attr=fluid.ParamAttr(name="reverse_lstm.b")).ops() reverse = reverse(rv_in_proj) return [forward, reverse]
def __init__(self, conf_dict): """ initialize """ super(LSTM,self).__init__() self.dict_size = conf_dict["dict_size"] self.task_mode = conf_dict["task_mode"] self.emb_dim = conf_dict["net"]["emb_dim"] self.lstm_dim = conf_dict["net"]["lstm_dim"] self.hidden_dim = conf_dict["net"]["hidden_dim"] self.emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb").ops() self.lstm_layer = layers.DynamicLSTMLayer(self.lstm_dim, "lstm").ops() self.fc_layer = layers.FCLayer(self.hidden_dim, None, "fc").ops() self.softmax_layer = layers.FCLayer(2, "softmax", "cos_sim").ops() self.proj_layer = Linear(input_dim = self.hidden_dim, output_dim=self.lstm_dim*4) self.seq_len = conf_dict["seq_len"]
def __init__(self, config): """ initialize """ super(MMDNN, self).__init__() self.vocab_size = int(config['dict_size']) self.emb_size = int(config['net']['embedding_dim']) self.lstm_dim = int(config['net']['lstm_dim']) self.kernel_size = int(config['net']['num_filters']) self.win_size1 = int(config['net']['window_size_left']) self.win_size2 = int(config['net']['window_size_right']) self.dpool_size1 = int(config['net']['dpool_size_left']) self.dpool_size2 = int(config['net']['dpool_size_right']) self.hidden_size = int(config['net']['hidden_size']) self.seq_len = int(config["seq_len"]) self.seq_len1 = self.seq_len #int(config['max_len_left']) self.seq_len2 = self.seq_len #int(config['max_len_right']) self.task_mode = config['task_mode'] self.zero_pad = True self.scale = False if int(config['match_mask']) != 0: self.match_mask = True else: self.match_mask = False if self.task_mode == "pointwise": self.n_class = int(config['n_class']) self.out_size = self.n_class elif self.task_mode == "pairwise": self.out_size = 1 else: logging.error("training mode not supported") # layers self.emb_layer = pd_layers.EmbeddingLayer( self.vocab_size, self.emb_size, name="word_embedding", padding_idx=(0 if self.zero_pad else None)).ops() self.fw_in_proj = Linear(input_dim=self.emb_size, output_dim=4 * self.lstm_dim, param_attr=fluid.ParamAttr(name="fw_fc.w"), bias_attr=False) self.lstm_layer = pd_layers.DynamicLSTMLayer(self.lstm_dim, "lstm").ops() self.rv_in_proj = Linear(input_dim=self.emb_size, output_dim=4 * self.lstm_dim, param_attr=fluid.ParamAttr(name="rv_fc.w"), bias_attr=False) self.reverse_layer = pd_layers.DynamicLSTMLayer(self.lstm_dim, is_reverse=True).ops() self.conv = Conv2D(num_channels=1, num_filters=self.kernel_size, stride=1, padding=(int(self.seq_len1 / 2), int(self.seq_len2 // 2)), filter_size=(self.seq_len1, self.seq_len2), bias_attr=fluid.ParamAttr( initializer=fluid.initializer.Constant(0.1))) self.pool_layer = Pool2D(pool_size=[ int(self.seq_len1 / self.dpool_size1), int(self.seq_len2 / self.dpool_size2) ], pool_stride=[ int(self.seq_len1 / self.dpool_size1), int(self.seq_len2 / self.dpool_size2) ], pool_type="max") self.fc_layer = pd_layers.FCLayer(self.hidden_size, "tanh", "fc").ops() self.fc1_layer = pd_layers.FCLayer(self.out_size, "softmax", "fc1").ops()