• Register
1 vote
517 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

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 1.6K views
1.6K 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 jwilliam 3.9k points
0 votes
1 answer 1.7K views
1.7K 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
2 answers 163 views
163 views
Problem : I am new to string handling & getting below error while trying to execute a program java.lang.stringindexoutofboundsexception: string index out of range: 0
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 249 views
249 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 32 views
32 views
Problem : I was getting the following error when running a SQL the conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
asked Nov 12, 2019 peterlaw 6.9k points