horsepurve / DeepRTplus

Deep (Transfer) Learning for Peptide Retention Time Prediction
MIT License
49 stars 22 forks source link

RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1, 1] because the unspecified dimension size -1 can be any value and is ambiguous #10

Closed stsour closed 3 years ago

stsour commented 4 years ago

When I try to predict RTs of sequences using a model I trained, I run into this error:

>> note: predicting using the model: param_cpu/Clark_500k_epoch_18_050520.pt Traceback (most recent call last): File "prediction_emb_cpu.py", line 83, in <module> obse,pred1=pred_from_model(conv1,conv1,round1model,RTtest,15) File "prediction_emb_cpu.py", line 35, in pred_from_model pred_batch = model(test_batch) File "/home/tsour.s/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__ result = self.forward(*input, **kwargs) File "/home/tsour.s/DeepRTplus/capsule_network_emb_cpu.py", line 275, in forward x = self.primary_capsules(x) File "/home/tsour.s/.local/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__ result = self.forward(*input, **kwargs) File "/home/tsour.s/DeepRTplus/capsule_network_emb_cpu.py", line 149, in forward outputs = [capsule(x).view(x.size(0), -1, 1) for capsule in self.capsules] File "/home/tsour.s/DeepRTplus/capsule_network_emb_cpu.py", line 149, in <listcomp> outputs = [capsule(x).view(x.size(0), -1, 1) for capsule in self.capsules] RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1, 1] because the unspecified dimension size -1 can be any value and is ambiguous

The same model successfully predicted RTs of a similar group of peptides from the same dataset with no issues. When I concatenate the two peptide lists, the prediction algorithm runs.

I assume this must have something to do with the input data, but I have not been able to identify any discrepancies.

horsepurve commented 4 years ago

The error said there was an empty batch (tensor of 0 elements) so I guess it has something to do with the number of peptides in the concatenated list. Can you try another batch size in line 83 of "prediction_emb_cpu.py":

obse,pred1=pred_from_model(conv1,conv1,round1model,RTtest,15) # change 15 to another value

If there is still this error, can you provide more information like the number of peptides, pytorch version (import torch print(torch.__version__)), etc.

stsour commented 4 years ago

I arbitrarily set the batch size to 100 and it ran smoothly.

It's interesting though because the concatenated list (A + B ~ 120,000 peptides) processed smoothly before. List B (~30,000 peptides) ran as well, but list A independently did not work.

horsepurve commented 4 years ago

Another user came across this bug before. If the number of the peptides is N, then if N % batch_size == 0 or 1, there will be an error: If N % batch_size == 0, such as in your case N = 15 * x, then the last batch is an empty batch which pytorch cannot handle. If N % batch_size == 1, i.e. there is only one row in a matrix, then pytorch will treat it differently as a vector. I plan to fix this bug in a future version... but currently simply change the batch size to circumvent this issue.

stsour commented 4 years ago

That makes sense, my exact N for list A was 99,510, so that aligns with N%15==0.

Thanks for the fix and clarifications!

MUKILAN-2003 commented 2 years ago

@horsepurve @stsour image

DeepLabV3+ Thrown an error. BatchSize=4 and Epoch=10 The Error was in DiceLoss Function. I am using Pytorch-Segmetation-Models Modules

Did you fix the issue !? If yes how ?