【发布时间】:2022-11-24 23:21:08
【问题描述】:
有什么想法可以解决这个运行时错误吗?
我需要创建:
- 一个二维卷积层,带有 10 个大小为 5x5 的过滤器,步幅为 1,零填充,后跟 通过 ReLU 激活,然后是大小为 2x2 的 2d 最大池化操作。
- 一个二维卷积层,带有 20 个大小为 5x5、步幅为 1、零填充的滤波器,其后 通过 ReLU 激活,然后是大小为 2x2 的 2d 最大池化操作。
- 全连接层后跟 ReLU 激活。
input_size = 1 * 28 * 28 # input spatial dimension of images hidden_size = 128 # width of hidden layer output_size = 10 # number of output neurons class CNN(torch.nn.Module): def __init__(self): super().__init__() self.flatten = torch.nn.Flatten(start_dim=1) # ------------------ # Write your implementation here. self.conv1 = torch.nn.Conv2d(in_channels = 1, out_channels = 10, kernel_size = 5, stride = 1, padding = 1, padding_mode = 'zeros') self.conv2 = torch.nn.Conv2d(in_channels = 10, out_channels = 20, kernel_size = 5, stride = 1, padding = 1, padding_mode = 'zeros') self.fc = torch.nn.Linear(input_size, output_size) self.max_pool2d = torch.nn.MaxPool2d(kernel_size = 2) self.act = torch.nn.ReLU() self.log_softmax = torch.nn.LogSoftmax(dim = 1) # ------------------ def forward(self, x): # Input image is of shape [batch_size, 1, 28, 28] # Need to flatten to [batch_size, 784] before feeding to fc1 # ------------------ # Write your implementation here. x = self.conv1(x) x = self.act(x) x = self.max_pool2d(x) x = self.conv2(x) x = self.act(x) x = self.max_pool2d(x) x = self.flatten(x) # x = x.view(x.size(0), -1) x = self.act(self.fc(x)) y_output = self.log_softmax(x) return y_output # ------------------ model = CNN().to(DEVICE) # sanity check print(model) from torchsummary import summary summary(model, (1,32,32))因为我不知道如何解决这个错误,所以遇到了麻烦。
【问题讨论】:
标签: python pytorch neural-network conv-neural-network