Batchsize's Change Causes Errors

Yimi: <p>I change the batchsize from 1 to 20, some errors caused.<br>
I guess the reason is that the batchsize cannot be divisible by the size of dataloader, how can I fix it?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1b583a78c0edb4167070427068e70bd826426813.png" data-download-href="https://discuss.pytorch.org/uploads/default/1b583a78c0edb4167070427068e70bd826426813" title="(258HLE0KS5WE9F}94A}AEP"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1b583a78c0edb4167070427068e70bd826426813.png" alt="(258HLE0KS5WE9F}94A}AEP" data-base62-sha1="3TTV3cBtCoHyiVRW8QoGN6SWvzd" width="690" height="99" data-dominant-color="232323"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">(258HLE0KS5WE9F}94A}AEP</span><span class="informations">1792×258 22.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Related code in train.py and models.py:</p>
<pre><code class="lang-auto">for epoch in range(opt.epoch, opt.n_epochs):
    for i, batch in enumerate(dataloader):


        # Set model input

        real_A = Variable(batch['A'].type(Tensor))
        real_B = Variable(batch['B'].type(Tensor))
        label_AB = Variable(batch['label_AB'].type(Tensor))
        label_BA = Variable(batch['label_BA'].type(Tensor))
        label = batch['label_name']

        real_A_features = vgg16(real_A)
        real_B_features = vgg16(real_B)

        realA_mean_std = mean_std(real_A_features)
        realB_mean_std = mean_std(real_B_features)

        weights_AB = metanet(realB_mean_std,label_AB)   #line206 in train.py
        weights_BA = metanet(realA_mean_std,label_BA)
</code></pre>
<pre><code class="lang-auto">def forward(self, mean_std_features,label):
        #mean_std_features = mean_std_features.view(-1,mean_std_features.shape[0] * mean_std_features.shape[1])
        #label_vector = torch.mean(label,dim=2).view(1,-1)
        index0 = torch.cuda.FloatTensor([torch.mean(label[0][0])])
        index1 = torch.cuda.FloatTensor([torch.mean(label[0][1])])
        index2 = torch.cuda.FloatTensor([torch.mean(label[0][2])])
        index3 = torch.cuda.FloatTensor([torch.mean(label[0][3])])
        #index4 = torch.cuda.FloatTensor([torch.mean(label[0][4])])
        label_vector = torch.cat((index0,index1,index2,index3)).view(1,-1)
        hidden = F.relu(self.hidden(torch.cat([mean_std_features,label_vector],dim=1)))   #line201 in models.py
        filters = {}
        for name, i in self.fc_dict.items():
            fc = getattr(self, 'fc{}'.format(i + 1))
            filters[name] = fc(hidden[:, i * 128:(i + 1) * 128])
        return filters
</code></pre>

charan_Vjy: <p>The error occurs at</p>
<blockquote>
<p>torch.cat([mean_std_features,label_vector],dim=1))</p>
</blockquote>
<p>From the error trace, I would guess the shapes are (batch_size,something) for <code>label vector</code> and (1,something) for <code>mean_std_features</code>. This implies that the the <code>mean_std()</code> is agnostic to changes in batch size. You would have to change the function to accomodate for different batch sizes. First, do verify this by printing out the shapes of the variables in question. Once you confirm, try tweaking the <code>mean_std()</code> to accmodate different batch sizes.</p>