text icon indicating copy to clipboard operation
text copied to clipboard

Saving my TabularDataset

Open SamLynnEvans opened this issue 8 years ago • 7 comments

Hi Everyone,

I am currently loading and processing a huge text body using TabularDataset, it takes about half an hour each time.

Whenever I have to restart the kernel, I have to load it again and wait another half an hour, this is a pain in the butt...

I wanted to know if there was any way to save the TabularDataset I've created to a file, so I can just quickly load it from that each time? This would save me a ton of time.

I tried with pickle and dill but an error message says they both don't work with generators...

Does anyone know a way?

Thanks in advance

SamLynnEvans avatar Aug 22 '18 13:08 SamLynnEvans

Hi I guess it's this line that's taking a long time. But after self.examples has been created, you should be able to pickle it. So a hackish way would be to load your dataset once, then pickle self.examples, then afterwards create a dummy TabularDataset with the same fields but a tiny dataset, unpickle your examples, then set dataset.examples = unpickled_examples. A better way would be to change the logic so that iterators are used everywhere, and re-created once they're extinguished.

chrishokamp avatar Aug 22 '18 13:08 chrishokamp

Thanks Chris that's extremely useful! I get the hacky method and am trying that out now. I don't quite understand the bit about creating iterators everywhere as I am new to torchtext...

my current code is this:

FR_TEXT = Field(tokenize=tokenize_fr.tokenizer, init_token="<sos>", eos_token="<eos>")
EN_TEXT = Field(tokenize=tokenize_en.tokenizer, init_token="<sos>", eos_token="<eos>")
data_fields = [("id", None), ("English", EN_TEXT), ("French", FR_TEXT)]
trn = TabularDataset("europarl_en-fr.csv", format="csv", fields=data_fields)
EN_TEXT.build_vocab(trn, min_freq = 8)
FR_TEXT.build_vocab(trn, min_freq = 8)
trn_iter = BucketIterator(trn, batch_size=5, sort_key=lambda x: len(x.French), shuffle=True)

I then later will run a seq2seq model where I train using:

for i, batch in enumerate(trn_iter):
    do_stuff(batch.English, batch.French)

If you have time could you help me know how could I change that logic to use 'iterators everywhere' as you suggest?

Thanks so much for the help already! :)

SamLynnEvans avatar Aug 22 '18 14:08 SamLynnEvans

np :smile: I'm also not super familiar with all the torchtext possibilities, but I think for your usecase it might suffice to change this line to examples = (make_example(line, fields) for line in reader) and to change the context manager block (with ... as) to keep the .csv file open, but then you have to handle what happens when you get to the end of the iterator. I'm sure there were deliberate design reasons why it's not implemented like this. Note Opennmt-py also has a much more heavyweight solution with sharding, etc... that works for big translation datasets.

chrishokamp avatar Aug 22 '18 14:08 chrishokamp

Thanks again Chris!

Interesting... however I suppose processing the text each iteration will mean longer training time though? As it stands I will already be training the data for 2 days or so...

In terms of time optimisation wouldn't the best way be way to take a big hit and process the text all at once at the start? Even if I initially have to wait a bit, I'll make up that time when training perhaps?

SamLynnEvans avatar Aug 22 '18 15:08 SamLynnEvans

yeah i guess that's the intuition behind doing it all upfront, but I don't think that would be the bottleneck for NMT at least, since tokenization and batching should be quite fast compared to the time taken by the model for each mini-batch. Anyway I'd say look at how Opennmt-py does it, because they also handle the case where your dataset doesn't fit into memory.

chrishokamp avatar Aug 22 '18 15:08 chrishokamp

@chrishokamp thanks for the help. I will just close the issue and feel free to reopen it.

zhangguanheng66 avatar May 30 '19 21:05 zhangguanheng66

Thanks Chris that's extremely useful! I get the hacky method and am trying that out now. I don't quite understand the bit about creating iterators everywhere as I am new to torchtext...

my current code is this:

FR_TEXT = Field(tokenize=tokenize_fr.tokenizer, init_token="<sos>", eos_token="<eos>")
EN_TEXT = Field(tokenize=tokenize_en.tokenizer, init_token="<sos>", eos_token="<eos>")
data_fields = [("id", None), ("English", EN_TEXT), ("French", FR_TEXT)]
trn = TabularDataset("europarl_en-fr.csv", format="csv", fields=data_fields)
EN_TEXT.build_vocab(trn, min_freq = 8)
FR_TEXT.build_vocab(trn, min_freq = 8)
trn_iter = BucketIterator(trn, batch_size=5, sort_key=lambda x: len(x.French), shuffle=True)

I then later will run a seq2seq model where I train using:

for i, batch in enumerate(trn_iter):
    do_stuff(batch.English, batch.French)

If you have time could you help me know how could I change that logic to use 'iterators everywhere' as you suggest?

Thanks so much for the help already! :)

hello,SamLynnEvans,following the link(https://towardsdatascience.com/how-to-code-the-transformer-in-pytorch-24db27c8f9ec),I got the same issue, have you solved this problem? it will waste some time during each training for load this dataset, after reading your post, I know we can use "self.examples",but how to apply it to the dataset you will create? hope to your reply, Thank you!

wartenhx avatar Jun 16 '19 03:06 wartenhx