I got error for dimension of tensor

username: Khadijeh_Arabi
cooked: <p>for adding conditions to VAE I used this data set and labels but I got an error in part of the code:<br>
here is my data generator:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e6644dcf1b537b3bfbc62a199c41477165639af.png" data-download-href="https://discuss.pytorch.org/uploads/default/1e6644dcf1b537b3bfbc62a199c41477165639af" title="112"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/e/1e6644dcf1b537b3bfbc62a199c41477165639af.png" alt="112" data-base62-sha1="4kVqVWggfkOO43IbZkvSuEjiN3h" width="690" height="307" data-dominant-color="EDEEEE"><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">112</span><span class="informations">737×328 16.3 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>and I got an error in this part:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/0/2/020316b058849e080dbf28638d3638e422d16b91.png" alt="113" data-base62-sha1="hNztUihigYmw0dUDY2MOyJIN0t" width="628" height="125"><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/6/26db660ad48fe916b04a5324f2ca54c3cfe85202.png" data-download-href="https://discuss.pytorch.org/uploads/default/26db660ad48fe916b04a5324f2ca54c3cfe85202" title="114"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/6/26db660ad48fe916b04a5324f2ca54c3cfe85202.png" alt="114" data-base62-sha1="5xKcB9XIohDV7MUusY1KYSW4KTU" width="690" height="405" data-dominant-color="F4F4F3"><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">114</span><span class="informations">889×523 44.1 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><br>
how can I fix it?</p>

username: ptrblck
cooked: <p><code>labels</code> seems to be a scalar and thus doesn’t have dimensions:</p>
<pre><code class="lang-python">labels = torch.tensor(1.)
print(labels.shape)
&gt; torch.Size([])
labels.size(0)
&gt; IndexError: dimension specified as 0 but tensor has no dimensions
</code></pre>
<p>Make sure <code>cond</code> is created as a tensor with the expected number of dimensions.</p>

username: Khadijeh_Arabi
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  Thanks. Could the problem be from this line?<br>
train_loader,train_labels = next(data_gen(args.batch_size))<br>
I tried to use pytorch dataloader but I got this error:<br>
TypeError: object of type ‘generator’ has no len()<br>
I used the same method of adding condition with MNIST dataset, but my dataset is different</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Khadijeh_Arabi" data-post="3" data-topic="138335">
<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/khadijeh_arabi/48/29939_2.png" class="avatar"> Khadijeh_Arabi:</div>
<blockquote>
<p>I tried to use pytorch dataloader but I got this error:</p>
</blockquote>
</aside>
<p>Based on your code snippet you are not using a <code>DataLoader</code>, but are creating a custom <code>generator</code>, so I’m unsure where the error is coming from.<br>
Could you post a minimal, executable code snippet to reproduce the issue?</p>

username: Khadijeh_Arabi
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> . No I am not using DataLoader but maybe I have to change my generator and add length on it!? then using DataLoader? here is the code and I am adding conditioning to VAE</p>
<pre><code class="lang-auto">K = int(input("Enter K:"))
# construct the argument parser and parser the arguments
parser = argparse.ArgumentParser(description='VAE Example')
parser.add_argument('--batch-size', type=int, default=2048, metavar='N',
                    help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
                    help='number of epochs to train (default: 10)')
parser.add_argument('--seed', type=int, default=1, metavar='S',
                    help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                    help='how many batches to wait before logging training status')
args = parser.parse_args()

torch.manual_seed(args.seed)

# we use CPU for computation
#device = torch.device("cpu")
use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")

def gen_batch(BATCH_SIZE):
    labels = torch.randint(0, 8, (BATCH_SIZE,)).long().to(device)
    theta = (np.pi/4) * labels.float().to(device)
    centers = torch.stack((torch.cos(theta), torch.sin(theta)), dim = -1)
    noise = torch.randn_like(centers) * 0.1

    return centers + noise, labels

def data_gen(BATCH_SIZE):
    #8 gaussians
    while 1:
        yield gen_batch(BATCH_SIZE)

train_loader,train_labels = next(data_gen(args.batch_size))
test_loader = train_loader
cond_dim = train_labels.unique().size(0)

class CVAE(nn.Module):
    def __init__(self, c_dim):
        super(CVAE, self).__init__()
        # encoder part
        self.fc0 = nn.Linear(2 + c_dim, K)
        self.fc1 = nn.Linear(K, K)
        self.fc21 = nn.Linear(K, K)
        self.fc22 = nn.Linear(K, K)
        # decoder part
        self.fc3 = nn.Linear(K + c_dim, K)
        self.fc4 = nn.Linear(K, K)
        self.fc5 = nn.Linear(K, 2)

    def encode(self, x, c):
        concat_input = torch.cat([x, c], 1)
        h1 = F.selu(self.fc1(F.selu(self.fc0(concat_input))))
        return self.fc21(h1), self.fc22(h1)

    def reparameterize(self, mu, logvar):
        std = torch.exp(0.5*logvar)
        eps = torch.randn_like(std)
        return mu + eps*std
    
    def sample(self, n=2048):
        """
        Generate n samples from the generative model.
        """
        sample = torch.randn(n, K).to(device)
        out = cvae.decode(sample)

        return out.cpu().detach().numpy()


    def decode(self, z, c):
        concat_input = torch.cat([z, c], 1)
        return self.fc5(F.selu(self.fc4(F.selu(self.fc3(concat_input)))))

    def forward(self, x, c):
        mu, logvar = self.encode(x.view(-1, 2), c)
        z = self.reparameterize(mu, logvar)
        return self.decode(z, c), mu, logvar
cvae = CVAE(c_dim=cond_dim).to(device)
optimizer = optim.Adam(cvae.parameters(), lr=1e-3)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, verbose = True, threshold = 1E-2, eps=1e-6)
# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(recon_x, x, mu, logvar):
    #BCE = F.binary_cross_entropy(recon_x, x.view(-1, 2), reduction='sum')
    L2 = torch.mean((recon_x-x)**2)
 
    # see Appendix B from VAE paper:
    # Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
    # https://arxiv.org/abs/1312.6114
    # 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
    KLD = -0.5 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
    return L2 + KLD

def one_hot(labels, class_size): 
    targets = torch.zeros(labels.size(0), class_size)
    for i, label in enumerate(labels):
        targets[i, label] = 1
    return Variable(targets)

def train(epoch):
    cvae.train()
    train_loss = 0
   
    for batch_idx, (data,cond) in enumerate(train_loader):
        if batch_idx &gt; 100:
            break #100 batches per epoch
       # data = data.to(device)
        data, cond = data.to(device), one_hot(cond, cond_dim).to(device)
        optimizer.zero_grad()
        recon_batch, mu, logvar = cvae(data, cond)
        print("mu", mu)
        print("logvar", logvar)
        loss = loss_function(recon_batch, data, mu, logvar)
        recon_batch = recon_batch.detach().numpy()
       
        loss.backward()
        train_loss += loss.item()
        optimizer.step()
    train_loss /= 100
    scheduler.step(train_loss)


    samples_model = cvae.sample()
    samples_gt = gen_batch(2048)     
    kde_skl = KernelDensity(kernel='gaussian', bandwidth=0.2)
    kde_skl.fit(samples_gt)

    # score_samples() returns the log-likelihood of the samples
    log_pdf = kde_skl.score_samples(samples_model)
     
   
    print ("Epoch  average reconstruction error   :" ,epoch ,  train_loss)
    print (" likelihood  :", np.mean(log_pdf))
    return train_loss, np.mean(log_pdf)

if __name__ == "__main__":
    print("Starting training....")
    train_5 = []
    log_5 = []
  
    for epoch in tqdm(range(1, args.epochs + 1)):
     
        a,b= train(epoch)
    #    with open("output.txt","a") as f:
    #        print("epoch:",epoch,",","re:",2+a,",","li:",b,",","los", file=f)
    #        print("VAL ---","re:",2+a,",","li:",b,",","los", file=f)
    #        print("update best loss", file=f)
        
        
  
            
    gt = next(train_loader)
        
    with torch.no_grad():
        out1 = cvae.sample()
        recon = cvae(gt)[0].cpu().numpy()

    rx,ry = recon[:,0], recon[:,1]
        
    gt = gt.cpu().numpy()
    gx, gy = gt[:,0], gt[:,1]
        
    xs, ys = out1[:,0], out1[:,1]

    plt.scatter(gx, gy, c = 'red', s=3)
    plt.scatter(xs, ys, c = 'blue', s=3)
    plt.axes().set_aspect('equal')
    plt.show()
</code></pre>

username: ptrblck
cooked: <p>In your current code snippet you are assigning <code>train_loader</code> to the input batch, not a “loader” or generator object.<br>
If you want to use your custom generator, create an iterator and call <code>next</code> on it:</p>
<pre><code class="lang-python">data_iter = data_gen(args.batch_size)
next(data_iter)
next(data_iter)
next(data_iter)
next(data_iter)
</code></pre>

username: Khadijeh_Arabi
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  sorry but how should I use it? I still get the  same error<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b253f1b23e2a2cfb7fe73cde2efc7803dfd1e32e.png" data-download-href="https://discuss.pytorch.org/uploads/default/b253f1b23e2a2cfb7fe73cde2efc7803dfd1e32e" title="12"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/2/b253f1b23e2a2cfb7fe73cde2efc7803dfd1e32e.png" alt="12" data-base62-sha1="pryQ1SY8YbHi3Y7DcqjkrCwfsC2" width="536" height="500" data-dominant-color="F3F3F3"><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">12</span><span class="informations">644×600 11.9 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>IndexError: dimension specified as 0 but tensor has no dimensions</p>

username: ptrblck
cooked: <p><code>next(data_iter)</code> will return a data and target sample, not a <code>DataLoader</code>.<br>
If I execute your code, <code>index, (data, cond) = enumerate(train_loader).__next__()</code> will return the first two values from the data sample, so I don’t think that’s what you want to use.<br>
If you want to create a custom iterator and get the next sample, use <code>next(data_iter)</code> in your training loop.</p>

username: Khadijeh_Arabi
cooked: <p>Thanks, it works but now I got this error:</p>
<pre><code class="lang-auto">&lt;ipython-input-112-4098c099a02e&gt; in sample(self, n)
     28         """
     29         sample = torch.randn(n, K).to(device)
---&gt; 30         out = cvae.decode(sample, cond_dim)
     31 
     32         return out.cpu().detach().numpy()

&lt;ipython-input-112-4098c099a02e&gt; in decode(self, z, c)
     34 
     35     def decode(self, z, c):
---&gt; 36         concat_input = torch.cat([z, c], 1)
     37         return self.fc5(F.selu(self.fc4(F.selu(self.fc3(concat_input)))))
     38 

TypeError: expected Tensor as element 1 in argument 0, but got int
</code></pre>
<p>Is it because of torch.cat?</p>

username: ptrblck
cooked: <p>Yes, I guess you are passing <code>z</code> or <code>c</code> as an <code>int</code> while a tensor is expected:</p>
<pre><code class="lang-python">z, c = torch.randn(1, 1), torch.randn(1, 1)

out = torch.cat([z, c], dim=1) # works if both inputs are tensors

c = 1
out = torch.cat([z, c], dim=1)
# &gt; TypeError: expected Tensor as element 0 in argument 0, but got int
</code></pre>

username: Khadijeh_Arabi
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> , here the inputs of the decoder are (sample, con_dim). con_dim was int I changed that to tensor but now I got this error:</p>
<pre><code class="lang-auto">---&gt; 31     samples_model = cvae.sample()
     32     samples_gt = gen_batch(2048)
     33     kde_skl = KernelDensity(kernel='gaussian', bandwidth=0.2)

&lt;ipython-input-86-826f0d9710ca&gt; in sample(self, n)
     28         """
     29         sample = torch.randn(n, K).to(device)
---&gt; 30         out = cvae.decode(sample, cond_dim)
     31 
     32         return out.cpu().detach().numpy()

&lt;ipython-input-86-826f0d9710ca&gt; in decode(self, z, c)
     34 
     35     def decode(self, z, c):
---&gt; 36         concat_input = torch.cat([z, c], 1)
     37         return self.fc5(F.selu(self.fc4(F.selu(self.fc3(concat_input)))))
     38 

RuntimeError: zero-dimensional tensor (at position 1) cannot be concatenated
</code></pre>
<p>because the size of sample is 2048x20 but cond_dim has no dimension<br>
cond_dim.shape<br>
torch.Size([])</p>
<p>Then I tried to use torch.stack instead of torch.cat for k=8 but I got the following error, I think the logic of my choice for condition is wrong and every time I got new error:((</p>
<pre><code class="lang-auto">&lt;ipython-input-73-3eb5f1d14a58&gt; in train(epoch)
     16         data, cond = data.to(device), one_hot(cond, cond_dim).to(device)
     17         optimizer.zero_grad()
---&gt; 18         recon_batch, mu, logvar = cvae(data, cond)
     19         print("mu", mu)
     20         print("logvar", logvar)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

&lt;ipython-input-68-127374cfac97&gt; in forward(self, x, c)
     40         mu, logvar = self.encode(x.view(-1, 2), c)
     41         z = self.reparameterize(mu, logvar)
---&gt; 42         return self.decode(z, c), mu, logvar

&lt;ipython-input-68-127374cfac97&gt; in decode(self, z, c)
     35     def decode(self, z, c):
     36         concat_input = torch.stack([z, c], 1)
---&gt; 37         return self.fc5(F.selu(self.fc4(F.selu(self.fc3(concat_input)))))
     38 
     39     def forward(self, x, c):

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     94 
     95     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 96         return F.linear(input, self.weight, self.bias)
     97 
     98     def extra_repr(self) -&gt; str:

~\anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1845     if has_torch_function_variadic(input, weight):
   1846         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-&gt; 1847     return torch._C._nn.linear(input, weight, bias)
   1848 
   1849 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (4096x8 and 16x8)
</code></pre>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Khadijeh_Arabi" data-post="11" data-topic="138335">
<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/khadijeh_arabi/48/29939_2.png" class="avatar"> Khadijeh_Arabi:</div>
<blockquote>
<p>because the size of sample is 2048x20 but cond_dim has no dimension</p>
</blockquote>
</aside>
<p>I’m not sure how the concatenation or stacking would work in this case.<br>
Could you explain how the single value should be concatenated to the tensor of <code>[2048, 20]</code>?</p>
<p>The second error is raised due to a shape mismatch in a linear layer, so check the feature dimension in the input activation and make sure it matches the <code>in_features</code> of the linear layer.</p>

username: Khadijeh_Arabi
cooked: <p>I wanted to include the decoder to condition(cond_dim) و Is there another way to do this?</p>

username: ptrblck
cooked: <p>It would depend on your idea what the concatenation would mean.<br>
Right now you are using a 2D tensor in the shape <code>[2048, 20]</code> and are trying to concatenate a single value to it, which won’t work.<br>
Think about this use case as having an image and trying to “concatenate” a single value (pixel) to it.<br>
You could of course repeat the single pixel value and create a row, column, or entire image plane, and concatenate these tensors, but I don’t know what your overall use case expects.</p>

username: Khadijeh_Arabi
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> I changed the cond_dim dimension to [2048,1]:<br>
cond_dim = cond_dim.repeat(2048,1)<br>
actually just  as an input for decode in sample function because for other part for example<br>
cvae = CVAE(c_dim=cond_dim1).to(device)<br>
I couldn’t add 2D tensor. but The following error appeared after running:</p>
<pre><code class="lang-auto">---&gt; 28     samples_model = cvae.sample()
     29     samples_gt = gen_batch(2048)
     30     kde_skl = KernelDensity(kernel='gaussian', bandwidth=0.2)

&lt;ipython-input-120-59830598ea49&gt; in sample(self, n)
     32       #  sample = cvae.decode(z, c)
     33         sample = torch.randn(n, K).to(device)
---&gt; 34         out = cvae.decode(sample, cond_dim)
     35 
     36         return out.cpu().detach().numpy()

&lt;ipython-input-120-59830598ea49&gt; in decode(self, z, c)
     39     def decode(self, z, c):
     40         concat_input = torch.cat([z, c], 1)
---&gt; 41         return self.fc5(F.selu(self.fc4(F.selu(self.fc3(concat_input)))))
     42 
     43     def forward(self, x, c):

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     94 
     95     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 96         return F.linear(input, self.weight, self.bias)
     97 
     98     def extra_repr(self) -&gt; str:

~\anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1845     if has_torch_function_variadic(input, weight):
   1846         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-&gt; 1847     return torch._C._nn.linear(input, weight, bias)
   1848 
   1849 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (2048x21 and 28x20)
</code></pre>
<p>I really stuck to this, could you please help me to go further. thanks in advance</p>

username: ptrblck
cooked: <p>One of the linear layers (<code>self.fc3</code>, <code>self.fc4</code>, or <code>self.fc5</code>) raises the shape mismatch error, so you would need to make sure the feature dimension of the input activation matches the <code>in_features</code> of the corresponding linear layer. My best guess is that <code>concat_input</code>'s feature dim doesn’t fir into <code>self.fc3</code>.</p>

username: Khadijeh_Arabi
cooked: <p>Thank you for your reply. it seems that the conditioning itself will not help much with multi-modality but it is rather about the specific structure of the implemented VAE. So I used MNIST dataset instead of the previous dataset. But when all the dimensions of decoder and encoder part were K I got this error:</p>
<pre><code class="lang-auto">&lt;ipython-input-96-ac87136b4d41&gt; in train(epoch)
     13         data, cond = data.to(device), one_hot(cond, cond_dim).to(device)
     14         optimizer.zero_grad()
---&gt; 15         recon_batch, mu, logvar = cvae(data, cond)
     16      #   print("mu", mu)
     17      #   print("logvar", logvar)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

&lt;ipython-input-88-258e170a951c&gt; in forward(self, x, c)
     42 
     43     def forward(self, x, c):
---&gt; 44         mu, logvar = self.encode(x.view(-1, 2), c)
     45         z = self.reparameterize(mu, logvar)
     46         return self.decode(z, c), mu, logvar

&lt;ipython-input-88-258e170a951c&gt; in encode(self, x, c)
     14 
     15     def encode(self, x, c):
---&gt; 16         concat_input = torch.cat([x, c], 1)
     17         h1 = F.selu(self.fc1(F.selu(self.fc0(concat_input))))
     18         return self.fc21(h1), self.fc22(h1)

RuntimeError: torch.cat(): Sizes of tensors must match except in dimension 1. Got 39200 and 100 in dimension 0 (The offending index is 1)
</code></pre>
<p>and it just works when the dimensions were  similar to the following code:</p>
<pre><code class="lang-auto">def __init__(self, c_dim):
        super(CVAE, self).__init__()
        # encoder part
        self.fc0 = nn.Linear(784 + c_dim, 512)
        self.fc1 = nn.Linear(512, 256)
        self.fc21 = nn.Linear(256, 2)
        self.fc22 = nn.Linear(256, 2)
        # decoder part
        self.fc3 = nn.Linear(2 + c_dim, 256)
        self.fc4 = nn.Linear(256, 512)
        self.fc5 = nn.Linear(512, 784)
</code></pre>

username: ptrblck
cooked: <p>I think you are again hitting the issue of trying to concatenate two tensors with a different shape as described e.g. in <a href="https://discuss.pytorch.org/t/i-got-error-for-dimension-of-tensor/138335/14">this post</a>. This time you are trying to concatenate <code>x</code> and <code>c</code> in dim1 while dim0 shows the shape mismatch.</p>