深度学习训练八股

一、模型中的函数的定义

1.torchmetrics.AUROC

(1).binary
复制代码
>>> from torch import tensor
>>> preds = tensor([0.13, 0.26, 0.08, 0.19, 0.34])
>>> target = tensor([0, 0, 1, 1, 1])
>>> auroc = AUROC(task="binary")
>>> auroc(preds, target)
tensor(0.5000)
(2).multiclass
复制代码
>>> preds = tensor([[0.90, 0.05, 0.05],
...                       [0.05, 0.90, 0.05],
...                       [0.05, 0.05, 0.90],
...                       [0.85, 0.05, 0.10],
...                       [0.10, 0.10, 0.80]])
>>> target = tensor([0, 1, 1, 2, 2])
>>> auroc = AUROC(task="multiclass", num_classes=3)
>>> auroc(preds, target)
tensor(0.7778)

注意函数中average参数的默认值为"macro"。

二、test_k_fold_test_copy.py---.logs_k_fold/result_draw

复制代码
# Test script
def test(model, test_loader, writer, device,criterion,roc_path,fold):
    model.eval()
    accuracy = Accuracy(task='multiclass', num_classes=2).to(device)
    precision = Precision(task='multiclass', average='macro', num_classes=2).to(device)
    recall = Recall(task='multiclass', average='macro', num_classes=2).to(device)
    auroc = AUROC(task='multiclass',num_classes=2).to(device)
    f1 = F1Score(num_classes=2, task='multiclass', average='macro').to(device)
    specificity=Specificity(num_classes=2, task='multiclass', average='macro').to(device)

    pred_scores = [] 
    true_labels = []
    pred_labels = []
    

    fold_results={}
    with torch.no_grad():
        for images, coords, labels, _, _  in test_loader:
            images = images.to(device)
            labels = labels.to(device) 
            outputs = model(images,coords)
            _, predicted = torch.max(outputs.data, 1)
                    
            accuracy(predicted, labels.data)
            precision(predicted, labels.data)
            recall(predicted, labels.data)
            f1(predicted, labels.data)
            #auroc(predicted, labels.data)
            specificity(predicted, labels.data)
            auroc(outputs, labels.data)
            pred_labels.extend(predicted.cpu().numpy())
            pred_scores.extend(outputs.cpu().numpy()) 
            true_labels.extend(labels.cpu().numpy())

    acc = accuracy.compute().item() 
    prec = precision.compute().item() 
    rec = recall.compute().item() 
    f1_score = f1.compute().item()
    auroc_score = auroc.compute().item()
    spec=specificity.compute().item()

    fold_results['fold']=fold
    fold_results['accuracy'] = acc
    fold_results['precision'] = prec
    fold_results['recall'] = rec
    fold_results['f1_score'] = f1_score
    fold_results['auroc_score'] = auroc_score
    fold_results['specificity'] = spec
    
    logging.info(f"Test Accuracy: {acc:.4f}, Test precision: {prec:.4f}, Test recall: {rec:.4f}, Test f1: {f1_score:.4f}, Test auroc: {auroc_score:4f},Test specificity:{spec:.4f}")
    logging.error("This is a fatal log!")   
    
    roc = MulticlassROC(num_classes=2, thresholds=None)
    pred_scores = torch.Tensor(pred_scores).to(device)
    true_labels = torch.Tensor(true_labels).int().to(device)
    fpr, tpr, thresholds = roc(pred_scores, true_labels)
    
    draw_fold_path = Path(os.path.join(fprs_tprs_path, f'fold_{fold}'))
    draw_fold_path.mkdir(parents=True, exist_ok=True)
    torch.save(tpr,os.path.join(draw_fold_path,"tpr.pt"))
    torch.save(fpr,os.path.join(draw_fold_path,"fpr.pt"))
   
    return fold_results, fpr, tpr
相关推荐
计算机编程-吉哥5 小时前
YOLO26 vs YOLO11 vs YOLOv8:深度学习咖啡果实成熟度分割系统【计算机毕业设计选题推荐】
人工智能·python·深度学习·yolo·django·毕业设计
冬奇Lab5 小时前
一年前没启动 AI 提效的团队,今年在付什么钱?
人工智能
NeoGressAI外贸数字化6 小时前
IOR新规9月18日生效:Form 5106六项资料自查清单
人工智能
根目录下的猫6 小时前
RK3588适配的轻量级AI模型推荐
人工智能·后端·python·目标检测
weixin_6686 小时前
Cursor 插件使用说明:Linear 与 Figma
人工智能·cursor
wshzd7 小时前
LLM之Agent(六十八)|PI(七)构建测试与开发流程
人工智能
H0311169857 小时前
App竞品数据平台功能梳理:月狐数据、七麦数据、点点数据
人工智能
YOLO数据集集合7 小时前
高铁轨道紧固件损坏检测数据集 | 高铁巡检 紧固件缺陷 轨道安全9093期
人工智能·目标检测·计算机视觉·目标跟踪·轨道·铁轨紧固件·铁轨
冬奇Lab7 小时前
一天一个开源项目(第223篇):TeamAI-CLI —— 腾讯开源的团队级 AI Agent 中间层,让每个人的 AI 能力变成团队共享能力
人工智能·开源·资讯
ShineWinsu7 小时前
对于Coze—AI:SDK的解析
人工智能·python·ai·sdk·项目·coze·字节跳动