RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-221-4a1832287d27> in <module>()
      1 model, criterion, optimizer = build_model()
----> 2 train_model(config, model, criterion, optimizer)
      3 
      4 if config.config_type == 'Q1_4' or config.config_type == 'Q1_5':
      5     dropout_validates = []

<ipython-input-218-85332b119cd8> in train_model(config, model, criterion, optimizer)
     33             parameter_norms.append(parameter_norm)
     34         # print the results for this epoch
---> 35         test_loss = find_testloss(model, testloader)
     36         test_losses.append(test_loss)
     37         loss_at_epoch = np.mean(losses)

<ipython-input-219-5d5ffb8b6f0d> in find_testloss(model, test_loader)
     15 
     16         outputs = model(inputs)
---> 17         test_loss = criterion(outputs, targets)
     18         test_loss_iter.append(test_loss)
     19     iteration_test_loss = np.mean(test_loss_iter)

/anaconda/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    355             result = self._slow_forward(*input, **kwargs)
    356         else:
--> 357             result = self.forward(*input, **kwargs)
    358         for hook in self._forward_hooks.values():
    359             hook_result = hook(self, input, result)

/anaconda/envs/py36/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    677         _assert_no_grad(target)
    678         return F.cross_entropy(input, target, self.weight, self.size_average,
--> 679                                self.ignore_index, self.reduce)
    680 
    681 

/anaconda/envs/py36/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce)
   1159         >>> loss.backward()
   1160     """
-> 1161     return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
   1162 
   1163 

/anaconda/envs/py36/lib/python3.6/site-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel)
    784     if dim is None:
    785         dim = _get_softmax_dim('log_softmax', input.dim(), _stacklevel)
--> 786     return torch._C._nn.log_softmax(input, dim)
    787 
    788 

RuntimeError: dimension out of range (expected to be in range of [-1, 0], but got 1)

The find_testloss method looks like this:

def find_testloss(model,test_loader):
    model.eval() 
    test_loss_iter = []
    for data in test_loader:
        inputs, targets = data

        if config.model_type == 'MLPa' or config.model_type == 'MLPb':
            inputs = Variable(inputs, volatile=True).view(-1,784)
            targets = Variable(targets, volatile=True).view(-1)
        elif config.model_type == 'CNN':
            inputs = Variable(inputs, volatile=True).view(-1,1,28,28)
            targets = Variable(targets, volatile=True).view(-1)
        if cuda_available:
            inputs, targets = inputs.cuda(), targets.cuda()

        outputs = model(inputs)
        test_loss = criterion(outputs, targets)
        test_loss_iter.append(test_loss)
    iteration_test_loss = np.mean(test_loss_iter)
    return iteration_test_loss.data[0]

The method above has worked correctly for previous models and I have verified that the correct view() transformation is occuring.

How can I solve this problem please?

1 Like