0
$\begingroup$

I am writing a NN in pytorch. I have a list of tensors as input i.e. I created a list Y by appending 1000 tensor vectors(linear tensors) of size 3072. So, each Y[i] is a linear tensor of size 3072. Now, when I defined train_loader as

train_loader= torch.utils.data.DataLoader(Y, batch_size= 5, shuffle= False)

Now, when I defined my NN, in training as:

epochs = 10

train_loss_list = []
test_loss_list = []

for epoch in range(1, epochs + 1):
    ### =====TRAINING=====
    model.train()
    train_loss = 0
    with tqdm(train_loader, unit="batch") as train_epoch_pbar:
        for data, _ in enumerate(train_epoch_pbar):
            train_epoch_pbar.set_description(f"Epoch {epoch}")
            #data = data.to(device)

            optimizer.zero_grad()
            outputs = model(data)
            loss = loss_function(outputs, Con)
            loss.backward()
            optimizer.step()

I got the following error:


TypeError                                 Traceback (most recent call last)
<ipython-input-105-73ca0f1b0caa> in <module>
     14 
     15             optimizer.zero_grad()
---> 16             outputs = model(data)
     17             loss = loss_function(outputs, Con)
     18             loss.backward()

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-86-4f58fb599c6f> in forward(self, input)
     15 
     16     def forward(self, input):
---> 17         x= self.decoder(input)
     18 
     19         print(x.size())

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
    117     def forward(self, input):
    118         for module in self:
--> 119             input = module(input)
    120         return input
    121 

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\Anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     92 
     93     def forward(self, input: Tensor) -> Tensor:
---> 94         return F.linear(input, self.weight, self.bias)
     95 
     96     def extra_repr(self) -> str:

~\Anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1751     if has_torch_function_variadic(input, weight):
   1752         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1753     return torch._C._nn.linear(input, weight, bias)
   1754 
   1755 

TypeError: linear(): argument 'input' (position 1) must be Tensor, not int

BTW, my input tensors are from CIFAR10 dataset which were normalized to [0,1]. So, the error make no sense to me. Also, if I do not comment the code ```data= data.to(device) in coding, get similar error on that line that int has no attribute to.. Seems that my network takes elements of my list Y as integer. Why is this happening?

$\endgroup$

1 Answer 1

0
$\begingroup$

You're unpacking the enumerate incorrectly.

for data, _ in enumerate(train_epoch_pbar):

The first value in enumerate is the index.

lst = ['a', 'b', 'c']
for index, value in enumerate(lst):
    print(index, value)

outputs

0 a
1 b
2 c

The errors are correct - you are passing the integer index value in as your data.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.