请用Anaconda3 写python代码: 设计自定义的ResNet 数据:MINST <导入必要的PyTorch包 1.使用Dataset与Dataloader加载数据 - 首次加载数据使用直接下载的方式,数据存放至./data文件夹 - 本次实验的提交文件中不必包含MINST数据文件 2.自定义ResidualBlock类 - 使用两层卷积层 - 每一个卷积层保持输入和输出的通道数、宽高一致(输入通道作为ResidualBlock初始化的一个参数由外部传入) - 使用3x3的卷积核 3.定义前面给出的模型结构并实例化网络模型、交叉熵损失、SGD优化器 4.定义单次训练/测试的函数 参考课程示例代码,训练阶段每300个batch打印一下损失 5.设置训练周期为10次,打印每个周期中训练过程中的模型损失及测试过程中的准确率 6.定义一个函数get_n_params,来计算上述模型中的参数个数 ```def get_n_params(model)``` Hint: 调研model.parameters()的使用 7.保存训练好的模型到文件resnet.pt
时间: 2024-02-09 12:08:56 浏览: 68
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
# 自定义ResidualBlock类
class ResidualBlock(nn.Module):
def __init__(self, in_channels):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(in_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(in_channels)
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu(out)
return out
# 定义ResNet模型
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, padding=2, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.layer1 = nn.Sequential(
ResidualBlock(16),
ResidualBlock(16),
ResidualBlock(16)
)
self.layer2 = nn.Sequential(
ResidualBlock(16),
ResidualBlock(16),
ResidualBlock(16)
)
self.layer3 = nn.Sequential(
ResidualBlock(16),
ResidualBlock(16),
ResidualBlock(16)
)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(16, 10)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = nn.functional.relu(out)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.avg_pool(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
# 加载数据
train_dataset = MNIST('./data', train=True, download=True, transform=ToTensor())
test_dataset = MNIST('./data', train=False, download=True, transform=ToTensor())
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)
# 实例化模型、损失函数、优化器
model = ResNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
# 定义训练函数
def train(model, dataloader, criterion, optimizer):
model.train()
running_loss = 0.0
for i, (inputs, labels) in enumerate(dataloader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if (i+1) % 300 == 0:
print('Batch [%d/%d], Loss: %.4f' % (i+1, len(dataloader), running_loss/300))
running_loss = 0.0
# 定义测试函数
def test(model, dataloader):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in dataloader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
return accuracy
# 定义获取模型参数个数的函数
def get_n_params(model):
return sum([p.numel() for p in model.parameters() if p.requires_grad])
# 训练模型
num_epochs = 10
for epoch in range(num_epochs):
train(model, train_loader, criterion, optimizer)
test_acc = test(model, test_loader)
print('Epoch [%d/%d], Loss: %.4f, Test Accuracy: %.2f%%' % (epoch+1, num_epochs, running_loss/len(train_loader), test_acc))
# 保存模型
torch.save(model.state_dict(), 'resnet.pt')
```
阅读全文