-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_training_wrapper.py
More file actions
83 lines (67 loc) · 2.66 KB
/
Copy pathreal_training_wrapper.py
File metadata and controls
83 lines (67 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
真实训练包装器代码
将此代码集成到 task_evaluator.py 中
"""
from typing import Dict, Any, Tuple
import argparse
def execute_real_training_with_progress(self, parameters: Dict[str, Any], args: argparse.Namespace,
fold: int, total_epochs: int) -> Tuple[float, float, float, float, float, float]:
"""
执行真实训练的包装器函数
"""
from nested_progress_manager import update_epoch_progress
try:
# 尝试执行真实训练
return self._execute_real_training(parameters, args, fold, total_epochs)
except Exception as e:
self.logger.error(f"真实训练失败,回退到模拟训练: {str(e)}")
return self._fallback_simulate_training(parameters, args, fold, total_epochs)
def _execute_real_training(self, parameters: Dict[str, Any], args: argparse.Namespace,
fold: int, total_epochs: int) -> Tuple[float, float, float, float, float, float]:
"""
执行真实的神经网络训练
"""
# 修复参数问题
from fix_training_issues import TrainingIssueFixer
fixer = TrainingIssueFixer()
fixed_params, was_fixed = fixer.validate_and_fix_parameters(parameters)
if was_fixed:
# 更新args中的参数
for key, value in fixed_params.items():
setattr(args, key, value)
# 导入训练模块
from train import train_model
import torch
import torch.optim as optim
# 设置设备
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# 创建模型(这里需要根据实际情况调整)
try:
from layer import GCN # 假设这是您的模型类
model = GCN(args).to(device)
except Exception as e:
self.logger.error(f"模型创建失败: {e}")
raise
# 创建优化器
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
# 准备数据
data_o, data_a, train_loader, test_loader = self._prepare_training_data(args, fold)
# 执行真实训练
results = train_model(model, optimizer, data_o, data_a, train_loader, test_loader, args, fold)
return (
results['auroc'],
results['auprc'],
results['f1'],
results['precision'],
results['recall'],
results['loss']
)
def _prepare_training_data(self, args: argparse.Namespace, fold: int):
"""
准备训练数据
"""
# 这里需要根据您的实际数据加载逻辑来实现
# 暂时抛出异常,提醒需要实现
raise NotImplementedError("需要实现数据加载逻辑")