在Pytorch中可以使用torch.( )函数将已经训练好的模型保存到指定的文件夹下的指定文件
时间: 2024-03-02 13:48:19 浏览: 122
在 PyTorch 中,可以使用 `torch.save()` 函数将已经训练好的模型保存到指定的文件夹下的指定文件。该函数可以将 PyTorch 模型的参数保存到指定文件中,以便后续恢复模型参数。具体语法如下:
```python
torch.save(model.state_dict(), 'file_path')
```
其中,`model.state_dict()` 表示保存模型的参数,`file_path` 是保存模型参数的文件路径。需要注意的是,保存的模型参数需要与当前代码的模型参数类型一致。同时,保存的模型参数需要保存在 CPU 或 GPU 中,具体与当前代码中的模型参数保存位置相同。
相关问题
1.使用PyTorch实现LeNet网络 2.使用本地文件夹的 Fashion MNIST 对LeNet进行训练和测试,优化算法采用SGD或Adam,torch.optim.SGD 或 torch.optim.Adam 3.分别绘制训练和测试的损失函数曲线和分类正确率曲线 4.调节BatchSize、学习率,并依据测试损失曲线的拐点确定最佳模型,保存该模型 5.使用测试集测试所保存模型的性能,以混淆矩阵展示
好的,我来给你讲解一下使用PyTorch实现LeNet网络的具体步骤:
1.导入所需要的库
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
```
2.定义LeNet网络
```python
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.pool2 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(torch.relu(self.conv1(x)))
x = self.pool2(torch.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
```
3.加载数据集
```python
batch_size = 64
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=2)
testset = torchvision.datasets.FashionMNIST(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=2)
```
4.定义优化器和损失函数
```python
net = LeNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
```
5.训练模型并绘制损失函数曲线和分类正确率曲线
```python
epochs = 10
train_losses = []
train_accs = []
test_losses = []
test_accs = []
for epoch in range(epochs):
running_loss = 0.0
running_acc = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
running_acc += (predicted == labels).sum().item()
train_loss = running_loss / len(trainloader.dataset)
train_acc = running_acc / len(trainloader.dataset)
train_losses.append(train_loss)
train_accs.append(train_acc)
test_loss = 0.0
test_acc = 0.0
with torch.no_grad():
for data in testloader:
inputs, labels = data
outputs = net(inputs)
loss = criterion(outputs, labels)
test_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
test_acc += (predicted == labels).sum().item()
test_loss /= len(testloader.dataset)
test_acc /= len(testloader.dataset)
test_losses.append(test_loss)
test_accs.append(test_acc)
print('Epoch [%d/%d], Train Loss: %.4f, Train Accuracy: %.4f, Test Loss: %.4f, Test Accuracy: %.4f'
% (epoch + 1, epochs, train_loss, train_acc, test_loss, test_acc))
plt.plot(train_losses, label='Training Loss')
plt.plot(test_losses, label='Testing Loss')
plt.legend()
plt.show()
plt.plot(train_accs, label='Training Accuracy')
plt.plot(test_accs, label='Testing Accuracy')
plt.legend()
plt.show()
```
6.保存最佳模型
```python
best_test_loss = min(test_losses)
best_epoch = test_losses.index(best_test_loss)
print('Best Epoch: %d, Best Test Loss: %.4f' % (best_epoch + 1, best_test_loss))
torch.save(net.state_dict(), 'best_model.pth')
```
7.使用混淆矩阵展示模型性能
```python
confusion_matrix = torch.zeros(10, 10)
with torch.no_grad():
for data in testloader:
inputs, labels = data
outputs = net(inputs)
_, predicted = torch.max(outputs, 1)
for i in range(len(labels)):
confusion_matrix[labels[i]][predicted[i]] += 1
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot')
plt.figure(figsize=(10, 10))
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.show()
```
通过以上步骤,我们就可以完成使用PyTorch实现LeNet网络的训练和测试,并且得到最佳模型,最后使用混淆矩阵展示模型性能。
请用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
好的,以下是代码实现:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
# 定义自定义的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, stride=1, 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, stride=1, 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, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.layer1 = nn.Sequential(
ResidualBlock(64),
ResidualBlock(64)
)
self.layer2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
ResidualBlock(128),
ResidualBlock(128)
)
self.layer3 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
ResidualBlock(256),
ResidualBlock(256)
)
self.layer4 = nn.Sequential(
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
ResidualBlock(512),
ResidualBlock(512)
)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# 定义自定义的MINST数据集
class CustomMNISTDataset(Dataset):
def __init__(self, train=True, transform=None):
self.train = train
self.transform = transform
if self.train:
self.data, self.labels = torch.utils.data.Subset(torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()), list(range(0, 50000)))
else:
self.data, self.labels = torch.utils.data.Subset(torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()), list(range(50000, 60000)))
def __getitem__(self, index):
img, target = self.data[index], self.labels[index]
if self.transform is not None:
img = self.transform(img)
return img, target
def __len__(self):
return len(self.data)
# 定义数据预处理方式
transform = transforms.Compose([
transforms.Normalize((0.1307,), (0.3081,))
])
# 加载训练集和测试集
train_dataset = CustomMNISTDataset(train=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
test_dataset = CustomMNISTDataset(train=False, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)
# 创建ResNet模型
model = ResNet()
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
# 定义单次训练函数
def train_one_epoch(model, train_loader, criterion, optimizer, epoch):
model.train()
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 300 == 299: # 每300个batch打印一下损失
print('[Epoch %d, Batch %d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 300))
running_loss = 0.0
# 定义测试函数
def test(model, test_loader):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
return accuracy
# 训练模型
for epoch in range(10):
train_one_epoch(model, train_loader, criterion, optimizer, epoch)
test_accuracy = test(model, test_loader)
print('[Epoch %d] test accuracy: %.2f%%' % (epoch + 1, test_accuracy))
# 定义函数计算模型参数个数
def get_n_params(model):
return sum([p.numel() for p in model.parameters()])
# 打印模型参数个数
print('Number of parameters: %d' % get_n_params(model))
# 保存模型
torch.save(model.state_dict(), 'resnet.pt')
```
注意:这段代码在第一次运行时会直接下载MINST数据集,存放至./data文件夹下。如果之后再次运行需要手动删除./data文件夹下的所有文件。同时,为了减小训练集大小,代码中只使用前50000个样本作为训练集,后10000个样本作为测试集。如果需要使用完整的训练集,可以将`self.data, self.labels`的赋值语句改为`torchvision.datasets.MNIST(root='./data', train=self.train, download=True, transform=transforms.ToTensor())`。
阅读全文