How to use linear layer after AdaptiveAvgPool2d?

username: n0obcoder
cooked: <p>when you load the model,<br>
model = models.resnet18(pretrained=True)<br>
and print it, you see AdaptiveAvgPool2d layer followed by a linear layer, at the end of the architecture.<br>
I dont understand how is it working without flattening the output of the AdaptiveAvgPool2d  layer.</p>
<p>how do i use a linear layer after the AdaptiveAvgPool2d layer?</p>
<p>Consider this example:</p>
<p>m = nn.AdaptiveAvgPool2d((5,7))<br>
dense = nn.Linear(5, 10)</p>
<p>input = torch.randn(1, 64, 8, 9)</p>
<p>output = m2(input) # its shape should be (1, 64, 5, 7)<br>
output2 = dense(output) # this line throws an error.</p>
<p>Please help me with this. Thanks in advance ! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ptrblck
cooked: <p>The activation is reshaped in the <code>forward</code> method of the resnet implementation as can be seen <a href="https://github.com/pytorch/vision/blob/3d5610391eaef38ae802ffe8b693ac17b13bd5d1/torchvision/models/resnet.py#L203" rel="nofollow noopener">here</a>.</p>

username: n0obcoder
cooked: <p>it answered my question. thanks a lot. But what the point of reducing the feature map to (1,1) as can be seen in the line</p>
<p>AdaptiveAvgPool2d(output_size=(1, 1))</p>
<p>Wouldn’t it be better to have a bigger output_size?</p>

username: ptrblck
cooked: <p>Maybe it might be better for some use cases. However, the current implementation just sticks to the original <a href="https://arxiv.org/pdf/1512.03385.pdf" rel="nofollow noopener">ResNet paper</a>.</p>

username: n0obcoder
cooked: <p>my siamese network’s training loss seems to be fluctuating a lot. Do you mind having a look at the code…</p>
<p><a href="https://www.kaggle.com/n0obcoder/face-recog/edit/run/15064106" rel="nofollow noopener">my implementation of siamese net using the triplet loss fucntion</a></p>

username: ptrblck
cooked: <p>The link is unfortunately dead (404).</p>

username: n0obcoder
cooked: <p><a href="https://www.kaggle.com/n0obcoder/siamese-net" rel="nofollow noopener">try this</a></p>

username: ptrblck
cooked: <p>The code looks generally fine. Is your loss decreasing at all or just randomly jumping?</p>

username: n0obcoder
cooked: <p>seems like jumping to me <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=9" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:"></p>
<p>This is how the training loss looks like,</p>
<p>Epoch 1/5<br>
loss @ batch#000: 0.8270422220230103<br>
loss @ batch#001: 0.7125710844993591<br>
loss @ batch#002: 0.660284698009491<br>
loss @ batch#003: 0.838929295539856<br>
loss @ batch#004: 1.083276391029358<br>
loss @ batch#005: 0.6964817047119141<br>
loss @ batch#006: 0.4421113133430481<br>
loss @ batch#007: 0.8267079591751099<br>
loss @ batch#008: 0.5320273637771606<br>
loss @ batch#009: 0.14518025517463684<br>
loss @ batch#010: 0.39652758836746216<br>
loss @ batch#011: 0.5080978870391846<br>
loss @ batch#012: 0.0<br>
Epoch 2/5<br>
loss @ batch#000: 0.03382734954357147<br>
loss @ batch#001: 0.21790757775306702<br>
loss @ batch#002: 0.354198694229126<br>
loss @ batch#003: 0.13982316851615906<br>
loss @ batch#004: 0.5416660308837891<br>
loss @ batch#005: 0.0642566978931427<br>
loss @ batch#006: 0.1361594796180725<br>
loss @ batch#007: 0.2777060270309448<br>
loss @ batch#008: 0.1839388906955719<br>
loss @ batch#009: 0.0<br>
loss @ batch#010: 0.006485641002655029<br>
loss @ batch#011: 0.0<br>
loss @ batch#012: 0.08765590190887451<br>
Epoch 3/5<br>
loss @ batch#000: 0.12175476551055908<br>
loss @ batch#001: 0.0<br>
loss @ batch#002: 0.40359675884246826<br>
loss @ batch#003: 0.03469619154930115<br>
loss @ batch#004: 0.3805411159992218<br>
loss @ batch#005: 0.07248783111572266<br>
loss @ batch#006: 0.07092243432998657<br>
loss @ batch#007: 0.438197523355484</p>

username: ptrblck
cooked: <p>Try to play around with some hyperparameters (e.g. learning rate, other optimizer) and see if the behavior improves. Also, it might be helpful to use a small subset of your data (e.g. just 10 pairs) and try to overfit your model on it. If your model cannot overfit this small data sample, you might have some bugs in your code or the architecture is just not suitable for the use case.</p>