Custom data loader and error in training loop

username: AlexSwiderski
cooked: <p>Hi everyone,</p>
<p>I am looking for some help in fixing an error in my model.  I utilized a custom dataloader that looks like this:</p>
<pre><code class="lang-auto">from torch.utils.data 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

</code></pre>
<p>Then create my train and validation data like this:</p>
<pre><code class="lang-auto">train_loader = CustomDataset(X_train, y_train)

valid_loader = CustomDataset(X_test, y_test)

</code></pre>
<p>The error arises when I run the model and occurs in my training loop:</p>
<pre><code class="lang-auto">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 = text.to(device), target.to(device)

</code></pre>
<p>The error I am getting is as follows:</p>
<pre><code class="lang-auto">     48     for idx, batch in enumerate(train_loader):
---&gt; 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)
</code></pre>
<p>Below is the code I use to initialze the model, train, and test:</p>
<pre><code class="lang-auto">set_seed(SEED)
vanilla_rnn_model = VanillaRNN(output_size, input_size, RNN_size, fc_size, DEVICE)
vanilla_rnn_model.to(DEVICE)
vanilla_rnn_start_time = time.time()
vanilla_train_loss, vanilla_train_acc, vanilla_validation_loss, vanilla_validation_acc = train(vanilla_rnn_model,
                                                                                               train_loader,
                                                                                               valid_loader,
                                                                                               DEVICE,
                                                                                               epochs = epochs,
                                                                                               learning_rate = learning_rate
                                                                                               )
</code></pre>
<p>Obviously the model does not like the DataLoader, but I am confused on how to remediate the issue.  Any help would be appreciated.</p>

username: gphilip
cooked: <p>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 <strong>not</strong> 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.</p>
<p>So the formatting will look like this:</p>
<p>line1 with just three back-ticks one after the other</p>
<p>your code goes here</p>
<p>line2 with just three back-ticks one after the other</p>
<p>Also: could you show the line where you <strong>call</strong> your <code>train()</code> function?</p>

username: AlexSwiderski
cooked: <p>Thanks for the help re: back-tick!  I have reformatted and added the requested code.</p>

username: gphilip
cooked: <p>Cool. Compare the way you call your <code>train()</code> function with the way you have <em>defined</em> the <code>train()</code> function, and see if you can find something different. That will solve this problem for you.</p>