Custom data loader and error in training loop

Hi everyone,

I am looking for some help in fixing an error in my model. I utilized a custom dataloader that looks like this:

from import Dataset, DataLoader

class CustomDataset(Dataset):
    def __init__(self, dat, labels):
        self.labels = labels
        self.dat = dat

    def __len__(self):
        return len(self.labels)
    def __getitem__(self, idx):
        label = self.labels[idx]
        dat = self.dat[idx]
        sample = {"Sample": dat, "Class": label}
        return sample

Then create my train and validation data like this:

train_loader = CustomDataset(X_train, y_train)

valid_loader = CustomDataset(X_test, y_test)

The error arises when I run the model and occurs in my training loop:

def train(model, device, train_loader, valid_loader, epochs, learning_rate):

    for idx, batch in enumerate(train_loader):
      text = batch["Sample"].to(device)
      target = batch['Class'].to(device)
      target = torch.autograd.Variable(target).long()**
      target =,

The error I am getting is as follows:

     48     for idx, batch in enumerate(train_loader):
---> 49       text = batch["Sample"].to(device)
     50       target = batch['Class'].to(device)
     51       print(type(text), text.shape)

TypeError: to() received an invalid combination of arguments - got (DataLoader), but expected one of:
 * (torch.device device, torch.dtype dtype, bool non_blocking, bool copy, *, torch.memory_format memory_format)
 * (torch.dtype dtype, bool non_blocking, bool copy, *, torch.memory_format memory_format)
 * (Tensor tensor, bool non_blocking, bool copy, *, torch.memory_format memory_format)

Below is the code I use to initialze the model, train, and test:

vanilla_rnn_model = VanillaRNN(output_size, input_size, RNN_size, fc_size, DEVICE)
vanilla_rnn_start_time = time.time()
vanilla_train_loss, vanilla_train_acc, vanilla_validation_loss, vanilla_validation_acc = train(vanilla_rnn_model,
                                                                                               epochs = epochs,
                                                                                               learning_rate = learning_rate

Obviously the model does not like the DataLoader, but I am confused on how to remediate the issue. Any help would be appreciated.

Your post is hard to read because of the way you have formatted the code. Could you enclose code between two lines, where each line has just three consecutive back-ticks on it? The back-tick is this character: ` . It may be on the same key as the tilde, ~ . It is not the same as the single quote (’) or the double quote ("). (You can just copy-paste the above back-tick, if you can’t find it on your keyboard.) Also, don’t use one single quote and a double quote together, instead of three back-ticks! That doesn’t work, as you can see from the way your post looks.

So the formatting will look like this:

line1 with just three back-ticks one after the other

your code goes here

line2 with just three back-ticks one after the other

Also: could you show the line where you call your train() function?

Thanks for the help re: back-tick! I have reformatted and added the requested code.

Cool. Compare the way you call your train() function with the way you have defined the train() function, and see if you can find something different. That will solve this problem for you.