コード例 #1
0
 def create_model(self, rnn_layer):
     inputs = Input(shape=(self.max_length, self.feature_size))
     masked_inputs = Masking(0.0)(inputs)
     encoded = RNNEncoder(
         RNNCell(
             rnn_layer(
                 self.hidden_size,
             ),
             Dense(
                 self.encoding_size
             ),
             dense_dropout=0.1
         )
     )(masked_inputs)
     outputs = RNNDecoder(
         RNNCell(
             rnn_layer(
                 self.hidden_size,
             ),
             Dense(
                 self.feature_size
             ),
             dense_dropout=0.1
         )
     )(encoded)
     model = Model(inputs, outputs)
     model.compile('sgd', 'mean_squared_error')
     return model
コード例 #2
0
ファイル: seq2vec_C2R_char.py プロジェクト: xuweiwen/seq2vec
    def create_model(
        self,
        rho=0.9,
        decay=0.0,
    ):

        inputs = Input(shape=(self.max_length, self.max_index))
        char_embedding = TimeDistributed(
            Dense(self.char_embedding_size, use_bias=False,
                  activation='tanh'))(inputs)

        char_embedding = Reshape(
            (self.max_length, self.char_embedding_size, 1))(char_embedding)
        masked_embedding = MaskConv(0.0)(char_embedding)
        masked_seq = MaskToSeq(layer=MaskConv(0.0),
                               time_axis=1)(char_embedding)

        char_feature = MaskConvNet(
            Conv2D(
                self.channel_size,
                (2, self.conv_size),
                strides=(1, self.conv_size),
                activation='tanh',
                padding='same',
                use_bias=False,
            ))(masked_embedding)

        mask_feature = MaskPooling(MaxPool2D((self.max_length, 1),
                                             padding='same'),
                                   pool_mode='max')(char_feature)

        encoded_feature = ConvEncoder()([mask_feature, masked_seq])

        dense_input = RNNDecoder(
            RNNCell(LSTM(
                units=self.latent_size,
                return_sequences=True,
                implementation=2,
                unroll=False,
                dropout=0.,
                recurrent_dropout=0.,
            ),
                    Dense(units=self.encoding_size, activation='tanh'),
                    dense_dropout=0.))(encoded_feature)

        outputs = TimeDistributed(
            Dense(self.word_embedding_size, activation='tanh'))(dense_input)

        model = Model(inputs, outputs)
        picked = Pick()(encoded_feature)
        encoder = Model(inputs, picked)

        optimizer = RMSprop(
            lr=self.learning_rate,
            rho=rho,
            decay=decay,
        )
        model.compile(loss='cosine_proximity', optimizer=optimizer)
        return model, encoder
コード例 #3
0
    def create_model(
        self,
        rho=0.9,
        decay=0.0,
    ):
        inputs = Input(shape=(self.max_length, self.word_embedding_size))
        masked_inputs = Masking(mask_value=0.0)(inputs)
        encoded_seq = BidirectionalRNNEncoder(
            RNNCell(LSTM(
                units=self.latent_size,
                use_bias=True,
                implementation=2,
                dropout=0.,
                recurrent_dropout=0.,
            ),
                    Dense(units=(self.encoding_size // 2), activation='tanh'),
                    dense_dropout=0.))(masked_inputs)
        decoded_seq = RNNDecoder(
            RNNCell(LSTM(
                units=self.latent_size,
                use_bias=True,
                implementation=2,
                dropout=0.,
                recurrent_dropout=0.,
            ),
                    Dense(units=self.encoding_size, activation='tanh'),
                    dense_dropout=0.))(encoded_seq)
        outputs = TimeDistributed(
            Dense(units=self.word_embedding_size,
                  activation='tanh'))(decoded_seq)

        model = Model(inputs, outputs)
        picked = Pick()(encoded_seq)
        encoder = Model(inputs, picked)

        optimizer = RMSprop(
            lr=self.learning_rate,
            rho=rho,
            decay=decay,
        )
        model.compile(loss='cosine_proximity', optimizer=optimizer)
        return model, encoder
コード例 #4
0
    def create_model(
        self,
        rho=0.9,
        decay=0.0,
    ):

        inputs = Input(shape=(self.max_length, ))
        char_embedding = Embedding(
            input_dim=self.max_index,
            output_dim=self.word_embedding_size,
            input_length=self.max_length,
            mask_zero=True,
        )(inputs)

        encoded = BidirectionalRNNEncoder(
            RNNCell(LSTM(units=self.latent_size,
                         dropout=0.,
                         recurrent_dropout=0.),
                    Dense(units=self.encoding_size // 2, activation='tanh'),
                    dense_dropout=0.))(char_embedding)
        decoded = RNNDecoder(
            RNNCell(LSTM(units=self.latent_size,
                         dropout=0.,
                         recurrent_dropout=0.),
                    Dense(units=self.encoding_size, activation='tanh'),
                    dense_dropout=0.))(encoded)
        outputs = TimeDistributed(
            Dense(units=self.max_index, activation='softmax'))(decoded)

        model = Model(inputs, outputs)

        picked = Pick()(encoded)
        encoder = Model(inputs, picked)

        optimizer = RMSprop(
            lr=self.learning_rate,
            rho=rho,
            decay=decay,
        )
        model.compile(loss='categorical_crossentropy', optimizer=optimizer)
        return model, encoder