pytorch怎么设置学习率自动下降并画出折线图,代码演示
时间: 2023-06-07 17:07:47 浏览: 83
您好,关于您的问题,可以使用PyTorch中的lr_scheduler模块实现学习率自动下降,并使用matplotlib模块绘制折线图。
以下是一个简单的示例代码,供参考:
```python
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import StepLR
# 定义模型
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = x.view(-1, 784)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# 加载数据集
train_data = ...
test_data = ...
train_loader = DataLoader(train_data, batch_size=64, shuffle=True)
test_loader = DataLoader(test_data, batch_size=64)
# 定义优化器和学习率下降策略
model = MyModel()
optimizer = optim.SGD(model.parameters(), lr=0.1)
scheduler = StepLR(optimizer, step_size=1, gamma=0.1)
# 训练和测试
train_loss_list = []
test_loss_list = []
for epoch in range(5):
# 训练
train_loss = 0.0
for batch_idx, (data, target) in enumerate(train_loader):
optimizer.zero_grad()
output = model(data)
loss = F.cross_entropy(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item()
scheduler.step()
# 测试
test_loss = 0.0
for data, target in test_loader:
output = model(data)
loss = F.cross_entropy(output, target)
test_loss += loss.item()
train_loss_list.append(train_loss / len(train_loader))
test_loss_list.append(test_loss / len(test_loader))
# 绘制折线图
plt.plot(train_loss_list, label='train loss')
plt.plot(test_loss_list, label='test loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
```
在代码中,StepLR类表示每个epoch后将学习率乘以gamma。在这个例子中,学习率从0.1开始,每个epoch后除以10。train_loss_list和test_loss_list分别记录训练和测试集合的平均损失,然后使用matplotlib模块画出折线图。
阅读全文