-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
341 lines (282 loc) · 11.3 KB
/
Copy pathexamples.py
File metadata and controls
341 lines (282 loc) · 11.3 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
Examples of how to use the neural network implementation.
This file contains various examples demonstrating different ways to train
and evaluate neural networks on the MNIST dataset.
"""
from network import Network
from loader import DataLoader
from trainer import Trainer
from error import cross_entropy
from experiments import ExperimentRunner
from visualization import Visualizer
import activation as act
# ============================================================================
# EXAMPLE 1: Direct Training with network.fit()
# ============================================================================
def example_direct_training():
"""
Train a network directly using network.fit().
You have full control over data splitting.
"""
print("\n" + "="*80)
print("EXAMPLE 1: Direct Training with network.fit()")
print("="*80 + "\n")
# Load data
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
# Split data into train and validation sets
train_ratio = 0.8
X_train_split, Y_train_split, X_valid, Y_valid = loader.split_data(train_ratio)
# Create network: 784 inputs -> 128 hidden -> 10 outputs
network = Network(784, [128], 10, activation_function=act.relu)
# Train the network directly
history = network.fit(
X_train=X_train_split,
Y_train=Y_train_split,
X_valid=X_valid,
Y_valid=Y_valid,
error_function=cross_entropy,
epoch_number=100,
eta=0.01,
patience=10
)
# Evaluate on test set
Z_test = network.forward_propagation(X_test)
test_accuracy = network.get_accuracy(Z_test, Y_test)
print(f"\nTest Accuracy: {test_accuracy:.4f} ({test_accuracy*100:.2f}%)")
print(f"Training stopped at epoch: {history['epochs_trained']}")
print(f"Final Validation Accuracy: {history['final_valid_accuracy']:.4f}")
# ============================================================================
# EXAMPLE 2: Using Trainer.holdout_validation()
# ============================================================================
def example_holdout_validation():
"""
Train using Trainer.holdout_validation().
The Trainer handles data splitting automatically (faster).
"""
print("\n" + "="*80)
print("EXAMPLE 2: Using Trainer.holdout_validation()")
print("="*80 + "\n")
# Load data
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
# Create network
network = Network(784, [128], 10, activation_function=act.relu)
# Train using Trainer (handles data splitting automatically)
history = Trainer.holdout_validation(
loader=loader,
network=network,
error_function=cross_entropy,
epoch_number=100,
eta=0.01,
patience=10,
train_ratio=0.8 # Trainer splits the data for you
)
# Evaluate on test set
Z_test = network.forward_propagation(X_test)
test_accuracy = network.get_accuracy(Z_test, Y_test)
print(f"\nTest Accuracy: {test_accuracy:.4f} ({test_accuracy*100:.2f}%)")
print(f"Final Validation Accuracy: {history['final_valid_accuracy']:.4f}")
# ============================================================================
# EXAMPLE 3: Using Trainer.kfold_cross_validation()
# ============================================================================
def example_kfold_cross_validation():
"""
Train using K-Fold Cross-Validation.
Most robust evaluation (slower but more reliable).
"""
print("\n" + "="*80)
print("EXAMPLE 3: Using Trainer.kfold_cross_validation()")
print("="*80 + "\n")
# Load data
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
# Train using K-Fold Cross-Validation
# Returns the best model among all folds
best_network, avg_history = Trainer.kfold_cross_validation(
loader=loader,
input_neurons=784,
hidden_neurons=[128],
output_neurons=10,
error_function=cross_entropy,
k=5, # 5-fold cross-validation
epoch_number=100,
eta=0.01,
patience=10,
activation_function=act.relu
)
# Evaluate on test set
Z_test = best_network.forward_propagation(X_test)
test_accuracy = best_network.get_accuracy(Z_test, Y_test)
print(f"\nTest Accuracy: {test_accuracy:.4f} ({test_accuracy*100:.2f}%)")
print(f"Average Final Validation Accuracy: {avg_history['final_valid_accuracy']:.4f}")
# ============================================================================
# EXAMPLE 4: Run a Custom Experiment
# ============================================================================
def example_custom_experiment():
"""
Run a single experiment with specific configuration using ExperimentRunner.
DIFFERENCE from Examples 1, 2, 3:
- Examples 1-3: Train a network and get results in memory only
- Example 4: Run a full experiment that SAVES results to JSON file
ExperimentRunner adds:
- Automatic result saving to experiments_results.json
- Complete experiment metadata (config, metrics, status)
- Structured format for comparing multiple experiments
- Used internally by main.py for grid search experiments
Use this when you want to TRACK and COMPARE experiments,
not just train a single network for immediate use.
"""
print("\n" + "="*80)
print("EXAMPLE 4: Run a Custom Experiment")
print("="*80 + "\n")
# Setup
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
runner = ExperimentRunner(loader, X_test, Y_test)
# Run single experiment with specific configuration
result = runner.run_single_experiment(
input_neurons=784,
hidden_neurons=[100, 50],
output_neurons=10,
eta=1.0,
epoch_number=500,
patience=10,
error_function=cross_entropy,
activation_function=act.relu,
activation_name="relu",
use_kfold=False
)
print(f"\nTest Accuracy: {result['test_accuracy']:.4f}")
print(f"Final Validation Accuracy: {result['final_valid_accuracy']:.4f}")
print(f"Epochs Trained: {result['epochs_trained']}")
# Save results to JSON file
from results_manager import ResultsManager
results_manager = ResultsManager()
results_manager.save_json([result], "experiments_custom_results.json")
print(f"\n✓ Results saved to experiments_custom_results.json")
# ============================================================================
# EXAMPLE 5: Visualize Predictions
# ============================================================================
def example_visualize_predictions():
"""
Train a network and visualize its predictions on test samples.
"""
print("\n" + "="*80)
print("EXAMPLE 5: Visualize Predictions")
print("="*80 + "\n")
# Load data
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
# Create and train network
network = Network(784, [128], 10, activation_function=act.relu)
print("Training network...")
history = Trainer.holdout_validation(
loader=loader,
network=network,
error_function=cross_entropy,
epoch_number=50,
eta=1.0,
patience=10,
train_ratio=0.8
)
# Show predictions on random test samples
print("\nVisualizing predictions...")
Visualizer.show_multiple_predictions(network, X_test, Y_test, num_samples=5)
# Show a single prediction with specific seed
Visualizer.show_prediction(network, X_test, Y_test, random_seed=42)
# ============================================================================
# EXAMPLE 6: Comparing Different Activation Functions
# ============================================================================
def example_compare_activations():
"""
Compare different activation functions on the same architecture.
"""
print("\n" + "="*80)
print("EXAMPLE 6: Comparing Different Activation Functions")
print("="*80 + "\n")
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
activations = [
(act.sigmoid, "sigmoid"),
(act.tanh, "tanh"),
(act.relu, "relu")
]
results = []
for activation_func, activation_name in activations:
print(f"\nTesting {activation_name} activation...")
network = Network(784, [100], 10, activation_function=activation_func)
history = Trainer.holdout_validation(
loader=loader,
network=network,
error_function=cross_entropy,
epoch_number=50,
eta=0.01,
patience=10,
train_ratio=0.8
)
Z_test = network.forward_propagation(X_test)
test_accuracy = network.get_accuracy(Z_test, Y_test)
results.append({
"activation": activation_name,
"test_accuracy": test_accuracy,
"final_valid_accuracy": history['final_valid_accuracy']
})
print("\n" + "="*80)
print("COMPARISON RESULTS")
print("="*80)
for r in results:
print(f"{r['activation']:10} - Test: {r['test_accuracy']:.4f}, Valid: {r['final_valid_accuracy']:.4f}")
# ============================================================================
# EXAMPLE 7: Comparing Different Architectures
# ============================================================================
def example_compare_architectures():
"""
Compare different network architectures.
"""
print("\n" + "="*80)
print("EXAMPLE 7: Comparing Different Architectures")
print("="*80 + "\n")
loader = DataLoader("./MNIST")
X_train, Y_train, X_test, Y_test = loader.get_train_test_data()
architectures = [
[50],
[100],
[100, 50],
[128, 64]
]
results = []
for hidden_neurons in architectures:
print(f"\nTesting architecture: 784 -> {hidden_neurons} -> 10")
network = Network(784, hidden_neurons, 10, activation_function=act.relu)
history = Trainer.holdout_validation(
loader=loader,
network=network,
error_function=cross_entropy,
epoch_number=50,
eta=0.01,
patience=10,
train_ratio=0.8
)
Z_test = network.forward_propagation(X_test)
test_accuracy = network.get_accuracy(Z_test, Y_test)
results.append({
"architecture": str(hidden_neurons),
"test_accuracy": test_accuracy,
"epochs_trained": history['epochs_trained']
})
print("\n" + "="*80)
print("COMPARISON RESULTS")
print("="*80)
for r in results:
print(f"{r['architecture']:20} - Test: {r['test_accuracy']:.4f}, Epochs: {r['epochs_trained']}")
# ============================================================================
# END OF EXAMPLES
if __name__ == "__main__":
# example_direct_training()
# example_holdout_validation()
# example_kfold_cross_validation()
example_custom_experiment()
# example_visualize_predictions()
# example_compare_activations()
# example_compare_architectures()