TypeError: 'Example' object does not support indexing

Legolas: <p>Hi,<br>
I’m trying to execute a cell code:<br>
import time<br>
from torch.utils.data.dataset import random_split<br>
N_EPOCHS = 5<br>
min_valid_loss = float(‘inf’)</p>
<p>criterion = torch.nn.CrossEntropyLoss().to(device)<br>
optimizer = torch.optim.SGD(model.parameters(), lr=4.0)<br>
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1, gamma=0.9)</p>
<p>train_len = int(len(train_dataset) * 0.95)<br>
sub_train_, sub_valid_ = <br>
random_split(train_dataset, [train_len, len(train_dataset) - train_len])</p>
<p>for epoch in range(N_EPOCHS):</p>
<pre><code>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)')
</code></pre>
<p>and got the following errors:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/8/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0" title="Example  object does not support indexing"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0_2_344x500.jpeg" alt="Example  object does not support indexing" data-base62-sha1="o6pCJAVmsMftKfmPumtY3Jbw17W" width="344" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0_2_344x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0_2_516x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/8/a8edc3e85b8e1cfdd12f99efddde4d39b07b7ac0.jpeg 2x" data-dominant-color="EFF1EF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Example  object does not support indexing</span><span class="informations">559×811 99.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Does anyone have some suggestion how to fix it?<br>
thank you</p>

ptrblck: <p>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 <code>Example</code> class.<br>
Could you check your <code>Dataset</code> for this class?</p>

Legolas: <p>Hi,<br>
thank you for your reply and your time.<br>
I have a simple dataset that looks something like:<br>
LABEL                          TEXT<br>
‘AB1’                             ‘Some text 1’<br>
‘AB2’                             ‘Some text 2’<br>
and so on<br>
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).<br>
How would you solve this problem with PyTorch in steps (or if you have some link for similar problems)?<br>
Thank you for your time</p>

ptrblck: <p>I’m not an expert in NLP, but you could use a lookup dict as done in the <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html">Seq2Seq tutorial</a>. I assume <code>torchtext</code> or other NLP-specific wrappers might also have some utility functions.</p>