AttributeError: 'list' object has no attribute 'cuda'

alpha2210: <p>Hello All,<br>
I am getting an above error when I am calling</p>
<pre><code class="lang-auto">fit(siamese_train_loader, siamese_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval)
</code></pre>
<p>for</p>
<pre><code class="lang-auto">for batch_idx, (data, target) in enumerate(train_loader):
        target = target if len(target) &gt; 0 else None
        if not type(data) in (tuple, list):
            data = (data,)
        if cuda:
            data = tuple(d.cuda() for d in data)
            if target is not None:
                print(target)
                target = target.cuda()
</code></pre>
<p>output of print list is as follows:</p>
<pre><code class="lang-auto">[tensor([[[[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.1943, -0.4279],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2509,  0.0435],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2016, -0.2016, -0.0885],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2320,  0.2132,  0.2320],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.1943, -0.4279],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2509,  0.0435],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2016, -0.2016, -0.0885],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2320,  0.2132,  0.2320],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.1943, -0.4279],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2509,  0.0435],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2016, -0.2016, -0.0885],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2320,  0.2132,  0.2320],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]]],


        [[[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]]],


        [[[ 0.2697,  0.2697,  0.2697,  ..., -0.2393, -0.3148, -0.2959],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.1754,  0.1377,  0.1566],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2582, -0.3713, -0.3336],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ..., -0.2393, -0.3148, -0.2959],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.1754,  0.1377,  0.1566],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2582, -0.3713, -0.3336],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ..., -0.2393, -0.3148, -0.2959],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.1754,  0.1377,  0.1566],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.2582, -0.3713, -0.3336],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]]],


        ...,


        [[[ 0.0058, -0.0885,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [-0.3148, -0.5410,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.1639,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.3148,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.8615,  0.2132,  0.2697]],

         [[ 0.0058, -0.0885,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [-0.3148, -0.5410,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.1639,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.3148,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.8615,  0.2132,  0.2697]],

         [[ 0.0058, -0.0885,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [-0.3148, -0.5410,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ..., -0.1639,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.3148,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ..., -0.8615,  0.2132,  0.2697]]],


        [[[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]]],


        [[[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]],

         [[ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          ...,
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697],
          [ 0.2697,  0.2697,  0.2697,  ...,  0.2697,  0.2697,  0.2697]]]]), 
tensor([24, 23, 12, 21,  7,  3, 24,  3, 14,  4, 10,  3, 11,  9, 15, 20, 14, 11,
         1, 11, 16,  2, 16, 26, 22, 10,  0, 17, 25,  0, 11, 11,  0,  5,  8, 13,
        20,  2, 26, 22, 19,  5,  6,  6, 10, 26,  2,  2,  1, 10, 11,  3, 17,  5,
        21,  6,  5, 19, 26,  1,  4,  5, 19, 12,  2, 25, 21, 18, 17,  4, 15, 20,
        24, 20,  5,  2, 23, 26, 11, 18, 14, 14,  0, 26, 20,  6, 19, 19, 12,  6,
        25, 21, 23, 18,  1, 17, 22, 18,  5,  1, 16, 15, 10,  5, 15,  2,  4, 19,
         8, 14, 19, 13,  1,  3, 19,  9, 11, 26, 10, 18, 21,  8, 13, 24, 15,  2,
        24,  2])]
</code></pre>
<p>I tried to convert this list into NumPy array but it gives me another error that<br>
<strong>“ValueError: only one element tensors can be converted to Python scalars”</strong></p>
<p>What is the solution for the above? Any suggestions are welcome.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/3/4318d987dec14d948728f56254611c6d4088bced.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/4318d987dec14d948728f56254611c6d4088bced" title="2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/4318d987dec14d948728f56254611c6d4088bced_2_690x98.jpeg" alt="2" data-base62-sha1="9zzejgQHVzE6q25OxqfpYGy476t" width="690" height="98" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/4318d987dec14d948728f56254611c6d4088bced_2_690x98.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/3/4318d987dec14d948728f56254611c6d4088bced_2_1035x147.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/3/4318d987dec14d948728f56254611c6d4088bced.jpeg 2x" data-dominant-color="2F2F2F"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">2</span><span class="informations">1241×177 56.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Thanks in advance.</p>

