簡體   English   中英

Pytorch RuntimeError: mat1 和 mat2 形狀不能相乘(32x246016 和 3136x1000)

[英]Pytorch RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x246016 and 3136x1000)

我正在 Pytorch 上構建一個 CNN。 我對輸入有點困惑。 我收到以下錯誤:

RuntimeError:mat1 和 mat2 形狀不能相乘(32x246016 和 3136x1000)

圖像為 250 x 250 灰度。

誰能看看我的構造函數並告訴我哪里出錯了? 如果您能向我解釋為什么我錯了以及為什么您的答案是正確的,則可以加分; ;)

class CNN(nn.Module):

# Contructor
def __init__(self):
    super(CNN, self).__init__()
    self.cnn1 = nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2)
    self.conv1_bn = nn.BatchNorm2d(32)

    self.maxpool1=nn.MaxPool2d(kernel_size=2, stride=2)
    
    self.cnn2 = nn.Conv2d(32, 64, kernel_size=5,stride=1, padding=2)
    self.conv2_bn = nn.BatchNorm2d(64)
    self.maxpool2=nn.MaxPool2d(kernel_size=2, stride=2)  
    
    self.drop_out1 = nn.Dropout()
    self.fc1 = nn.Linear(7 * 7 * 64, 1000)
    self.bn_fc1 = nn.BatchNorm2d(1000)
    
    self.fc2 = nn.Linear(1000, 1)
    

    

# Prediction
def forward(self, x):
    x = self.cnn1(x)
    x = self.conv1_bn(x)
    x = torch.relu(x)
    x = self.maxpool1(x)
    x = self.cnn2(x)
    x = self.conv2_bn(x)
    x = torch.relu(x)
    x = self.maxpool2(x)
    x = x.view(x.size(0), -1)
    x = self.drop_out1(x)
    x = self.fc1(x)
    x = self.bn_fc1(x)
    x = torch.relu(x)
    x = self.fc2(x)
    x = torch.sigmoid(x)
   
    return x

您的 fc1 層需要一個形狀為(-1, 7*7*64)的張量,但您傳遞給它的是一個形狀為[-1, 246016]的張量( -1是批量大小)。

要計算卷積網絡的 output 大小,請參閱這篇文章或任何神經網絡教科書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM