第四十五天打卡

知识点回顾:
tensorboard的发展历史和原理
tensorboard的常见操作
tensorboard在cifar上的实战:MLP和CNN模型
效果展示如下,很适合拿去组会汇报撑页数:

作业:对resnet18在cifar10上采用微调策略下,用tensorboard监控训练过程。

!pip install tensorboard -i https://pypi.tuna.tsinghua.edu.cn/simple
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import os
from torch.utils.tensorboard import SummaryWriter  # 添加TensorBoard支持
import torchvision  # 用于图像网格可视化# 设置中文字体支持
plt.rcParams["font.family"] = ["SimHei"]
plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题# 检查GPU是否可用
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"使用设备: {device}")# 1. 数据预处理(训练集增强,测试集标准化)
train_transform = transforms.Compose([transforms.RandomCrop(32, padding=4),transforms.RandomHorizontalFlip(),transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),transforms.RandomRotation(15),transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])test_transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])# 2. 加载CIFAR-10数据集
train_dataset = datasets.CIFAR10(root='./data',train=True,download=True,transform=train_transform
)test_dataset = datasets.CIFAR10(root='./data',train=False,transform=test_transform
)# 3. 创建数据加载器
batch_size = 64
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)# 4. 定义ResNet18模型
def create_resnet18(pretrained=True, num_classes=10):model = models.resnet18(pretrained=pretrained)# 修改最后一层全连接层in_features = model.fc.in_featuresmodel.fc = nn.Linear(in_features, num_classes)return model.to(device)# 5. 冻结/解冻模型层的函数
def freeze_model(model, freeze=True):"""冻结或解冻模型的卷积层参数"""# 冻结/解冻除fc层外的所有参数for name, param in model.named_parameters():if 'fc' not in name:param.requires_grad = not freeze# 打印冻结状态frozen_params = sum(p.numel() for p in model.parameters() if not p.requires_grad)total_params = sum(p.numel() for p in model.parameters())if freeze:print(f"已冻结模型卷积层参数 ({frozen_params}/{total_params} 参数)")else:print(f"已解冻模型所有参数 ({total_params}/{total_params} 参数可训练)")return model# 6. 训练函数(支持阶段式训练并集成TensorBoard)
def train_with_freeze_schedule(model, train_loader, test_loader, criterion, optimizer, scheduler, device, epochs, freeze_epochs=5, writer=None):"""前freeze_epochs轮冻结卷积层,之后解冻所有层进行训练"""train_loss_history = []test_loss_history = []train_acc_history = []test_acc_history = []all_iter_losses = []iter_indices = []global_step = 0  # 用于TensorBoard的全局步数计数器# 记录模型结构和样本图像到TensorBoardif writer is not None:dataiter = iter(train_loader)images, _ = next(dataiter)images = images.to(device)writer.add_graph(model, images)  # 记录模型计算图# 可视化训练样本img_grid = torchvision.utils.make_grid(images[:8].cpu())writer.add_image('训练样本', img_grid, global_step=0)# 初始冻结卷积层if freeze_epochs > 0:model = freeze_model(model, freeze=True)for epoch in range(epochs):# 解冻控制:在指定轮次后解冻所有层if epoch == freeze_epochs:model = freeze_model(model, freeze=False)# 解冻后调整优化器(可选)optimizer.param_groups[0]['lr'] = 1e-4  # 降低学习率防止过拟合if writer is not None:writer.add_text('训练状态', f'Epoch {epoch+1}: 解冻所有层并调整学习率', global_step)model.train()  # 设置为训练模式running_loss = 0.0correct_train = 0total_train = 0for batch_idx, (data, target) in enumerate(train_loader):data, target = data.to(device), target.to(device)optimizer.zero_grad()output = model(data)loss = criterion(output, target)loss.backward()optimizer.step()# 记录Iteration损失iter_loss = loss.item()all_iter_losses.append(iter_loss)iter_indices.append(epoch * len(train_loader) + batch_idx + 1)# 统计训练指标running_loss += iter_loss_, predicted = output.max(1)total_train += target.size(0)correct_train += predicted.eq(target).sum().item()# ================= TensorBoard记录 =================if writer is not None:# 每100批次记录一次训练指标if (batch_idx + 1) % 100 == 0:batch_acc = 100. * correct_train / total_trainwriter.add_scalar('Train/Batch Loss', iter_loss, global_step)writer.add_scalar('Train/Batch Accuracy', batch_acc, global_step)writer.add_scalar('Train/Learning Rate', optimizer.param_groups[0]['lr'], global_step)# 每200批次记录一次权重和梯度分布if (batch_idx + 1) % 200 == 0:for name, param in model.named_parameters():if param.requires_grad:  # 只记录可训练参数writer.add_histogram(f'Weights/{name}', param, global_step)if param.grad is not None:writer.add_histogram(f'Gradients/{name}', param.grad, global_step)# =================================================# 每100批次打印进度if (batch_idx + 1) % 100 == 0:print(f"Epoch {epoch+1}/{epochs} | Batch {batch_idx+1}/{len(train_loader)} "f"| 单Batch损失: {iter_loss:.4f}")global_step += 1  # 更新全局步数# 计算 epoch 级指标epoch_train_loss = running_loss / len(train_loader)epoch_train_acc = 100. * correct_train / total_train# 测试阶段model.eval()correct_test = 0total_test = 0test_loss = 0.0# 用于收集错误预测样本wrong_images = []wrong_labels = []wrong_preds = []with torch.no_grad():for data, target in test_loader:data, target = data.to(device), target.to(device)output = model(data)test_loss += criterion(output, target).item()_, predicted = output.max(1)total_test += target.size(0)correct_test += predicted.eq(target).sum().item()# 收集错误预测样本wrong_mask = (predicted != target)if wrong_mask.any():wrong_images.append(data[wrong_mask].cpu())wrong_labels.append(target[wrong_mask].cpu())wrong_preds.append(predicted[wrong_mask].cpu())epoch_test_loss = test_loss / len(test_loader)epoch_test_acc = 100. * correct_test / total_test# 记录历史数据train_loss_history.append(epoch_train_loss)test_loss_history.append(epoch_test_loss)train_acc_history.append(epoch_train_acc)test_acc_history.append(epoch_test_acc)# ================= TensorBoard记录 =================if writer is not None:# 记录epoch级指标writer.add_scalar('Train/Epoch Loss', epoch_train_loss, epoch)writer.add_scalar('Train/Epoch Accuracy', epoch_train_acc, epoch)writer.add_scalar('Test/Epoch Loss', epoch_test_loss, epoch)writer.add_scalar('Test/Epoch Accuracy', epoch_test_acc, epoch)# 记录冻结状态frozen_params = sum(1 for p in model.parameters() if not p.requires_grad)writer.add_scalar('Train/Frozen Parameters', frozen_params, epoch)# 在最后一个epoch记录错误预测样本if epoch == epochs - 1 and wrong_images:wrong_images = torch.cat(wrong_images)[:8]  # 最多取8个错误样本wrong_labels = torch.cat(wrong_labels)[:8]wrong_preds = torch.cat(wrong_preds)[:8]img_grid = torchvision.utils.make_grid(wrong_images)writer.add_image('错误预测样本', img_grid, epoch)# 添加错误预测标签classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')labels_text = '\n'.join([f'真实: {classes[l]}, 预测: {classes[p]}' for l, p in zip(wrong_labels, wrong_preds)])writer.add_text('错误预测详情', labels_text, epoch)# =================================================# 更新学习率调度器if scheduler is not None:scheduler.step(epoch_test_loss)# 打印 epoch 结果print(f"Epoch {epoch+1} 完成 | 训练损失: {epoch_train_loss:.4f} "f"| 训练准确率: {epoch_train_acc:.2f}% | 测试准确率: {epoch_test_acc:.2f}%")# 关闭TensorBoard写入器if writer is not None:writer.close()# 绘制损失和准确率曲线plot_iter_losses(all_iter_losses, iter_indices)plot_epoch_metrics(train_acc_history, test_acc_history, train_loss_history, test_loss_history)return epoch_test_acc  # 返回最终测试准确率# 7. 绘制Iteration损失曲线
def plot_iter_losses(losses, indices):plt.figure(figsize=(10, 4))plt.plot(indices, losses, 'b-', alpha=0.7)plt.xlabel('Iteration(Batch序号)')plt.ylabel('损失值')plt.title('训练过程中的Iteration损失变化')plt.grid(True)plt.show()# 8. 绘制Epoch级指标曲线
def plot_epoch_metrics(train_acc, test_acc, train_loss, test_loss):epochs = range(1, len(train_acc) + 1)plt.figure(figsize=(12, 5))# 准确率曲线plt.subplot(1, 2, 1)plt.plot(epochs, train_acc, 'b-', label='训练准确率')plt.plot(epochs, test_acc, 'r-', label='测试准确率')plt.xlabel('Epoch')plt.ylabel('准确率 (%)')plt.title('准确率随Epoch变化')plt.legend()plt.grid(True)# 损失曲线plt.subplot(1, 2, 2)plt.plot(epochs, train_loss, 'b-', label='训练损失')plt.plot(epochs, test_loss, 'r-', label='测试损失')plt.xlabel('Epoch')plt.ylabel('损失值')plt.title('损失值随Epoch变化')plt.legend()plt.grid(True)plt.tight_layout()plt.show()# 主函数:训练模型
def main():# 参数设置epochs = 40  # 总训练轮次freeze_epochs = 5  # 冻结卷积层的轮次learning_rate = 1e-3  # 初始学习率weight_decay = 1e-4  # 权重衰减# ================= 创建TensorBoard写入器 =================log_dir = "runs/cifar10_resnet18_finetune"if os.path.exists(log_dir):version = 1while os.path.exists(f"{log_dir}_v{version}"):version += 1log_dir = f"{log_dir}_v{version}"writer = SummaryWriter(log_dir)print(f"TensorBoard日志目录: {log_dir}")print("训练后执行: tensorboard --logdir=runs 查看可视化")# =======================================================# 创建ResNet18模型(加载预训练权重)model = create_resnet18(pretrained=True, num_classes=10)# 定义优化器和损失函数optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay)criterion = nn.CrossEntropyLoss()# 定义学习率调度器scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=2, verbose=True)# 开始训练(前5轮冻结卷积层,之后解冻)final_accuracy = train_with_freeze_schedule(model=model,train_loader=train_loader,test_loader=test_loader,criterion=criterion,optimizer=optimizer,scheduler=scheduler,device=device,epochs=epochs,freeze_epochs=freeze_epochs,writer=writer  # 传入TensorBoard写入器)print(f"训练完成!最终测试准确率: {final_accuracy:.2f}%")# # 保存模型# torch.save(model.state_dict(), 'resnet18_cifar10_finetuned.pth')# print("模型已保存至: resnet18_cifar10_finetuned.pth")if __name__ == "__main__":main()
已冻结模型卷积层参数 (11176512/11181642 参数)
Epoch 1/40 | Batch 100/782 | 单Batch损失: 1.9845
Epoch 1/40 | Batch 200/782 | 单Batch损失: 1.9221
Epoch 1/40 | Batch 300/782 | 单Batch损失: 2.1262
Epoch 1/40 | Batch 400/782 | 单Batch损失: 1.9537
Epoch 1/40 | Batch 500/782 | 单Batch损失: 1.6979
Epoch 1/40 | Batch 600/782 | 单Batch损失: 1.7894
Epoch 1/40 | Batch 700/782 | 单Batch损失: 1.9485
Epoch 1 完成 | 训练损失: 1.9627 | 训练准确率: 30.03% | 测试准确率: 32.34%
Epoch 2/40 | Batch 100/782 | 单Batch损失: 1.7793
Epoch 2/40 | Batch 200/782 | 单Batch损失: 1.8816
Epoch 2/40 | Batch 300/782 | 单Batch损失: 1.7683
Epoch 2/40 | Batch 400/782 | 单Batch损失: 1.9009
Epoch 2/40 | Batch 500/782 | 单Batch损失: 1.9358
Epoch 2/40 | Batch 600/782 | 单Batch损失: 1.7030
Epoch 2/40 | Batch 700/782 | 单Batch损失: 1.8201
Epoch 2 完成 | 训练损失: 1.8657 | 训练准确率: 33.73% | 测试准确率: 33.70%
Epoch 3/40 | Batch 100/782 | 单Batch损失: 1.7812
Epoch 3/40 | Batch 200/782 | 单Batch损失: 1.8254
Epoch 3/40 | Batch 300/782 | 单Batch损失: 2.0188
Epoch 3/40 | Batch 400/782 | 单Batch损失: 1.8106
Epoch 3/40 | Batch 500/782 | 单Batch损失: 1.8855
Epoch 3/40 | Batch 600/782 | 单Batch损失: 1.7753
Epoch 3/40 | Batch 700/782 | 单Batch损失: 1.7371
Epoch 3 完成 | 训练损失: 1.8509 | 训练准确率: 34.66% | 测试准确率: 33.65%
Epoch 4/40 | Batch 100/782 | 单Batch损失: 1.8383
Epoch 4/40 | Batch 200/782 | 单Batch损失: 1.8095
Epoch 4/40 | Batch 300/782 | 单Batch损失: 2.0029
Epoch 4/40 | Batch 400/782 | 单Batch损失: 1.7765
Epoch 4/40 | Batch 500/782 | 单Batch损失: 1.8919
Epoch 4/40 | Batch 600/782 | 单Batch损失: 1.7515
Epoch 4/40 | Batch 700/782 | 单Batch损失: 1.8964
Epoch 4 完成 | 训练损失: 1.8491 | 训练准确率: 34.36% | 测试准确率: 34.47%
Epoch 5/40 | Batch 100/782 | 单Batch损失: 1.9020
Epoch 5/40 | Batch 200/782 | 单Batch损失: 2.0807
Epoch 5/40 | Batch 300/782 | 单Batch损失: 1.8857
Epoch 5/40 | Batch 400/782 | 单Batch损失: 1.9153
Epoch 5/40 | Batch 500/782 | 单Batch损失: 1.8053
Epoch 5/40 | Batch 600/782 | 单Batch损失: 1.8766
Epoch 5/40 | Batch 700/782 | 单Batch损失: 1.8560
Epoch 5 完成 | 训练损失: 1.8454 | 训练准确率: 34.51% | 测试准确率: 35.81%
已解冻模型所有参数 (11181642/11181642 参数可训练)
Epoch 6/40 | Batch 100/782 | 单Batch损失: 1.7783
Epoch 6/40 | Batch 200/782 | 单Batch损失: 1.3492
Epoch 6/40 | Batch 300/782 | 单Batch损失: 1.2985
Epoch 6/40 | Batch 400/782 | 单Batch损失: 1.1477
Epoch 6/40 | Batch 500/782 | 单Batch损失: 1.0184
Epoch 6/40 | Batch 600/782 | 单Batch损失: 1.0792
Epoch 6/40 | Batch 700/782 | 单Batch损失: 0.9014
Epoch 6 完成 | 训练损失: 1.2920 | 训练准确率: 54.37% | 测试准确率: 68.44%
Epoch 7/40 | Batch 100/782 | 单Batch损失: 1.1539
Epoch 7/40 | Batch 200/782 | 单Batch损失: 1.0359
Epoch 7/40 | Batch 300/782 | 单Batch损失: 0.9367
Epoch 7/40 | Batch 400/782 | 单Batch损失: 0.9108
Epoch 7/40 | Batch 500/782 | 单Batch损失: 0.8993
Epoch 7/40 | Batch 600/782 | 单Batch损失: 0.7143
Epoch 7/40 | Batch 700/782 | 单Batch损失: 0.9862
Epoch 7 完成 | 训练损失: 0.9915 | 训练准确率: 65.46% | 测试准确率: 74.44%
Epoch 8/40 | Batch 100/782 | 单Batch损失: 0.9431
Epoch 8/40 | Batch 200/782 | 单Batch损失: 0.9887
Epoch 8/40 | Batch 300/782 | 单Batch损失: 0.7520
Epoch 8/40 | Batch 400/782 | 单Batch损失: 0.8691
Epoch 8/40 | Batch 500/782 | 单Batch损失: 0.8537
Epoch 8/40 | Batch 600/782 | 单Batch损失: 0.8077
Epoch 8/40 | Batch 700/782 | 单Batch损失: 0.9450
Epoch 8 完成 | 训练损失: 0.8706 | 训练准确率: 69.68% | 测试准确率: 76.69%
Epoch 9/40 | Batch 100/782 | 单Batch损失: 0.6912
Epoch 9/40 | Batch 200/782 | 单Batch损失: 1.0554
Epoch 9/40 | Batch 300/782 | 单Batch损失: 0.8509
Epoch 9/40 | Batch 400/782 | 单Batch损失: 0.6163
Epoch 9/40 | Batch 500/782 | 单Batch损失: 0.6654
Epoch 9/40 | Batch 600/782 | 单Batch损失: 0.7246
Epoch 9/40 | Batch 700/782 | 单Batch损失: 0.7361
Epoch 9 完成 | 训练损失: 0.8005 | 训练准确率: 72.25% | 测试准确率: 78.66%
Epoch 10/40 | Batch 100/782 | 单Batch损失: 0.7592
Epoch 10/40 | Batch 200/782 | 单Batch损失: 0.6813
Epoch 10/40 | Batch 300/782 | 单Batch损失: 0.8109
Epoch 10/40 | Batch 400/782 | 单Batch损失: 0.8108
Epoch 10/40 | Batch 500/782 | 单Batch损失: 0.4869
Epoch 10/40 | Batch 600/782 | 单Batch损失: 0.6479
Epoch 10/40 | Batch 700/782 | 单Batch损失: 0.6231
Epoch 10 完成 | 训练损失: 0.7458 | 训练准确率: 74.18% | 测试准确率: 79.61%
Epoch 11/40 | Batch 100/782 | 单Batch损失: 0.9745
Epoch 11/40 | Batch 200/782 | 单Batch损失: 0.7001
Epoch 11/40 | Batch 300/782 | 单Batch损失: 0.5166
Epoch 11/40 | Batch 400/782 | 单Batch损失: 0.9212
Epoch 11/40 | Batch 500/782 | 单Batch损失: 0.8682
Epoch 11/40 | Batch 600/782 | 单Batch损失: 0.7065
Epoch 11/40 | Batch 700/782 | 单Batch损失: 0.5803
Epoch 11 完成 | 训练损失: 0.7058 | 训练准确率: 75.47% | 测试准确率: 80.05%
Epoch 12/40 | Batch 100/782 | 单Batch损失: 0.5478
Epoch 12/40 | Batch 200/782 | 单Batch损失: 0.8005
Epoch 12/40 | Batch 300/782 | 单Batch损失: 0.7657
Epoch 12/40 | Batch 400/782 | 单Batch损失: 0.5238
Epoch 12/40 | Batch 500/782 | 单Batch损失: 0.6686
Epoch 12/40 | Batch 600/782 | 单Batch损失: 0.6046
Epoch 12/40 | Batch 700/782 | 单Batch损失: 0.4792
Epoch 12 完成 | 训练损失: 0.6713 | 训练准确率: 76.52% | 测试准确率: 80.93%
Epoch 13/40 | Batch 100/782 | 单Batch损失: 1.0814
Epoch 13/40 | Batch 200/782 | 单Batch损失: 0.8355
Epoch 13/40 | Batch 300/782 | 单Batch损失: 0.7163
Epoch 13/40 | Batch 400/782 | 单Batch损失: 0.8455
Epoch 13/40 | Batch 500/782 | 单Batch损失: 0.7083
Epoch 13/40 | Batch 600/782 | 单Batch损失: 0.4937
Epoch 13/40 | Batch 700/782 | 单Batch损失: 0.7588
Epoch 13 完成 | 训练损失: 0.6509 | 训练准确率: 77.38% | 测试准确率: 81.50%
Epoch 14/40 | Batch 100/782 | 单Batch损失: 0.7048
Epoch 14/40 | Batch 200/782 | 单Batch损失: 0.6938
Epoch 14/40 | Batch 300/782 | 单Batch损失: 0.6346
Epoch 14/40 | Batch 400/782 | 单Batch损失: 0.5922
Epoch 14/40 | Batch 500/782 | 单Batch损失: 0.6982
Epoch 14/40 | Batch 600/782 | 单Batch损失: 0.7085
Epoch 14/40 | Batch 700/782 | 单Batch损失: 0.7181
Epoch 14 完成 | 训练损失: 0.6167 | 训练准确率: 78.43% | 测试准确率: 82.15%
Epoch 15/40 | Batch 100/782 | 单Batch损失: 0.9140
Epoch 15/40 | Batch 200/782 | 单Batch损失: 0.5345
Epoch 15/40 | Batch 300/782 | 单Batch损失: 0.4287
Epoch 15/40 | Batch 400/782 | 单Batch损失: 0.5277
Epoch 15/40 | Batch 500/782 | 单Batch损失: 0.7996
Epoch 15/40 | Batch 600/782 | 单Batch损失: 0.4521
Epoch 15/40 | Batch 700/782 | 单Batch损失: 0.5768
Epoch 15 完成 | 训练损失: 0.5981 | 训练准确率: 79.22% | 测试准确率: 82.28%
Epoch 16/40 | Batch 100/782 | 单Batch损失: 0.5512
Epoch 16/40 | Batch 200/782 | 单Batch损失: 0.3907
Epoch 16/40 | Batch 300/782 | 单Batch损失: 0.5873
Epoch 16/40 | Batch 400/782 | 单Batch损失: 0.7727
Epoch 16/40 | Batch 500/782 | 单Batch损失: 0.6523
Epoch 16/40 | Batch 600/782 | 单Batch损失: 0.8028
Epoch 16/40 | Batch 700/782 | 单Batch损失: 0.3571
Epoch 16 完成 | 训练损失: 0.5809 | 训练准确率: 79.84% | 测试准确率: 82.45%
Epoch 17/40 | Batch 100/782 | 单Batch损失: 0.4884
Epoch 17/40 | Batch 200/782 | 单Batch损失: 0.6235
Epoch 17/40 | Batch 300/782 | 单Batch损失: 0.6000
Epoch 17/40 | Batch 400/782 | 单Batch损失: 0.5768
Epoch 17/40 | Batch 500/782 | 单Batch损失: 0.6355
Epoch 17/40 | Batch 600/782 | 单Batch损失: 0.9570
Epoch 17/40 | Batch 700/782 | 单Batch损失: 0.3773
Epoch 17 完成 | 训练损失: 0.5583 | 训练准确率: 80.59% | 测试准确率: 83.44%
Epoch 18/40 | Batch 100/782 | 单Batch损失: 0.4415
Epoch 18/40 | Batch 200/782 | 单Batch损失: 0.4202
Epoch 18/40 | Batch 300/782 | 单Batch损失: 0.4529
Epoch 18/40 | Batch 400/782 | 单Batch损失: 0.5010
Epoch 18/40 | Batch 500/782 | 单Batch损失: 0.5142
Epoch 18/40 | Batch 600/782 | 单Batch损失: 0.3954
Epoch 18/40 | Batch 700/782 | 单Batch损失: 0.5276
Epoch 18 完成 | 训练损失: 0.5491 | 训练准确率: 80.88% | 测试准确率: 83.22%
Epoch 19/40 | Batch 100/782 | 单Batch损失: 0.4030
Epoch 19/40 | Batch 200/782 | 单Batch损失: 0.4581
Epoch 19/40 | Batch 300/782 | 单Batch损失: 0.5019
Epoch 19/40 | Batch 400/782 | 单Batch损失: 0.4664
Epoch 19/40 | Batch 500/782 | 单Batch损失: 0.4308
Epoch 19/40 | Batch 600/782 | 单Batch损失: 0.4998
Epoch 19/40 | Batch 700/782 | 单Batch损失: 0.4180
Epoch 19 完成 | 训练损失: 0.5372 | 训练准确率: 81.29% | 测试准确率: 83.87%
Epoch 20/40 | Batch 100/782 | 单Batch损失: 0.5643
Epoch 20/40 | Batch 200/782 | 单Batch损失: 0.6189
Epoch 20/40 | Batch 300/782 | 单Batch损失: 0.4015
Epoch 20/40 | Batch 400/782 | 单Batch损失: 0.4931
Epoch 20/40 | Batch 500/782 | 单Batch损失: 0.5194
Epoch 20/40 | Batch 600/782 | 单Batch损失: 0.5057
Epoch 20/40 | Batch 700/782 | 单Batch损失: 0.5244
Epoch 20 完成 | 训练损失: 0.5196 | 训练准确率: 81.80% | 测试准确率: 84.25%
Epoch 21/40 | Batch 100/782 | 单Batch损失: 0.3585
Epoch 21/40 | Batch 200/782 | 单Batch损失: 0.2221
Epoch 21/40 | Batch 300/782 | 单Batch损失: 0.5224
Epoch 21/40 | Batch 400/782 | 单Batch损失: 0.4546
Epoch 21/40 | Batch 500/782 | 单Batch损失: 0.3895
Epoch 21/40 | Batch 600/782 | 单Batch损失: 0.4467
Epoch 21/40 | Batch 700/782 | 单Batch损失: 0.4560
Epoch 21 完成 | 训练损失: 0.4971 | 训练准确率: 82.57% | 测试准确率: 83.88%
Epoch 22/40 | Batch 100/782 | 单Batch损失: 0.3642
Epoch 22/40 | Batch 200/782 | 单Batch损失: 0.7551
Epoch 22/40 | Batch 300/782 | 单Batch损失: 0.4533
Epoch 22/40 | Batch 400/782 | 单Batch损失: 0.6884
Epoch 22/40 | Batch 500/782 | 单Batch损失: 0.5062
Epoch 22/40 | Batch 600/782 | 单Batch损失: 0.5316
Epoch 22/40 | Batch 700/782 | 单Batch损失: 0.3991
Epoch 22 完成 | 训练损失: 0.4949 | 训练准确率: 82.76% | 测试准确率: 84.23%
Epoch 23/40 | Batch 100/782 | 单Batch损失: 0.4934
Epoch 23/40 | Batch 200/782 | 单Batch损失: 0.3914
Epoch 23/40 | Batch 300/782 | 单Batch损失: 0.5075
Epoch 23/40 | Batch 400/782 | 单Batch损失: 0.6494
Epoch 23/40 | Batch 500/782 | 单Batch损失: 0.4456
Epoch 23/40 | Batch 600/782 | 单Batch损失: 0.4376
Epoch 23/40 | Batch 700/782 | 单Batch损失: 0.5029
Epoch 23 完成 | 训练损失: 0.4820 | 训练准确率: 83.05% | 测试准确率: 84.24%
Epoch 24/40 | Batch 100/782 | 单Batch损失: 0.4478
Epoch 24/40 | Batch 200/782 | 单Batch损失: 0.4439
Epoch 24/40 | Batch 300/782 | 单Batch损失: 0.6566
Epoch 24/40 | Batch 400/782 | 单Batch损失: 0.3610
Epoch 24/40 | Batch 500/782 | 单Batch损失: 0.3373
Epoch 24/40 | Batch 600/782 | 单Batch损失: 0.4565
Epoch 24/40 | Batch 700/782 | 单Batch损失: 0.5308
Epoch 24 完成 | 训练损失: 0.4746 | 训练准确率: 83.40% | 测试准确率: 84.45%
Epoch 25/40 | Batch 100/782 | 单Batch损失: 0.4398
Epoch 25/40 | Batch 200/782 | 单Batch损失: 0.3060
Epoch 25/40 | Batch 300/782 | 单Batch损失: 0.5542
Epoch 25/40 | Batch 400/782 | 单Batch损失: 0.4484
Epoch 25/40 | Batch 500/782 | 单Batch损失: 0.3688
Epoch 25/40 | Batch 600/782 | 单Batch损失: 0.5104
Epoch 25/40 | Batch 700/782 | 单Batch损失: 0.5033
Epoch 25 完成 | 训练损失: 0.4536 | 训练准确率: 83.94% | 测试准确率: 84.60%
Epoch 26/40 | Batch 100/782 | 单Batch损失: 0.6212
Epoch 26/40 | Batch 200/782 | 单Batch损失: 0.4445
Epoch 26/40 | Batch 300/782 | 单Batch损失: 0.4936
Epoch 26/40 | Batch 400/782 | 单Batch损失: 0.4348
Epoch 26/40 | Batch 500/782 | 单Batch损失: 0.4900
Epoch 26/40 | Batch 600/782 | 单Batch损失: 0.5532
Epoch 26/40 | Batch 700/782 | 单Batch损失: 0.3503
Epoch 26 完成 | 训练损失: 0.4523 | 训练准确率: 84.11% | 测试准确率: 85.29%
Epoch 27/40 | Batch 100/782 | 单Batch损失: 0.4809
Epoch 27/40 | Batch 200/782 | 单Batch损失: 0.3168
Epoch 27/40 | Batch 300/782 | 单Batch损失: 0.4344
Epoch 27/40 | Batch 400/782 | 单Batch损失: 0.2452
Epoch 27/40 | Batch 500/782 | 单Batch损失: 0.4902
Epoch 27/40 | Batch 600/782 | 单Batch损失: 0.4841
Epoch 27/40 | Batch 700/782 | 单Batch损失: 0.4331
Epoch 27 完成 | 训练损失: 0.4347 | 训练准确率: 84.82% | 测试准确率: 84.93%
Epoch 28/40 | Batch 100/782 | 单Batch损失: 0.5113
Epoch 28/40 | Batch 200/782 | 单Batch损失: 0.4025
Epoch 28/40 | Batch 300/782 | 单Batch损失: 0.5368
Epoch 28/40 | Batch 400/782 | 单Batch损失: 0.4800
Epoch 28/40 | Batch 500/782 | 单Batch损失: 0.5973
Epoch 28/40 | Batch 600/782 | 单Batch损失: 0.4407
Epoch 28/40 | Batch 700/782 | 单Batch损失: 0.5990
Epoch 28 完成 | 训练损失: 0.4299 | 训练准确率: 84.99% | 测试准确率: 84.38%
Epoch 29/40 | Batch 100/782 | 单Batch损失: 0.2680
Epoch 29/40 | Batch 200/782 | 单Batch损失: 0.2952
Epoch 29/40 | Batch 300/782 | 单Batch损失: 0.4970
Epoch 29/40 | Batch 400/782 | 单Batch损失: 0.3671
Epoch 29/40 | Batch 500/782 | 单Batch损失: 0.3845
Epoch 29/40 | Batch 600/782 | 单Batch损失: 0.2393
Epoch 29/40 | Batch 700/782 | 单Batch损失: 0.4481
Epoch 29 完成 | 训练损失: 0.4242 | 训练准确率: 85.12% | 测试准确率: 85.04%
Epoch 30/40 | Batch 100/782 | 单Batch损失: 0.5809
Epoch 30/40 | Batch 200/782 | 单Batch损失: 0.3701
Epoch 30/40 | Batch 300/782 | 单Batch损失: 0.2804
Epoch 30/40 | Batch 400/782 | 单Batch损失: 0.3507
Epoch 30/40 | Batch 500/782 | 单Batch损失: 0.1962
Epoch 30/40 | Batch 600/782 | 单Batch损失: 0.4570
Epoch 30/40 | Batch 700/782 | 单Batch损失: 0.2103
Epoch 30 完成 | 训练损失: 0.3778 | 训练准确率: 86.65% | 测试准确率: 85.71%
Epoch 31/40 | Batch 100/782 | 单Batch损失: 0.3076
Epoch 31/40 | Batch 200/782 | 单Batch损失: 0.2548
Epoch 31/40 | Batch 300/782 | 单Batch损失: 0.3434
Epoch 31/40 | Batch 400/782 | 单Batch损失: 0.3898
Epoch 31/40 | Batch 500/782 | 单Batch损失: 0.5643
Epoch 31/40 | Batch 600/782 | 单Batch损失: 0.2407
Epoch 31/40 | Batch 700/782 | 单Batch损失: 0.2459
Epoch 31 完成 | 训练损失: 0.3647 | 训练准确率: 87.05% | 测试准确率: 85.72%
Epoch 32/40 | Batch 100/782 | 单Batch损失: 0.5242
Epoch 32/40 | Batch 200/782 | 单Batch损失: 0.3581
Epoch 32/40 | Batch 300/782 | 单Batch损失: 0.5027
Epoch 32/40 | Batch 400/782 | 单Batch损失: 0.2311
Epoch 32/40 | Batch 500/782 | 单Batch损失: 0.4504
Epoch 32/40 | Batch 600/782 | 单Batch损失: 0.4259
Epoch 32/40 | Batch 700/782 | 单Batch损失: 0.2881
Epoch 32 完成 | 训练损失: 0.3509 | 训练准确率: 87.40% | 测试准确率: 85.88%
Epoch 33/40 | Batch 100/782 | 单Batch损失: 0.2308
Epoch 33/40 | Batch 200/782 | 单Batch损失: 0.2749
Epoch 33/40 | Batch 300/782 | 单Batch损失: 0.3666
Epoch 33/40 | Batch 400/782 | 单Batch损失: 0.3093
Epoch 33/40 | Batch 500/782 | 单Batch损失: 0.4387
Epoch 33/40 | Batch 600/782 | 单Batch损失: 0.3664
Epoch 33/40 | Batch 700/782 | 单Batch损失: 0.3048
Epoch 33 完成 | 训练损失: 0.3424 | 训练准确率: 87.83% | 测试准确率: 85.93%
Epoch 34/40 | Batch 100/782 | 单Batch损失: 0.1878
Epoch 34/40 | Batch 200/782 | 单Batch损失: 0.2186
Epoch 34/40 | Batch 300/782 | 单Batch损失: 0.2953
Epoch 34/40 | Batch 400/782 | 单Batch损失: 0.4248
Epoch 34/40 | Batch 500/782 | 单Batch损失: 0.4961
Epoch 34/40 | Batch 600/782 | 单Batch损失: 0.2806
Epoch 34/40 | Batch 700/782 | 单Batch损失: 0.3832
Epoch 34 完成 | 训练损失: 0.3364 | 训练准确率: 88.01% | 测试准确率: 85.88%
Epoch 35/40 | Batch 100/782 | 单Batch损失: 0.4153
Epoch 35/40 | Batch 200/782 | 单Batch损失: 0.2748
Epoch 35/40 | Batch 300/782 | 单Batch损失: 0.3258
Epoch 35/40 | Batch 400/782 | 单Batch损失: 0.2264
Epoch 35/40 | Batch 500/782 | 单Batch损失: 0.2102
Epoch 35/40 | Batch 600/782 | 单Batch损失: 0.2262
Epoch 35/40 | Batch 700/782 | 单Batch损失: 0.3287
Epoch 35 完成 | 训练损失: 0.3117 | 训练准确率: 88.96% | 测试准确率: 86.21%
Epoch 36/40 | Batch 100/782 | 单Batch损失: 0.2270
Epoch 36/40 | Batch 200/782 | 单Batch损失: 0.2434
Epoch 36/40 | Batch 300/782 | 单Batch损失: 0.2651
Epoch 36/40 | Batch 400/782 | 单Batch损失: 0.1981
Epoch 36/40 | Batch 500/782 | 单Batch损失: 0.3411
Epoch 36/40 | Batch 600/782 | 单Batch损失: 0.3588
Epoch 36/40 | Batch 700/782 | 单Batch损失: 0.4123
Epoch 36 完成 | 训练损失: 0.3059 | 训练准确率: 89.21% | 测试准确率: 86.49%
Epoch 37/40 | Batch 100/782 | 单Batch损失: 0.2236
Epoch 37/40 | Batch 200/782 | 单Batch损失: 0.1829
Epoch 37/40 | Batch 300/782 | 单Batch损失: 0.3180
Epoch 37/40 | Batch 400/782 | 单Batch损失: 0.1161
Epoch 37/40 | Batch 500/782 | 单Batch损失: 0.2394
Epoch 37/40 | Batch 600/782 | 单Batch损失: 0.3800
Epoch 37/40 | Batch 700/782 | 单Batch损失: 0.1893
Epoch 37 完成 | 训练损失: 0.2973 | 训练准确率: 89.51% | 测试准确率: 86.20%
Epoch 38/40 | Batch 100/782 | 单Batch损失: 0.2446
Epoch 38/40 | Batch 200/782 | 单Batch损失: 0.3209
Epoch 38/40 | Batch 300/782 | 单Batch损失: 0.2194
Epoch 38/40 | Batch 400/782 | 单Batch损失: 0.3172
Epoch 38/40 | Batch 500/782 | 单Batch损失: 0.3022
Epoch 38/40 | Batch 600/782 | 单Batch损失: 0.2326
Epoch 38/40 | Batch 700/782 | 单Batch损失: 0.3124
Epoch 38 完成 | 训练损失: 0.2827 | 训练准确率: 90.02% | 测试准确率: 86.50%
Epoch 39/40 | Batch 100/782 | 单Batch损失: 0.2458
Epoch 39/40 | Batch 200/782 | 单Batch损失: 0.3421
Epoch 39/40 | Batch 300/782 | 单Batch损失: 0.2558
Epoch 39/40 | Batch 400/782 | 单Batch损失: 0.2393
Epoch 39/40 | Batch 500/782 | 单Batch损失: 0.3710
Epoch 39/40 | Batch 600/782 | 单Batch损失: 0.2537
Epoch 39/40 | Batch 700/782 | 单Batch损失: 0.1849
Epoch 39 完成 | 训练损失: 0.2763 | 训练准确率: 90.11% | 测试准确率: 86.72%
Epoch 40/40 | Batch 100/782 | 单Batch损失: 0.2092
Epoch 40/40 | Batch 200/782 | 单Batch损失: 0.2279
Epoch 40/40 | Batch 300/782 | 单Batch损失: 0.1306
Epoch 40/40 | Batch 400/782 | 单Batch损失: 0.3211
Epoch 40/40 | Batch 500/782 | 单Batch损失: 0.3480
Epoch 40/40 | Batch 600/782 | 单Batch损失: 0.1992
Epoch 40/40 | Batch 700/782 | 单Batch损失: 0.3205
Epoch 40 完成 | 训练损失: 0.2782 | 训练准确率: 90.06% | 测试准确率: 86.57%

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/bicheng/83736.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用高斯朴素贝叶斯算法对鸢尾花数据集进行分类

高斯朴素贝叶斯算法通常用于特征变量是连续变量,符合高素分布的情况。 使用高斯朴素贝叶斯算法对鸢尾花数据集进行分类 """ 使用高斯贝叶斯堆鸢尾花进行分类 """ #导入需要的库 from sklearn.datasets import load_iris from skle…

【docker】Windows安装docker

环境及工具(点击下载) Docker Desktop Installer.exe (windows 环境下运行docker的一款产品) wsl_update_x64 (Linux 内核包) 前期准备 系统要求2: Windows 11:64 位系统&am…

量化Quantization初步之--带量化(QAT)的XOR异或pyTorch版250501

量化(Quantization)这词儿听着玄,经常和量化交易Quantitative Trading (量化交易)混淆。 其实机器学习(深度学习)领域的量化Quantization是和节约内存、提高运算效率相关的概念(因大模型的普及,这个量化问题尤为迫切)。 揭秘机器…

【Redis】zset 类型

zset 一. zset 类型介绍二. zset 命令zaddzcard、zcountzrange、zrevrange、zrangebyscorezpopmax、zpopminzrank、zrevrank、zscorezrem、zremrangebyrank、zremrangebyscorezincrby阻塞版本命令:bzpopmax、bzpopmin集合间操作:zinterstore、zunionstor…

Mermaid 绘图--以企业权限视图为例

文章目录 一、示例代码二、基础结构设计2.1 组织架构树2.2 权限视图设计 三、销售数据权限系统四、关键语法技巧汇总 一、示例代码 在企业管理系统开发中,清晰的权限视图设计至关重要。本文将分享如何使用 Mermaid 绘制直观的企业权限关系图,复制以下代…

[pdf、epub]300道《软件方法》强化自测题业务建模需求分析共257页(202505更新)

DDD领域驱动设计批评文集 做强化自测题获得“软件方法建模师”称号 《软件方法》各章合集 在本账号CSDN资源下载,或者访问链接: http://www.umlchina.com/url/quizad.html 如果需要提取码:umlc 文件夹中的“300道软件方法强化自测题2025…

std__map,std__unordered_map,protobuf__map之间的性能比较

简单比较下 std::map、std::unordered_map 和 protobuf::Map 的性能,主要关注在 插入、查找 和 删除 操作上的效率以及内存管理的差异。 std::map 底层实现:std::map 使用红黑树作为底层数据结构,红黑树是一种平衡二叉查找树的变体结构&…

文档处理组件Aspose.Words 25.5全新发布 :六大新功能与性能深度优化

在数字化办公日益普及的今天,文档处理的效率与质量直接影响到企业的运营效率。Aspose.Words 作为业界领先的文档处理控件,其最新发布的 25.5 版本带来了六大新功能和多项性能优化,旨在为开发者和企业用户提供更强大、高效的文档处理能力。 六…

Three.js + Vue3 加载GLB模型项目代码详解

本说明结合 src/App.vue 代码,详细解释如何在 Vue3 项目中用 three.js 加载并显示 glb 模型。 1. 依赖与插件导入 import {onMounted, onUnmounted } from vue import * as THREE from three import Stats from stats.js import {OrbitControls } from three/examples/jsm/co…

Flutter如何支持原生View

在 Flutter 中集成原生 View(如 Android 的 SurfaceView、iOS 的 WKWebView)是通过 平台视图(Platform View) 实现的。这一机制允许在 Flutter UI 中嵌入原生组件,解决了某些场景下 Flutter 自身渲染能力的不足&#x…

vue-11(命名路由和命名视图)

命名路由和命名视图 命名路由和命名视图提供了组织和导航 Vue.js 应用程序的强大方法,尤其是在它们的复杂性增加时。它们提供了一种语义更合理、可维护的路由方法,使您的代码更易于理解和修改。命名路由允许您按名称引用路由,而不是依赖 URL…

微软认证考试科目众多?该如何选择?

在云计算、人工智能、数据分析等技术快速发展的今天,微软认证(Microsoft Certification)已成为IT从业者、开发者、数据分析师提升竞争力的重要凭证。但面对众多考试科目,很多人不知道如何选择。本文将详细介绍微软认证的考试方向、…

视频汇聚平台EasyCVR“明厨亮灶”方案筑牢旅游景区餐饮安全品质防线

一、背景分析​ 1)政策监管刚性需求​:国家食品安全战略及 2024年《关于深化智慧城市发展的指导意见》要求构建智慧餐饮场景,推动数字化监管。多地将“AI明厨亮灶”纳入十四五规划考核,要求餐饮单位操作可视化并具备风险预警能力…

Mysql莫名奇妙重启

收到客户反馈有时接口报504,查看应用日志发现故障期间数据库连接失败 com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not receive…

半监督学习:低密度分离假设 (Low-Density Separation Assumption)

半监督学习(SSL)的目标是借助未标记数据辅助训练,以期获得比仅用带标签的监督学习范式更好的效果。但是,SSL的前提是数据分布需满足某些假设。否则,SSL可能无法提升监督学习的效果,甚至会因误导性推断降低预测准确性。 半监督学习…

Python Day44

Task: 1.预训练的概念 2.常见的分类预训练模型 3.图像预训练模型的发展史 4.预训练的策略 5.预训练代码实战:resnet18 1. 预训练的概念 预训练(Pre-training)是指在大规模数据集上,先训练模型以学习通用的特征表示&am…

vue3 eslint ts 关闭多单词命名检查

无效做法 import { globalIgnores } from eslint/config import {defineConfigWithVueTs,vueTsConfigs, } from vue/eslint-config-typescript import pluginVue from eslint-plugin-vue import skipFormatting from vue/eslint-config-prettier/skip-formatting// To allow m…

贪心,回溯,动态规划

1.贪心算法 ​ 贪心算法是一种在每一步选择中都采取在当前状态下最好或最优的选择,从而希望全局最好或是最优的算法。 特点 局部最优选择不能保证全局最优高效 适用条件 局部最优可以导致全局最优问题的最优解包含子问题的最优解 经典问题 活动选择问题最短路径最…

【Netty4核心原理⑧】【揭开Bootstrap的神秘面纱 - 服务端Bootstrap❶】

文章目录 一、前言二、流程分析1. 创建 EventLoopGroup2. 指定 Channel 类型2.1 Channel 的创建2.2 Channel 的初始化 3. 配置自定义的业务处理器 Handler3.1 ServerBootstrap#childHandler3.2 handler 与 childHandler 的区别 4. 绑定端口服务启动 三、bossGroup 与 workerGro…

为什么需要自动下载浏览器驱动?

为什么需要自动下载浏览器驱动? 血泪场景重现 新人入职第一天: 花3小时配置Chrome/Firefox驱动版本不匹配导致SessionNotCreatedException 浏览器自动更新后: 所有测试脚本突然崩溃手动查找驱动耗时长 终极解决方案:自动下载驱…