Doing a neural style transfer project with PyTorch and getting this error, but not exactly sure where in the code it is being caused:
RuntimeError: The expanded size of the tensor (256) must match the existing size (32) at non-singleton dimension 2. at /opt/conda/conda-bld/pytorch_1503970438496/work/torch/lib/THC/generic/THCTensor.c:323
Here’s the code immediately preceding the error. If you think the source could be a problem with network architecture instead, I can post that as well for diagnostics.
transformer = TransformerNet()
mse_loss = torch.nn.MSELoss()
# l1_loss = torch.nn.L1Loss()
if torch.cuda.is_available():
    transformer.cuda()

CONTENT_WEIGHT = 1
STYLE_WEIGHT = 1e5
LOG_INTERVAL = 200
REGULARIZATION = 1e-7

LR = 1e-3
optimizer = Adam(transformer.parameters(), LR)
transformer.train()

for epoch in range(2):
    agg_content_loss = 0.
    agg_style_loss = 0.
    agg_reg_loss = 0.
    count = 0
    
    for batch_id, (x, _) in tqdm_notebook(enumerate(train_loader), total=len(train_loader)):
        n_batch = len(x)
        count += n_batch
        optimizer.zero_grad()
        x = Variable(x)
        if torch.cuda.is_available():
            x = x.cuda()

        y = transformer(x)
        xc = Variable(x.data, volatile=True)

        features_y = loss_network(y)
        features_xc = loss_network(xc)

        f_xc_c = Variable(features_xc[1].data, requires_grad=False)

        content_loss = CONTENT_WEIGHT * mse_loss(features_y[1], f_xc_c)

        reg_loss = REGULARIZATION * (
            torch.sum(torch.abs(y[:, :, :, :-1] - y[:, :, :, 1:])) + 
            torch.sum(torch.abs(y[:, :, :-1, :] - y[:, :, 1:, :])))

        style_loss = 0.
        for m in range(len(features_y)):
            gram_s = gram_style[m]
            gram_y = gram_matrix(features_y[m])
            style_loss += STYLE_WEIGHT * mse_loss(gram_y, gram_s.expand_as(gram_y))

        total_loss = content_loss + style_loss + reg_loss
        total_loss.backward()
        optimizer.step()

        agg_content_loss += content_loss.data[0]
        agg_style_loss += style_loss.data[0]
        agg_reg_loss += reg_loss.data[0]

        if (batch_id + 1) % LOG_INTERVAL == 0:
            mesg = "{} [{}/{}] content: {:.6f}  style: {:.6f}  reg: {:.6f}  total: {:.6f}".format(
                        time.ctime(), count, len(train_dataset),
                        agg_content_loss / LOG_INTERVAL,
                        agg_style_loss / LOG_INTERVAL,
                        agg_reg_loss / LOG_INTERVAL,
                        (agg_content_loss + agg_style_loss + agg_reg_loss) / LOG_INTERVAL
                    )
            print(mesg)
            agg_content_loss = 0
            agg_style_loss = 0
            agg_reg_loss = 0
            transformer.eval()
            y = transformer(x)
            save_debug_image(x.data, y.data, "debug/{}_{}.png".format(epoch, count))
            transformer.train()

Full error log below. If this isn’t enough diagnostic information can also post the code.

RuntimeError                              Traceback (most recent call last)
<ipython-input-46-76b6b94861f4> in <module>()
     20             x = x.cuda()
     21 
---> 22         y = transformer(x)
     23         xc = Variable(x.data, volatile=True)
     24 

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/Documents/pytorch/fast-neural-style/transformer_net.py in forward(self, X)
     35     def forward(self, X):
     36         in_X = X
---> 37         y = self.relu(self.in1(self.conv1(in_X)))
     38         y = self.relu(self.in2(self.conv2(y)))
     39         y = self.relu(self.in3(self.conv3(y)))

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/Documents/pytorch/fast-neural-style/transformer_net.py in forward(self, x)
    129         n = x.size(2) * x.size(3)
    130         t = x.view(x.size(0), x.size(1), n)
--> 131         mean = torch.mean(t, 2).unsqueeze(2).expand_as(x)
    132         # Calculate the biased var. torch.var returns unbiased var
    133         var = torch.var(t, 2).unsqueeze(2).expand_as(x) * ((n - 1) / float(n))

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/autograd/variable.py in expand_as(self, tensor)
    723 
    724     def expand_as(self, tensor):
--> 725         return Expand.apply(self, (tensor.size(),))
    726 
    727     def t(self):

~/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/autograd/_functions/tensor.py in forward(ctx, i, new_size)
    109     # tuple containing torch.Size or a list
    110     def forward(ctx, i, new_size):
--> 111         result = i.expand(*new_size)
    112         ctx.num_unsqueezed = result.dim() - i.dim()
    113         ctx.expanded_dims = [dim for dim, (expanded, original)

RuntimeError: The expanded size of the tensor (256) must match the existing size (32) at non-singleton dimension 2. at /opt/conda/conda-bld/pytorch_1503970438496/work/torch/lib/THC/generic/THCTensor.c:323