eqy: <p>It looks like there is a custom dataloader implementation being used that doesn’t return target tensors but rather a list of tensors.<br>
Do you want to convert the targets to a single tensor? You can likely just use <a href="https://pytorch.org/docs/stable/generated/torch.stack.html" rel="noopener nofollow ugc">stack</a> or <a href="https://pytorch.org/docs/stable/generated/torch.cat.html" rel="noopener nofollow ugc">cat</a> depending on whether you want a dimension to be added.</p>
<p>Otherwise, you can do something similar to what is already done for data (<code>target = [t.cuda() for t in target]</code>).</p>

alpha2210: <aside class="quote no-group" data-username="eqy" data-post="2" data-topic="121173">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eqy/48/46233_2.png" class="avatar"> eqy:</div>
<blockquote>
<p><code>target = [t.cuda() for t in target]</code>).</p>
</blockquote>
</aside>
<p>Thanks for the reply, after applying the solution and putting``(<code>target = [t.cuda() for t in target]</code> )`<br>
it gives me another following error, what would be the reason for this?<br>
shall I use cat/stack?<br>
<strong>RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 5, 5], but got 1-dimensional input of size [128] instead</strong></p>

eqy: <p>It looks like you may be passing your target where you intend to pass the data (maybe double check the target and data are the shapes you expect).</p>

alpha2210: <p>I am using this neural network architecture.</p>
<pre><code class="lang-auto">File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/sharad/Few-shot-classification-siamese/siamese-triplet-master/networks.py", line 68, in forward
    output2 = self.embedding_net(x2)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/sharad/Few-shot-classification-siamese/siamese-triplet-master/networks.py", line 21, in forward
    output = self.convnet(x)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/container.py", line 119, in forward
   input = module(input)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 399, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "/home/sharad/miniconda3/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 395, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Expected 4-dimensional input for 4-dimensional weight [32, 3, 5, 5], but got 1-dimensional input of size [128] instead
</code></pre>
<p>according to errrorlog its causing problem in Siamesenet as I mentioned above.</p>
<pre><code class="lang-auto">import torch.nn as nn
import torch.nn.functional as F


class EmbeddingNet(nn.Module):
    def __init__(self):
        super(EmbeddingNet, self).__init__()
        self.convnet = nn.Sequential(nn.Conv2d(3, 32, 5), nn.PReLU(),
                                     nn.MaxPool2d(2, stride=2),
                                     nn.Conv2d(32, 64, 5), nn.PReLU(),
                                     nn.MaxPool2d(2, stride=2))

        self.fc = nn.Sequential(nn.Linear(64 * 61 * 61, 256),
                                nn.PReLU(),
                                nn.Linear(256, 256),
                                nn.PReLU(),
                                nn.Linear(256, 2)
                                )

    def forward(self, x):
        output = self.convnet(x)
        output = output.view(output.size()[0], -1)
        output = self.fc(output)
        return output

    def get_embedding(self, x):
        return self.forward(x)


class EmbeddingNetL2(EmbeddingNet):
    def __init__(self):
        super(EmbeddingNetL2, self).__init__()

    def forward(self, x):
        output = super(EmbeddingNetL2, self).forward(x)
        output /= output.pow(2).sum(1, keepdim=True).sqrt()
        return output

    def get_embedding(self, x):
        return self.forward(x)


class ClassificationNet(nn.Module):
    def __init__(self, embedding_net, n_classes):
        super(ClassificationNet, self).__init__()
        self.embedding_net = embedding_net
        self.n_classes = n_classes
        self.nonlinear = nn.PReLU()
        self.fc1 = nn.Linear(2, n_classes)

    def forward(self, x):
        output = self.embedding_net(x)
        output = self.nonlinear(output)
        scores = F.log_softmax(self.fc1(output), dim=-1)
        return scores

    def get_embedding(self, x):
        return self.nonlinear(self.embedding_net(x))


class SiameseNet(nn.Module):
    def __init__(self, embedding_net):
        super(SiameseNet, self).__init__()
        self.embedding_net = embedding_net

    def forward(self, x1, x2):
        output1 = self.embedding_net(x1)
        print(output1.shape)
        output2 = self.embedding_net(x2)
        print(output2.shape)
        return output1, output2

    def get_embedding(self, x):
        return self.embedding_net(x)


class TripletNet(nn.Module):
    def __init__(self, embedding_net):
        super(TripletNet, self).__init__()
        self.embedding_net = embedding_net

    def forward(self, x1, x2, x3):
        output1 = self.embedding_net(x1)
        output2 = self.embedding_net(x2)
        output3 = self.embedding_net(x3)
        return output1, output2, output3

    def get_embedding(self, x):
        return self.embedding_net(x)
</code></pre>
<p>I have tried to change the input size but still its giving me an error.</p>