RuntimeError: mat1 dim 1 must match mat2 dim 0?

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 ?

The model is :

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__() = channel
        self.height = height
        self.width = width

    def forward(self, input):
        return input.view(input.size(0),, 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.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
            nn.Linear(80000, 1024),

        # hidden => mu
        self.fc1 = nn.Linear(1024, self.latent_size)

        # hidden => logvar
        self.fc2 = nn.Linear(1024, self.latent_size)

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

    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):
            std = torch.exp(0.5 * logvar)
            eps = torch.randn_like(std)
            return eps.mul(std).add_(mu)
            return mu

    def forward(self, x):
        mu, logvar = self.encode(x)
        z = self.reparameterize(mu, logvar)
        return self.decode(z), mu, logvar

The main code is :

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()


    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 =, batch_size=1, shuffle=True,  num_workers=0)

    test_dataset = CustomDataset(test_image_paths, test_mask_paths, transform=transform, train=False)
    test_loader =, 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('=> loading checkpoint %s' % args.resume)
            checkpoint = torch.load(args.resume)
            start_epoch = checkpoint['epoch'] + 1
            best_test_loss = checkpoint['best_test_loss']
            print('=> loaded checkpoint %s' % args.resume)
            print('=> 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 < best_test_loss
        best_test_loss = min(test_loss, best_test_loss)
            '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__':

And the error code is :

train:   0%|          | 0/7 [00:00<?, ?it/s]
RuntimeError                              Traceback (most recent call last)
<ipython-input-10-ea45d861fe2d> in <module>()
     85 if __name__ == '__main__':
---> 86      main()

9 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/ in linear(input, weight, bias)
   1688     if input.dim() == 2 and bias is not None:
   1689         # fused op is marginally faster
-> 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

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

sir still iam unable to solve the error

this is the colab link for my code if possible help me solve this

The Colab notebook requires access rights, so could you post the error you are seeing now with my suggested changes, please?

below is my code and the image shows the error which iam getting please ty to help

3x3 convolution

class CNN(nn.Module):

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.Conv2d(in_channels=15, out_channels=64, kernel_size=3, padding=1),
        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.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
        nn.MaxPool2d(kernel_size=2, stride=2),

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

    self.fc_layer = nn.Sequential(
        nn.Linear(4096, 1024),
        nn.Linear(1024, 512),
        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

from keras.optimizers import Adam
model = CNN().to(device)

defining the optimizer

optimizer = Adam(model.parameters(), lr=0.07)

defining the loss function

criterion = nn.CrossEntropyLoss()

checking if GPU is available

if torch.cuda.is_available():
model = model.cuda()
criterion = criterion.cuda()

model = CNN().to(device)

Loss and optimizer

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

For updating learning rate

def update_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group[‘lr’] = lr

Train the model

total_step = len(train_loader)
curr_lr = learning_rate
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images =
labels =

    # Forward pass
    outputs = model(images)
    loss = criterion(outputs, labels)

    # Backward and optimize

    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>20:
    curr_lr /= 3
    update_lr(optimizer, curr_lr)
    # Test the model
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            images =
            labels =
            outputs = model(images)
            _, predicted = torch.max(, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
        print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))

Your first linear layer has input number of features = 4096.

You have to make sure that after doing above quoted operations the number of features of X (X.size(1)) is also equal to 4096.

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: