# 如何设计一个轻量化网络模型

1. 使用较少的卷积层和全连接层，减少模型的参数数量和计算量；
2. 使用卷积层进行特征提取，使用全局池化层进行特征整合；
3. 加入注意力机制，提升模型的语义理解能力；
4. 使用残差连接，增强模型的稳定性和泛化能力；
5. 对模型进行轻量化的优化，如参数量的剪枝、量化等。

import torch
import torch.nn as nn
import torch.nn.functional as F

class LightCNN(nn.Module):
def __init__(self):
super(LightCNN, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(128, 10)

def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
return x

# 训练模型
model = LightCNN()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 2000 == 1999:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0

# 测试模型
correct = 0
total = 0
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

print('Accuracy: %d %%' % (100 * correct / total))


posted @ 2023-05-02 22:52  Xu_Lin  阅读(125)  评论(0编辑  收藏  举报