def train(net, train_iter, val_iter, num_epochs, lr, wd, devices, lr_period, lr_decay): global val_acc, metric net.to(devices[0]) trainer = torch.optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=wd) scheduler = torch.optim.lr_scheduler.StepLR(trainer, lr_period, lr_decay) num_batches, timer = len(train_iter), d2l.Timer() train_loss, train_accs, val_accs = [], [], [] for epoch in range(num_epochs): net.train() metric = d2l.Accumulator(3) for i, (features, labels) in enumerate(train_iter): trainer.zero_grad() features, labels = features.to(devices[0]), labels.to(devices[0]) l, acc = d2l.train_batch_ch13(net, features, labels, loss, trainer, devices) metric.add(l, acc, labels.shape[0]) train_loss.append(metric[0] / metric[2]) train_accs.append(metric[1] / metric[2]) if val_iter is not None: val_acc = d2l.evaluate_accuracy_gpu(net, val_iter) val_accs.append(val_acc) d2l.plot(range(1, epoch + 2), [train_loss, train_accs, val_accs], xlabel='epoch', legend=['train loss', 'train acc', 'val acc'], figsize=(8, 6)) scheduler.step() measures = (f'train loss {metric[0] / metric[2]:.3f},'f'train acc {metric[1] / metric[2]:.3f}') if val_iter is not None: measures += f', val acc {val_acc :.3f}' print(measures + f'\n {metric[2] * num_epochs / timer.sum() :.1f}' f'examples /sec on {str(devices)}')以上代码可以在pycharm中帮我实现画图功能吗?请优化代码
时间: 2024-03-06 22:47:49 浏览: 135
pose_iter_584000.caffemodel,pose_iter_584000.caffemodel百度网盘
5星 · 资源好评率100%
可以使用 PyCharm 中的 Matplotlib 库来绘制图表。
下面是优化后的代码:
```python
import matplotlib.pyplot as plt
def train(net, train_iter, val_iter, num_epochs, lr, wd, devices, lr_period, lr_decay):
global val_acc, metric
net.to(devices[0])
trainer = torch.optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=wd)
scheduler = torch.optim.lr_scheduler.StepLR(trainer, lr_period, lr_decay)
num_batches, timer = len(train_iter), d2l.Timer()
train_loss, train_accs, val_accs = [], [], []
fig, ax = plt.subplots(figsize=(8, 6))
for epoch in range(num_epochs):
net.train()
metric = d2l.Accumulator(3)
for i, (features, labels) in enumerate(train_iter):
trainer.zero_grad()
features, labels = features.to(devices[0]), labels.to(devices[0])
l, acc = d2l.train_batch_ch13(net, features, labels, loss, trainer, devices)
metric.add(l, acc, labels.shape[0])
train_loss.append(metric[0] / metric[2])
train_accs.append(metric[1] / metric[2])
if val_iter is not None:
val_acc = d2l.evaluate_accuracy_gpu(net, val_iter)
val_accs.append(val_acc)
ax.plot(range(1, epoch + 2), [train_loss, train_accs, val_accs])
ax.set_xlabel('epoch')
ax.set_ylabel('value')
ax.legend(['train loss', 'train acc', 'val acc'])
plt.show()
scheduler.step()
measures = (f'train loss {metric[0] / metric[2]:.3f},'f'train acc {metric[1] / metric[2]:.3f}')
if val_iter is not None:
measures += f', val acc {val_acc :.3f}'
print(measures + f'\n {metric[2] * num_epochs / timer.sum() :.1f}' f'examples /sec on {str(devices)}')
```
在这个版本的代码中,我们创建了一个图表对象,然后在每个 epoch 结束时更新图表。此外,我们还添加了一些标签和图例,以使图表更具可读性。最后,我们使用 `plt.show()` 函数来显示图表。这将在 PyCharm 中打开一个新的窗口,显示图表。
阅读全文