-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
248 lines (202 loc) · 6.76 KB
/
run.py
File metadata and controls
248 lines (202 loc) · 6.76 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
Main script for training various models on the pep2prob benchmark.
Supports multiple model types: Global, BoF, LR, ResNN, Transformer
"""
import argparse
import sys
import os
from pathlib import Path
import torch
import numpy as np
import random
from pep2prob.dataset import Pep2ProbDataSet
def parse_arguments():
"""Parse command line arguments for model training."""
parser = argparse.ArgumentParser(
description="Train models on the pep2prob benchmark dataset",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
# Model selection
parser.add_argument(
"--model",
type=str,
required=True,
choices=["Global", "BoF", "LR", "ResNN", "Transformer"],
help="Model type to train"
)
# Data split index
parser.add_argument(
"--data_split",
type=int,
required=True,
choices=[1, 2, 3, 4, 5],
help="Data split index (1-5)"
)
# Training parameters
parser.add_argument(
"--epochs",
type=int,
required=True,
help="Number of training epochs"
)
parser.add_argument(
"--batch_size",
type=int,
default=1024,
help="Batch size for training"
)
parser.add_argument(
"--learning_rate",
type=float,
default=0.001,
help="Learning rate for optimizer"
)
parser.add_argument(
"--weight_decay",
type=float,
default=0.001,
help="Weight decay for optimizer"
)
# Input sequence length constraints
parser.add_argument(
"--min_length_input",
type=int,
default=7,
help="Minimum input sequence length"
)
parser.add_argument(
"--max_length_input",
type=int,
default=40,
help="Maximum input sequence length"
)
parser.add_argument(
"--min_num_psm_for_statistics",
type=int,
default=10,
help="Minimum number of PSMs required for statistics calculation"
)
# Additional optional parameters
parser.add_argument(
"--output_dir",
type=str,
default="outputs",
help="Directory to save model outputs and checkpoints"
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed for reproducibility"
)
parser.add_argument(
"--device",
type=str,
default="auto",
choices=["auto", "cpu", "cuda"],
help="Device to use for training (auto will detect best available)"
)
return parser.parse_args()
def validate_arguments(args):
"""Validate the parsed arguments."""
# Validate sequence length constraints
if args.min_length_input >= args.max_length_input:
raise ValueError(
f"min_length_input ({args.min_length_input}) must be less than "
f"max_length_input ({args.max_length_input})"
)
if args.min_length_input < 1:
raise ValueError("min_length_input must be at least 1")
# Validate training parameters
if args.epochs <= 0:
raise ValueError("epochs must be a positive integer")
if args.batch_size <= 0:
raise ValueError("batch_size must be a positive integer")
if args.learning_rate <= 0:
raise ValueError("learning_rate must be positive")
if args.weight_decay < 0:
raise ValueError("weight_decay must be non-negative")
# Create output directory if it doesn't exist
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
return True
def setup_device(device_arg):
"""Setup and return the appropriate device for training."""
import torch
if device_arg == "auto":
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
else:
device = device_arg
print(f"Using device: {device}")
return device
def main():
"""Main function to run the training pipeline."""
# Parse arguments
args = parse_arguments()
# Validate arguments
try:
validate_arguments(args)
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
# Print configuration
print("=" * 50)
print("Training Configuration")
print("=" * 50)
print(f"Model: {args.model}")
print(f"Data split: {args.data_split}")
print(f"Epochs: {args.epochs}")
print(f"Batch size: {args.batch_size}")
print(f"Learning rate: {args.learning_rate}")
print(f"Weight decay: {args.weight_decay}")
print(f"Min input length: {args.min_length_input}")
print(f"Max input length: {args.max_length_input}")
print(f"Min PSMs for statistics: {args.min_num_psm_for_statistics}")
print(f"Output directory: {args.output_dir}")
print(f"Random seed: {args.seed}")
print("=" * 50)
# Setup device
device = setup_device(args.device)
# Set random seed for reproducibility
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
if device == "cuda":
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
# Setup paths
this_dir = Path(__file__).parent.resolve()
data_dir = this_dir / "data"
# Load data
print("Loading data...")
dataset = Pep2ProbDataSet(data_dir, split_idx=args.data_split, min_length_input=args.min_length_input, max_length_input=args.max_length_input, min_num_psm_for_statistics=args.min_num_psm_for_statistics, skip_download_if_exists=True)
# TODO: Import and initialize the selected model
print(f"\nInitializing {args.model} model...")
if args.model == "Transformer":
# Import Transformer model
print("Loading Transformer model...")
# TODO: from models.Transformer_baseline.main import train_transformer
# train_transformer(args)
elif args.model == "Global":
print("Loading Global model...")
# TODO: Implement Global model training
elif args.model == "BoF":
print("Loading Bag of Features model...")
# TODO: Implement BoF model training
elif args.model == "LR":
print("Loading Linear Regression model...")
# TODO: Implement LR model training
elif args.model == "ResNN":
print("Loading Residual Neural Network model...")
# TODO: Implement ResNN model training
print(f"\nTraining {args.model} model with data split {args.data_split}...")
print("Training pipeline not yet implemented. This is a template structure.")
# TODO: Load data based on data_split
# TODO: Initialize model
# TODO: Run training loop
# TODO: Save results to output_dir
print("Training completed!")
if __name__ == "__main__":
main()