bert icon indicating copy to clipboard operation
bert copied to clipboard

How to get the word embedding after pre-training?

Open mfxss opened this issue 8 years ago • 9 comments

Hi, I am excited on this great model. And I want to get the word embedding . Where shold I find the file from output or should I change to code to do this? Thanks, Yuguang

mfxss avatar Nov 06 '18 02:11 mfxss

If you want to get the contextual embeddings (like ELMo) see the section here.

If you want the actual word embeddings, the word->id mapping is just the index of the row in vocab.txt, and the embedding matrix is in bert_model.ckpt with the variable name bert/embeddings/word_embeddings.

jacobdevlin-google avatar Nov 06 '18 03:11 jacobdevlin-google

And I download your released model of chinese_L-12_H-768_A-12. In vocab.txt, I found some token such as [unused1] [CLS][SEP][MASK] <S> <T> .
What do these tokens mean?

mfxss avatar Nov 06 '18 03:11 mfxss

The [CLS], [SEP] and [MASK] tokens are used as described in the paper and README. The [unused] tokens were not used in our model and are randomly initialized.

jacobdevlin-google avatar Nov 06 '18 03:11 jacobdevlin-google

What is your training data of chinese_L-12_H-768_A-12? And what is it's size?

mfxss avatar Nov 06 '18 07:11 mfxss

It's Chinese wikipedia with both Traditional and Simplified characters.

jacobdevlin-google avatar Nov 06 '18 18:11 jacobdevlin-google

Hello @mfxss , Not sure if you still have problem to get the word embedding from BERT. I implement a BERT embedding library which makes you can get word embedding in a programatic way.

https://github.com/imgarylai/bert-embedding

Because I'm working closely with mxnet & gluonnlp team, my implementation is done by using mxnet and gluonnlp. However, I am trying to implement it in all other different frameworks.

Hope my works can help you.

imgarylai avatar Feb 10 '19 19:02 imgarylai

Hey guys, if you don't want to install an extra module, here is an example:

BERT_PATH = 'HOME_DIR/bert_en_uncased_L-12_H-768_A-12'

import tensorflow as tf
imported = tf.saved_model.load(BERT_PATH)

for i in imported.trainable_variables:
    if i.name == 'bert_model/word_embeddings/embeddings:0':
        embeddings = i

And embeddings is the tensor of word embedding that you want!

rainorangelemon avatar Jan 04 '20 22:01 rainorangelemon

Hi @jacobdevlin-google Thanks for the pointers. I see the output with the extract_features.py gives subword representations. I'm sure to be missing something but my question is how can we get a word (not subword) representation instead? Thanks in advance for your help!

arjunrajanna avatar Aug 19 '20 21:08 arjunrajanna

Hi @jacobdevlin-google Thanks for the pointers. I see the output with the extract_features.py gives subword representations. I'm sure to be missing something but my question is how can we get a word (not subword) representation instead? Thanks in advance for your help!

Excuse me did you find a solution for word not subword , please

mathshangw avatar Jan 09 '22 08:01 mathshangw