大小不匹配,m1:[3584 x 28],m2:[784 x 128] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp:940
Posted
技术标签:
【中文标题】大小不匹配,m1:[3584 x 28],m2:[784 x 128] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp:940【英文标题】:size mismatch, m1: [3584 x 28], m2: [784 x 128] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:940 【发布时间】:2019-06-10 15:08:08 【问题描述】:我已经执行了以下代码并得到了显示在最底部的错误。我想知道如何解决这个问题。谢谢
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torchvision import transforms
_tasks = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
from torchvision.datasets import MNIST
mnist = MNIST("data", download=True, train=True, transform=_tasks)
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
create training and validation split
split = int(0.8 * len(mnist))
index_list = list(range(len(mnist)))
train_idx, valid_idx = index_list[:split], index_list[split:]
create sampler objects using SubsetRandomSampler
tr_sampler = SubsetRandomSampler(train_idx)
val_sampler = SubsetRandomSampler(valid_idx)
create iterator objects for train and valid datasets
trainloader = DataLoader(mnist, batch_size=256, sampler=tr_sampler)
validloader = DataLoader(mnist, batch_size=256, sampler=val_sampler)
创建执行模型
class Model(nn.Module):
def init(self):
super().init()
self.hidden = nn.Linear(784, 128)
self.output = nn.Linear(128, 10)
def forward(self, x):
x = self.hidden(x)
x = F.sigmoid(x)
x = self.output(x)
return x
model = Model()
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, weight_decay= 1e-6, momentum = 0.9, nesterov = True)
for epoch in range(1, 11): ## run the model for 10 epochs
train_loss, valid_loss = [], []
#training part
model.train()
for data, target in trainloader:
optimizer.zero_grad()
#1. forward propagation
output = model(data)
#2. loss calculation
loss = loss_function(output, target)
#3. backward propagation
loss.backward()
#4. weight optimization
optimizer.step()
train_loss.append(loss.item())
# evaluation part
model.eval()
for data, target in validloader:
output = model(data)
loss = loss_function(output, target)
valid_loss.append(loss.item())
执行此操作时出现以下错误:
RuntimeError Traceback (最近一次调用最后一次) in () ----> 1 output = model(data) 2 3 ## 2. 损失计算 4 loss = loss_function(output, target) 5
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py 呼叫(自我,*输入,**kwargs)487 结果=自我。_slow_forward(*输入, **kwargs)
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py 在 线性(输入,权重,偏差)1352 ret = torch.addmm(torch.jit._unwrap_optional(偏差),输入,weight.t())1353 别的: -> 1354 输出 = input.matmul(weight.t()) 1355 如果偏差不是无:1356 输出 += torch.jit._unwrap_optional(bias)
RuntimeError:大小不匹配,m1:[3584 x 28],m2:[784 x 128] /pytorch/aten/src/TH/generic/THTensorMath.cpp:940
【问题讨论】:
【参考方案1】:您的输入 MNIST 数据的形状为 [256, 1, 28, 28]
,对应于 [B, C, H, W]
。您需要将输入图像展平为单个 784 长向量,然后将其馈送到线性层 Linear(784, 128)
,以便输入变为与 [B, N]
对应的 [256, 784]
,其中 N 为 1x28x28,即您的图像大小。这可以按如下方式完成:
for data, target in trainloader:
# Flatten MNIST images into a 784 long vector
data = data.view(data.shape[0], -1)
optimizer.zero_grad()
...
在验证循环中也需要这样做。
【讨论】:
以上是关于大小不匹配,m1:[3584 x 28],m2:[784 x 128] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp:940的主要内容,如果未能解决你的问题,请参考以下文章