def build_model(self, inputs, outputs): """ build_model. Args: inputs: tensor, input of model outputs: tensor, output of model Returns: None """ embed_char = outputs[0] embed_word = outputs[1] if self.wclstm_embed_type == "ATTNENTION": x_word = L.TimeDistributed( SelfAttention(K.int_shape(embed_word)[-1]))(embed_word) x_word_shape = K.int_shape(x_word) x_word = L.Reshape(target_shape=(x_word_shape[:2], x_word_shape[2] * x_word_shape[3])) x_word = L.Dense(self.embed_size, activation=self.activate_mid)(x_word) # elif self.wclstm_embed_type == "SHORT": else: x_word = L.Lambda(lambda x: x[:, :, 0, :])(embed_word) outputs_concat = L.Concatenate(axis=-1)([embed_char, x_word]) # LSTM or GRU if self.rnn_type == "LSTM": rnn_cell = L.LSTM elif self.rnn_type == "CuDNNLSTM": rnn_cell = L.CuDNNLSTM elif self.rnn_type == "CuDNNGRU": rnn_cell = L.CuDNNGRU else: rnn_cell = L.GRU # Bi-LSTM-CRF for nrl in range(self.num_rnn_layers): x = L.Bidirectional( rnn_cell( units=self.rnn_unit * (nrl + 1), return_sequences=True, activation=self.activate_mid, ))(outputs_concat) outputs = L.Dropout(self.dropout)(x) if self.use_crf: x = L.Dense(units=self.label, activation=self.activate_end)(outputs) self.CRF = ConditionalRandomField(self.crf_lr_multiplier, name="crf_bert4keras") self.outputs = self.CRF(x) self.trans = K.eval(self.CRF.trans).tolist() self.loss = self.CRF.dense_loss if self.use_onehot else self.CRF.sparse_loss self.metrics = [ self.CRF.dense_accuracy if self.use_onehot else self.CRF.sparse_accuracy ] else: self.outputs = L.TimeDistributed( L.Dense(units=self.label, activation=self.activate_end))(outputs) self.model = M.Model(inputs, self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): x = None if self.rnn_type == "LSTM": rnn_cell = L.LSTM elif self.rnn_type == "CuDNNLSTM": rnn_cell = L.CuDNNLSTM elif self.rnn_type == "CuDNNGRU": rnn_cell = L.CuDNNGRU else: rnn_cell = L.GRU # Bi-RNN(LSTM/GRU) for _ in range(self.rnn_layer_repeat): x = L.Bidirectional( rnn_cell(units=self.rnn_unit, return_sequences=True, activation=self.activate_mid))(outputs) x = L.Dropout(self.dropout)(x) # dense-mid x = L.Flatten()(x) x = L.Dense(units=min(max(self.label, 128), self.embed_size), activation=self.activate_mid)(x) x = L.Dropout(self.dropout)(x) # dense-end self.outputs = L.Dense(units=self.label, activation=self.activate_end)(x) self.model = M.Model(inputs=inputs, outputs=self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): # rnn type, RNN的类型 if self.rnn_unit == "LSTM": layer_cell = L.LSTM elif self.rnn_unit == "CuDNNLSTM": layer_cell = L.CuDNNLSTM elif self.rnn_unit == "CuDNNGRU": layer_cell = L.CuDNNGRU else: layer_cell = L.GRU x = L.Activation(self.activate_mid)(outputs) # embedding遮挡 x = L.SpatialDropout1D(self.dropout_spatial)(x) lstm_0_output = L.Bidirectional(layer_cell( units=self.rnn_unit, return_sequences=True, activation='relu', kernel_regularizer=keras.regularizers.l2(self.l2), recurrent_regularizer=keras.regularizers.l2(self.l2)), name="bi_lstm_0")(x) lstm_1_output = L.Bidirectional(layer_cell( units=self.rnn_unit, return_sequences=True, activation='relu', kernel_regularizer=keras.regularizers.l2(self.l2), recurrent_regularizer=keras.regularizers.l2(self.l2)), name="bi_lstm_1")(lstm_0_output) x = L.Concatenate()([lstm_1_output, lstm_0_output, x]) x = AttentionWeightedAverage(name='attlayer', return_attention=False)(x) x = L.Dropout(self.dropout)(x) x = L.Flatten()(x) # dense-mid x = L.Dense(units=min(max(self.label, 64), self.embed_size), activation=self.activate_mid)(x) x = L.Dropout(self.dropout)(x) # dense-end, 最后一层, dense到label self.outputs = L.Dense(units=self.label, activation=self.activate_end)(x) self.model = M.Model(inputs=inputs, outputs=self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): # rnn type, RNN的类型 if self.rnn_unit == "LSTM": layer_cell = L.LSTM elif self.rnn_unit == "CuDNNLSTM": layer_cell = L.CuDNNLSTM elif self.rnn_unit == "CuDNNGRU": layer_cell = L.CuDNNGRU else: layer_cell = L.GRU # embedding遮挡 embedding_output_spatial = L.SpatialDropout1D( self.dropout_spatial)(outputs) # CNN convs = [] for kernel_size in self.filters_size: conv = L.Conv1D( self.filters_num, kernel_size=kernel_size, strides=1, padding='SAME', kernel_regularizer=keras.regularizers.l2(self.l2), bias_regularizer=keras.regularizers.l2(self.l2), )(embedding_output_spatial) convs.append(conv) x = L.Concatenate(axis=1)(convs) # Bi-LSTM, 论文中使用的是LSTM x = L.Bidirectional( layer_cell(units=self.rnn_unit, return_sequences=True, activation='relu', kernel_regularizer=keras.regularizers.l2(self.l2), recurrent_regularizer=keras.regularizers.l2( self.l2)))(x) x = L.Dropout(self.dropout)(x) x = L.Flatten()(x) # dense-mid x = L.Dense(units=min(max(self.label, 64), self.embed_size), activation=self.activate_mid)(x) x = L.Dropout(self.dropout)(x) # dense-end, 最后一层, dense到label self.outputs = L.Dense(units=self.label, activation=self.activate_end)(x) self.model = M.Model(inputs=inputs, outputs=self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): """ build_model. Args: inputs: tensor, input of model outputs: tensor, output of model Returns: None """ # CuDNNGRU or GRU x = None if self.rnn_type.upper() == "CUDNNGRU": rnn_cell = L.CuDNNGRU else: rnn_cell = L.GRU # Bi-GRU for nrl in range(self.num_rnn_layers): x = L.Bidirectional( rnn_cell( units=self.rnn_unit, return_sequences=True, activation=self.activate_mid, ))(outputs) x = L.Dropout(self.dropout)(x) if self.use_crf: x = L.Dense(units=self.label, activation=self.activate_end)(x) self.CRF = ConditionalRandomField(self.crf_lr_multiplier, name="crf_bert4keras") self.outputs = self.CRF(x) self.trans = K.eval(self.CRF.trans).tolist() self.loss = self.CRF.dense_loss if self.use_onehot else self.CRF.sparse_loss self.metrics = [ self.CRF.dense_accuracy if self.use_onehot else self.CRF.sparse_accuracy ] else: self.outputs = L.TimeDistributed( L.Dense(units=self.label, activation=self.activate_end))(x) self.model = M.Model(inputs, self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): """ build_model. Args: inputs: tensor, input of model outputs: tensor, output of model Returns: None """ # LSTM or GRU if self.rnn_type == "LSTM": rnn_cell = L.LSTM elif self.rnn_type == "CuDNNLSTM": rnn_cell = L.CuDNNLSTM elif self.rnn_type == "CuDNNGRU": rnn_cell = L.CuDNNGRU else: rnn_cell = L.GRU # Bi-LSTM-LAN for nrl in range(self.num_rnn_layers): x = L.Bidirectional(rnn_cell(units=self.rnn_unit*(nrl+1), return_sequences=True, activation=self.activate_mid, ))(outputs) x_att = SelfAttention(K.int_shape(x)[-1])(x) outputs = L.Concatenate()([x, x_att]) outputs = L.Dropout(self.dropout)(outputs) if self.use_crf: x = L.Dense(units=self.label, activation=self.activate_end)(outputs) self.CRF = ConditionalRandomField(self.crf_lr_multiplier, name="crf_bert4keras") self.outputs = self.CRF(x) self.trans = K.eval(self.CRF.trans).tolist() self.loss = self.CRF.dense_loss if self.use_onehot else self.CRF.sparse_loss self.metrics = [self.CRF.dense_accuracy if self.use_onehot else self.CRF.sparse_accuracy] else: self.outputs = L.TimeDistributed(L.Dense(units=self.label, activation=self.activate_end))(outputs) self.model = M.Model(inputs, self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): """ build_model. Args: inputs: tensor, input of model outputs: tensor, output of model Returns: None """ # LSTM or GRU if self.rnn_type == "LSTM": rnn_cell = L.LSTM elif self.rnn_type == "CuDNNLSTM": rnn_cell = L.CuDNNLSTM elif self.rnn_type == "CuDNNGRU": rnn_cell = L.CuDNNGRU else: rnn_cell = L.GRU # CNN-LSTM, 提取n-gram特征和最大池化, 一般不用平均池化 conv_pools = [] for i in range(len(self.filters_size)): conv = L.Conv1D( name="conv-{0}-{1}".format(i, self.filters_size[i]), kernel_size=self.filters_size[i], activation=self.activate_mid, filters=self.filters_num, padding='same', )(outputs) conv_rnn = L.Bidirectional( rnn_cell( name="bi-lstm-{0}-{1}".format(i, self.filters_size[i]), activation=self.activate_mid, return_sequences=True, units=self.rnn_unit, ))(conv) x_dropout = L.Dropout(rate=self.dropout, name="dropout-{0}-{1}".format( i, self.filters_size[i]))(conv_rnn) conv_pools.append(x_dropout) # 拼接 x = L.Concatenate(axis=-1)(conv_pools) x = L.Dropout(self.dropout)(x) # CRF or Dense if self.use_crf: x = L.Dense(units=self.label, activation=self.activate_end)(x) self.CRF = ConditionalRandomField(self.crf_lr_multiplier, name="crf_bert4keras") self.outputs = self.CRF(x) self.trans = K.eval(self.CRF.trans).tolist() self.loss = self.CRF.dense_loss if self.use_onehot else self.CRF.sparse_loss self.metrics = [ self.CRF.dense_accuracy if self.use_onehot else self.CRF.sparse_accuracy ] else: self.outputs = L.TimeDistributed( L.Dense(units=self.label, activation=self.activate_end, name="dense-output"))(x) self.model = M.Model(inputs, self.outputs) self.model.summary(132)
def build_model(self, inputs, outputs): """ build_model. Args: inputs: tensor, input of model outputs: tensor, output of model Returns: None """ # CNN, 提取n-gram特征和最大池化, DGCNN膨胀卷积(IDCNN) conv_pools = [] for i in range(len(self.filters_size)): conv = L.Conv1D( name="conv-{0}-{1}".format(i, self.filters_size[i]), dilation_rate=self.atrous_rates[0], kernel_size=self.filters_size[i], activation=self.activate_mid, filters=self.filters_num, padding="SAME", )(outputs) for j in range(len(self.atrous_rates) - 1): conv = L.Conv1D( name="conv-{0}-{1}-{2}".format(i, self.filters_size[i], j), dilation_rate=self.atrous_rates[j], kernel_size=self.filters_size[i], activation=self.activate_mid, filters=self.filters_num, padding="SAME", )(conv) conv = L.Dropout( name="dropout-{0}-{1}-{2}".format(i, self.filters_size[i], j), rate=self.dropout, )(conv) conv_pools.append(conv) # 拼接 x = L.Concatenate(axis=-1)(conv_pools) x = L.Dropout(self.dropout)(x) # CRF or Dense if self.use_crf: x = L.Dense(units=self.label, activation=self.activate_end)(x) self.CRF = ConditionalRandomField(self.crf_lr_multiplier, name="crf_bert4keras") self.outputs = self.CRF(x) self.trans = K.eval(self.CRF.trans).tolist() self.loss = self.CRF.dense_loss if self.use_onehot else self.CRF.sparse_loss self.metrics = [ self.CRF.dense_accuracy if self.use_onehot else self.CRF.sparse_accuracy ] else: x = L.Bidirectional( L.GRU( activation=self.activate_mid, return_sequences=True, units=self.rnn_unit, name="bi-gru", ))(x) self.outputs = L.TimeDistributed( L.Dense( activation=self.activate_end, name="dense-output", units=self.label, ))(x) self.model = M.Model(inputs, self.outputs) self.model.summary(132)