examples: RuntimeError: invalid argument 2: size '[-1 x 300]' for SNLI example

I get the following error when I try to run the SNLI example on my machine.

Traceback (most recent call last):
  File "train.py", line 35, in <module>
    inputs.vocab.load_vectors(wv_dir=args.data_cache, wv_type=args.word_vectors, wv_dim=args.d_embed)
  File "/usr/local/lib/python2.7/dist-packages/torchtext/vocab.py", line 162, in load_vectors
    wv_dict, wv_arr, self.wv_size = load_word_vectors(wv_dir, wv_type, wv_dim)
  File "/usr/local/lib/python2.7/dist-packages/torchtext/vocab.py", line 70, in load_word_vectors
    wv_arr = torch.Tensor(wv_arr).view(-1, wv_size)
RuntimeError: invalid argument 2: size '[-1 x 300]' is invalid for input of with 544881656 elements at /pytorch/torch/lib/TH/THStorage.c:37

Not sure if this is an error in the way the vectors are being loaded, or an error in torchtext itself. I’m using Python 2.7.12 on Ubuntu 16.04.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15

Most upvoted comments

Had kind of the same issue with the same error but my padding was correct. With my issue I was flattening incorrectly and was able to resolve by verifying the size of x before flattening the tensor and corrected my x.view and the fc1 layer accordingly.

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # convolutional layer
        self.conv1 = nn.Conv2d(3, 6, 3, padding=1)
        self.conv2 = nn.Conv2d(6, 16, 3, padding=1)
        self.conv3 = nn.Conv2d(16, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        ->self.fc1 = nn.Linear(32 * 4 * 4, 120)
        self.fc2 = nn.Linear(120, 10)

    def forward(self, x):
        # add sequence of convolutional and max pooling layers
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        ->print(x.size())
        ->x = x.view(-1, 32 * 4 * 4)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        
        return x

I got the similar error, can you elaborate a little bit more on “You need to calculate the input size of this layer according to the input size of the net.”

I got the following error:

RuntimeError: invalid argument 2: size '[-1 x 1024]' is invalid for input with 11520 elements at 
..\aten\src\TH\THStorage.cpp:80

Full error information:

RuntimeError                              Traceback (most recent call last)
<ipython-input-9-6694bd56733d> in <module>()
     21         optimizer.zero_grad()
     22         # forward pass: compute predicted outputs by passing inputs to the model
---> 23         output = model(data)
     24         # calculate the batch loss
     25         loss = criterion(output, target)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

<ipython-input-7-971843679cb8> in forward(self, x)
     21         x = self.pool(F.relu(self.conv2(x)))
     22         x = self.pool(F.relu(self.conv3(x)))
---> 23         x = x.view(-1, 64*4*4)
     24         x = self.dropout(x)
     25         x = F.relu(self.fc1(x))

My code block:

import torch.nn as nn
import torch.nn.functional as F

# define the CNN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) # convolutional layer
        self.conv2 = nn.Conv2d(16, 32, kernel_size=3)
        self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
        self.pool = nn.MaxPool2d(2, 2) # max pooling layer
        #self.drop2d = nn.Dropout2d(0.2)
        self.fc1 = nn.Linear(64*4*4, 500) # densely connected layer
        self.fc2 = nn.Linear(500, 10)
        self.dropout = nn.Dropout(0.2)

    def forward(self, x):
        # add sequence of convolutional and max pooling layers
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        x = x.view(-1, 64*4*4)
        x = self.dropout(x)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x

# create a complete CNN
model = Net()

Found the cause: “parameter padding=1” was missing in one layer.

Wrong code:

    self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) # convolutional layer
    self.conv2 = nn.Conv2d(16, 32, kernel_size=3)
    self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)

Right code:

    self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) # convolutional layer
    self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
    self.conv3 = nn.Conv2d(32, 64, kernel_size=3, padding=1)