class SimpleCNN(nn.Module):
时间: 2023-10-23 22:11:04 浏览: 239
这是一个简单的卷积神经网络模型的定义,使用了PyTorch框架。下面是代码实现:
```
import torch.nn as nn
class SimpleCNN(nn.Module):
def __init__(self, num_classes):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.relu3 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 4 * 4, 128)
self.relu4 = nn.ReLU(inplace=True)
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.relu3(x)
x = self.pool3(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.relu4(x)
x = self.fc2(x)
return x
```
这个模型包含了3个卷积层、3个池化层和2个全连接层。输入的数据是一个3通道的图像,输出的是一个长度为num_classes的向量,每个元素表示对应类别的概率。
阅读全文