-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
199 lines (171 loc) · 6.13 KB
/
train.py
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
# train.py
import argparse
from pathlib import Path
from src.config import Config
from src.training.model_builder import ModelBuilder
from src.training.text_processor import TextProcessor
from src.utils.text_file_reader import TextFileReader
from src.utils.progress_tracker import ProgressTracker
from src.utils.graceful_killer import GracefulKiller
def main():
# Set up environment and directories
Config.setup_environment()
Config.setup_directories()
parser = argparse.ArgumentParser(
description='Train LSTM model on preprocessed markdown files.'
)
parser.add_argument(
'--data-dir',
type=str,
default=str(Config.PROCESSED_DATA_DIR),
help='Directory containing preprocessed markdown files'
)
parser.add_argument(
'--model-dir',
type=str,
default=str(Config.MODEL_DIR),
help='Directory to save trained model and tokenizer'
)
parser.add_argument(
'--sequence-length',
type=int,
default=Config.DEFAULT_SEQUENCE_LENGTH,
help='Length of input sequences'
)
parser.add_argument(
'--embedding-dim',
type=int,
default=Config.DEFAULT_EMBEDDING_DIM,
help='Dimension of word embeddings'
)
parser.add_argument(
'--batch-size',
type=int,
default=Config.DEFAULT_BATCH_SIZE,
help='Training batch size'
)
args = parser.parse_args()
# Initialize components
model_dir = Path(args.model_dir)
model_dir.mkdir(parents=True, exist_ok=True)
# Initialize progress tracker
progress_file = model_dir / '.training_progress.json'
progress_tracker = ProgressTracker(progress_file)
# Initialize graceful shutdown handler
killer = GracefulKiller()
reader = TextFileReader(args.data_dir, file_type="markdown")
processor = TextProcessor(sequence_length=args.sequence_length)
print("\nStarting model training...")
print(f"Data directory: {args.data_dir}")
print(f"Model directory: {args.model_dir}")
print(f"Sequence length: {args.sequence_length}")
print(f"Embedding dimension: {args.embedding_dim}")
print(f"Batch size: {args.batch_size}")
# First pass: fit tokenizer if not already done
if not progress_tracker.is_completed("tokenizer_fitting"):
print("\nFitting tokenizer...")
all_texts = []
for filename, content in reader:
if content and not killer.kill_now:
all_texts.append(content)
progress_tracker.update_file_progress(
Path(filename),
completed=True,
stage="tokenizer_fitting"
)
if not all_texts:
raise ValueError("No valid training data found")
processor.fit_tokenizer(all_texts)
print(f"\nVocabulary size: {processor.vocab_size:,} words")
# Save the processor
processor_path = model_dir / 'text_processor.pkl'
processor.save(processor_path)
print(f"Saved text processor to {processor_path}")
progress_tracker.update_file_progress(
Path("tokenizer"),
completed=True,
stage="tokenizer_fitting"
)
else:
print("\nLoading existing tokenizer...")
processor = TextProcessor.load(model_dir / 'text_processor.pkl')
print(f"Vocabulary size: {processor.vocab_size:,} words")
# Create and configure the model
print("\nCreating model...")
model = ModelBuilder.create_model(
vocab_size=processor.vocab_size,
sequence_length=processor.sequence_length,
embedding_dim=args.embedding_dim
)
model.summary()
# Train the model
print("\nTraining model...")
total_sequences = 0
total_files = 0
# Get remaining files to process
remaining_files = progress_tracker.get_remaining_files([
Path(f) for f in reader.files
])
print(f"Files to process: {len(remaining_files)}")
for file_path in remaining_files:
if killer.kill_now:
print("\nGraceful shutdown requested...")
break
try:
content = reader.read_file(file_path.name)
if not content:
continue
X, y = processor.create_sequences(content)
if len(X) > 0:
model.fit(
X, y,
epochs=1,
batch_size=args.batch_size,
verbose=0
)
total_sequences += len(X)
total_files += 1
# Update progress
progress_tracker.update_file_progress(
file_path,
completed=True,
metadata={
"sequences": len(X),
"total_sequences": total_sequences
}
)
# Print progress
print(f"\rProcessed: {total_files} files, {total_sequences:,} sequences",
end="", flush=True)
except Exception as e:
print(f"\nError processing {file_path}: {str(e)}")
continue
print(f"\n\nTraining completed:")
print(f"- Processed {total_files:,} files")
print(f"- Total sequences trained on: {total_sequences:,}")
if total_files > 0:
print(
f"- Average sequences per file: {total_sequences/total_files:,.1f}")
# Save the model
try:
model_path = model_dir / 'text_generation_model.keras'
model.save(model_path)
print(f"\nModel saved to {model_path}")
# Mark training as complete
progress_tracker.update_file_progress(
Path("model"),
completed=True,
stage="model_training",
metadata={
"total_files": total_files,
"total_sequences": total_sequences,
"avg_sequences_per_file": total_sequences/total_files if total_files > 0 else 0
}
)
except Exception as e:
print(f"\nError saving model: {str(e)}")
raise
finally:
progress_tracker.save_progress()
if __name__ == "__main__":
main()