neural-network - Torchtext 属性错误 : 'Example' object has no attribute 'text_content'

标签 neural-network nlp pytorch recurrent-neural-network torchtext

我正在使用 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)

当我想构建 vocab 时,我收到了这个烦人的错误:

AttributeError: 'Example' object has no attribute 'text_content'

我敢肯定,没有遗漏 text_content属性。我做了 try-catch 以显示这个特定案例:

try:
    print(len(trainds[i]))
except:
    print(trainds[i].text_content)

令人惊讶的是,我没有收到任何错误,这个特定的打印命令显示:

['znana', 'okresie', 'masarni', 'walc', 'y', 'myśl', 'programie', 'sprawy', ...]

所以它表明, text_content属性。当我在较小的数据集上执行此操作时,它就像一个魅力。当我想使用正确的数据时会出现此问题。我的想法用完了。也许有人有类似的情况,可以解释一下。

我的完整追溯:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-cf31866a07e7> in <module>()
    155 
    156 if __name__ == "__main__":
--> 157     main()
    158 

<ipython-input-16-cf31866a07e7> in main()
    117             break
    118 
--> 119     TEXT.build_vocab(trainds, testds)
    120     print('zbudowano dla text')
    121     LABEL.build_vocab(trainds)

/usr/local/lib/python3.6/dist-packages/torchtext/data/field.py in build_vocab(self, *args, **kwargs)
    260                 sources.append(arg)
    261         for data in sources:
--> 262             for x in data:
    263                 if not self.sequential:
    264                     x = [x]

/usr/local/lib/python3.6/dist-packages/torchtext/data/dataset.py in __getattr__(self, attr)
    152         if attr in self.fields:
    153             for x in self.examples:
--> 154                 yield getattr(x, attr)
    155 
    156     @classmethod

AttributeError: 'Example' object has no attribute 'text_content'

最佳答案

当字段的传递顺序与 csv/tsv 文件中的顺序不同时,就会出现此问题。顺序必须相同。还要检查是否没有提到比 csv/tsv 文件中提到的额外或更少的字段。

关于neural-network - Torchtext 属性错误 : 'Example' object has no attribute 'text_content' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55060888/

相关文章:

java - 3D 数组中的 ArrayIndexOutOfBoundsException

python - RNN 的 Tensorflow 示例

python - 值错误: Unexpected character found when decoding 'true' while converting IOB to JSONL for SpaCy

matrix - 如何在 text2vec 中对齐两个 GloVe 模型?

python - 如何在 Keras Tensorflow 中为采样创建自定义层?

python - FizzBu​​zz 神经网络

python - 为 Caffe 生成 LMDB

python - 简单的 Keras 神经网络不学习

constructor - 为什么 Pytorch 中未初始化的张量有初始值?

pytorch - 没有这样的运算符(operator) torchvision::nms