我正在使用 RNN 并使用 Pytorch 和 Torchtext。我在 RNN 中构建词汇时遇到问题。我的代码如下:
TEXT = Field(tokenize=tokenizer, lower=True)
LABEL = LabelField(dtype=torch.float)
trainds = TabularDataset(
path='drive/{}'.format(TRAIN_PATH), format='tsv',
fields=[
('label_start', LABEL),
('label_end', None),
('title', None),
('symbol', None),
('text_content', TEXT),
])
testds = TabularDataset(
path='drive/{}'.format(TEST_PATH), format='tsv',
fields=[
('text_content', TEXT),
])
TEXT.build_vocab(trainds, testds)
Run Code Online (Sandbox Code Playgroud)
当我想构建 vocab 时,我收到了这个烦人的错误:
AttributeError: 'Example' object has no attribute 'text_content'
Run Code Online (Sandbox Code Playgroud)
我敢肯定,没有缺少text_contentattr。我做了 try-catch 以显示这个特定案例:
try:
print(len(trainds[i]))
except:
print(trainds[i].text_content)
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,我没有收到任何错误,这个特定的打印命令显示:
['znana', 'okresie', 'masarni', 'walc', 'y', 'my?l', 'programie', 'sprawy', ...]
Run Code Online (Sandbox Code Playgroud)
所以它表明,有 text_contentattr。当我在较小的数据集上执行此操作时,它就像一个魅力。当我想使用正确的数据时会出现此问题。我的想法用完了。也许有人有类似的情况,可以解释一下。
我的完整追溯:
AttributeError Traceback (most …Run Code Online (Sandbox Code Playgroud) nlp neural-network recurrent-neural-network pytorch torchtext