How to fix an error with the quickstart tutorial for pytorch?
I am trying to follow the tutorial on pytorch HERE, but there seems to be a problem. I have created a custom dataloader named training_data that returns an object as required HERE which is a dictionary
{&quot;image&quot;: image, &quot;label&quot;: label}

where image is a tensor and label is a string. I then follow the tutorial and create a DataLoader as follows:
train_dataloader = DataLoader(training_data, batch_size=batch_size)

and use that DataLoader in the method train:
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader)
    for batch, (X, y) in enumerate(dataloader):
        X, y = X.to(device), y.to(device)

        # Compute prediction error
        pred = model(X)
        loss = loss_fn(pred, y)

        # Backpropagation
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f&quot;loss: {loss:&gt;7f}  [{current:&gt;5d}/{size:&gt;5d}]&quot;)

However, when I call the training method for a batch
train(train_dataloader, model, loss_fn, optimizer)

I get an error
Traceback (most recent call last):
  File &quot;train_network.py&quot;, line 110, in &lt;module&gt;
    train(train_dataloader, model, loss_fn, optimizer)
  File &quot;train_network.py&quot;, line 76, in train
    X, y = X.to(device), y.to(device)
AttributeError: 'str' object has no attribute 'to'

as y is a string with the content label. What am I doing wrong?

Your labels y need to be torch tensors. Since you currently have strings, and assuming you are doing classification among n classes, you can simply map them using a list. For example, with three classes, inside the __init__ of your Dataset class:
self.label_names = [&quot;class1&quot;, &quot;class2&quot;, &quot;class3&quot;]

Then, in __getitem__, you could add:
label = torch.tensor(label_names.index(label))

where label previously stored a string.
