RuntimeError: mat1 dim 1 must match mat2 dim 0?

oguzeraysal: <p>I got this error when i run my code "RuntimeError: mat1 dim 1 must match mat2 dim 0 ". Can anyone help to solve this problem ?</p>
<p>The model is :</p>
<pre><code class="lang-auto">class Flatten(nn.Module):
    def forward(self, input):
        return input.view(input.size(0), -1)


class Unflatten(nn.Module):
    def __init__(self, channel, height, width):
        super(Unflatten, self).__init__()
        self.channel = channel
        self.height = height
        self.width = width

    def forward(self, input):
        return input.view(input.size(0), self.channel, self.height, self.width)


class ConvVAE(nn.Module):

    def __init__(self, latent_size):
        super(ConvVAE, self).__init__()

        self.latent_size = latent_size

        self.encoder = nn.Sequential(
            nn.Conv2d(19, 64, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            Flatten(),
            nn.Linear(80000, 1024),
            nn.ReLU()
        )

        # hidden =&gt; mu
        self.fc1 = nn.Linear(1024, self.latent_size)

        # hidden =&gt; logvar
        self.fc2 = nn.Linear(1024, self.latent_size)

        self.decoder = nn.Sequential(
            nn.Linear(self.latent_size, 1024),
            nn.ReLU(),
            nn.Linear(1024,80000),
            nn.ReLU(),
            Unflatten(128, 25, 25),
            nn.ReLU(),
            nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1),
            nn.ReLU(),
            nn.ConvTranspose2d(64, 19, kernel_size=3, stride=2, padding=1),
            nn.Sigmoid()
        )

    def encode(self, x):
        h = self.encoder(x)
        mu, logvar = self.fc1(h), self.fc2(h)
        return mu, logvar

    def decode(self, z):
        z = self.decoder(z)
        return z

    def reparameterize(self, mu, logvar):
        if self.training:
            std = torch.exp(0.5 * logvar)
            eps = torch.randn_like(std)
            return eps.mul(std).add_(mu)
        else:
            return mu

    def forward(self, x):
        mu, logvar = self.encode(x)
        z = self.reparameterize(mu, logvar)
        return self.decode(z), mu, logvar
</code></pre>
<p>The main code is :</p>
<pre><code class="lang-auto">def main():
    parser = argparse.ArgumentParser(description='Convolutional VAE MNIST Example')
    parser.add_argument('--result_dir', type=str, default='results', metavar='DIR',
                        help='output directory')
    parser.add_argument('--batch_size', type=int, default=100, 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('--resume', default='', type=str, metavar='PATH',
                        help='path to latest checkpoint (default: None')

    # model options
    parser.add_argument('--latent_size', type=int, default=32, metavar='N',
                        help='latent vector size of encoder')
    parser.add_argument("-f", "--fff", help="a dummy argument to fool ipython", default="1")
    args = parser.parse_args()

    torch.manual_seed(args.seed)

    kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}

    train_dataset = CustomDataset(train_image_paths, train_mask_paths, transform=transform, train=True)
    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True,  num_workers=0)

    test_dataset = CustomDataset(test_image_paths, test_mask_paths, transform=transform, train=False)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False, num_workers=0)

    model = ConvVAE(args.latent_size).to(device)
    optimizer = optim.Adam(model.parameters(), lr=1e-3)

    start_epoch = 0
    best_test_loss = np.finfo('f').max

    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print('=&gt; loading checkpoint %s' % args.resume)
            checkpoint = torch.load(args.resume)
            start_epoch = checkpoint['epoch'] + 1
            best_test_loss = checkpoint['best_test_loss']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            print('=&gt; loaded checkpoint %s' % args.resume)
        else:
            print('=&gt; no checkpoint found at %s' % args.resume)

    writer = SummaryWriter()

    for epoch in range(start_epoch, args.epochs):
        train_loss = train(epoch, model, train_loader, optimizer, args)
        test_loss = test(epoch, model, test_loader, writer, args)

        # logging
        writer.add_scalar('train/loss', train_loss, epoch)
        writer.add_scalar('test/loss', test_loss, epoch)

        print('Epoch [%d/%d] loss: %.3f val_loss: %.3f' % (epoch + 1, args.epochs, train_loss, test_loss))

        is_best = test_loss &lt; best_test_loss
        best_test_loss = min(test_loss, best_test_loss)
        save_checkpoint({
            'epoch': epoch,
            'best_test_loss': best_test_loss,
            'state_dict': model.state_dict(),
            'optimizer': optimizer.state_dict(),
        }, is_best)

        with torch.no_grad():
            sample = torch.randn(64, 32).to(device)
            sample = model.decode(sample).cpu()
            img = make_grid(sample)
            writer.add_image('sampling', img, epoch)
            save_image(sample.view(64, 1, 28, 28), 'results/sample_' + str(epoch) + '.png')

if __name__ == '__main__':
     main()
</code></pre>
<p>And the error code is :</p>
<pre><code class="lang-auto">train:   0%|          | 0/7 [00:00&lt;?, ?it/s]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-10-ea45d861fe2d&gt; in &lt;module&gt;()
     84 
     85 if __name__ == '__main__':
---&gt; 86      main()
     87 

9 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1688     if input.dim() == 2 and bias is not None:
   1689         # fused op is marginally faster
-&gt; 1690         ret = torch.addmm(bias, input, weight.t())
   1691     else:
   1692         output = input.matmul(weight.t())

RuntimeError: mat1 dim 1 must match mat2 dim 0
</code></pre>

ptrblck: <p>The output of the 2nd conv layer in <code>self.encoder</code> will have the shape <code>[batch_size, 128, 7, 7]</code> for an input of <code>[batch_size, 19, 28, 28]</code> and the <code>Flatten()</code> layer will thus create an activation of <code>[batch_size, 6272]</code>, which doesn’t fit the expected 80000 input features in the following linear layer.</p>

swarup: <p>sir still iam unable to solve the error</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20201216-085601-RC00_347824393" class="site-icon" width="128" height="128">
      <a href="https://colab.research.google.com/drive/1RPG2-jVzgpHN0ATRCRvGc1IP4UnSnKtd" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/drive/1RPG2-jVzgpHN0ATRCRvGc1IP4UnSnKtd" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<p>
this is the colab link for my code if possible help me solve this</p>

ptrblck: <p>The Colab notebook requires access rights, so could you post the error you are seeing now with my suggested changes, please?</p>

swarup: <p>below is my code and the image shows the error which iam getting please ty to help</p>
<h1><a name="h-3x3-convolution-1" class="anchor" href="#h-3x3-convolution-1"></a>3x3 convolution</h1>
<p>class CNN(nn.Module):<br>
“”“CNN.”“”</p>
<pre><code>def __init__(self):
    """CNN Builder."""
    super(CNN, self).__init__()

    self.conv_layer = nn.Sequential(
    

        # Conv Layer block 1
        nn.Conv2d(in_channels=3, out_channels=15, kernel_size=3, padding=1),
        nn.BatchNorm2d(15),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=15, out_channels=64, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),

        # Conv Layer block 2
        nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
        nn.BatchNorm2d(128),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),
        nn.Dropout2d(p=0.05),

        # Conv Layer block 3
        nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
        nn.BatchNorm2d(256),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),
    )


    self.fc_layer = nn.Sequential(
        nn.Dropout(p=0.1),
        nn.Linear(4096, 1024),
        nn.ReLU(inplace=True),
        nn.Linear(1024, 512),
        nn.ReLU(inplace=True),
        nn.Dropout(p=0.1),
        nn.Linear(512, 10)
    )


def forward(self, X):
    """Perform forward."""
    
    # conv layers
    X = self.conv_layer(X)
    
    # flatten
    X = X.view(X.size(0), -1)
    # fc layer
    X = self.fc_layer(X)

    return X
</code></pre>
<p>print(X)<br>
from keras.optimizers import Adam<br>
model = CNN().to(device)</p>
<h1><a name="defining-the-optimizer-2" class="anchor" href="#defining-the-optimizer-2"></a>defining the optimizer</h1>
<p>optimizer = Adam(model.parameters(), lr=0.07)</p>
<h1><a name="defining-the-loss-function-3" class="anchor" href="#defining-the-loss-function-3"></a>defining the loss function</h1>
<p>criterion = nn.CrossEntropyLoss()</p>
<h1><a name="checking-if-gpu-is-available-4" class="anchor" href="#checking-if-gpu-is-available-4"></a>checking if GPU is available</h1>
<p>if torch.cuda.is_available():<br>
model = model.cuda()<br>
criterion = criterion.cuda()</p>
<p>print(model)<br>
model = CNN().to(device)</p>
<h1><a name="loss-and-optimizer-5" class="anchor" href="#loss-and-optimizer-5"></a>Loss and optimizer</h1>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>
<h1><a name="for-updating-learning-rate-6" class="anchor" href="#for-updating-learning-rate-6"></a>For updating learning rate</h1>
<p>def update_lr(optimizer, lr):<br>
for param_group in optimizer.param_groups:<br>
param_group[‘lr’] = lr</p>
<h1><a name="train-the-model-7" class="anchor" href="#train-the-model-7"></a>Train the model</h1>
<p>total_step = len(train_loader)<br>
curr_lr = learning_rate<br>
for epoch in range(num_epochs):<br>
for i, (images, labels) in enumerate(train_loader):<br>
images = images.to(device)<br>
labels = labels.to(device)</p>
<pre><code>    # Forward pass
    outputs = model(images)
    loss = criterion(outputs, labels)

    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if (i+1) % 500 == 0:
        print ("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}"
               .format(epoch+1, num_epochs, i+1, total_step, loss.item()))

# Decay learning rate
if (epoch) == 1 or epoch&gt;20:
    curr_lr /= 3
    update_lr(optimizer, curr_lr)
    
    # Test the model
    model.eval()
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    
        print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
            
    model.train()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/5/b5447106a52775653b95f64f874a0f6546941e30.png" data-download-href="https://discuss.pytorch.org/uploads/default/b5447106a52775653b95f64f874a0f6546941e30" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b5447106a52775653b95f64f874a0f6546941e30_2_690x303.png" alt="image" data-base62-sha1="pRz3yFfAMVO63312akKmMjMvkGc" width="690" height="303" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b5447106a52775653b95f64f874a0f6546941e30_2_690x303.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/5/b5447106a52775653b95f64f874a0f6546941e30_2_1035x454.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/5/b5447106a52775653b95f64f874a0f6546941e30.png 2x" data-dominant-color="F6F6F6"><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">image</span><span class="informations">1178×518 46.4 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>

Alexey_Demyanchuk: <aside class="quote no-group" data-username="swarup" data-post="5" data-topic="106623">
<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/swarup/48/31639_2.png" class="avatar"> swarup:</div>
<blockquote>
<p><code>nn.Linear(4096, 1024),</code></p>
</blockquote>
</aside>
<p>Your first linear layer has input number of features = 4096.</p>
<aside class="quote no-group" data-username="swarup" data-post="5" data-topic="106623">
<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/swarup/48/31639_2.png" class="avatar"> swarup:</div>
<blockquote>
<pre><code class="lang-auto">X = self.conv_layer(X)
    
# flatten
X = X.view(X.size(0), -1)
</code></pre>
</blockquote>
</aside>
<p>You have to make sure that after doing above quoted operations the number of features of <code>X</code> (<code>X.size(1)</code>) is also equal to 4096.</p>
<p>In general, you have to adapt the number of input features of the first linear layer to the number of output features after the last convolution operation, which in your case is:</p>
<aside class="quote no-group" data-username="swarup" data-post="5" data-topic="106623">
<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/swarup/48/31639_2.png" class="avatar"> swarup:</div>
<blockquote>
<pre><code class="lang-auto">        nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),
</code></pre>
</blockquote>
</aside>