d2l-en icon indicating copy to clipboard operation
d2l-en copied to clipboard

Chapter 15.4. Pretraining word2vec: AttributeError: Can't pickle local object 'load_data_ptb.<locals>.PTBDataset'

Open keyuchen21 opened this issue 1 year ago • 2 comments

AttributeError: Can't pickle local object 'load_data_ptb..PTBDataset'

image

can anyone help with this error?

keyuchen21 avatar Apr 30 '23 20:04 keyuchen21

这个问题搞定了了吗 请问

HJL-echoandy avatar May 30 '23 13:05 HJL-echoandy

@HJL-echoandy to anyone who is facing this problem I sort of found a quick fix.... After struggling with this same issue on a MacBook Pro M2, according to what I found online, this error message was mostly related to how Jupyter handles multithreading, particularly pickle.

So this solution worked for me:

  1. Create a python file in the same folder where you have your word_embedding_dataset.ipynb file, let's say you'll call it ptb_dataset.py
  2. Move (i.e., delete from the word_embedding_dataset.ipynb) the class PTBDataset(torch.utils.data.Dataset), and the function def batchify. Move them both to the ptb_dataset.py
  3. Import the class PTBDataset and the function batchify now into the word_embedding_dataset.ipynb (i.e., from ptb_dataset import PTBDataset, batchify
  4. Run the notebook again, and you shall be fine.

Note: You can refer to my notes in case of doubt.

Another thing to pay attention is that the only way I found to deal with this issue was to combine both lesson 15.3 and 15.4 in one notebook (i.e., dataset_word_embedding.ipynb), because I defined load_data_ptb locally.

cx-olquinjica avatar Jul 11 '23 11:07 cx-olquinjica