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() legend = ['train loss', 'train acc'] if val_iter is not None: legend.append('val acc') animator = Animator(xlabel='epoch', xlim=[1, num_epochs], legend=legend) net = nn.DataParallel(net, device_ids=devices).to(devices[0]) for epoch in range(num_epochs): net.train() metric = d2l.Accumulator(3) for i, (features, labels) in enumerate(train_iter): timer.start() l, acc = d2l.train_batch_ch13(net, features, labels, loss, trainer, devices) metric.add(l, acc, labels.shape[0]) timer.stop() if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1: animator.add(epoch + (i + 1) / num_batches, ( metric[0] / metric[2], metric[1] / metric[2], None)) if val_iter is not None: val_acc = d2l.evaluate_accuracy_gpu(net, val_iter) animator.add(epoch + 1, (None, None, val_acc)) 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',valid acc {val_acc:.3f}' print(measures + f'\n {metric[2] * num_epochs / timer.sum():.1f}' f'examples /sec on {str(devices)}') 代码在jupyter中可以画图,但是在pycharm中不显示图像
时间: 2024-03-11 19:48:42 浏览: 126
pose_iter_584000.caffemodel,pose_iter_584000.caffemodel百度网盘
5星 · 资源好评率100%
这是因为在PyCharm中默认情况下无法自动显示Matplotlib图形窗口,您需要在代码中添加以下两行代码:
```python
import matplotlib.pyplot as plt
plt.show()
```
这将强制显示Matplotlib图形窗口。记住,这些代码应该放在您的绘图代码的末尾。如果这仍然无法解决您的问题,请检查您的代码是否存在任何语法错误,并确保您的PyCharm已正确安装Matplotlib库。
阅读全文