File size: 3,705 Bytes
ee594f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import re
import os

with open('Train_CMTLNet_Step2.py', 'r') as f:
    code = f.read()

target = """    global_step = 0  # Initialize global step
    best_score = -1  # Initialize best score

    # Initialize DataFrames for logging
    log_1p19q = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_IDH = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_LHG = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_mean = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    model.train()  # Set model to training mode

    # Initialize iterator indices
    iter_index_IDH = 0
    iter_index_LHG = 0

    iter_IDH = iter(train_loader_IDH)
    iter_LHG = iter(train_loader_LHG)

    for epoch in range(config.epochs_s2):"""

replacement = """    global_step = 0  # Initialize global step
    best_score = -1  # Initialize best score
    start_epoch = 0
    scaler = GradScaler()
    save_name = getattr(model, 'name', 'UFE_UCFC')
    save_path = os.path.join(config.data_dir, 'results', 'UFE_UCFC', f'Fold_{fold}')
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    # Resume logic
    import glob
    checkpoints = glob.glob(os.path.join(save_path, f'checkpoint_fold{fold}_epoch*.pkl'))
    if checkpoints:
        latest_checkpoint = max(checkpoints, key=lambda x: int(x.split('epoch')[1].split('.pkl')[0]))
        print(f"Resuming from checkpoint: {latest_checkpoint}")
        checkpoint = torch.load(latest_checkpoint, weights_only=False)
        model.load_state_dict(checkpoint['model_state_dict'])
        if 'optimizer_state_dict' in checkpoint:
            optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        if 'scheduler_state_dict' in checkpoint:
            scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
        if 'scaler_state_dict' in checkpoint:
            scaler.load_state_dict(checkpoint['scaler_state_dict'])
        if 'global_step' in checkpoint:
            global_step = checkpoint['global_step']
        if 'best_f1' in checkpoint:
            best_score = checkpoint['best_f1']
        if 'epoch' in checkpoint:
            start_epoch = checkpoint['epoch'] + 1

    # Initialize DataFrames for logging
    log_1p19q = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_IDH = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_LHG = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    log_mean = pd.DataFrame(columns=[
        'epoch',
        'Train_loss', 'Train_Accuracy', 'Train_F1',
        'Val_loss', 'Val_Accuracy', 'Val_F1'
    ])

    model.train()  # Set model to training mode

    # Initialize iterator indices
    iter_index_IDH = 0
    iter_index_LHG = 0

    iter_IDH = iter(train_loader_IDH)
    iter_LHG = iter(train_loader_LHG)

    for epoch in range(start_epoch, config.epochs_s2):"""

if target in code:
    code = code.replace(target, replacement)
else:
    print("Failed to replace target 1")

with open('Train_CMTLNet_Step2.py', 'w') as f:
    f.write(code)
print("Part 2 replacements done.")