import matplotlib.pyplot as plt train_losses, train_accs, test_losses, test_accs = [], [], [], [] for epoch in range(100): train_loss, train_acc = train(model, train_loader, criterion, optimizer) test_loss, test_acc = test(model, test_loader, criterion) train_losses.append(train_loss) train_accs.append(train_acc) test_losses.append(test_loss) test_accs.append(test_acc) print('Epoch: {} Train Loss: {:.4f} Train Acc: {:.4f} Test Loss: {:.4f} Test Acc: {:.4f}'.format( epoch, train_loss, train_acc, test_loss, test_acc)) # 绘制损失函数曲线 plt.plot(train_losses, label='Train Loss') plt.plot(test_losses, label='Test Loss') plt.legend() plt.show() # 绘制准确率曲线 plt.plot(train_accs, label='Train Acc') plt.plot(test_accs, label='Test Acc') plt.legend() plt.show() 图像纵坐标设置为0~1
时间: 2023-12-24 13:13:15 浏览: 162
import numpy as np import matplotlib.pyplot as plt import math
可以通过设置y轴的范围来将图像纵坐标设置为0~1。可以使用`ylim()`函数设置y轴的范围,例如:
``` python
# 绘制损失函数曲线
plt.plot(train_losses, label='Train Loss')
plt.plot(test_losses, label='Test Loss')
plt.ylim([0, 1]) # 设置y轴范围为0~1
plt.legend()
plt.show()
# 绘制准确率曲线
plt.plot(train_accs, label='Train Acc')
plt.plot(test_accs, label='Test Acc')
plt.ylim([0, 1]) # 设置y轴范围为0~1
plt.legend()
plt.show()
```
在这段代码中,`ylim()`函数被用于设置y轴的范围为0~1。这样,损失函数和准确率的图表纵坐标的范围就被限制在了0~1之间。
阅读全文