http://zh-v2.d2l.ai/chapter_natural-language-processing-pretraining/word-embedding-dataset.html
Can’t pickle local object ‘load_data ptb.<locals>.PTB Dataset’
我在运行“把所有东西放一起”小节的代码块时,出现如上错误。
猜测可能是
class PTBDataset 的定义放在 load_data_ptb 中导致这个问题的出现。
我按你的操作把class PTBDataset放在外面了,运行到for batch in data_iter:
是提示另外一个错误:
Can't pickle <class '__main__.PTBDataset'>: attribute lookup PTBDataset on __main__ failed
上网搜了一下,把num_workers
改为0
就可以运行,不知道为什么
看网上把说num_workers
改为 0
就不启用多进程,不会预加载多批次数据进入内存。
d2l里默认值是4。
def get_dataloader_workers():
"""Use 4 processes to read the data."""
return 4
不清楚为什么在我的台式机上也是改成0才行,试过1、2、4都不行。。。
多线程问题,改
num_workers
也行,或者将代码重构,如图
def load_data_ptb(batch_size, max_window_size, num_noise_words):
"""下载PTB数据集,然后将其加载到内存中"""
sentences = read_ptb()
vocab = d2l.Vocab(sentences, min_freq=10)
subsampled, counter = subsample(sentences, vocab)
corpus = [vocab[line] for line in subsampled]
all_centers, all_contexts = get_centers_and_contexts(
corpus, max_window_size)
all_negatives = get_negatives(
all_contexts, vocab, counter, num_noise_words)
return all_centers, all_contexts, all_negatives, vocab
class PTBDataset(torch.utils.data.Dataset):
def __init__(self, centers, contexts, negatives):
assert len(centers) == len(contexts) == len(negatives)
self.centers = centers
self.contexts = contexts
self.negatives = negatives
def __getitem__(self, index):
return (self.centers[index], self.contexts[index],
self.negatives[index])
def __len__(self):
return len(self.centers)
谢谢大佬,问题已经解决,请问这个问题出现的原因是什么呢
负采样时使用的词频分布是用下采样之前的词频生成的,为什么不用下采样之后的词频生成这个分布?
负采样的时候应该尽可能选一个高频词,因为一个高频词是负例比一个稀有词是负例更常见、更重要。而下采样是剔除了高频词,和负采样略微冲突。
一开始 AttributeError: module ‘d2l.torch’ has no attribute ‘show_list_len_pair_hist’ 更新d2l至d2l 1.0.0a0 以后
AttributeError: module ‘d2l.torch’ has no attribute d2l.count_corpus了 肿么办呀
快点看到我哦!在线等 挺急的 拜托拜托
pip install d2l==0.17.5 -I
这样行不行?
tiny_dataset = [list(range(7)), list(range(7, 10))]
print('数据集', tiny_dataset)
for center, context in zip(*get_centers_and_contexts(tiny_dataset, 2)):
print('中心词', center, '的上下文词是', context)
我想知道这段代码的运行结果中为什么0的上下文词不是[1, 2]
这个function在第RNN那一章text preprocessing。你跑一次它就存在d2l里里