简体   繁体   English

AttributeError:“示例”object 没有属性“文本”

[英]AttributeError: 'Example' object has no attribute 'text'

So I have been trying to run the sample code for BERT on another dataset.所以我一直在尝试在另一个数据集上运行 BERT 的示例代码。 Namely, this is the website I used to try to implement the BERT model, and I managed to follow the instructions and successfully run the code.也就是说,这是我用来尝试实现 BERT model 的网站,我设法按照说明操作并成功运行了代码。 However, when I attempt to run the same code on my own dataset, I get the following error:但是,当我尝试在自己的数据集上运行相同的代码时,出现以下错误:

```
model = BERT().to(device)
optimizer = optim.Adam(model.parameters(), lr=2e-5)

train(model=model, optimizer=optimizer)
``` 

```---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-57-e4474bff9c36> in <module>()
      2 optimizer = optim.Adam(model.parameters(), lr=2e-5)
      3 
----> 4 train(model=model, optimizer=optimizer)

5 frames
<ipython-input-45-4d0fa7f8acd5> in <lambda>(x)
     19 # Iterators
     20 
---> 21 train_iter = BucketIterator(train, batch_size=15, sort_key=lambda x: len(x.text),
     22                             device=device, train=True, sort=True, sort_within_batch=True)
     23 valid_iter = BucketIterator(valid, batch_size=32, sort_key=lambda x: len(x.text),

AttributeError: 'Example' object has no attribute 'text'```
         Label                                               Text
1178093      3                renal and urinary disorders common:
1170768      3                              orodispersible tablet
4339706      4  remotely manage the transmission bittorrent cl...
6513296      0                     what do you think of her, ay ?
7013664      0  how that could become a film is more than i ca... 

Additionally, I checked for empty rows as some articles and questions on StackOverflow suggested, but I there were none.此外,我按照 StackOverflow 上的一些文章和问题的建议检查了空行,但我没有。 These are the relevant lines of code, to which the error message points to:这些是错误消息指向的相关代码行:

```
# Model parameter
MAX_SEQ_LEN = 128
PAD_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.pad_token)
UNK_INDEX = tokenizer.convert_tokens_to_ids(tokenizer.unk_token)

# Fields

label_field = Field(sequential=True, use_vocab=False, batch_first=True, dtype=torch.float)
text_field = Field(use_vocab=False, tokenize=tokenizer.encode, lower=False, include_lengths=False, batch_first=True,
                   fix_length=MAX_SEQ_LEN, pad_token=PAD_INDEX, unk_token=UNK_INDEX)
fields = [('Label', label_field), ('Text', text_field)]

# TabularDataset


train, valid, test = TabularDataset.splits(path=source_folder, train='train_bert.csv', validation='valid_bert.csv',
                                           test='test_bert.csv', format='CSV', fields=fields, skip_header=True)

# Iterators

train_iter = BucketIterator(train, batch_size=15, sort_key=lambda x: len(x.text),
                            device=device, train=True, sort=True, sort_within_batch=True)
valid_iter = BucketIterator(valid, batch_size=32, sort_key=lambda x: len(x.text),
                            device=device, train=True, sort=True, sort_within_batch=True)
test_iter = Iterator(test, batch_size=32, device=device, train=False, shuffle=False, sort=False)
```

I've been using google colab.我一直在使用谷歌colab。 It might be a newbie question but I've been stuck on this for days and would really appreciate your help in this.这可能是一个新手问题,但我已经坚持了好几天了,非常感谢您在这方面的帮助。

The problem was that I was trying to use a multiclass dataset on the code which is apparently meant for binary classification.问题是我试图在代码上使用多类数据集,这显然是为了二进制分类。 Kindest thanks to everyone who tried helping!衷心感谢所有尝试提供帮助的人!

I also faced this issue it was because there was an empty line in my dataset which caused this.我也遇到了这个问题,因为我的数据集中有一个空行导致了这个问题。 Removing it fixed the issue删除它解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Torchtext AttributeError:“示例”对象没有属性“ text_content” - Torchtext AttributeError: 'Example' object has no attribute 'text_content' Pytorch 文本 AttributeError: &#39;BucketIterator&#39; 对象没有属性 - Pytorch Text AttributeError: ‘BucketIterator’ object has no attribute AttributeError:“ InputLayer”对象没有属性“ W” - AttributeError:'InputLayer' object has no attribute 'W' AttributeError:“ Tensor”对象没有属性“ attention” - AttributeError: 'Tensor' object has no attribute 'attention' AttributeError:“ParentedTree”对象没有属性“标签” - AttributeError: 'ParentedTree' object has no attribute 'label' AttributeError: 类型对象“语言”没有属性“工厂” - AttributeError: type object 'Language' has no attribute 'factory' AttributeError:“MaskedLMOutput”对象没有属性“视图” - AttributeError: 'MaskedLMOutput' object has no attribute 'view' AttributeError: 'list' object 没有属性 'lower' 与 CountVectorizer - AttributeError: 'list' object has no attribute 'lower' with CountVectorizer AttributeError: &#39;TFSequenceClassifierOutput&#39; 对象没有属性 &#39;argmax&#39; - AttributeError: 'TFSequenceClassifierOutput' object has no attribute 'argmax' 获取“ AttributeError: 'float' object has no attribute 'lower' ” - Getting “ AttributeError: 'float' object has no attribute 'lower' ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM