Pytorch: Building 3D Dense Network run into error adaptive_avg_pool3d: output_size must be 3
I ran into such an error when trying to train my 3D Dense Network. There is an average pooling layer at the end of convolution blocks. As can be seen in the message below, it says that my code out = F.adaptive_avg_pool3d(input=out, output_size=[1,1,1]) does not give the right-sized output.
I have tried with output_size=1, output_size=(1,1,1) and tried to use layer F.avg_pool instead, but all ran into such error. It's quiet strange since my output does have the right size.
P.S. I have imported torch.nn.functional as F

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

/tmp/ipykernel_37419/694291867.py in &lt;module&gt;
     11 trainer = Trainer(model, device, optimizer, loss_fn)
     12 
---&gt; 13 trainer.fit(N_EPOCHS, train_dataloader, val_dataloader, &quot;4C&quot;, N_EPOCHS / 3, )
     14 
     15 trainer.display_plots(&quot;4C&quot;)

/tmp/ipykernel_37419/579275755.py in fit(self, epochs, train_dataloader, valid_dataloader, mrimodule, patience)
     26             self.info_message(&quot;EPOCH: {}&quot;, n_epoch)
     27 
---&gt; 28             train_loss, train_auc, train_time = self.train_epoch(train_dataloader)
     29             valid_loss, valid_auc, valid_time = self.valid_epoch(valid_dataloader)
     30 

/tmp/ipykernel_37419/579275755.py in train_epoch(self, train_dataloader)
     71             Y = Y.to(self.device)
     72             self.optimizer.zero_grad()
---&gt; 73             pred = self.model(X).squeeze(1)
     74             loss = self.loss_fn(pred, Y)
     75 

~/miniconda3/envs/MGMT_Classify/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~/MGMT_Classify_Scripts/DenseNet3D.py in forward(self, x)
    216         features = self.features(x)
    217         out = F.relu(features, inplace=True)
--&gt; 218         out = F.adaptive_avg_pool3d(input=out, output_size=[1,1,1])
    219         out = torch.flatten(out, 1)
    220         out = self.classifier(out)

~/miniconda3/envs/MGMT_Classify/lib/python3.7/site-packages/torch/nn/functional.py in adaptive_avg_pool3d(input, output_size)
    954                 adaptive_avg_pool3d, (input,), input, output_size)
    955     _output_size = _list_with_default(output_size, input.size())
--&gt; 956     return torch._C._nn.adaptive_avg_pool3d(input, _output_size)
    957 
    958 

RuntimeError: adaptive_avg_pool3d: output_size must be 3


I have at least found one temporary solution to this by comparing my script with other 3D densenet models on GitHub.
I used the code:
out = F.adaptive_avg_pool3d(input=out, output_size=(1, 1, 1)).view(features.size(0), -1)

In place of the following, from the original script:
out = F.adaptive_avg_pool3d(input=out, output_size=[1,1,1])
out = torch.flatten(out, 1)

After that, it works. I have not tried it on my task now, but I have confirmed that it can overfit a very small test dataset.
I am still very curious what on earth lead to the error and why it can be fixed in this way. If you have insight, please offer it in the comments. Otherwise, this offers a workaround for anyone else that needs it.
