简体   繁体   English

RuntimeError:尺寸不匹配,m1:[32 x 1],m2:[32 x 9]

[英]RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9]

I'm building a CNN and training it on hand sign gesture classification for letters A through I (9 classes), each image is RGB with 224x224 size.我正在构建一个 CNN 并对其进行字母 A 到 I(9 类)的手势分类训练,每个图像都是 224x224 大小的 RGB。

Not sure which matrix I need to transpose and how.不确定我需要转置哪个矩阵以及如何转置。 I have managed to match the inputs and outputs of layers, but that matrix multiplication thing, not really sure how to fix it.我已经设法匹配层的输入和输出,但是矩阵乘法的事情,不太确定如何解决它。

class LargeNet(nn.Module):
    def __init__(self):
        super(LargeNet, self).__init__()
        self.name = "large"
        self.conv1 = nn.Conv2d(3, 5, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(5, 10, 5)
        self.fc1 = nn.Linear(10 * 53 * 53, 32)
        self.fc2 = nn.Linear(32, 9)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        print('x1')
        x = self.pool(F.relu(self.conv2(x)))
        print('x2')
        x = x.view(-1, 10*53*53)
        print('x3')
        x = F.relu(self.fc1(x))
        print('x4')
        x = x.view(-1, 1)
        x = self.fc2(x)
        print('x5')
        x = x.squeeze(1) # Flatten to [batch_size]
        return x

and training code和培训代码

#Loss and optimizer
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(model2.parameters(), lr=learning_rate, momentum=0.9)

# Train the model
total_step = len(train_loader)
loss_list = []
acc_list = []
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        print(i,images.size(),labels.size())
        # Run the forward pass
        outputs = model2(images)
        labels=labels.unsqueeze(1)
        labels=labels.float()
        loss = criterion(outputs, labels)

The code prints up to x4 and then I get this error RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] at C:\w\1\s\tmp_conda_3.7_055457\conda\conda-bld\pytorch_1565416617654\work\aten\src\TH/generic/THTensorMath.cpp:752代码打印到 x4,然后我收到此错误 RuntimeError: size mismatch, m1: [32 x 1], m2: [32 x 9] at C:\w\1\s\tmp_conda_3.7_055457\conda\conda- bld\pytorch_1565416617654\work\aten\src\TH/generic/THTensorMath.cpp:752

Complete traceback error: https://ibb.co/ykqy5wM完整的回溯错误: https://ibb.co/ykqy5wM

You don't need x=x.view(-1,1) and x = x.squeeze(1) in your forward function.在您的forward function 中不需要x=x.view(-1,1)x = x.squeeze(1) Remove these two lines.删除这两行。 Your output shape would be (batch_size, 9) .您的 output 形状将是(batch_size, 9)

Also, you need to convert labels to one-hot encoding, which is in shape of (batch_size, 9) .此外,您需要将labels转换为单热编码,其形状为(batch_size, 9)

class LargeNet(nn.Module):
    def __init__(self):
        super(LargeNet, self).__init__()
        self.name = "large"
        self.conv1 = nn.Conv2d(3, 5, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(5, 10, 5)
        self.fc1 = nn.Linear(10 * 53 * 53, 32)
        self.fc2 = nn.Linear(32, 9)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 10*53*53)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

model2 = LargeNet()
#Loss and optimizer
criterion = nn.BCEWithLogitsLoss()
# nn.BCELoss()
optimizer = optim.SGD(model2.parameters(), lr=0.1, momentum=0.9)

images = torch.from_numpy(np.random.randn(2,3,224,224)).float() # fake images, batch_size is 2
labels = torch.tensor([1,2]).long() # fake labels

outputs = model2(images)
one_hot_labels = torch.eye(9)[labels] 
loss = criterion(outputs, one_hot_labels)

暂无
暂无

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

相关问题 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 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:大小不匹配,m1:[28 x 28],m2:[784 x 128] - RuntimeError: size mismatch, m1: [28 x 28], m2: [784 x 128] 初学者 PyTorch:运行时错误:大小不匹配,m1:[16 x 2304000],m2:[600 x 120] - Beginner PyTorch : RuntimeError: size mismatch, m1: [16 x 2304000], m2: [600 x 120] 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:[64 x 103],m2:[550 x 50] - How do I fix this RuntimeError: size mismatch, m1: [64 x 103], m2: [550 x 50] 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] 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]) 将格式为M = [((a,x),(b,y)]]的列表分成M1 = [a,b]和M2 = [x,y] - Separate lists of format M=[(a,x), (b,y)] into M1=[a,b] and M2=[x,y]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM