Pytorch proper way of feeding 5D tensor input into neural network

username: ElleryL
cooked: <p>Hi; Consider I have a neural network ;</p>
<pre><code class="lang-auto">nn.Sequential(
            nn.Linear(in_features, out_features)
            UnFlatten(), # reshape the output of previous layers so that its input shape to the next layers is proper
            nn.ConvTranspose2d()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.Sigmoid(),
        )

</code></pre>
<p>in the regular cases; it takes data <code>X.shape = (batch_size, in_features)</code>; and output a batch of images,</p>
<p>but right now; I have an input looks like this <code>X_shape = ( batch_size, another_features, in_features)</code> and I want to output a batch of batch of images</p>
<p>In the for loop way, the implementation goes like this</p>
<pre><code class="lang-auto">result = []
for i in range(another_features):
    temp.append(NN(X[:,i,:])) # feed each slice of another_features 
result = torch.stack(result) # leads to 5D tensor has shape (another_features, batch_sizes, out_channels, out_features, out_features)
# then reshape into  (batch_sizes , another_features, out_channels, out_features, out_features)
</code></pre>
<p>I’m wondering if there is an efficient way because technically, the process isn’t sequential and can be parallelized.</p>

username: JuanFMontesinos
cooked: <p>I don’t undestand at all.<br>
Which architecture are you using?<br>
You probably can reshape everything in the batch dimension and then undo for the 3D processing.<br>
ie</p>
<pre><code class="lang-auto">input = torch.rand(1,5,3,224,224) which is a 3D tensor of shape (B,T,C,H,W)
so the  your reshape
input=input.view(-1,3,224,224)
#you apply 2d processing
output = conv(input)
# undo the reshaping
input3d = output.view(1,5,C',H',W')
output3d = conv3d(input3d) 
</code></pre>

username: ElleryL
cooked: <p>Hi thanks for the reply and sorry for confusion ;</p>
<p>I have already re-edited my post hope it can be more clear and concise.</p>
<p>In addition, please see this one; it might be more clear <a href="https://discuss.pytorch.org/t/how-to-feed-a-3d-tensor-into-neural-network-forward-pass/19544/3">How to feed a 3D tensor into Neural Network Forward() pass</a></p>
<p>A similar post earlier (from me); that solves easier version of this one</p>

username: JuanFMontesinos
cooked: <pre><code class="lang-auto">class Conv2DExt(nn.Module):
    def __init__(self,*args,**kwargs):
        super().__init__(self)
        self.conv2d = nn.ConvTranspose2d(*args,**kwargs)
    def forward(self, input, output_size=None):  # type: (Tensor, Optional[List[int]]) -&gt; Tensor
        if input.ndimensions() == 5:
            B, F, C, H, W = input.shape
            input = input.view(-1, C, H, W)
        return self.conv2d(input, output_size=output_size)

nn.Sequential(
            nn.Linear(in_features, out_features)
            UnFlatten(), # reshape the output of previous layers so that its input shape to the next layers is proper
            nn.Conv2DExt()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.ReLU(),
            nn.ConvTranspose2d()
            nn.Sigmoid(),
        )
# You have to reshape in the forward after this module.

</code></pre>

username: ElleryL
cooked: <p>Thanks ! This is exactly what I’m looking for</p>