Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TEXT.build_vocab for two datasets #648

Open
antgr opened this issue Nov 20, 2019 · 6 comments
Open

TEXT.build_vocab for two datasets #648

antgr opened this issue Nov 20, 2019 · 6 comments

Comments

@antgr
Copy link

antgr commented Nov 20, 2019

❓ Questions and Help

Description

Hi, I want to do multitask learning, and to use two datasets.
I want to create common vocabulary from the two datasets. For example from imdb and snli. How should I use TEXT.build_vocab to achieve this?

@zhangguanheng66
Copy link
Contributor

@mttk Can we pass a pretrained vocab object to build a torchtext dataset?

@mttk
Copy link
Contributor

mttk commented Nov 22, 2019

You can build vocab from multiple datasets (the argument for TEXT.build_vocab can be a sequence of Dataset instances).
Illustratively:

TEXT = Field(...)
imdb_train, imdb_test = IMDB.splits(text_field=TEXT, ...)
snli_train, snli_valid, snli_test = SNLI.splits(text_field=TEXT, ...)
TEXT.build_vocab(imdb_train, snli_train)

AFAIK there is no way to pass a pretrained vocab to build a dataset without some hacking, but this should work in this case.

@antgr
Copy link
Author

antgr commented Nov 22, 2019

You mean that I could not use for example "Glove" etc. Right?
For example something like:

TEXT.build_vocab(imdb_train, snli_train
                 min_freq = MIN_FREQ,
                 vectors = "glove.6B.300d",
                 unk_init = torch.Tensor.normal_)

@mttk
Copy link
Contributor

mttk commented Nov 22, 2019

That code should work with GloVe. The Field has to be shared between the Datasets (as it is in the example above), and you will get a common vocab. The vector assignment is done after the vocabulary is determined.

Concretely, the word frequencies are computed here from all the Dataset instances: https://github.com/pytorch/text/blob/master/torchtext/data/field.py#L304
And after that point, the Vectors class doesn't know how many source datasets were used to obtain the frequencies.

@antgr
Copy link
Author

antgr commented Nov 22, 2019

OK thank you! And if i would like to use a custom dataset instead of, for example, imdb? How could I use there the TEXT? Can I use split (with text_field as argument) in a custom dataset as well?

@mttk
Copy link
Contributor

mttk commented Nov 22, 2019

You have to load that dataset by e.g. using TabularDataset and assigning TEXT as a field in the constructor, then you can use the builtin dataset.split method. Now you have your custom dataset splits loaded.

Then, you can pass any instance of Dataset as an argument to TEXT.build_vocab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants