• Register
1 vote
1.9k views

Problem :

I am very new to Pytorch. I am currently trying to train my pytorch model I am using the unet model. I am getting dimension out of range error as shown below:

/usr/local/lib/python3.5/dist-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)` 

Some part of my code:

def forward(self, logits, targets):
    probs = F.sigmoid(logits)
    probs_flat = probs.view(-1)
    targets_flat = targets.view(-1)
    return self.crossEntropy_loss(probs_flat, targets_flat)`

Please let me know how to fix above error.

2.3k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

I saw your question and your logs and according to your below code:

probs_flat = probs.view(-1)
targets_flat = targets.view(-1)
return self.crossEntropy_loss(probs_flat, targets_flat)

I guess you are trying to give the two 1d tensor to the nn.CrossEntropyLoss but according to documentation, it expects code as shown below:

Input: (N,C) where C = number of classes
Target: (N) where each value is 0 <= targets[i] <= C-1
Output: scalar. If reduce is False, then (N) instead.

I can surely say that was the cause of your problem which you had encountered

I hope it helps you fix your issue.

5k points