-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
234 lines (189 loc) · 7.6 KB
/
train.py
File metadata and controls
234 lines (189 loc) · 7.6 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
"""
Training script for the baseline Forest Covertype classification model
"""
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
from pathlib import Path
from dataset import get_data
from model import build_model
# Training configuration
NUM_EPOCHS = 10
def evaluate_model(model, X, y, device=None):
"""Evaluate model and return accuracy and detailed metrics"""
if device is None:
device = next(model.parameters()).device
model.eval()
with torch.no_grad():
X_tensor = torch.FloatTensor(X).to(device)
predictions = model(X_tensor)
predicted_classes = torch.argmax(predictions, dim=1).cpu().numpy()
accuracy = accuracy_score(y, predicted_classes)
return accuracy, predicted_classes
def train():
"""Train the model"""
# Detect and set device (CUDA > MPS > CPU)
if torch.cuda.is_available():
device = torch.device('cuda')
print(f"Using device: CUDA ({torch.cuda.get_device_name(0)})")
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
device = torch.device('mps')
print(f"Using device: MPS (Apple Silicon)")
else:
device = torch.device('cpu')
print(f"Using device: CPU")
print()
# Load data with splits
X_train, X_val, X_test, y_train, y_val, y_test, num_classes, scaler = get_data()
# Convert to PyTorch tensors and move to device
X_train_tensor = torch.FloatTensor(X_train).to(device)
y_train_tensor = torch.LongTensor(y_train).to(device)
X_val_tensor = torch.FloatTensor(X_val).to(device)
y_val_tensor = torch.LongTensor(y_val).to(device)
# Create dataset and dataloader
train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
train_dataloader = DataLoader(train_dataset, batch_size=256, shuffle=True)
# Build model and move to device
print("\nBuilding model...")
model = build_model(input_dim=X_train.shape[1], num_classes=num_classes)
model = model.to(device)
print(model)
print(f"\nModel Parameters: {sum(p.numel() for p in model.parameters()):,}")
# Define loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
print("\n" + "="*60)
print("TRAINING")
print("="*60)
print(f"Training for {NUM_EPOCHS} epochs...")
print()
# Track metrics for plotting
train_losses = []
train_accuracies = []
val_losses = []
val_accuracies = []
best_val_accuracy = 0.0
# Calculate validation loss
def compute_val_loss(model, X_val, y_val):
model.eval()
with torch.no_grad():
X_val_tensor = torch.FloatTensor(X_val).to(device)
y_val_tensor = torch.LongTensor(y_val).to(device)
outputs = model(X_val_tensor)
loss = criterion(outputs, y_val_tensor)
return loss.item()
for epoch in range(NUM_EPOCHS):
# Training phase
model.train()
epoch_loss = 0.0
correct = 0
total = 0
for batch_X, batch_y in train_dataloader:
# Zero the gradients
optimizer.zero_grad()
# Forward pass
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
# Backward pass and optimization
loss.backward()
optimizer.step()
# Track metrics
epoch_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += batch_y.size(0)
correct += (predicted == batch_y).sum().item()
train_accuracy = correct / total
avg_loss = epoch_loss / len(train_dataloader)
# Validation phase
val_accuracy, _ = evaluate_model(model, X_val, y_val, device)
val_loss = compute_val_loss(model, X_val, y_val)
# Track metrics
train_losses.append(avg_loss)
train_accuracies.append(train_accuracy)
val_losses.append(val_loss)
val_accuracies.append(val_accuracy)
if val_accuracy > best_val_accuracy:
best_val_accuracy = val_accuracy
print(f"Epoch {epoch+1:2d}/{NUM_EPOCHS} - Loss: {avg_loss:.4f} | "
f"Train Acc: {train_accuracy:.4f} | Val Acc: {val_accuracy:.4f}")
print(f"\nBest Validation Accuracy: {best_val_accuracy:.4f}")
# Create and save plots
print("\n" + "="*60)
print("SAVING TRAINING PLOTS")
print("="*60)
# Create plots directory if it doesn't exist
plots_dir = Path("plots")
plots_dir.mkdir(exist_ok=True)
epochs = range(1, len(train_losses) + 1)
# Plot 1: Loss curves
plt.figure(figsize=(10, 6))
plt.plot(epochs, train_losses, 'b-', label='Training Loss', linewidth=2)
plt.plot(epochs, val_losses, 'r-', label='Validation Loss', linewidth=2)
plt.xlabel('Epoch', fontsize=12)
plt.ylabel('Loss', fontsize=12)
plt.title('Training and Validation Loss', fontsize=14, fontweight='bold')
plt.legend(fontsize=11)
plt.grid(True, alpha=0.3)
plt.tight_layout()
loss_path = plots_dir / "training_loss.png"
plt.savefig(loss_path, dpi=150, bbox_inches='tight')
print(f" ✓ Saved loss plot: {loss_path}")
plt.close()
# Plot 2: Accuracy curves
plt.figure(figsize=(10, 6))
plt.plot(epochs, train_accuracies, 'b-', label='Training Accuracy', linewidth=2)
plt.plot(epochs, val_accuracies, 'r-', label='Validation Accuracy', linewidth=2)
plt.xlabel('Epoch', fontsize=12)
plt.ylabel('Accuracy', fontsize=12)
plt.title('Training and Validation Accuracy', fontsize=14, fontweight='bold')
plt.legend(fontsize=11)
plt.grid(True, alpha=0.3)
plt.tight_layout()
acc_path = plots_dir / "training_accuracy.png"
plt.savefig(acc_path, dpi=150, bbox_inches='tight')
print(f" ✓ Saved accuracy plot: {acc_path}")
plt.close()
# Combined plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))
# Loss subplot
ax1.plot(epochs, train_losses, 'b-', label='Training Loss', linewidth=2)
ax1.plot(epochs, val_losses, 'r-', label='Validation Loss', linewidth=2)
ax1.set_xlabel('Epoch', fontsize=12)
ax1.set_ylabel('Loss', fontsize=12)
ax1.set_title('Training and Validation Loss', fontsize=13, fontweight='bold')
ax1.legend(fontsize=11)
ax1.grid(True, alpha=0.3)
# Accuracy subplot
ax2.plot(epochs, train_accuracies, 'b-', label='Training Accuracy', linewidth=2)
ax2.plot(epochs, val_accuracies, 'r-', label='Validation Accuracy', linewidth=2)
ax2.set_xlabel('Epoch', fontsize=12)
ax2.set_ylabel('Accuracy', fontsize=12)
ax2.set_title('Training and Validation Accuracy', fontsize=13, fontweight='bold')
ax2.legend(fontsize=11)
ax2.grid(True, alpha=0.3)
plt.tight_layout()
combined_path = plots_dir / "training_curves.png"
plt.savefig(combined_path, dpi=150, bbox_inches='tight')
print(f" ✓ Saved combined plot: {combined_path}")
plt.close()
print("="*60)
# Final evaluation on test set
print("\n" + "="*60)
print("TEST SET EVALUATION")
print("="*60)
test_accuracy, test_predictions = evaluate_model(
model, X_test, y_test, device
)
print(f"\nTest Accuracy: {test_accuracy:.4f}")
print("\n" + "="*60)
print("BASELINE MODEL COMPLETE")
print("="*60)
print()
return model
if __name__ == "__main__":
train()