示例#1
0
    def build_model(self):
        data = self.data
        inputs = Input(shape=(data.seq_length, ))
        embeddings = Embedding(
            data.max_feature,
            data.embed_dim,
            weights=[data.embed_matrix],
            trainable=False)(inputs)
        x = SpatialDropout1D(self.dropout)(embeddings)
        # x = Bidirectional(CuDNNGRU(data.embed_dim, return_sequences=True))(x)
        # x = Bidirectional(CuDNNGRU(data.embed_dim, return_sequences=True))(x)
        x = Bidirectional(CuDNNGRU(100, return_sequences=True))(x)
        x = Bidirectional(CuDNNGRU(100, return_sequences=True))(x)
        ave_pool = GlobalAveragePooling1D()(x)
        max_pool = GlobalMaxPooling1D()(x)
        conc = concatenate([ave_pool, max_pool])
        x = Dense(self.dense_size, activation="relu")(conc)
        outputs = Dense(6, activation="sigmoid")(x)
        model = Model(inputs=inputs, outputs=outputs)

        optimizer = self.get_optimizer(self.lr, self.optim_name)
        model.compile(
            loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])
        self.model = model
    def _build_decoder(self,
                       state_size,
                       add_dropout=True,
                       embedding_model=None):
        # NOTE: self.max_seq_length+2 is used because of added start and end symbols

        feedback_in = Input(batch_shape=(None, self.max_seq_length + 2,
                                         self.num_unique_symbols),
                            name='decoder-feedback-input')

        thought_in = Input(batch_shape=(None, state_size),
                           name='decoder-thought-input')

        embedded_feedback_in = feedback_in
        if embedding_model:
            embedded_feedback_in = embedding_model(feedback_in)

        out = CuDNNGRU(state_size,
                       return_sequences=True)(embedded_feedback_in,
                                              initial_state=thought_in)

        if add_dropout:
            out = Dropout(0.1,
                          noise_shape=(None, 1, None),
                          name='encoder-dropout')(out)

        out = TimeDistributed(
            Dense(self.num_unique_symbols,
                  activation='softmax',
                  name='decoder-output-layer'))(out)

        return Model(inputs=[feedback_in, thought_in],
                     outputs=[out],
                     name='decoder-model')
示例#3
0
def BIGRU(hidden, seq=True, rgr=l2(1e-4), name=None):
    return Bidirectional(CuDNNGRU(hidden,
                                  return_sequences=seq,
                                  kernel_regularizer=rgr,
                                  bias_regularizer=rgr),
                         merge_mode='concat',
                         name=name)
示例#4
0
    def build_model(self):
        data = self.data
        maxlen = data.seq_length
        inputs = Input(shape=(data.seq_length, ), dtype="int32")
        embeddings = Embedding(
            data.max_feature,
            data.embed_dim,
            weights=[data.embed_matrix],
            trainable=False)(inputs)
        x = SpatialDropout1D(self.dropout)(embeddings)

        def k_slice(x, start, end):
            return x[:, start:end]

        left_1 = Lambda(
            k_slice, arguments={'start': maxlen - 1,
                                'end': maxlen})(x)
        left_2 = Lambda(k_slice, arguments={'start': 0, 'end': maxlen - 1})(x)
        l_embedding = concatenate([left_1, left_2], axis=1)

        right_1 = Lambda(k_slice, arguments={'start': 1, 'end': maxlen})(x)
        right_2 = Lambda(k_slice, arguments={'start': 0, 'end': 1})(x)
        r_embedding = concatenate([right_1, right_2], axis=1)

        forward = CuDNNGRU(data.embed_dim, return_sequences=True)(l_embedding)
        backward = CuDNNGRU(
            data.embed_dim, return_sequences=True,
            go_backwards=True)(r_embedding)

        together = concatenate([forward, x, backward], axis=2)
        semantic = TimeDistributed(
            Dense(self.dense_size, activation="tanh"))(together)
        max_pool = GlobalMaxPooling1D()(semantic)
        ave_pool = GlobalAveragePooling1D()(semantic)
        conc = concatenate([ave_pool, max_pool])
        outputs = Dense(6, activation="sigmoid")(conc)

        model = Model(inputs=inputs, outputs=outputs)
        optimizer = self.get_optimizer(self.lr, self.optim_name)
        model.compile(
            loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])
        self.model = model
    def _build_encoder(self,
                       state_size,
                       add_dropout=True,
                       embedding_model=None):
        # NOTE: self.max_seq_length+2 is used because of added start and end symbols

        encoder_in = Input(batch_shape=(None, self.max_seq_length + 2,
                                        self.num_unique_symbols),
                           name='encoder-input')

        embedded_in = encoder_in
        if embedding_model:
            embedded_in = embedding_model(encoder_in)

        out = CuDNNGRU(state_size)(embedded_in)

        if add_dropout:
            out = Dropout(0.1, name='encoder-dropout')(out)

        return Model(inputs=[encoder_in], outputs=[out], name='encoder-model')
示例#6
0
    def build_model(self):
        data = self.data
        inputs = Input(shape=(data.seq_length, ))
        embeddings = Embedding(
            data.max_feature,
            data.embed_dim,
            weights=[data.embed_matrix],
            trainable=False)(inputs)
        x = SpatialDropout1D(self.dropout)(embeddings)
        x = Bidirectional(CuDNNGRU(100, return_sequences=True))(x)
        capsule = Capsule(num_capsule=32, dim_capsule=32, routings=3)(x)
        flat = Flatten()(capsule)
        x = Dense(self.dense_size, activation="relu")(flat)
        outputs = Dense(6, activation="sigmoid")(flat)
        model = Model(inputs=inputs, outputs=outputs)

        optimizer = self.get_optimizer(self.lr, self.optim_name)
        model.compile(
            loss='binary_crossentropy',
            optimizer=optimizer,
            metrics=['accuracy'])
        self.model = model