Reputation: 1117
I want to use the BERT Word Vector Embeddings in the Embeddings layer of LSTM instead of the usual default embedding layer. Is there any way I can do it?
Upvotes: 6
Views: 9249
Reputation: 124
Hope these links are of help:
Huggingface transformers with Tf2.0 (TPU training) with embeddings: (https://www.kaggle.com/abhilash1910/nlp-workshop-2-ml-india)
Contextual Similarity with BERT Embeddings(Pytorch): https://github.com/abhilash1910/BERTSimilarity
For generating unique sentence embeddings using BERT/BERT variants, it is recommended to select the correct layers. In some cases the following pattern can be taken into consideration for determining the embeddings(TF 2.0/Keras):
transformer_model = transformers.TFBertModel.from_pretrained('bert-large-uncased')
input_ids = tf.keras.layers.Input(shape=(128,), name='input_token', dtype='int32')
input_masks_ids = tf.keras.layers.Input(shape=(128,), name='masked_token', dtype='int32')
X = transformer_model(input_ids, input_masks_ids)[0]
X = tf.keras.layers.Dropout(0.2)(X)
X = tf.keras.layers.Dense(6, activation='softmax')
model = tf.keras.Model(inputs=[input_ids, input_masks_ids], outputs = X)(X)
import numpy as np
from transformers import AutoTokenizer, pipeline, TFDistilBertModel
from scipy.spatial.distance import cosine
def transformer_embedding(name,inp,model_name):
model = model_name.from_pretrained(name)
tokenizer = AutoTokenizer.from_pretrained(name)
pipe = pipeline('feature-extraction', model=model,
tokenizer=tokenizer)
features = pipe(inp)
features = np.squeeze(features)
return features
z=['The brown fox jumped over the dog','The ship sank in the Atlantic Ocean']
embedding_features1=transformer_embedding('distilbert-base-uncased',z[0],TFDistilBertModel)
embedding_features2=transformer_embedding('distilbert-base-uncased',z[1],TFDistilBertModel)
distance=1-cosine(embedding_features1[0],embedding_features2[0])
print(distance)
Thanks.
Upvotes: 3