TypeError: 'Example' object does not support indexing

Hi,
I’m trying to execute a cell code:
import time
from torch.utils.data.dataset import random_split
N_EPOCHS = 5
min_valid_loss = float(‘inf’)

criterion = torch.nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=4.0)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1, gamma=0.9)

train_len = int(len(train_dataset) * 0.95)
sub_train_, sub_valid_ =
random_split(train_dataset, [train_len, len(train_dataset) - train_len])

for epoch in range(N_EPOCHS):

start_time = time.time()
train_loss, train_acc = train_func(sub_train_)
valid_loss, valid_acc = test(sub_valid_)

secs = int(time.time() - start_time)
mins = secs / 60
secs = secs % 60

print('Epoch: %d' %(epoch + 1), " | time in %d minutes, %d seconds" %(mins, secs))
print(f'\tLoss: {train_loss:.4f}(train)\t|\tAcc: {train_acc * 100:.1f}%(train)')
print(f'\tLoss: {valid_loss:.4f}(valid)\t|\tAcc: {valid_acc * 100:.1f}%(valid)')

and got the following errors:

Does anyone have some suggestion how to fix it?
thank you

Based on the error message it seems that your data does not contain tensors (or any object, which could be indexed), but instead objects of an Example class.
Could you check your Dataset for this class?

Hi,
thank you for your reply and your time.
I have a simple dataset that looks something like:
LABEL TEXT
‘AB1’ ‘Some text 1’
‘AB2’ ‘Some text 2’
and so on
I have 10 different labels and I wanna create a model for multiclass classification according to these labels. Before Dataset, just to check me if I have set up the problem correctly: I should encode data in LABEL columns and make embedding for words in column TEXT (of course, after cleaning and tokenization).
How would you solve this problem with PyTorch in steps (or if you have some link for similar problems)?
Thank you for your time

I’m not an expert in NLP, but you could use a lookup dict as done in the Seq2Seq tutorial. I assume torchtext or other NLP-specific wrappers might also have some utility functions.