Pytorch CNN mat1 和 mat2 形状不能相乘((128x29040 和 2048x50)
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论