RuntimeError: The expanded size of the tensor (64) must match the existing size (256) at non-singleton dimension 0. Target sizes: [64, 2]. Tensor sizes: [256, 2]

username: sharif_sagar
cooked: <blockquote>
<p>from <strong>future</strong> import print_function</p>
<p>import os<br>
import argparse</p>
<p>import torch<br>
import numpy as np<br>
from torch.optim import Adam, lr_scheduler, SGD<br>
from torch.nn import functional as F</p>
<p>import data<br>
import utils<br>
import metrics<br>
import config_bayesian as cfg<br>
from models.BayesianModels.Bayesian3Conv3FC import BBB3Conv3FC<br>
from models.BayesianModels.BayesianAlexNet import BBBAlexNet<br>
from models.BayesianModels.BayesianLeNet import BBBLeNet<br>
from models.BayesianModels.BayesianVGG import BBBVGGNet</p>
<h1>CUDA settings</h1>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<h1>device = torch.device(“cpu”)</h1>
<p>def getModel(net_type, inputs, outputs, priors, layer_type, activation_type):<br>
if net_type == ‘lenet’:<br>
return BBBLeNet(outputs, inputs, priors, layer_type, activation_type)<br>
elif net_type == ‘alexnet’:<br>
return BBBAlexNet(outputs, inputs, priors, layer_type, activation_type)<br>
elif net_type == ‘3conv3fc’:<br>
return BBB3Conv3FC(outputs, inputs, priors, layer_type, activation_type)<br>
elif net_type == ‘VGG’:<br>
return BBBVGGNet(outputs, inputs, priors, layer_type, activation_type)<br>
else:<br>
raise ValueError(‘Network should be either [LeNet / AlexNet / 3Conv3FC’)</p>
<p>def train_model(net, optimizer, criterion, trainloader, num_ens=1, beta_type=0.1, epoch=None, num_epochs=None):<br>
net.train()<br>
training_loss = 0.0<br>
accs = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
kl_list = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for i, (inputs, labels) in enumerate(trainloader, 1):</p>
<pre><code>    optimizer.zero_grad()

    inputs, labels = inputs.to(device), labels.to(device)
    print(inputs.shape[0])
    outputs = torch.zeros(inputs.shape[0], net.num_classes, num_ens).to(device)

    kl = 0.0
    for j in range(num_ens):
        net_out, _kl = net(inputs)
        kl += _kl
        outputs[:, :, j] = F.log_softmax(net_out, dim=1)

    kl = kl / num_ens
    kl_list.append(kl.item())
    log_outputs = utils.logmeanexp(outputs, dim=2)

    beta = metrics.get_beta(i - 1, len(trainloader), beta_type, epoch, num_epochs)
    loss = criterion(log_outputs, labels, kl, beta)
    loss.backward()
    optimizer.step()

    accs.append(metrics.acc(log_outputs.data, labels))
    training_loss += loss.cpu().data.numpy()
return training_loss / len(trainloader), np.mean(accs), np.mean(kl_list)
</code></pre>
<p>def validate_model(net, criterion, validloader, num_ens=1, beta_type=0.1, epoch=None, num_epochs=None):<br>
“”“Calculate ensemble accuracy and NLL Loss”“”<br>
net.train()<br>
valid_loss = 0.0<br>
accs = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>for i, (inputs, labels) in enumerate(validloader):
    inputs, labels = inputs.to(device), labels.to(device)
    outputs = torch.zeros(inputs.shape[0], net.num_classes, num_ens).to(device)
    kl = 0.0
    for j in range(num_ens):
        net_out, _kl = net(inputs)
        kl += _kl
        outputs[:, :, j] = F.log_softmax(net_out, dim=1).data

    log_outputs = utils.logmeanexp(outputs, dim=2)

    beta = metrics.get_beta(i - 1, len(validloader), beta_type, epoch, num_epochs)
    valid_loss += criterion(log_outputs, labels, kl, beta).item()
    accs.append(metrics.acc(log_outputs, labels))

return valid_loss / len(validloader), np.mean(accs)
</code></pre>
<p>def run(dataset, net_type):<br>
# Hyper Parameter settings<br>
layer_type = cfg.layer_type<br>
activation_type = cfg.activation_type<br>
priors = cfg.priors</p>
<pre><code>train_ens = cfg.train_ens
valid_ens = cfg.valid_ens
n_epochs = cfg.n_epochs
lr_start = cfg.lr_start
num_workers = cfg.num_workers
valid_size = cfg.valid_size
batch_size = cfg.batch_size
beta_type = cfg.beta_type

trainset, testset, inputs, outputs = data.getDataset(dataset)
train_loader, valid_loader, test_loader = data.getDataloader(
    trainset, testset, valid_size, batch_size, num_workers)
net = getModel(net_type, inputs, outputs, priors, layer_type, activation_type).to(device)
# net.load_state_dict(torch.load(weight_path))

ckpt_dir = f'checkpoints/{dataset}/bayesian'
ckpt_name = f'checkpoints/{dataset}/bayesian/model_{net_type}_{layer_type}_{activation_type}.pt'

if not os.path.exists(ckpt_dir):
    os.makedirs(ckpt_dir, exist_ok=True)

criterion = metrics.ELBO(len(trainset)).to(device)
optimizer = Adam(net.parameters(), lr=lr_start)
lr_sched = lr_scheduler.ReduceLROnPlateau(optimizer, patience=6, verbose=True)
valid_loss_max = np.Inf
for epoch in range(n_epochs):  # loop over the dataset multiple times

    train_loss, train_acc, train_kl = train_model(net, optimizer, criterion, train_loader, num_ens=train_ens,
                                                  beta_type=beta_type, epoch=epoch, num_epochs=n_epochs)
    valid_loss, valid_acc = validate_model(net, criterion, valid_loader, num_ens=valid_ens, beta_type=beta_type,
                                           epoch=epoch, num_epochs=n_epochs)
    lr_sched.step(valid_loss)

    print(
        'Epoch: {} \tTraining Loss: {:.4f} \tTraining Accuracy: {:.4f} \tValidation Loss: {:.4f} \tValidation Accuracy: {:.4f} \ttrain_kl_div: {:.4f}'.format(
            epoch, train_loss, train_acc, valid_loss, valid_acc, train_kl))

    # save model if validation accuracy has increased
    if valid_loss &lt;= valid_loss_max:
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
            valid_loss_max, valid_loss))
        torch.save(net.state_dict(), ckpt_name)
        valid_loss_max = valid_loss
