How to feed Bert embeddings to LSTM
Asked Answered
R

1

6

I am working on a Bert + MLP model for text classification problem. Essentially, I am trying to replace the MLP model with a basic LSTM model.

Is it possible to create a LSTM with embedding? Or, is best to create a LSTM with embedded layer?

More specifically, I am having a hard time trying to create embedded matrix so I can create embedding layer using Bert embedding.

def get_bert_embeddings(dataset='gap_corrected_train',
                        dataset_path=TRAIN_PATH,
                        bert_path=BERT_UNCASED_LARGE_PATH,
                        bert_layers=BERT_LAYERS):
    """Get BERT embeddings for all files in dataset_path and specified BERT layers and write them to file."""
    df = None
    for file in os.listdir(dataset_path):
        if df is None:
            df = pd.read_csv(dataset_path+'/'+file, sep='\t')
        else:
            next_df = pd.read_csv(dataset_path+'/'+file, sep='\t')
            df = pd.concat([df, next_df], axis=0)
            df.reset_index(inplace=True, drop=True)

    for i, layer in enumerate(bert_layers):
        embeddings_file = INTERIM_PATH + 'emb_bert' + str(layer) + '_' + dataset + '.h5'
        if not os.path.exists(embeddings_file):
            print('Embeddings file: ', embeddings_file)
            print('Extracting BERT Layer {0} embeddings for {1}...'.format(layer, dataset))
            print("Started at ", time.ctime())

            emb = get_bert_token_embeddings(df, bert_path, layer)
            emb.to_hdf(embeddings_file, 'table')

            print("Finished at ", time.ctime())

def build_mlp_model(input_shape):
    input_layer = layers.Input(input_shape)



    input_features = layers.Input((len(FEATURES),))
    x = layers.Concatenate(axis=1, name="concate_layer")([input_layer, input_features]) 


    x = layers.Dense(HIDDEN_SIZE, name='dense1')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.Dropout(DROPOUT, seed=RANDOM)(x)

    x = layers.Dense(HIDDEN_SIZE//2, name='dense2')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.Dropout(DROPOUT//2, seed=RANDOM)(x)

    x = layers.Dense(HIDDEN_SIZE//4, name='dense3')(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.Dropout(DROPOUT//2, seed=RANDOM)(x)

    output_layer = layers.Dense(3, name='output', kernel_regularizer = regularizers.l2(LAMBDA))(x)
    output_layer = layers.Activation('softmax')(output_layer)

    model = models.Model(input=[input_layer, input_features], output=output_layer, name="mlp")
    return model

Rimmer answered 13/4, 2019 at 20:42 Comment(7)
Did you figure it out?Inanity
Not yet. There are a few example in github.Rimmer
LSTMs can be created with the embedding layer. Keras provides and Embedding layer that you can use alongside with LSTMUniparous
@AshwinGeetD'Sa Is the site that has that info?Rimmer
There is info about Embedding Layer here: keras.io/layers/embeddingsUniparous
@AshwinGeetD'Sa Thank you!Rimmer
Why does one add LSTM layer? Isnt a dense enough? I mean the word embeddings are already cotextualized by their order with the attention mechanism?Outoftheway
M
1

You can create model that uses first the Embedding layer which is followed by LSTM and then Dense. Such as here:

deep_inputs = Input(shape=(length_of_your_data,))
embedding_layer = Embedding(vocab_size, output_dim = 3000, trainable=True)(deep_inputs)
LSTM_Layer_1 = LSTM(512)(embedding_layer) 
dense_layer_1 = Dense(number_of_classes, activation='softmax')(LSTM_Layer_1) 
model_AdGroups = Model(inputs=deep_inputs, outputs=dense_layer_1) 
Mexico answered 19/6, 2020 at 8:42 Comment(4)
Excuse me . Can I replace lstm in your code with Bert if I want to apply Bert only ?Scavenge
Yes, I think so.Mexico
excuse me where bert model here ?Eardrop
Here deep_inputs mean, Bert embeddings in numpy array with shape like (num_of_instance_df, dimension) or a different interpretation?Rapids

© 2022 - 2024 — McMap. All rights reserved.