简体   繁体   中英

Can Anyone please help me in finding the solution to the error "AttributeError: 'Example' object has no attribute 'src_len'"?

I am working on a project which aims to make transformer neural network for machine translation using pytorch. I am facing the error which says "AttributeError: 'Example' object has no attribute 'src_len'".. Any help would be appreciated. Thank you

fields = {"English": ("eng", english), "Urdu": ("ur", urdu)}

train_data, test_data,valid_data= TabularDataset.splits(
path="", train="train.json", test="test.json",validation="val.json", format="json", fields=fields
)

english.build_vocab(train_data, max_size=10000, min_freq=2)
urdu.build_vocab(train_data, max_size=10000, min_freq=2)

train_iterator, valid_iterator, test_iterator = BucketIterator.splits(
(train_data, valid_data, test_data),
batch_size=32,
sort_within_batch=True,
sort_key=lambda x: (x.src_len),
device='cuda',
)

for batch in train_iterator:
print(batch) 

Error Message:

AttributeError                            
Traceback (most recent call last)
<ipython-input-27-9fe6d4873776> in <module>()
----> 1 for batch in train_iterator:
  2   print(batch)
  2 frames
<ipython-input-26-1c01ecf39930> in <lambda>(x)
   4     batch_size=32,
   5     sort_within_batch=True,
----> 6     sort_key=lambda x: (x.src_len),
   7     device='cuda',
   8 )

   AttributeError: 'Example' object has no attribute 'src_len'`

The object x does not have any attribute called src_len . Please check if len(x) works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM