Torchtext AttributeError: 'Example' 对象没有属性 'text_content' [英] Torchtext AttributeError: 'Example' object has no attribute 'text_content'

查看:168
本文介绍了Torchtext AttributeError: 'Example' 对象没有属性 'text_content'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RNN 并使用 Pytorch &火炬文本.我在 RNN 中构建词汇时遇到问题.我的代码如下:

I'm working with RNN and using Pytorch & Torchtext. I've got a problem with building vocab in my RNN. My code is as follows:

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)

当我想构建词汇时,我遇到了这个烦人的错误:

When I want to build vocab, I'm getting this annoying error:

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

我敢肯定,没有丢失 text_content 属性.我进行了 try-catch 以显示此特定情况:

I'm sure, that there is no missing text_content attr. I made try-catch in order to display this specific case:

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

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

Surprisingly, I don't get any error and this specific print command shows:

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

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

So it indicates, that there is text_content attr. When I perform this on a smaller dataset, it works like a charm. This problem occurs when I want to work with proper data. I ran out of ideas. Maybe someone had a similar case and can explain it.

我的完整追溯:

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 文件中提到的额外或更少的字段..

This problem arises when the fields are not passed in the same order as they are in the csv/tsv file. Order must be same. Also check if no extra or less fields are mentioned than there are in the csv/tsv file..

这篇关于Torchtext AttributeError: 'Example' 对象没有属性 'text_content'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