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
{"image": image, "label": 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"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
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 "train_network.py", line 110, in <module>
train(train_dataloader, model, loss_fn, optimizer)
File "train_network.py", 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?