RuntimeError: 尺寸不匹配 m1: [a x b], m2: [c x d] [英] RuntimeError: size mismatch m1: [a x b], m2: [c x d]

查看:27
本文介绍了RuntimeError: 尺寸不匹配 m1: [a x b], m2: [c x d]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我吗?我得到以下错误.我使用谷歌 Colab.如何解决这个错误?

Can anyone help me in this.? I am getting below error. I use Google Colab. How to Solve this error.?

大小不匹配,m1:[64 x 100],m2:[784 x 128] 在/pytorch/aten/src/TH/generic/THTensorMath.cpp:2070

size mismatch, m1: [64 x 100], m2: [784 x 128] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:2070

下面的代码我正在尝试运行.

Below Code I am trying to Run.

    import torch
    from torch import nn
    import torch.nn.functional as F
    from torchvision import datasets, transforms

    # Define a transform to normalize the data
    transform = 
    transforms.Compose([transforms.CenterCrop(10),transforms.ToTensor(),])
    # Download the load the training data
    trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, 
    train=True, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, 
    shuffle=True)

    # Build a feed-forward network
    model = nn.Sequential(nn.Linear(784, 128),nn.ReLU(),nn.Linear(128, 
    64),nn.ReLU(),nn.Linear(64, 10))

    # Define the loss
    criterion = nn.CrossEntropyLoss()

   # Get our data
   images, labels = next(iter(trainloader))
   # Faltten images
   images = images.view(images.shape[0], -1)

   # Forward pass, get our logits
   logits = model(images)
   # Calculate the loss with the logits and the labels
   loss = criterion(logits, labels)
   print(loss)

推荐答案

你只需要关心 b=c 就完成了:

All you have to care is b=c and you are done:

m1: [a x b], m2: [c x d]

m1[a x b][batch size x in features]

m2[c x d][in features x out features]

这篇关于RuntimeError: 尺寸不匹配 m1: [a x b], m2: [c x d]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