首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >形状'[58,2048,-1]‘对于大小为534528的输入无效

形状'[58,2048,-1]‘对于大小为534528的输入无效
EN

Stack Overflow用户
提问于 2022-11-17 08:55:24
回答 1查看 49关注 0票数 0

我是PyTorch的新手。我在mnist上找到了胶囊网络的示例代码,我将其更改为使用自己的数据集,但它给了我一个运行时错误。

代码语言:javascript
运行
复制
Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3248\67117472.py in <module>
    176         train(capsule_net, optimizer,mnist.train_loader, e)
    177         print('start test')
--> 178         test(capsule_net, mnist.test_loader, e)

~\AppData\Local\Temp\ipykernel_3248\67117472.py in test(capsule_net, test_loader, epoch)
    142             data, target = data.cuda(), target.cuda()
    143 
--> 144         output, reconstructions, masked = capsule_net(data)
    145         loss = capsule_net.loss(data, output, target, reconstructions)
    146 

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~\AppData\Local\Temp\ipykernel_3248\1108288962.py in forward(self, data)
    142     def forward(self, data):
    143         #2
--> 144         output = self.digit_capsules(self.primary_capsules(self.conv_layer(data)))
    145         reconstructions, masked = self.decoder(output, data)
    146         return output, reconstructions, masked

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

~\AppData\Local\Temp\ipykernel_3248\1108288962.py in forward(self, x)
     34         u = [capsule(x) for capsule in self.capsules]
     35         u = torch.stack(u, dim=1)
---> 36         u = u.view(x.size(0), self.num_routes, -1)
     37         return self.squash(u)
     38 

RuntimeError: shape '[58, 2048, -1]' is invalid for input of size 534528

图像大小为32*32。有人能告诉我如何纠正这个错误吗?

有3层,Cov层,主上限和数字上限。列车数据集包含100幅图像,测试数据集包含20幅图像。

代码语言:javascript
运行
复制
class ConvLayer(nn.Module):
    def __init__(self, in_channels=3, out_channels=256, kernel_size=9):
        super(ConvLayer, self).__init__()

        self.conv = nn.Conv2d(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size,
                              stride=1
                              )

    def forward(self, x):
        return F.relu(self.conv(x))


class PrimaryCaps(nn.Module):
    def __init__(self, num_capsules=8, in_channels=256, out_channels=32, kernel_size=9, num_routes=32 * 6 * 6):
        super(PrimaryCaps, self).__init__()
        self.num_routes = num_routes
        self.capsules = nn.ModuleList([
            nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=2, padding=0)
            for _ in range(num_capsules)])

    def forward(self, x):
        print(x)
        u = [capsule(x) for capsule in self.capsules]
        u = torch.stack(u, dim=1)
        u = u.view(x.size(0), self.num_routes, -1)
        return self.squash(u)

    def squash(self, input_tensor):
        # take norm of input vectors
        squared_norm = (input_tensor ** 2).sum(-1, keepdim=True)
        output_tensor = squared_norm * input_tensor / ((1. + squared_norm) * torch.sqrt(squared_norm))
        return output_tensor


class DigitCaps(nn.Module):
    def __init__(self, num_capsules=10, num_routes=32 * 6 * 6, in_channels=8, out_channels=16):
        super(DigitCaps, self).__init__()

        self.in_channels = in_channels
        self.num_routes = num_routes
        self.num_capsules = num_capsules

        self.W = nn.Parameter(torch.randn(1, num_routes, num_capsules, out_channels, in_channels))

    def forward(self, x):
        batch_size = x.size(0)            
        x = torch.stack([x] * self.num_capsules, dim=2).unsqueeze(4)

        W = torch.cat([self.W] * batch_size, dim=0)
        u_hat = torch.matmul(W, x)    
        b_ij = Variable(torch.zeros(1, self.num_routes, self.num_capsules, 1))
        if USE_CUDA:
            b_ij = b_ij.cuda()

        num_iterations = 3
        for iteration in range(num_iterations):
            c_ij = F.softmax(b_ij, dim=1)
            c_ij = torch.cat([c_ij] * batch_size, dim=0).unsqueeze(4)

            s_j = (c_ij * u_hat).sum(dim=1, keepdim=True)
            v_j = self.squash(s_j)

            if iteration < num_iterations - 1:
                a_ij = torch.matmul(u_hat.transpose(3, 4), torch.cat([v_j] * self.num_routes, dim=1))
                b_ij = b_ij + a_ij.squeeze(4).mean(dim=0, keepdim=True)

        return v_j.squeeze(1)

    def squash(self, input_tensor):
        squared_norm = (input_tensor ** 2).sum(-1, keepdim=True)
        output_tensor = squared_norm * input_tensor / ((1. + squared_norm) * torch.sqrt(squared_norm))
        return output_tensor


class Decoder(nn.Module):
    def __init__(self, input_width=28, input_height=28, input_channel=1):
        super(Decoder, self).__init__()
        self.input_width = input_width
        self.input_height = input_height
        self.input_channel = input_channel
        self.reconstraction_layers = nn.Sequential(
            nn.Linear(16 * 10, 512),
            nn.ReLU(inplace=True),
            nn.Linear(512, 1024),
            nn.ReLU(inplace=True),
            nn.Linear(1024, self.input_height * self.input_width * self.input_channel),
            nn.Sigmoid()
        )

    def forward(self, x, data):
        classes = torch.sqrt((x ** 2).sum(2))
        classes = F.softmax(classes, dim=0)

        _, max_length_indices = classes.max(dim=1)
        masked = Variable(torch.sparse.torch.eye(10))
        if USE_CUDA:
            masked = masked.cuda()
        masked = masked.index_select(dim=0, index=Variable(max_length_indices.squeeze(1).data))
        t = (x * masked[:, :, None, None]).view(x.size(0), -1)
        reconstructions = self.reconstraction_layers(t)
        reconstructions = reconstructions.view(-1, self.input_channel, self.input_width, self.input_height)
        return reconstructions, masked


class CapsNet(nn.Module):
    def __init__(self, config=None):
        super(CapsNet, self).__init__()
        if config:
            self.conv_layer = ConvLayer(config.cnn_in_channels, config.cnn_out_channels, config.cnn_kernel_size)
            print(self.conv_layer)
            self.primary_capsules = PrimaryCaps(config.pc_num_capsules, config.pc_in_channels, config.pc_out_channels,
                                               config.pc_kernel_size, config.pc_num_routes)
            print(self.primary_capsules)
            self.digit_capsules = DigitCaps(config.dc_num_capsules, config.dc_num_routes, config.dc_in_channels,
                                            config.dc_out_channels)
            print(self.digit_capsules)
            self.decoder = Decoder(config.input_width, config.input_height, config.cnn_in_channels)
            print(self.decoder)
        else:
            self.conv_layer = ConvLayer()
            self.primary_capsules = PrimaryCaps()
            self.digit_capsules = DigitCaps()
            self.decoder = Decoder()

        self.mse_loss = nn.MSELoss()

    def forward(self, data):
        output = self.digit_capsules(self.primary_capsules(self.conv_layer(data)))
        reconstructions, masked = self.decoder(output, data)
        return output, reconstructions, masked

以下是主要功能的相关部分

代码语言:javascript
运行
复制
for e in range(1, N_EPOCHS + 1):
        transform = transforms.Compose([transforms.Resize(255),
                                transforms.CenterCrop(224),
                                transforms.ToTensor()])
       
        train(capsule_net, optimizer,mnist.train_loader, e)
        print('start test')
        test(capsule_net, mnist.test_loader, e)
EN

回答 1

Stack Overflow用户

发布于 2022-11-17 14:03:54

您正在尝试在forward类的PrimaryCaps方法中重新构造张量。但是,您正在尝试将其重塑为[58, 2048, -1],但是您有一个534528大小。534528不是58*2048的倍数。我的猜测是,您的self.num_routes的值应该是32 * 6 * 6,但是在代码中的某个地方,您将它定义为2048

代码语言:javascript
运行
复制
class PrimaryCaps(nn.Module):
    def __init__(self, num_capsules=8, in_channels=256, out_channels=32, kernel_size=9, num_routes=32 * 6 * 6):
        ...

    def forward(self, x):
        print(x)
        u = [capsule(x) for capsule in self.capsules]
        u = torch.stack(u, dim=1)
        u = u.view(x.size(0), self.num_routes, -1) #HERE
        return self.squash(u)

希望这能有所帮助。

编辑:您可能在这一行上设置了错误的值

代码语言:javascript
运行
复制
self.primary_capsules = PrimaryCaps(config.pc_num_capsules, config.pc_in_channels, config.pc_out_channels,
                                           config.pc_kernel_size, config.pc_num_routes)

其中config.pc_num_routes可能设置为2048年,这将覆盖您的32 * 6 * 6值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74472573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档