could not determine the shape of object type 'Data' - converting pytorch list to cuda?

Viewed 13

I want to convert a list to a format that can be read into a model in CUDA format.

I have this:

print(type(train_dataset))
train_dataset = torch.tensor(train_dataset, device='cuda:0')
print(type(train_dataset))

The output is:

<class 'list'>
Traceback (most recent call last):
  File "test_pytorch_test_gpu2.py", line 882, in <module>
    train_dataset = torch.tensor(train_dataset, device='cuda:0')
ValueError: could not determine the shape of object type 'Data'

Could someone explain how to convert a list to a format for cuda/what is wrong with what I did?

1 Answers

A Dataset is an object wrapping around your training data and it's main function is to organize the data, the labels, and possibly their augmentations.

On top of Dataset one usually uses a DataLoader: this object collects training samples from the underlying Dataset and puts them into tensors representing mini-batches for training.

Your code should look something like this:

# one-time setup of training data
train_dataset = ...  # your code that construct the Dataset
train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=2)

# training loop
for e in range(num_epochs):
  # each epoch represents one pass over all the training samples.
  for (x, y) in train_loader:  # iterate over the data one _batch_ at a time
    # move training tensors, extracted from the Dataset, to GPU
    x = x.to(device)  
    y = y.to(device)
    # rest of your training code here:
    pred = model(x) 
    # ...
Related