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

Deadly 提交于 2020-08-23 05:04:03

问题


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'

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', ...]

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.

My full traceback:

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'

回答1:


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..




回答2:


I had the same problem. The reason was that some rows in my input csv dataset were empty.



来源:https://stackoverflow.com/questions/55060888/torchtext-attributeerror-example-object-has-no-attribute-text-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!