如何修复:RuntimeError:pyTorch中的大小不匹配

时间:2019-08-17 07:05:17

标签: python deep-learning pytorch

我是pyTorch的新手,并遇到以下“大小不匹配”错误:

    <div class="container-fluid flex-grow-1 container-p-y">
        <div class="uikit-example">
          <div class="row">

            @foreach ($employee as $key => $emp)
            <div class="col-lg-4 col-md-4 col-sm-12">
              <div class="card mb-4">
                <div class="card-body">
                  <div class="media align-items-center">
                    <img src="{{ asset('Upload/Images/Media/Avatar/Thumb/'.$emp->image) }}" alt class="ui-w-60 rounded-circle">
                    <div class="media-body pt-2 ml-3">
                      <h5 class="mb-2">{{ $emp->name }}</h5>
                      <div class="text-muted small mb-2">{{ $emp->phone }}</div>
                      <div class="text-muted small mb-2">{{ $emp->email }}</div>
                      <div class="text-muted small mb-2"><span>ID:&nbsp;</span>{{ $emp->user_code }}</div>
                    </div>

                    <div class="">
                      <toggleonline user-id="{{ $emp->id }}" onlinestat="{{ $emp->onlinestatus }}"></toggleonline>
                    </div>

                  </div>
                </div>
                <div class="card-footer text-center py-3">
                  {{-- <a href="#" class="btn btn-success rounded-pill">+&nbsp; Go Online</a> --}}

                  &nbsp;
                  {{-- <a href="#" class="btn icon-btn btn-default md-btn-flat rounded-pill"><span class="ion ion-md-mail"></span></a> --}}
                  {{-- <div class="float-right"> --}}
                      <a href="#" class="btn btn-primary rounded-pill mt-2">&nbsp; My Reservations</a>
                  {{-- </div> --}}
                </div>
              </div>
            </div>
          @endforeach
          </div>
        </div>

    </div>

型号:

RuntimeError: size mismatch, m1: [7 x 2092500], m2: [180 x 120] at ..\aten\src\TH/generic/THTensorMath.cpp:961

我曾经尝试过将class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 200, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(200, 180, 5) self.fc1 = nn.Linear(180, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84,5) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(x.shape[0], -1) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x 更改为x = x.view(x.shape[0], -1),但是那也没有用。图片尺寸为512x384。并使用了以下转换:

x = x.view(x.size(0), -1)

1 个答案:

答案 0 :(得分:2)

问题是最后一个最大池化层的输出尺寸与第一个完全连接的层的输入尺寸不匹配。这是直到输入形状(3, 512, 384)的最后一个最大池层为止的网络结构:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1        [-1, 200, 508, 380]          15,200
         MaxPool2d-2        [-1, 200, 254, 190]               0
            Conv2d-3        [-1, 180, 250, 186]         900,180
         MaxPool2d-4         [-1, 180, 125, 93]               0
================================================================

该表的最后一行表示MaxPool2d-4输出180个通道(滤波器输出),宽度为125,高度为93。因此,您需要第一个完全连接的层具有180 * 125 * 93 = 2092500个输入大小。这很多,所以我建议您改进体系结构。无论如何,如果将第一个完全连接的图层的输入大小更改为2092500,它将起作用:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 200, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(200, 180, 5)
        #self.fc1 = nn.Linear(180, 120)
        self.fc1 = nn.Linear(2092500, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84,5)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(x.shape[0], -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

提供以下架构:

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1        [-1, 200, 508, 380]          15,200
         MaxPool2d-2        [-1, 200, 254, 190]               0
            Conv2d-3        [-1, 180, 250, 186]         900,180
         MaxPool2d-4         [-1, 180, 125, 93]               0
            Linear-5                  [-1, 120]     251,100,120
            Linear-6                   [-1, 84]          10,164
            Linear-7                    [-1, 5]             425
================================================================
Total params: 252,026,089
Trainable params: 252,026,089
Non-trainable params: 0

(您可以使用documentation包来生成这些表。)