How can I get the batch shape in a Model class?

IneedMrmeeseeks: <p>This question is more like basic python Class question I think.(If this question is not belong here, plz let me know)<br>
I am trying to build a simple conditional GAN model.<br> I created my own custom Dataset class and collate_fn which is fed to DataLoader class.<br> I customized those since input data has varying shape.<br> Thankfully, I followed <a href="https://discuss.pytorch.org/t/how-to-create-a-dataloader-with-variable-size-input/8278/2">this solution</a> using rnn.pad_sequence function in order to padding <strong>within each batches</strong>.(thx to <a class="mention" href="/u/jdhao">@jdhao</a> and <a class="mention" href="/u/pinocchio">@pinocchio</a>)</p>
<pre><code class="lang-auto">n_epochs = 100

for epoch in range(n_epochs):

    for batch_idx, (data_input, labels) in enumerate(data_loader):

        print(data_input.size())
</code></pre>
<p>Above code gives me the output looks like the following one.</p>
<pre><code class="lang-auto">torch.Size([10, 86, 408])
torch.Size([10, 105, 408])
torch.Size([10, 110, 408])
torch.Size([10, 87, 408])
torch.Size([10, 76, 408])
torch.Size([10, 44, 408])
torch.Size([10, 54, 408])
torch.Size([10, 52, 408])
torch.Size([10, 89, 408])
torch.Size([10, 59, 408])
torch.Size([10, 60, 408])
torch.Size([10, 43, 408])
</code></pre>
<p>So you can see that first one is the batch length, but the second varying number represents zero-padded input.</p>
<p>The problem is that I need to get these second number inside Generator class.</p>
<pre><code class="lang-auto">class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()

        self.label_emb = nn.Embedding(n_class, n_class)

        self.model = nn.Sequential(
            nn.Linear(latent_dim + n_class, 256),
            nn.LeakyReLU(0.2, inplace= True),
            nn.Linear(256, 512),
            nn.LeakyReLU(0.2, inplace= True),
            nn.Linear(512, 1024),
            nn.LeakyReLU(0.2, inplace= True),
            nn.Linear(1024, int(dt_h * dt_w)),
            nn.Sigmoid()
        )

    def forward(self, z, labels):

        gen_input = torch.cat((self.label_emb(labels), z), -1)

        tech = self.model(gen_input)

        tech = tech.view(batch_size, dt_h, dt_w)
        return tech
</code></pre>
<p>The <strong>dt_h</strong> in the code is the one that should be replaced by the varying number.</p>
<p>Any advice will be appriciated. Thanks! <img src="https://discuss.pytorch.org/images/emoji/apple/fist_right.png?v=12" title=":fist_right:" class="emoji" alt=":fist_right:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/fist_left.png?v=12" title=":fist_left:" class="emoji" alt=":fist_left:" loading="lazy" width="20" height="20"></p>

AlphaBetaGamma96: <p>if <code>batch_size</code> and <code>dt_w</code> are of constant size, you can just replaced <code>dt_h</code> with <code>-1</code> and it will automatically select the right size.</p>

IneedMrmeeseeks: <p>First, Thanks to the advice. I will give it a try.<br> Just one thing in mind keep bothering me.<br> Shouldn’t I write the code something like <code>self.data_input</code> and get the number with <code>self.data_input.size()[1]</code> inside the Generator class? <img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>

IneedMrmeeseeks: <p>I get the idea of your advice since .view() is act like reshape in numpy.<br> However What about the dt_h in the last layer before the sigmoid activation?</p>

AlphaBetaGamma96: <p>You can indeed do something like <code>self.data_input.shape[1]</code> to get the same on-the-fly if you’d want to. The <code>-1</code> (as you said) is just to exploit reshaping and get pytorch to automatically select how many rows I can get if I specify the other dimensions.</p>
<p>With regards to <code>dt_h</code> in the last <code>nn.Linear</code> module, I’m not too sure how to resolve that. I know there’s a <code>nn.LazyLinear</code> which takes arbitrary inputs but there’s no equivalent for outputs (documentation <a href="https://pytorch.org/docs/stable/generated/torch.nn.LazyLinear.html" rel="noopener nofollow ugc">here</a>). You could do something along the lines of defining the final <code>nn.Linear</code> as 1024 inputs and the output as <code>110 * 408</code> (as 110 is the maximum output I see from your example output above), then you can just pad all outputs with zeros so they have the same shape.</p>
<p>Perhaps, on the forum somewhere someone has asked about linear layers with variable outputs?</p>

IneedMrmeeseeks: <p>Thanks for all the answers! you are the best! <img src="https://discuss.pytorch.org/images/emoji/apple/hugs.png?v=12" title=":hugs:" class="emoji" alt=":hugs:" loading="lazy" width="20" height="20"></p>