Text: Can I get embedding vectors from my pretrained vectors?

Created on 3 Nov 2019  ยท  3Comments  ยท  Source: pytorch/text

โ“ Questions and Help

Description

    tokenize = lambda x: x.split()
    TEXT = data.Field(sequential=True, tokenize=tokenize, lower=True, include_lengths=True, batch_first=True, fix_length=200)
    LABEL = data.LabelField()

    train_data, test_data = datasets.IMDB.splits(TEXT, LABEL)
    train_data


    TEXT.build_vocab(train_data, vectors=GloVe(name='6B', dim=300))
    LABEL.build_vocab(train_data)

    word_embeddings = TEXT.vocab.vectors
    TEXT.vocab

I want to get TEXT.vocab.vectors from not the vectors torchtext had but my pretrained vector.

for example

TEXT.build_vocab(train_data, vectors=my_pretrained_vectors)

and then I will get the torch text embedding vectors from my_pretrained_vectors.

Most helpful comment

@lsh23 I assume you want to load some vector file which you produced yourself (not one of the pretrained GloVe, word2vec,... files).
In that case you need to format your vector file in the standard format (each line contains a word and a vector, where all the values are separated by a single space). Then construct your own Vectors object by passing the path to your vector file to the constructor, e.g.:

my_pretrained_vectors = Vectors('/path/to/my/vectors.txt')
TEXT.build_vocab(train_data, vectors=my_pretrained_vectors)

All 3 comments

I'm confused. Glove pretrained vector works fine in another issue https://github.com/pytorch/text/issues/634.
@mttk Any ideas?

@lsh23 I assume you want to load some vector file which you produced yourself (not one of the pretrained GloVe, word2vec,... files).
In that case you need to format your vector file in the standard format (each line contains a word and a vector, where all the values are separated by a single space). Then construct your own Vectors object by passing the path to your vector file to the constructor, e.g.:

my_pretrained_vectors = Vectors('/path/to/my/vectors.txt')
TEXT.build_vocab(train_data, vectors=my_pretrained_vectors)

thank you for the answer!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rj7 picture Rj7  ยท  3Comments

dilettacal picture dilettacal  ยท  6Comments

NoaKel picture NoaKel  ยท  6Comments

Taekyoon picture Taekyoon  ยท  3Comments

vince62s picture vince62s  ยท  3Comments