优化曲线的绘制,避免中文不显示。
This commit is contained in:
+12
-7
@@ -5,10 +5,15 @@ import torch.nn.functional as F
|
||||
from torch.utils.data import DataLoader
|
||||
from torchvision import datasets, transforms
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib
|
||||
import numpy as np
|
||||
from tqdm import tqdm
|
||||
import os
|
||||
|
||||
# 设置matplotlib支持中文显示
|
||||
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans'] # 指定默认字体
|
||||
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
|
||||
|
||||
# 设置设备
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
print(f"使用设备: {device}")
|
||||
@@ -71,7 +76,7 @@ class FoodCNN(nn.Module):
|
||||
|
||||
# 数据预处理
|
||||
transform_train = transforms.Compose([
|
||||
transforms.Resize((32, 32)), # 调整为32x32以匹配CIFAR10结构
|
||||
transforms.Resize((32, 32)),
|
||||
transforms.RandomHorizontalFlip(p=0.5),
|
||||
transforms.RandomRotation(10),
|
||||
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
|
||||
@@ -254,18 +259,18 @@ if __name__ == '__main__':
|
||||
plt.figure(figsize=(12, 4))
|
||||
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.plot(train_losses, label='训练损失')
|
||||
plt.plot(val_losses, label='验证损失')
|
||||
plt.title('损失曲线')
|
||||
plt.plot(train_losses, label='Train Loss')
|
||||
plt.plot(val_losses, label='Val Loss')
|
||||
plt.title('Loss Curve')
|
||||
plt.xlabel('Epoch')
|
||||
plt.ylabel('Loss')
|
||||
plt.legend()
|
||||
plt.grid(True)
|
||||
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.plot(train_accuracies, label='训练准确率')
|
||||
plt.plot(val_accuracies, label='验证准确率')
|
||||
plt.title('准确率曲线')
|
||||
plt.plot(train_accuracies, label='Train Accuracy')
|
||||
plt.plot(val_accuracies, label='Val Accuracy')
|
||||
plt.title('Accuracy Curve')
|
||||
plt.xlabel('Epoch')
|
||||
plt.ylabel('Accuracy (%)')
|
||||
plt.legend()
|
||||
|
||||
Reference in New Issue
Block a user