|
3 | 3 | import npyfile |
4 | 4 | import array |
5 | 5 | import gc |
| 6 | +import time |
6 | 7 |
|
7 | 8 | def load_npy_as_array(filename, dtype='f'): |
8 | 9 | """Load .npy file and convert to MicroPython array.""" |
@@ -39,7 +40,7 @@ def compare_regularization(): |
39 | 40 |
|
40 | 41 | model = emlearn_linreg.new(n_features, alpha, l1_ratio, 0.01) |
41 | 42 | emlearn_linreg.train(model, X_subset, y_subset, |
42 | | - max_iterations=2000, check_interval=50, verbose=1) |
| 43 | + max_iterations=100, check_interval=50, verbose=1) |
43 | 44 |
|
44 | 45 | # Calculate predictions and MSE |
45 | 46 | mse = model.score_mse(X_subset, y_subset) |
@@ -75,11 +76,17 @@ def test_elasticnet_full(): |
75 | 76 | print("Creating ElasticNet model...") |
76 | 77 | model = emlearn_linreg.new(n_features, 0.001, 0.5, 0.01) # Lower learning rate for stability |
77 | 78 |
|
| 79 | + train_start = time.ticks_ms() |
78 | 80 | # Train on full dataset |
79 | 81 | print("Training on full dataset...") |
80 | | - emlearn_linreg.train(model, X_train_data, y_train_data, |
81 | | - max_iterations=2000, check_interval=50, verbose=1, tolerance=0.001, score_limit=0.60) |
82 | | - |
| 82 | + stop_iter, stop_mse = emlearn_linreg.train(model, |
| 83 | + X_train_data, y_train_data, |
| 84 | + max_iterations=2000, check_interval=50, |
| 85 | + verbose=2, tolerance=0.001, score_limit=0.60, |
| 86 | + ) |
| 87 | + train_duration = time.ticks_diff(time.ticks_ms(), train_start) |
| 88 | + print('Train time (ms)', train_duration, 'per iter', train_duration/stop_iter) |
| 89 | + |
83 | 90 | # Get final parameters |
84 | 91 | weights = array.array('f', [0.0] * n_features) |
85 | 92 | model.get_weights(weights) |
@@ -117,8 +124,9 @@ def main(): |
117 | 124 |
|
118 | 125 | try: |
119 | 126 | # Compare regularization approaches |
120 | | - compare_regularization() |
121 | | - |
| 127 | + #compare_regularization() |
| 128 | + gc.collect() |
| 129 | + |
122 | 130 | test_elasticnet_full() |
123 | 131 | print("\n=== All Tests Completed Successfully! ===") |
124 | 132 |
|
|
0 commit comments