epoch in range(epoch_num): for batch_id, (img, label, bbox, landmark) in enumerate(train_loader): img = img.to(device) label = label.to(device).long() bbox = bbox.to(device) landmark = landmark.to(device) class_out, bbox_out, landmark_out = model(img) cls_loss = class_loss(class_out, label) box_loss = bbox_loss(bbox_out, bbox, label) landmarks_loss = landmark_loss(landmark_out, landmark, label) total_loss = radio_cls_loss * cls_loss + radio_bbox_loss * box_loss + radio_landmark_loss * landmarks_loss optimizer.zero_grad() total_loss.backward() optimizer.step() if batch_id % 100 == 0: acc = accuracy(class_out, label) print('[%s] Train epoch %d, batch %d, total_loss: %f, cls_loss: %f, box_loss: %f, landmarks_loss: %f, ' 'accuracy:%f' % (datetime.now(), epoch, batch_id, total_loss, cls_loss, box_loss, landmarks_loss, acc)) scheduler.step()我想绘制cls_loss, box_loss, landmarks_loss, acc随自变量变化的图像,那自变量选什么比较合适,帮我修改成完整的代码
时间: 2024-01-28 21:05:19 浏览: 165
自变量应该选取 epoch,因为这是训练的迭代次数。下面是修改后的完整代码:
```python
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
# define your train_loader, model, optimizer, scheduler, etc.
# define the lists to store the training statistics
train_losses = []
train_cls_losses = []
train_box_losses = []
train_landmarks_losses = []
train_accs = []
epoch_num = 10
radio_cls_loss = 1.0
radio_bbox_loss = 1.0
radio_landmark_loss = 1.0
device = 'cuda' if torch.cuda.is_available() else 'cpu'
for epoch in range(epoch_num):
for batch_id, (img, label, bbox, landmark) in enumerate(train_loader):
img = img.to(device)
label = label.to(device).long()
bbox = bbox.to(device)
landmark = landmark.to(device)
class_out, bbox_out, landmark_out = model(img)
cls_loss = class_loss(class_out, label)
box_loss = bbox_loss(bbox_out, bbox, label)
landmarks_loss = landmark_loss(landmark_out, landmark, label)
total_loss = radio_cls_loss * cls_loss + radio_bbox_loss * box_loss + radio_landmark_loss * landmarks_loss
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
if batch_id % 100 == 0:
acc = accuracy(class_out, label)
print('[%s] Train epoch %d, batch %d, total_loss: %f, cls_loss: %f, box_loss: %f, landmarks_loss: %f, '
'accuracy:%f' % (datetime.now(), epoch, batch_id, total_loss, cls_loss, box_loss, landmarks_loss, acc))
# append the training statistics to the lists
train_losses.append(total_loss.item())
train_cls_losses.append(cls_loss.item())
train_box_losses.append(box_loss.item())
train_landmarks_losses.append(landmarks_loss.item())
train_accs.append(acc.item())
scheduler.step()
# plot the training statistics
x = np.arange(epoch_num)
plt.plot(x, train_losses, label='total_loss')
plt.plot(x, train_cls_losses, label='cls_loss')
plt.plot(x, train_box_losses, label='box_loss')
plt.plot(x, train_landmarks_losses, label='landmarks_loss')
plt.plot(x, train_accs, label='accuracy')
plt.legend()
plt.show()
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)