Pytorch CNN mat1 和 mat2 形状不能相乘((128x29040 和 2048x50)

发布于 2025-01-12 18:53:47 字数 3296 浏览 1 评论 0原文

在 Pytorch 上构建 CNN 我遇到了关于矩阵维度的错误,并且无法解决它,因为我对 Pytorch 和 CNN 有点陌生,我知道这是我的层中特征值的问题,但不完全是这些维度的问题,这是模型和错误跟踪,我的批量大小是128,我的数据是转换为张量的图像,图像是100*100像素:

     def __init__(self,classes):
       super(roadModel, self).__init__()
       self.conv1 = Conv2d(3,30,kernel_size=(5,5))
       self.relu1 = ReLU()
       self.maxpool1 = MaxPool2d(kernel_size=(2,2), stride=(2,2))
       self.conv2 = Conv2d(30,60,kernel_size=(5,5))
       self.relu2 = ReLU()
       self.maxpool2 = MaxPool2d(kernel_size=(2,2), stride=(2,2)) 
       self.fc1 = Linear(in_features=128*4*4, out_features=50)
       self.relu3 = ReLU()
       self.fc2 = Linear(in_features=50, out_features=43)
       
    
    def forward(self, x):

        x = self.conv1(x)
        x = self.relu1(x)
        x = self.maxpool1(x)

        x = self.conv2(x)
        x = self.relu2(x)
        x = self.maxpool2(x)

        x = flatten(x, 1)
        x = self.fc1(x)
        x = self.relu3(x)

        out = self.fc2(x)
        

        return out
    
    RuntimeError                              Traceback (most recent call last)
<ipython-input-15-978518dfa1ac> in <module>
     12     for (inputs, labels) in trainDataLoader:
     13         (inputs, labels) = (inputs.to(device), labels.to(device))
---> 14         prediction = model(inputs)
     15         loss = lossFunction(prediction, labels)
     16         opt.zero_grad()

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-13-58a66aa14326> in forward(self, x)
     24 
     25         x = flatten(x, 1)
---> 26         x = self.fc1(x)
     27         x = self.relu3(x)
     28 

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -> Tensor:
--> 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -> str:

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x29040 and 2048x50)

Building a CNN on Pytorch i have this error about dimensions of matrix and i cannot resolve it since iam a bit new to Pytorch and CNNs , i know it's a problem with the values of in features in my layers but not exactly what wrong with these dimensions , here is the model and the trace of error , my batch size is 128 , my data are images transformed to tensors , images are 100*100 px:

     def __init__(self,classes):
       super(roadModel, self).__init__()
       self.conv1 = Conv2d(3,30,kernel_size=(5,5))
       self.relu1 = ReLU()
       self.maxpool1 = MaxPool2d(kernel_size=(2,2), stride=(2,2))
       self.conv2 = Conv2d(30,60,kernel_size=(5,5))
       self.relu2 = ReLU()
       self.maxpool2 = MaxPool2d(kernel_size=(2,2), stride=(2,2)) 
       self.fc1 = Linear(in_features=128*4*4, out_features=50)
       self.relu3 = ReLU()
       self.fc2 = Linear(in_features=50, out_features=43)
       
    
    def forward(self, x):

        x = self.conv1(x)
        x = self.relu1(x)
        x = self.maxpool1(x)

        x = self.conv2(x)
        x = self.relu2(x)
        x = self.maxpool2(x)

        x = flatten(x, 1)
        x = self.fc1(x)
        x = self.relu3(x)

        out = self.fc2(x)
        

        return out
    
    RuntimeError                              Traceback (most recent call last)
<ipython-input-15-978518dfa1ac> in <module>
     12     for (inputs, labels) in trainDataLoader:
     13         (inputs, labels) = (inputs.to(device), labels.to(device))
---> 14         prediction = model(inputs)
     15         loss = lossFunction(prediction, labels)
     16         opt.zero_grad()

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

<ipython-input-13-58a66aa14326> in forward(self, x)
     24 
     25         x = flatten(x, 1)
---> 26         x = self.fc1(x)
     27         x = self.relu3(x)
     28 

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -> Tensor:
--> 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -> str:

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x29040 and 2048x50)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文