Example #1
0
    def build_model(self, inputs, outputs):
        outputs_spati = L.SpatialDropout1D(self.dropout_spatial)(outputs)

        conv_pools = []
        for filter in self.filters_size:
            x = L.Conv1D(
                filters=self.filters_num,
                kernel_size=filter,
                padding="valid",
                kernel_initializer="normal",
                activation="relu",
            )(outputs_spati)
            capsule = Capsule_bojone(num_capsule=self.num_capsule,
                                     dim_capsule=self.dim_capsule,
                                     routings=self.routings,
                                     kernel_size=(filter, 1),
                                     share_weights=True)(x)
            conv_pools.append(capsule)
        capsule = L.Concatenate(axis=-1)(conv_pools)
        x = L.Flatten()(capsule)
        x = L.Dropout(self.dropout)(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)
Example #2
0
 def build_model(self, inputs, outputs):
     x = L.SpatialDropout1D(self.dropout_spatial)(outputs)
     x = SelfAttention(K.int_shape(outputs)[-1])(x)
     x_max = L.GlobalMaxPooling1D()(x)
     x_avg = L.GlobalAveragePooling1D()(x)
     x = L.Concatenate()([x_max, x_avg])
     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)
Example #3
0
 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)
Example #4
0
    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)