Input tensor shape to a model with only linear layers

username: ritesh313
cooked: <p>Hi, I am trying to clarify a doubt about the shape of the input tensor. I’m aware that PyTorch requires the shape to be <code>[batch_size, num_channels, H, W]</code></p>
<pre><code class="lang-auto">class auto_encode1(nn.Module):
    def __init__(self, encoding_size = 3):
        super(auto_encode1, self).__init__()
        self.encoding_size = encoding_size
        self.input_size = 369
        self.encoder = nn.Sequential(
            nn.Linear(self.input_size, 250), nn.ReLU(),
            nn.Linear(250, 125), nn.ReLU(),
            nn.Linear(125, 60), nn.ReLU(),
            nn.Linear(60, self.encoding_size), nn.ReLU()
        )
            
        self.decoder = nn.Sequential(
            nn.Linear(self.encoding_size, 60), nn.ReLU(),
            nn.Linear(60, 125), nn.ReLU(),
            nn.Linear(125, 250), nn.ReLU(),
            nn.Linear(250, self.input_size), nn.Tanh(),
        )

    def encode(self, x):
        # print(x.size())
        x = x.reshape(x.size(0), -1)
        return self.encoder(x)
        
    def decode(self, x):
        return self.decoder(x)
    
    def forward(self, x):
        print(x.shape)
        x1 = self.encode(x)
        xd = self.decode(x1)
        
        return xd
</code></pre>
<p>In my case, the input is <code>[num_pixels, 369]</code>, <code>num_pixels</code> is variable here. When I use a batch size of 1 (because if I use more than one it throws an error since the input shape is different for images), this is how the input to the model looks like:</p>
<pre><code class="lang-auto">torch.Size([1, 16, 369])
torch.Size([1, 16, 369])
torch.Size([1, 25, 369])
torch.Size([1, 4, 369])
torch.Size([1, 36, 369])
</code></pre>
<p>As you can see the <code>num_pixels</code> is variable, I cannot reshape them for data integrity in this particular case.</p>
<p>Because of this I have to use a collate function:</p>
<pre><code class="lang-auto">def collate_fn(input):
    image = torch.cat(input,0)
    return image
</code></pre>
<p>which makes the input to the model look like:</p>
<pre><code class="lang-auto">with batch size 1
torch.Size([16, 369])
torch.Size([16, 369])
torch.Size([25, 369])
torch.Size([4, 369])
torch.Size([36, 369])

with batch size 3
torch.Size([57, 369])
torch.Size([40, 369])
</code></pre>
<p>My question is:<br>
Does it make a difference if the input the model is not in shape  <code>[batch_size, num_channels, H, W]</code>, because in my case it is <code>[batch_size*num_channels*H, W], where num_channels = 1 and H = num_pixels</code>?</p>

username: ptrblck
cooked: <p>Both approaches should yield the same result for linear layers as seen here:</p>
<pre><code class="lang-python"># setup
batch_size, num_channels, H, W = 3, 4, 5, 6
lin = nn.Linear(W, 10)

# multi-dim approach
x = torch.randn([batch_size, num_channels, H, W])
out = lin(x)

# 2D approach
y = x.view(-1, W)
out_y = lin(y)

# comparison
out_y = out_y.view_as(out)
print((out - out_y).abs().max())
# &gt; tensor(0., grad_fn=&lt;MaxBackward1&gt;)
</code></pre>
<p>as additional dimensions are used as separate samples in <code>nn.Linear</code>.</p>

username: ritesh313
cooked: <p>Thanks for the snippet!</p>