在Keras嵌入层中使用BERT嵌入

时间:2020-07-07 09:12:50

标签: python-3.x keras nlp embedding bert-language-model

我想在LSTM的Embeddings层中使用BERT Word Vector Embeddings,而不是通常的默认嵌入层。我有什么办法吗?

1 个答案:

答案 0 :(得分:1)

希望这些链接对您有所帮助:

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)
from transformers import AutoTokenizer, pipeline, TFBertModel
bert_features1=transformer_embedding('bert-base-uncased',text1,TFBertModel)
bert_features2=transformer_embedding('bert-base-uncased',text2,TFBertModel)
distance=1-cosine(bert_features1[0],bert_features2[0])
print(distance)

谢谢。