title: How to fix AttributeError: 'tuple' object has no attribute 'to'?
body: <p><br />
  Today i faced with problem
  <code>AttributeError: 'tuple' object has no attribute 'to'</code><br />
  I read a data from csv file with 2 columns: Image (where the file path is) and finding (where the photo's label is)<br />
  Model:</p>
  <pre><code>model = models.resnet18(pretrained=False)
  model.fc = nn.Sequential(nn.Linear(model.fc.in_features, 256),
                           nn.ReLU(),
                           nn.Dropout(p=0.3),
                           nn.Linear(256, 100),
                           nn.ReLU(),
                           nn.Dropout(p=0.4),
                           nn.Linear(100,9))
  # model.load_state_dict(torch.load('model.pth'))
  
  for name, param in model.named_parameters():
      if(&quot;bn&quot; not in name):
          param.requires_grad = False
  </code></pre>
  <p>Transforms:</p>
  <pre><code>transform = transforms.Compose([
      transforms.Resize((224, 224)),
      transforms.ToTensor(),
      transforms.Normalize(mean=[0.5457, 0.5457, 0.5457], std=[0.2342, 0.2342, 0.2342])    
  ])
  </code></pre>
  <p>Dataset class:</p>
  <pre><code>class Col(Dataset):
      def __init__(self, csv, main_dir, transform):
          self.df = pd.read_csv(csv)
          self.main_dir = main_dir
          self.transform = transform
  
      def __len__(self):
          return self.df.shape[0]
  
      def __getitem__(self, idx):
          image = transform(Image.open(self.df.Image[idx]).convert(&quot;RGB&quot;))
          label = self.df.Finding[idx]
          return image, label
  </code></pre>
  <p>Prepairing Data:</p>
  <pre><code>data = Col(main_dir=root_dir,csv=csv_file, transform=transform)
  dataset = torch.utils.data.DataLoader(data, batch_size=130)
  train_set, validate_set= torch.utils.data.random_split(dataset, [round(len(dataset)*0.7), (len(dataset) - round(len(dataset)*0.7))])
  </code></pre>
  <p>Train func:</p>
  <pre><code>def train(model, optimizer, loss_fn, train_set, validate_set, epochs=20, device=&quot;cpu&quot;):
      for epoch in range(1, epochs+1):
          training_loss = 0.0
          valid_loss = 0.0
          model.train()
          for batch in train_set:
              optimizer.zero_grad()
              inputs, labels = batch
              inputs = inputs.to(device)
              labels = labels.to(device)
              output = model(inputs)
              loss = loss_fn(output, labels)
              loss.backward()
              optimizer.step()
              training_loss += loss.data.item() * inputs.size(0)
          training_loss /= len(train_set.dataset)
          
          model.eval()
          num_correct = 0 
          num_examples = 0
          for batch in validate_set:
              inputs, labels = batch
              inputs = inputs.to(device)
              output = model(inputs)
              labels = labels.to(device)
              loss = loss_fn(output, labels) 
              valid_loss += loss.data.item() * inputs.size(0)
              correct = torch.eq(torch.max(F.softmax(output, dim=1), dim=1)[1], targets)
              num_correct += torch.sum(correct).item()
              num_examples += correct.shape[0]
          valid_loss /= len(validate_set.dataset)
  
          print('Epoch: {}, Training Loss: {:.2f}, Validation Loss: {:.2f}, accuracy = {:.2f}'.format(epoch, training_loss,
          valid_loss, num_correct / num_examples))
  </code></pre>
  <p>Optimazer:</p>
  <pre><code>optimizer = optim.Adam(model.parameters(), lr=0.0001)
  </code></pre>
  <p>After calling train func</p>
  <pre><code>train(model, optimizer,torch.nn.CrossEntropyLoss(), train_set.dataset, validate_set.dataset, epochs=100, device=device)
  </code></pre>
  <p>I've got this error:</p>
  <pre><code>---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  /tmp/ipykernel_9156/634509595.py in &lt;module&gt;
  ----&gt; 1 train(model, optimizer,torch.nn.CrossEntropyLoss(), train_set.dataset, validate_set.dataset, epochs=100, device=device)
  
  /tmp/ipykernel_9156/2858123881.py in train(model, optimizer, loss_fn, train_set, validate_set, epochs, device)
        8             inputs, labels = batch
        9             inputs = inputs.to(device)
  ---&gt; 10             labels = labels.to(device)
       11             output = model(inputs)
       12             loss = loss_fn(output, labels)
  
  AttributeError: 'tuple' object has no attribute 'to'
  </code></pre>
  <p>If i try to call this</p>
  <pre><code>for batch in train_set.dataset:
      inputs, labels = batch
      print(labels)
  </code></pre>
  <p>then it will display many large tuples with labels.(like this)</p>
  <pre><code>('polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps', 'polyps')
  </code></pre>
  <p>How can i fix this? Help me please</p>