• Register
1 vote
1.4k 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.

8 4 2
2,300 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.

5 2 1
4,980 points

Related questions

0 votes
1 answer 20 views
20 views
Problem: i will be thankful to the person who could help me in solve this problem? only one element tensors can be converted to python scalars.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 2.8K views
2.8K views
Problem : I am trying to convert my list into an array using Python, But I got below error ValueError: only one element tensors can be converted to Python scalars.
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 370 views
370 views
Problem : I am trying to compute a loss on the jacobian of the network but encountered following error one of the variables needed for gradient computation has been modified by an inplace operation
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 17 views
17 views
Problem: why is this still happinig with me ? Expected response code 250 but got code "530", with message "530 5.7.1 authentication required ".
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 15 views
15 views
Problem: expected response code 250 but got code "530", with message "530 5.7.1 authentication required
asked Feb 23 Muneeb Saadii 125k points
0 votes
1 answer 2.9K views
2.9K views
Problem : I got stuck on below error I am trying to configure my SMTP mail on laravel &ldquo;Expected response code 250 but got code "530", with message "530 5.7.1 authentication required "&rdquo; Below is the configuration on .env MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io ... I have tried to authenticate my SMTP on the env but I keep facing the above error so for now I am stuck on that error.
asked Feb 4, 2020 jwilliam 3.9k points
0 votes
1 answer 37 views
37 views
How is this caused and how can I solve it?
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 25 views
25 views
Problem: expected response code 220 but got code "", with message ""
asked Feb 17 Dan phillip 4.1k points
0 votes
1 answer 8 views
8 views
Problem: Can someone help me valueerror: buffer has wrong number of dimensions (expected 1, got 2)?
asked Mar 6 Umar Ahmad 4.1k points