import torch
import torch.nn as nn
import torch.nn.functional as F
# 定义一个简单的CNN模型
class SimpleCNN(nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
# 第一个卷积层,输入通道数为1,输出通道数为6,卷积核大小为2
self.conv1 = nn.Conv2d(3, 6, 2)
# 最大池化层,池化窗口大小为2,步长为2
self.pool = nn.MaxPool2d(2, 2)
# 第二个卷积层,输入通道数为6,输出通道数为16,卷积核大小为2
self.conv2 = nn.Conv2d(6, 16, 2)
# 全连接层
self.fc1 = nn.Linear(16 * 15 * 15, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# x = self.conv1(x)
# # breakpoint()
# x = self.pool(x)
# breakpoint()
x = self.pool(F.relu(self.conv1(x))) #(1,6,63,63) (1,6,31,31)
x = self.pool(F.relu(self.conv2(x))) #(1,16,30,30) (1,16,15,15)
x = x.view(-1, 16 * 15 * 15)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
# 创建模型实例
model = SimpleCNN()
# 假设输入数据是一个32x32的灰度图像,batch size为1
input_data = torch.randn(1, 3, 64, 64)
# 将输入数据传递给模型
output = model(input_data)
# 打印输出数据的形状
print(output.shape)
03-10
1848
03-18
4516
04-15
1794
07-04
9271
10-26
7910
09-27