• Register
1 vote
827 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 2.3K views
2.3K 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 2.1K views
2.1K 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 308 views
308 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 6 views
6 views
How can I solve this?
asked Dec 30, 2020 TeamScript 9.2k points
0 votes
2 answers 199 views
199 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 41 views
41 views
Problem : I want to add the data as one by one row to my datagridview. But I am facing the following error: "Index was out of range. Must be non-negative and less than the size of the collection parameter name: index" Why am I facing this error ? ... ColumFive = Convert.ToString(sum); string[] row = new string[]{ ColumOne, ColumTwo, ColumThree, ColumFour, ColumFive }; myDataGridView.Rows.Add(row);
asked Dec 4, 2019 alecxe 7.5k points
0 votes
1 answer 46 views
46 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
0 votes
1 answer 103 views
103 views
Problem : I am getting bellow error related to datetime the conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
0 answers 62 views
62 views
Not able to fix the below error for the below logistic regression training=(IBM$Serial<625) data=IBM[!training,] dim(data) stock.direction <- data$Direction training_model=glm(stock.direction~data$lag2,data=data,family=binomial) ###Error### ---- Error in eval(family$initialize) : y ... 4936400 0.003630178 0.003630178 -0.001894039 -0.005576956 Up 3 4 03-12-2012 190.759995 191.300003 188.360001 189.
asked Oct 30, 2020 psandprop 2.4k points
0 votes
1 answer 139 views
139 views
Problem: I have fundamental knowledge on java , json and gson. I have below method: public static Object parseStringToObject(String json) {     String Object = json;     Gson gson = new Gson();     Object objects = gson.fromJson(object, Object. ... strange error message: "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1"
asked Aug 4, 2020 Raphael Pacheco 4.9k points