</code></pre>
</blockquote>
<blockquote>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:<br>
parser = argparse.ArgumentParser(description=“PyTorch Bayesian Model Training”)<br>
parser.add_argument(‘–net_type’, default=‘VGG’, type=str, help=‘model’)<br>
parser.add_argument(‘–dataset’, default=‘crack’, type=str, help=‘dataset = ‘dataset’)<br>
# parser.add_argument(’–weights_path’, default=‘checkpoints/crack/bayesian/model_VGG_lrt_softplus.pt’, type=str,<br>
#                     help=‘weights for model’)<br>
args = parser.parse_args()</p>
<pre><code>run(args.dataset, args.net_type)
</code></pre>
</blockquote>
<p>When i try to run the code above for other than 64*64 image size …I get the following error:</p>
<blockquote>
<p>File “/home/sharif/PyTorch-BayesianCNN-master/main_bayesian.py”, line 156, in <br>
run(args.dataset, args.net_type)<br>
File “/home/sharif/PyTorch-BayesianCNN-master/main_bayesian.py”, line 130, in run<br>
train_loss, train_acc, train_kl = train_model(net, optimizer, criterion, train_loader, num_ens=train_ens,<br>
File “/home/sharif/PyTorch-BayesianCNN-master/main_bayesian.py”, line 57, in train_model<br>
outputs[:, :, j] = F.log_softmax(net_out, dim=1)<br>
RuntimeError: The expanded size of the tensor (64) must match the existing size (256) at non-singleton dimension 0.  Target sizes: [64, 2].  Tensor sizes: [256, 2]</p>
</blockquote>

username: ptrblck
cooked: <aside class="quote no-group" data-username="sharif_sagar" data-post="1" data-topic="136152">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sharif_sagar/48/43717_2.png" class="avatar"> sharif_sagar:</div>
<blockquote>
<p>When i try to run the code above for other than 64*64 image size …I get the following error:</p>
</blockquote>
</aside>
<p>My best guess is that you are using a <code>view</code> or <code>reshape</code> operation inside the model’s <code>forward</code>, which might be using hard-coded and wrong shapes.<br>
Based on the error message it seems that you are changing the batch size, so I guess in your code a similar operation such as:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.view(-1, 2)
</code></pre>
<p>might be used while:</p>
<pre data-code-wrap="python"><code class="lang-python">x = x.view(x.size(0), -1)
</code></pre>
<p>would be the recommended way.</p>
<p>In any case, this change might just yield another shape mismatch if you haven’t written the model in a way it can accept variable spatial input shapes. Could you describe a bit more how the model is handling these different spatial shapes (e.g. via adaptive pooling layers)?</p>