Skip to content

Sizes of tensors must match except in dimension 1 #17

@Krik1997

Description

@Krik1997

Good afternoon, Mohammed,

Recently, I decided to take your course on building a neural network model for liver segmentation. In my case, I chose lung tumor segmentation from the same website where you downloaded the lung dataset. However, I encountered a problem that I have not been able to solve yet. I would appreciate your help!

Currently, I am trying to start training the model, but I am getting the following error:
"Sizes of tensors must match except in dimension 1. Expected size 15 but got size 16 for tensor number 1 in the list."
How can I resolve this issue?

You can read the Error Message below:

RuntimeError                              Traceback (most recent call last)
Cell In[12], [line 16](vscode-notebook-cell:?execution_count=12&line=16)
     [13](vscode-notebook-cell:?execution_count=12&line=13) optimizer = torch.optim.Adam(model.parameters(), 1e-5, weight_decay=1e-5, amsgrad=True)
     [15](vscode-notebook-cell:?execution_count=12&line=15) if __name__ == '__main__':
---> [16](vscode-notebook-cell:?execution_count=12&line=16)     train(model, data_in, loss_function, optimizer, 600, model_dir)

File c:\Users\alexp\.conda\envs\NNCT_Py39\code\utilities.py:52, in train(model, data_in, loss, optim, max_epochs, model_dir, test_interval, device)
     [49](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/code/utilities.py:49) volume, label = (volume.to(device), label.to(device))
     [51](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/code/utilities.py:51) optim.zero_grad()
---> [52](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/code/utilities.py:52) outputs = model(volume)
     [54](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/code/utilities.py:54) train_loss = loss(outputs, label)
     [56](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/code/utilities.py:56) train_loss.backward()

File c:\Users\alexp\.conda\envs\NNCT_Py39\lib\site-packages\torch\nn\modules\module.py:1739, in Module._wrapped_call_impl(self, *args, **kwargs)
   [1737](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1737)     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   [1738](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1738) else:
-> [1739](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1739)     return self._call_impl(*args, **kwargs)

File c:\Users\alexp\.conda\envs\NNCT_Py39\lib\site-packages\torch\nn\modules\module.py:1750, in Module._call_impl(self, *args, **kwargs)
   [1745](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1745) # If we don't have any hooks, we want to skip the rest of the logic in
   [1746](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1746) # this function, and just call forward.
   [1747](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1747) if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   [1748](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1748)         or _global_backward_pre_hooks or _global_backward_hooks
   [1749](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/nn/modules/module.py:1749)         or _global_forward_hooks or _global_forward_pre_hooks):
...
-> [1648](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/_tensor.py:1648)     ret = func(*args, **kwargs)
   [1649](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/_tensor.py:1649)     if func in get_default_nowrap_functions():
   [1650](file:///C:/Users/alexp/.conda/envs/NNCT_Py39/lib/site-packages/torch/_tensor.py:1650)         return ret

RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 15 but got size 16 for tensor number 1 in the list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions