how to save torchtext Dataset?












1















I'm working with text and use torchtext.data.Dataset.
Creating the dataset takes a considerable amount of time.
For just running the program this is still acceptable. But I would like to debug the torch code for the neural network. And if python is started in debug mode, the dataset creation takes roughly 20 minutes (!!). That's just to get a working environment where I can debug-step through the neural network code.



I would like to save the Dataset, for example with pickle. This sample code is taken from here, but I removed everything that is not necessary for this example:



from torchtext import data
from fastai.nlp import *

PATH = 'data/aclImdb/'

TRN_PATH = 'train/all/'
VAL_PATH = 'test/all/'
TRN = f'{PATH}{TRN_PATH}'
VAL = f'{PATH}{VAL_PATH}'

TEXT = data.Field(lower=True, tokenize="spacy")

bs = 64;
bptt = 70

FILES = dict(train=TRN_PATH, validation=VAL_PATH, test=VAL_PATH)
md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=10)

with open("md.pkl", "wb") as file:
pickle.dump(md, file)


To run the code, you need the aclImdb dataset, it can be downloaded from here. Extract it into a data/ folder next to this code snippet. The code produces an error in the last line, where pickle is used:



Traceback (most recent call last):
File "/home/lhk/programming/fastai_sandbox/lesson4-imdb2.py", line 27, in <module>
pickle.dump(md, file)
TypeError: 'generator' object is not callable


The samples from fastai often use dill instead of pickle. But that doesn't work for me either.










share|improve this question



























    1















    I'm working with text and use torchtext.data.Dataset.
    Creating the dataset takes a considerable amount of time.
    For just running the program this is still acceptable. But I would like to debug the torch code for the neural network. And if python is started in debug mode, the dataset creation takes roughly 20 minutes (!!). That's just to get a working environment where I can debug-step through the neural network code.



    I would like to save the Dataset, for example with pickle. This sample code is taken from here, but I removed everything that is not necessary for this example:



    from torchtext import data
    from fastai.nlp import *

    PATH = 'data/aclImdb/'

    TRN_PATH = 'train/all/'
    VAL_PATH = 'test/all/'
    TRN = f'{PATH}{TRN_PATH}'
    VAL = f'{PATH}{VAL_PATH}'

    TEXT = data.Field(lower=True, tokenize="spacy")

    bs = 64;
    bptt = 70

    FILES = dict(train=TRN_PATH, validation=VAL_PATH, test=VAL_PATH)
    md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=10)

    with open("md.pkl", "wb") as file:
    pickle.dump(md, file)


    To run the code, you need the aclImdb dataset, it can be downloaded from here. Extract it into a data/ folder next to this code snippet. The code produces an error in the last line, where pickle is used:



    Traceback (most recent call last):
    File "/home/lhk/programming/fastai_sandbox/lesson4-imdb2.py", line 27, in <module>
    pickle.dump(md, file)
    TypeError: 'generator' object is not callable


    The samples from fastai often use dill instead of pickle. But that doesn't work for me either.










    share|improve this question

























      1












      1








      1








      I'm working with text and use torchtext.data.Dataset.
      Creating the dataset takes a considerable amount of time.
      For just running the program this is still acceptable. But I would like to debug the torch code for the neural network. And if python is started in debug mode, the dataset creation takes roughly 20 minutes (!!). That's just to get a working environment where I can debug-step through the neural network code.



      I would like to save the Dataset, for example with pickle. This sample code is taken from here, but I removed everything that is not necessary for this example:



      from torchtext import data
      from fastai.nlp import *

      PATH = 'data/aclImdb/'

      TRN_PATH = 'train/all/'
      VAL_PATH = 'test/all/'
      TRN = f'{PATH}{TRN_PATH}'
      VAL = f'{PATH}{VAL_PATH}'

      TEXT = data.Field(lower=True, tokenize="spacy")

      bs = 64;
      bptt = 70

      FILES = dict(train=TRN_PATH, validation=VAL_PATH, test=VAL_PATH)
      md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=10)

      with open("md.pkl", "wb") as file:
      pickle.dump(md, file)


      To run the code, you need the aclImdb dataset, it can be downloaded from here. Extract it into a data/ folder next to this code snippet. The code produces an error in the last line, where pickle is used:



      Traceback (most recent call last):
      File "/home/lhk/programming/fastai_sandbox/lesson4-imdb2.py", line 27, in <module>
      pickle.dump(md, file)
      TypeError: 'generator' object is not callable


      The samples from fastai often use dill instead of pickle. But that doesn't work for me either.










      share|improve this question














      I'm working with text and use torchtext.data.Dataset.
      Creating the dataset takes a considerable amount of time.
      For just running the program this is still acceptable. But I would like to debug the torch code for the neural network. And if python is started in debug mode, the dataset creation takes roughly 20 minutes (!!). That's just to get a working environment where I can debug-step through the neural network code.



      I would like to save the Dataset, for example with pickle. This sample code is taken from here, but I removed everything that is not necessary for this example:



      from torchtext import data
      from fastai.nlp import *

      PATH = 'data/aclImdb/'

      TRN_PATH = 'train/all/'
      VAL_PATH = 'test/all/'
      TRN = f'{PATH}{TRN_PATH}'
      VAL = f'{PATH}{VAL_PATH}'

      TEXT = data.Field(lower=True, tokenize="spacy")

      bs = 64;
      bptt = 70

      FILES = dict(train=TRN_PATH, validation=VAL_PATH, test=VAL_PATH)
      md = LanguageModelData.from_text_files(PATH, TEXT, **FILES, bs=bs, bptt=bptt, min_freq=10)

      with open("md.pkl", "wb") as file:
      pickle.dump(md, file)


      To run the code, you need the aclImdb dataset, it can be downloaded from here. Extract it into a data/ folder next to this code snippet. The code produces an error in the last line, where pickle is used:



      Traceback (most recent call last):
      File "/home/lhk/programming/fastai_sandbox/lesson4-imdb2.py", line 27, in <module>
      pickle.dump(md, file)
      TypeError: 'generator' object is not callable


      The samples from fastai often use dill instead of pickle. But that doesn't work for me either.







      python pickle pytorch torch torchtext






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 23:44









      lhklhk

      6,67585589




      6,67585589
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421999%2fhow-to-save-torchtext-dataset%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421999%2fhow-to-save-torchtext-dataset%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin