深度学习训练八股

一、模型中的函数的定义

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 小时前
PINN物理信息神经网络用于求解二阶常微分方程(ODE)的边值问题,Matlab实现
人工智能·神经网络·matlab·物理信息神经网络·二阶常微分方程
zandy10115 小时前
LLM与数据工程的融合:衡石Data Agent的语义层与Agent框架设计
大数据·人工智能·算法·ai·智能体
大千AI助手5 小时前
梯度消失问题:深度学习中的「记忆衰退」困境与解决方案
人工智能·深度学习·神经网络·梯度·梯度消失·链式法则·vanishing
研梦非凡5 小时前
CVPR 2025|无类别词汇的视觉-语言模型少样本学习
人工智能·深度学习·学习·语言模型·自然语言处理
seegaler5 小时前
WrenAI:开源革命,重塑商业智能未来
人工智能·microsoft·ai
max5006005 小时前
本地部署开源数据生成器项目实战指南
开发语言·人工智能·python·深度学习·算法·开源
他们叫我技术总监5 小时前
【保姆级选型指南】2025年国产开源AI算力平台怎么选?覆盖企业级_制造业_国际化场景
人工智能·开源·算力调度·ai平台·gpu国产化
IT_陈寒5 小时前
🔥5个必学的JavaScript性能黑科技:让你的网页速度提升300%!
前端·人工智能·后端
czijin5 小时前
【论文阅读】Security of Language Models for Code: A Systematic Literature Review
论文阅读·人工智能·安全·语言模型·软件工程
蛋先生DX6 小时前
零压力了解 LoRA 微调原理
人工智能·llm