简体   繁体   English

Pytorch vsion 大小不匹配,m1

[英]Pytorch vsion size mismatch, m1

im trying to run a simple linear regression but i have error when i try to train.我试图运行一个简单的线性回归,但是当我尝试训练时出现错误。

The size of images is the shapes of data train print(dataset_train[0][0].shape) shows me torch.Size([3, 227, 227])图像的大小是数据火车的形状print(dataset_train[0][0].shape)告诉我torch.Size([3, 227, 227])


size_of_image=3*227*227

class linearRegression(nn.Module):
    def __init__(self, inputSize, outputSize):
        super(linearRegression, self).__init__()
        self.linear = nn.Linear(inputSize, outputSize)

    def forward(self, x):
        out = self.linear(x)
        return out

model = linearRegression(size_of_image, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
criterion = torch.nn.CrossEntropyLoss()         
trainloader = DataLoader(dataset = dataset_train, batch_size = 1000)
for epoch in range(5):
    for x, y in trainloader:
        yhat = model(x)
        loss = criterion(yhat, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()            

I tried to unserstand what its the mean of error but i dont found a solution, can anyone help me?我试图理解错误的含义,但我没有找到解决方案,有人可以帮助我吗?

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-44-6f00f9272a22> in <module>
      1 for epoch in range(5):
      2     for x, y in trainloader:
----> 3         yhat = model(x)
      4         loss = criterion(yhat, y)
      5         optimizer.zero_grad()

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

<ipython-input-21-d20eb6e0c349> in forward(self, x)
      5 
      6     def forward(self, x):
----> 7         out = self.linear(x)
      8         return out

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---> 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~/PycharmProjects/estudios/venv/lib/python3.8/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1610         ret = torch.addmm(bias, input, weight.t())
   1611     else:
-> 1612         output = input.matmul(weight.t())
   1613         if bias is not None:
   1614             output += bias

Im RuntimeError: size mismatch, m1: [681000 x 227], m2: [154587 x 1] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

You need to flatten the 2D input image into a 1D signal:您需要将 2D 输入图像flatten为 1D 信号:
Your input is a 4D tensor of shape 1000-3-227-227 (batch-channel-height-width).您的输入是形状为 1000-3-227-227(批量通道高度宽度)的 4D 张量。 However, nn.Linear expects as input 2D tensors of shape batch-channels.然而, nn.Linear期望作为形状批处理通道的输入 2D 张量。

Youc forward code should look something like: Youc forward代码应类似于:

def forward(self, x):
  flat_x = x.view(x.shape[0], -1)  # collapse all dimensions to the second one
  out = self.linear(flat_x)
  return out

In linearRegression , you have defined the linear transformation as: nn.Linear(3*227*227, 1) which means the Linear layer expects 3*227*227 input features and it will output 1 feature.linearRegression中,您已将线性变换定义为: nn.Linear(3*227*227, 1)这意味着线性层需要3*227*227输入特征,它将 output 1 个特征。

However, you feed a 4D tensor of shape [1000, 3, 227, 227] (batch-channel-height-width) to the Linear layer which considers the last dimension as the feature dimension.但是,您将形状为[1000, 3, 227, 227] (batch-channel-height-width)的 4D 张量提供给将最后一个维度视为特征维度的线性层。 It means Linear layer is getting 227 input features instead of 3*227*227 .这意味着线性层获得 227 个输入特征而不是3*227*227 So, you are getting the following error.因此,您收到以下错误。

RuntimeError: size mismatch, m1: [681000 x 227], m2: [154587 x 1]

Note that, Linear layers are associated with a weight matrix of shape in_features x out_features (in your case, it is [154587 x 1] ).请注意,线性层与形状为in_features x out_features的权重矩阵相关联(在您的情况下,它是[154587 x 1] )。 And the input to a Linear layer is flattened to a 2D tensor, in your case, it is [1000*3*227 x 227] = [681000 x 227] .线性层的输入被展平为二维张量,在您的情况下,它是[1000*3*227 x 227] = [681000 x 227]

So, an attempt to perform matrix multiplication of two tensors with shape [681000 x 227] and [154587 x 1] results in the above error.因此,尝试对形状为[681000 x 227][154587 x 1]的两个张量执行矩阵乘法会导致上述错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初学者 PyTorch:运行时错误:大小不匹配,m1:[16 x 2304000],m2:[600 x 120] - Beginner PyTorch : RuntimeError: size mismatch, m1: [16 x 2304000], m2: [600 x 120] Pytorch RuntimeError:大小不匹配,m1:[1 x 7744],m2:[400 x 120] - Pytorch RuntimeError: size mismatch, m1: [1 x 7744], m2: [400 x 120] Pytorch GRU 错误 RuntimeError:尺寸不匹配,m1:[1600 x 3],m2:[50 x 20] - Pytorch GRU error RuntimeError : size mismatch, m1: [1600 x 3], m2: [50 x 20] RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136 - RuntimeError: size mismatch, m1: [4 x 784], m2: [4 x 784] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:136 RuntimeError:大小不匹配,m1:[5 x 10],m2:[5 x 32] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp - RuntimeError: size mismatch, m1: [5 x 10], m2: [5 x 32] at /pytorch/aten/src/TH/generic/THTensorMath.cpp Pytorch:尺寸不匹配错误,尽管矩阵的尺寸匹配(m1:[256 x 200],m2:[256 x 200]) - Pytorch: size mismatch error although the sizes of the matrices do match (m1: [256 x 200], m2: [256 x 200]) RuntimeError:大小不匹配,m1:[28 x 28],m2:[784 x 128] - RuntimeError: size mismatch, m1: [28 x 28], m2: [784 x 128] RuntimeError:尺寸不匹配,m1:[32 x 1],m2:[32 x 9] - RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] python 中的 CNN 模块给出错误大小不匹配,m1:[12288 x 26],m2:[12288 x 26] - CNN module in python gives error size mismatch, m1: [12288 x 26], m2: [12288 x 26] 如何修复此 RuntimeError:大小不匹配,m1:[64 x 103],m2:[550 x 50] - How do I fix this RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM