简体   繁体   English

如何解决 pytorch 中的尺寸不匹配错误?

[英]How to solve size mismatch error in pytorch?

I am trying to create a logistic model by using CIFAR10 data in PyTorch.我正在尝试通过使用 PyTorch 中的 CIFAR10 数据来创建逻辑 model。 After running the model for evaluation I run into an error:运行 model 进行评估后,我遇到了一个错误:

RuntimeError: size mismatch, m1: [750 x 4096], m2: [1024 x 10] at C:\w\1\s\tmp_conda_3.7_100118\conda\conda-bld\pytorch_1579082551706\work\aten\src\TH/generic/THTensorMath.cpp:136 RuntimeError:尺寸不匹配,m1:[750 x 4096],m2:[1024 x 10] 在 C:\w\1\s\tmp_conda_3.7_100118\conda\conda-bld\pytorch_15790825\TH///s通用/THTensorMath.cpp:136

It seems like input_size is creating a problem, I dont know I am new to this.似乎 input_size 正在制造一个问题,我不知道我是新手。 Please let me know what changes should I make in order to overcome this error.请让我知道我应该进行哪些更改以克服此错误。

These are the hyperparameters:这些是超参数:

batch_size = 100
learning_rate = 0.001

# Other constants
input_size = 4*4*64
num_classes = 10

This is the cell that downloads and splits the dataset into train, validation and test.这是下载数据集并将其拆分为训练、验证和测试的单元。

transform = torchvision.transforms.Compose(
    [torchvision.transforms.ToTensor(),
     torchvision.transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])

testset = torchvision.datasets.CIFAR10(root='D:\PyTorch\cifar-10-python', train=False,download=False, transform=transform)
trainvalset = torchvision.datasets.CIFAR10(root='D:\PyTorch\cifar-10-python', train=True,download=False, transform=transform)
trainset, valset = torch.utils.data.random_split(trainvalset, [45000, 5000]) # 10% for validation

train_loader = torch.utils.data.DataLoader(trainset, batch_size=50, shuffle=True)
test_loader = torch.utils.data.DataLoader(testset, batch_size=1000, shuffle=False)
val_loader = torch.utils.data.DataLoader(valset, batch_size=1000, shuffle=False)

This is the architecture of my model.这是我的 model 的架构。

class CifarModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(input_size,  num_classes)
    def forward(self, xb):
        xb = xb.view(-1, 64*8*8)
        #xb = xb.reshape(-1, 784)
        print(xb.shape)
        out = self.linear(xb)
        return out

    def training_step(self, batch):
        images, labels = batch 
        out = self(images)                  # Generate predictions
        loss = F.cross_entropy(out, labels) # Calculate loss
        return loss

    def validation_step(self, batch):
        images, labels = batch 
        out = self(images)                    # Generate predictions
        loss = F.cross_entropy(out, labels)   # Calculate loss
        acc = accuracy(out, labels)           # Calculate accuracy
        return {'val_loss': loss.detach(), 'val_acc': acc.detach()}

    def validation_epoch_end(self, outputs):
        batch_losses = [x['val_loss'] for x in outputs]
        epoch_loss = torch.stack(batch_losses).mean()   # Combine losses
        batch_accs = [x['val_acc'] for x in outputs]
        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies
        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}

    def epoch_end(self, epoch, result):
        print("Epoch [{}], val_loss: {:.4f}, val_acc: {:.4f}".format(epoch, result['val_loss'], result['val_acc']))

model = CifarModel()
def accuracy(outputs, labels):
    _, preds = torch.max(outputs, dim=1)
    return torch.tensor(torch.sum(preds == labels).item() / len(preds))
def evaluate(model, val_loader):
    outputs = [model.validation_step(batch) for batch in val_loader]
    return model.validation_epoch_end(outputs)

def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
    history = []
    optimizer = opt_func(model.parameters(), lr)
    for epoch in range(epochs):
        # Training Phase 
        for batch in train_loader:
            loss = model.training_step(batch)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        # Validation phase
        result = evaluate(model, val_loader)
        model.epoch_end(epoch, result)
        history.append(result)
    return history
evaluate(model, val_loader)

Here you are specifying that the number of output classes should be 10:在这里,您指定 output 类的数量应为 10:

num_classes = 10

Your forward function does not reflect this:您的转发 function 没有反映这一点:

xb = xb.view(-1, 64*8*8) # you get 750x4096
out = self.linear(xb) # here an input of 
# input_size to linear layer = 4*4*64 # 1024
# num_classes = 10 

Modify it like this:像这样修改它:

xb = xb.view(-1, 64*4*4) # you get 750x1024
out = self.linear(xb) # M1 750x1024 M2 1024x10:
# input_size = 4*4*64 # 1024
# num_classes = 10 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 Python (Pytorch) 中的大小不匹配错误 - How to solve size mismatch error in Python (Pytorch) 如何解决 pytorch 中 Multi Head Attention 的大小不匹配? - How to solve size mismatch of Multi Head Attention in pytorch? 如何解决由于 PyTorch 中大小不匹配导致的运行时错误? - How to resolve runtime error due to size mismatch in PyTorch? 将 TensorFlow model 转换为 Pytorch 时出现大小不匹配错误 - Size mismatch error in converting TensorFlow model to Pytorch 如何修复:运行时错误:pyTorch 中的大小不匹配 - How To Fix: RuntimeError: size mismatch in pyTorch 如何解决错误:PyTorch 中的预期输入批量大小和目标批量大小不匹配? - How to solve error: no match between expected input batch size and target batch size in PyTorch? 在 Pytorch 卷积神经网络中展平张量(大小不匹配错误) - Flatten Tensor in Pytorch Convolutional Neural Network (size mismatch error) VGG16 在 Pytorch 上的实现给出了大小不匹配错误 - Implementation of VGG16 on Pytorch giving size mismatch error 尝试加载 PyTorch Model 时出现大小不匹配运行时错误 - Size Mismatch Runtime Error When Trying to Load a PyTorch Model 如何解决PyTorch中奇怪的CUDA错误? - How to solve strange cuda error in PyTorch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM