为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

收到一只叮咚 2022-06-22 18:53:47
我正在构建一个 CNN 并对其进行字母 A 到 I(9 类)的手势分类训练,每个图像都是 224x224 大小的 RGB。不确定我需要转置哪个矩阵以及如何转置。我已经设法匹配层的输入和输出,但是矩阵乘法的事情,不太确定如何解决它。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和培训代码#Loss and optimizercriterion = nn.BCEWithLogitsLoss()optimizer = optim.SGD(model2.parameters(), lr=learning_rate, momentum=0.9)# Train the modeltotal_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)代码打印到 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完整的回溯错误:https ://ibb.co/ykqy5wM
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

您不需要x=x.view(-1,1)andx = x.squeeze(1)在您的forward功能中。删除这两行。您的输出形状将是(batch_size, 9).


此外,您需要转换labels为 one-hot 编码,其形状为(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)


查看完整回答
反对 回复 2022-06-22
  • 1 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信