Created
March 9, 2019 15:02
-
-
Save MLWhiz/a03627240351cb55b07701cea1078389 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # BiDirectional LSTM | |
| def model_lstm_du(embedding_matrix): | |
| inp = Input(shape=(maxlen,)) | |
| x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp) | |
| ''' | |
| Here 64 is the size(dim) of the hidden state vector as well as the output vector. Keeping return_sequence we want the output for the entire sequence. So what is the dimension of output for this layer? | |
| 64*70(maxlen)*2(bidirection concat) | |
| CuDNNLSTM is fast implementation of LSTM layer in Keras which only runs on GPU | |
| ''' | |
| x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(x) | |
| avg_pool = GlobalAveragePooling1D()(x) | |
| max_pool = GlobalMaxPooling1D()(x) | |
| conc = concatenate([avg_pool, max_pool]) | |
| conc = Dense(64, activation="relu")(conc) | |
| conc = Dropout(0.1)(conc) | |
| outp = Dense(1, activation="sigmoid")(conc) | |
| model = Model(inputs=inp, outputs=outp) | |
| model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) | |
| return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment