-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVBAF.ML.TransferLearning.ps1
More file actions
1253 lines (1107 loc) · 49.8 KB
/
VBAF.ML.TransferLearning.ps1
File metadata and controls
1253 lines (1107 loc) · 49.8 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ==============================================================================
# VBAF.ML.TransferLearning.ps1
# Visual Brain AI Framework — Transfer Learning Module
# Version : v1.0.0 | Requires PS 5.1 | Part of VBAF v2.1.0
# ==============================================================================
#
# FEATURES
# --------
# 1. Toy model zoo — hand-crafted pretrained weights (no training needed)
# 2. Layer freezing — freeze/unfreeze individual layers
# 3. Feature extraction — get activations at any layer depth
# 4. Fine-tuning — backprop only through unfrozen layers
# 5. Weight save/load — JSON persistence compatible with VBAF.ML.CNN.ps1
#
# LAYER FORMAT
# ------------
# All layers are hashtables (same format as VBAF.ML.Autoencoder.ps1):
# @{ W; B; InSize; OutSize; Activation; Frozen; LastInput; LastPreact; LastOutput }
# Stored in ArrayLists → reference semantics → weight mutations persist ✅
#
# PS 5.1 RULES (same as Autoencoder module)
# ------------------------------------------
# • Hashtables in ArrayList = always references
# • Direct element assignment: $layer.W[$i] = value ← persists ✅
# • Functions in class methods: & (Get-Command Func) -Param val
# • Index loops only — no foreach/ForEach-Object/switch on arrays
# • Comma operator on return: return ,$array ← prevents unrolling
#
# QUICK START
# -----------
# . .\VBAF.ML.TransferLearning.ps1
# Get-VBAFModelZoo # list available models
# $model = Get-VBAFPretrainedModel -Name "ShapeEncoder"
# $feats = Get-VBAFFeatures -Model $model -X $inputVec -FromLayer 1
# Set-VBAFLayerFrozen -Model $model -LayerIndex 0 -Frozen $true
# $r = Invoke-VBAFFineTune -Model $model -Data $ds.X -Labels $ds.Labels -Epochs 50 -LR 0.05
# Test-VBAFTransferLearning
# ==============================================================================
Write-Host ''
Write-Host ' ╔══════════════════════════════════════════════╗' -ForegroundColor Magenta
Write-Host ' ║ VBAF · Transfer Learning Module v1.0.0 ║' -ForegroundColor Magenta
Write-Host ' ║ ModelZoo · Freeze · Features · FineTune ║' -ForegroundColor Magenta
Write-Host ' ║ PS 5.1 | Hashtable-reference layers ║' -ForegroundColor Magenta
Write-Host ' ╚══════════════════════════════════════════════╝' -ForegroundColor Magenta
Write-Host ''
# ==============================================================================
# LAYER PRIMITIVES
# (self-contained — compatible with VBAF.ML.Autoencoder.ps1 layer format)
# ==============================================================================
function New-TLLayer {
<#
.SYNOPSIS
Create a fully-connected layer hashtable for Transfer Learning.
Compatible with VBAF.ML.Autoencoder.ps1 layer format.
.DESCRIPTION
Layers are hashtables stored in ArrayList → always references in PS 5.1.
Direct element assignment $layer.W[$i] = val PERSISTS. ✅
Frozen=$false by default — set to $true to skip weight updates.
.PARAMETER InSize Input dimension
.PARAMETER OutSize Output dimension
.PARAMETER Activation 'relu' | 'sigmoid' | 'linear' | 'softmax'
.PARAMETER Seed RNG seed (default 42)
.OUTPUTS Hashtable layer
#>
param(
[int] $InSize,
[int] $OutSize,
[string]$Activation = 'relu',
[int] $Seed = 42
)
$rng = [System.Random]::new($Seed)
$scale = [Math]::Sqrt(2.0 / $InSize) # He init
$wLen = $InSize * $OutSize
$W = @(0.0) * $wLen
$B = @(0.0) * $OutSize
for ($i = 0; $i -lt $wLen; $i++) {
$W[$i] = ($rng.NextDouble() * 2.0 - 1.0) * $scale
}
return @{
W = $W
B = $B
InSize = $InSize
OutSize = $OutSize
Activation = $Activation
Frozen = $false
LastInput = $null
LastPreact = $null
LastOutput = $null
}
}
function Invoke-TLActivation {
<#
.SYNOPSIS Apply activation function to preactivation array.
.OUTPUTS [double[]] (comma-protected)
#>
param([double[]]$Z, [string]$Name)
$n = $Z.Length
$out = [double[]]::new($n)
if ($Name -eq 'relu') {
for ($i = 0; $i -lt $n; $i++) {
$out[$i] = if ($Z[$i] -gt 0.0) { $Z[$i] } else { 0.0 }
}
} elseif ($Name -eq 'sigmoid') {
for ($i = 0; $i -lt $n; $i++) {
$zc = [Math]::Max(-500.0, [Math]::Min(500.0, $Z[$i]))
$out[$i] = 1.0 / (1.0 + [Math]::Exp(-$zc))
}
} elseif ($Name -eq 'softmax') {
# Numerically stable softmax: shift by max
$maxZ = $Z[0]
for ($i = 1; $i -lt $n; $i++) { if ($Z[$i] -gt $maxZ) { $maxZ = $Z[$i] } }
$sumE = 0.0
for ($i = 0; $i -lt $n; $i++) {
$out[$i] = [Math]::Exp($Z[$i] - $maxZ)
$sumE += $out[$i]
}
for ($i = 0; $i -lt $n; $i++) { $out[$i] /= $sumE }
} else {
# linear
for ($i = 0; $i -lt $n; $i++) { $out[$i] = $Z[$i] }
}
return ,$out
}
function Invoke-TLActivationGrad {
<#
.SYNOPSIS Elementwise activation derivative × upstream gradient.
.OUTPUTS [double[]] dPreact (comma-protected)
#>
param([double[]]$DOut, [double[]]$Z, [double[]]$A, [string]$Name)
$n = $DOut.Length
$d = [double[]]::new($n)
if ($Name -eq 'relu') {
for ($i = 0; $i -lt $n; $i++) {
$d[$i] = if ($Z[$i] -gt 0.0) { $DOut[$i] } else { 0.0 }
}
} elseif ($Name -eq 'sigmoid') {
for ($i = 0; $i -lt $n; $i++) {
$d[$i] = $DOut[$i] * $A[$i] * (1.0 - $A[$i])
}
} elseif ($Name -eq 'softmax') {
# Jacobian diagonal approximation (cross-entropy upstream assumed)
for ($i = 0; $i -lt $n; $i++) {
$d[$i] = $DOut[$i] * $A[$i] * (1.0 - $A[$i])
}
} else {
for ($i = 0; $i -lt $n; $i++) { $d[$i] = $DOut[$i] }
}
return ,$d
}
function Invoke-TLLayerForward {
<#
.SYNOPSIS
Forward pass for one TL layer. Caches input/preact/output for backprop.
.OUTPUTS [double[]] (comma-protected)
#>
param([hashtable]$Layer, [double[]]$X)
$inSz = [int]$Layer.InSize
$outSz = [int]$Layer.OutSize
$W = $Layer.W
$B = $Layer.B
# Cache input copy
$inp = [double[]]::new($inSz)
for ($i = 0; $i -lt $inSz; $i++) { $inp[$i] = [double]$X[$i] }
$Layer.LastInput = $inp
# Preactivation z[j] = W[j,:] · x + B[j]
$Z = [double[]]::new($outSz)
for ($j = 0; $j -lt $outSz; $j++) {
$sum = [double]$B[$j]
$base = $j * $inSz
for ($i = 0; $i -lt $inSz; $i++) {
$sum += [double]$W[$base + $i] * [double]$X[$i]
}
$Z[$j] = $sum
}
$Layer.LastPreact = $Z
$A = Invoke-TLActivation -Z $Z -Name $Layer.Activation
$Layer.LastOutput = $A
return ,$A
}
function Invoke-TLLayerBackward {
<#
.SYNOPSIS
Backward pass for one TL layer.
SKIPS weight update if $Layer.Frozen -eq $true.
Returns dLoss/dInput for propagation to earlier layers.
.OUTPUTS [double[]] dX (comma-protected)
#>
param([hashtable]$Layer, [double[]]$DOut, [double]$LR)
$inSz = [int]$Layer.InSize
$outSz = [int]$Layer.OutSize
$X = [double[]]$Layer.LastInput
$Z = [double[]]$Layer.LastPreact
$A = [double[]]$Layer.LastOutput
# Activation gradient
$DZ = Invoke-TLActivationGrad -DOut $DOut -Z $Z -A $A -Name $Layer.Activation
# dX[i] = Σ_j DZ[j] * W[j,i]
$DX = [double[]]::new($inSz)
for ($i = 0; $i -lt $inSz; $i++) {
$sum = 0.0
for ($j = 0; $j -lt $outSz; $j++) {
$sum += [double]$DZ[$j] * [double]$Layer.W[$j * $inSz + $i]
}
$DX[$i] = $sum
}
# Weight update — SKIPPED if frozen ✅
if (-not $Layer.Frozen) {
for ($j = 0; $j -lt $outSz; $j++) {
$dz_j = [double]$DZ[$j]
$base = $j * $inSz
for ($i = 0; $i -lt $inSz; $i++) {
# Direct element assignment on hashtable array persists! ✅
$Layer.W[$base + $i] = [double]$Layer.W[$base + $i] - $LR * $dz_j * [double]$X[$i]
}
$Layer.B[$j] = [double]$Layer.B[$j] - $LR * $dz_j
}
}
return ,$DX
}
# ==============================================================================
# TL MODEL (wrapper class — thin shell, all state in hashtable layers)
# ==============================================================================
class TLModel {
<#
A sequential model for transfer learning.
Layers are hashtables in an ArrayList — always references in PS 5.1.
Forward/backward delegate to standalone functions via & (Get-Command ...).
#>
[System.Collections.ArrayList] $Layers
[string] $Name
[string] $Description
TLModel([string]$name, [string]$description) {
$this.Name = $name
$this.Description = $description
$this.Layers = [System.Collections.ArrayList]::new()
}
[void] AddLayer([hashtable]$layer) {
$this.Layers.Add($layer) | Out-Null
}
# Forward pass — returns output of last layer
[object] Forward([double[]]$x) {
$current = $x
for ($li = 0; $li -lt $this.Layers.Count; $li++) {
$lyr = [hashtable]$this.Layers[$li]
$current = [double[]]( & (Get-Command Invoke-TLLayerForward) -Layer $lyr -X ([double[]]$current) )
}
return $current
}
# Forward up to (and including) $ToLayer — for feature extraction
[object] ForwardTo([double[]]$x, [int]$toLayer) {
$current = $x
$limit = [Math]::Min($toLayer, $this.Layers.Count - 1)
for ($li = 0; $li -le $limit; $li++) {
$lyr = [hashtable]$this.Layers[$li]
$current = [double[]]( & (Get-Command Invoke-TLLayerForward) -Layer $lyr -X ([double[]]$current) )
}
return $current
}
# Backward — updates only UNFROZEN layers
[void] Backward([double[]]$dLoss, [double]$lr) {
$grad = [double[]]$dLoss
for ($li = $this.Layers.Count - 1; $li -ge 0; $li--) {
$lyr = [hashtable]$this.Layers[$li]
$grad = [double[]]( & (Get-Command Invoke-TLLayerBackward) -Layer $lyr -DOut $grad -LR $lr )
}
}
[string] ToString() {
$s = ('TLModel [{0}] ' -f $this.Name)
for ($li = 0; $li -lt $this.Layers.Count; $li++) {
$lyr = [hashtable]$this.Layers[$li]
$frz = if ($lyr.Frozen) { '❄' } else { '' }
if ($li -gt 0) { $s += ' → ' }
$s += ('{0}({1}){2}' -f $lyr.Activation, $lyr.OutSize, $frz)
}
return $s
}
}
# ==============================================================================
# MODEL ZOO (toy pretrained models with hand-crafted weights)
# ==============================================================================
function Get-VBAFModelZoo {
<#
.SYNOPSIS
List all available pretrained models in the VBAF toy model zoo.
.EXAMPLE
Get-VBAFModelZoo
#>
Write-Host ''
Write-Host ' ── VBAF Toy Model Zoo ──────────────────────────' -ForegroundColor Magenta
Write-Host ''
Write-Host ' Name Input Output Description' -ForegroundColor DarkGray
Write-Host ' ─────────────────────────────────────────────────' -ForegroundColor DarkGray
Write-Host ' EdgeDetector 16 8 Sobel-inspired edge features on 4×4 grid' -ForegroundColor White
Write-Host ' ShapeEncoder 16 3 Encodes HBar/VBar/Diag patterns (Shapes2D)' -ForegroundColor White
Write-Host ' PatternFilter 16 4 Low/high frequency pattern detector' -ForegroundColor White
Write-Host ''
Write-Host ' Usage: $m = Get-VBAFPretrainedModel -Name "ShapeEncoder"' -ForegroundColor Yellow
Write-Host ''
}
function Get-VBAFPretrainedModel {
<#
.SYNOPSIS
Return a TLModel with hand-crafted "pretrained" weights.
.DESCRIPTION
These are TOY models — weights are carefully hand-designed to capture
meaningful structure, not trained by gradient descent. They serve as
realistic starting points for transfer learning experiments.
EdgeDetector : 16→8 — detects horizontal/vertical edges in 4×4 grids
ShapeEncoder : 16→8→3 — approximately classifies HBar/VBar/Diag shapes
PatternFilter: 16→4 — detects low/high frequency spatial patterns
.PARAMETER Name 'EdgeDetector' | 'ShapeEncoder' | 'PatternFilter'
.OUTPUTS TLModel with Layers pre-populated
.EXAMPLE
$m = Get-VBAFPretrainedModel -Name "ShapeEncoder"
$m.ToString()
[double[]]$m.Forward(@(1,1,1,1, 0,0,0,0, 0,0,0,0, 1,1,1,1))
#>
param([Parameter(Mandatory)][string]$Name)
if ($Name -eq 'EdgeDetector') {
# ── EdgeDetector: 16 → 8 ─────────────────────────────────────────────
# Detects 8 edge features in a 4×4 binary grid:
# Units 0-3: horizontal edge detectors (row transitions)
# Units 4-7: vertical edge detectors (column transitions)
#
# Input layout (row-major):
# 0 1 2 3
# 4 5 6 7
# 8 9 10 11
# 12 13 14 15
#
# Horizontal edge unit k detects transition between row k and row k+1:
# w = +1 for pixels in row k, -1 for pixels in row k+1
# Vertical edge unit k detects transition between col k and col k+1:
# w = +1 for pixels in col k, -1 for pixels in col k+1
$model = [TLModel]::new('EdgeDetector', 'Sobel-inspired edge detector on 4x4 grids (16→8)')
$W = @(0.0) * (16 * 8)
$B = @(0.0) * 8
# Units 0-3: horizontal edge detectors
for ($unit = 0; $unit -lt 4; $unit++) {
for ($col = 0; $col -lt 4; $col++) {
if ($unit -lt 3) {
# +1 for current row, -1 for next row
$W[$unit * 16 + $unit * 4 + $col] = 1.0
$W[$unit * 16 + ($unit+1) * 4 + $col] = -1.0
} else {
# Last unit: detects bottom row activation
$W[$unit * 16 + 12 + $col] = 1.0
}
}
}
# Units 4-7: vertical edge detectors
for ($unit = 0; $unit -lt 4; $unit++) {
for ($row = 0; $row -lt 4; $row++) {
if ($unit -lt 3) {
# +1 for current col, -1 for next col
$W[($unit+4) * 16 + $row * 4 + $unit] = 1.0
$W[($unit+4) * 16 + $row * 4 + $unit + 1] = -1.0
} else {
# Last unit: detects right column activation
$W[($unit+4) * 16 + $row * 4 + 3] = 1.0
}
}
}
$layer = New-TLLayer -InSize 16 -OutSize 8 -Activation 'relu' -Seed 1
$layer.W = $W
$layer.B = $B
$model.AddLayer($layer)
return $model
}
elseif ($Name -eq 'ShapeEncoder') {
# ── ShapeEncoder: 16 → 8 → 3 ─────────────────────────────────────────
# Approximately classifies Shapes2D patterns: HBar / VBar / Diag
#
# Layer 1 (16→8): extracts shape-relevant features
# Units 0-1: horizontal bar detectors (top/bottom row sums)
# Units 2-3: vertical bar detectors (left/right column sums)
# Units 4-5: diagonal detectors (main/anti diagonal)
# Units 6-7: density detectors (total pixel count high/low)
#
# Layer 2 (8→3): combines features → class scores
# Output 0 = HBar score, 1 = VBar score, 2 = Diag score
$model = [TLModel]::new('ShapeEncoder', 'Encodes HBar/VBar/Diag shapes from Shapes2D (16→8→3)')
# Layer 1 weights (8 × 16)
$W1 = @(0.0) * (16 * 8)
$B1 = @(0.0) * 8
# Unit 0: top row detector (pixels 0-3)
for ($i = 0; $i -lt 4; $i++) { $W1[0 * 16 + $i] = 1.0 }
# Unit 1: bottom row detector (pixels 12-15)
for ($i = 0; $i -lt 4; $i++) { $W1[1 * 16 + 12 + $i] = 1.0 }
# Unit 2: left column detector (pixels 0,4,8,12)
for ($r = 0; $r -lt 4; $r++) { $W1[2 * 16 + $r * 4] = 1.0 }
# Unit 3: right column detector (pixels 3,7,11,15)
for ($r = 0; $r -lt 4; $r++) { $W1[3 * 16 + $r*4+3] = 1.0 }
# Unit 4: main diagonal (pixels 0,5,10,15)
$diag = @(0,5,10,15)
for ($i = 0; $i -lt 4; $i++) { $W1[4 * 16 + $diag[$i]] = 1.0 }
# Unit 5: anti-diagonal (pixels 3,6,9,12)
$anti = @(3,6,9,12)
for ($i = 0; $i -lt 4; $i++) { $W1[5 * 16 + $anti[$i]] = 1.0 }
# Unit 6: overall density (all pixels positive)
for ($i = 0; $i -lt 16; $i++) { $W1[6 * 16 + $i] = 0.25 }
# Unit 7: sparsity detector (all pixels negative — fires for sparse)
for ($i = 0; $i -lt 16; $i++) { $W1[7 * 16 + $i] = -0.25 }
$B1[7] = 2.0 # bias so it fires when few pixels active
$layer1 = New-TLLayer -InSize 16 -OutSize 8 -Activation 'relu' -Seed 1
$layer1.W = $W1
$layer1.B = $B1
$model.AddLayer($layer1)
# Layer 2 weights (3 × 8)
# HBar ← top-row (unit 0) + bottom-row (unit 1), suppress col/diag
# VBar ← left-col (unit 2) + right-col (unit 3), suppress row/diag
# Diag ← main-diag (unit 4) + anti-diag (unit 5), suppress row/col
$W2 = @(
2.0, 2.0,-1.0,-1.0,-1.0,-1.0, 0.0, 0.0, # HBar
-1.0,-1.0, 2.0, 2.0,-1.0,-1.0, 0.0, 0.0, # VBar
-1.0,-1.0,-1.0,-1.0, 2.0, 2.0, 0.0, 0.0 # Diag
)
$B2 = @(0.0, 0.0, 0.0)
$layer2 = New-TLLayer -InSize 8 -OutSize 3 -Activation 'softmax' -Seed 2
$layer2.W = $W2
$layer2.B = $B2
$model.AddLayer($layer2)
return $model
}
elseif ($Name -eq 'PatternFilter') {
# ── PatternFilter: 16 → 4 ────────────────────────────────────────────
# Detects spatial frequency characteristics of 4×4 patterns:
# Unit 0: low-freq horizontal (broad horizontal bands)
# Unit 1: low-freq vertical (broad vertical bands)
# Unit 2: high-freq checker (checkerboard / diagonal texture)
# Unit 3: overall brightness (total pixel density)
$model = [TLModel]::new('PatternFilter', 'Spatial frequency detector on 4x4 grids (16→4)')
$W = @(0.0) * (16 * 4)
$B = @(0.0) * 4
# Unit 0: low-freq horizontal — top half positive, bottom half negative
for ($r = 0; $r -lt 4; $r++) {
$sign = if ($r -lt 2) { 1.0 } else { -1.0 }
for ($c = 0; $c -lt 4; $c++) {
$W[0 * 16 + $r * 4 + $c] = $sign * 0.5
}
}
# Unit 1: low-freq vertical — left half positive, right half negative
for ($r = 0; $r -lt 4; $r++) {
for ($c = 0; $c -lt 4; $c++) {
$sign = if ($c -lt 2) { 1.0 } else { -1.0 }
$W[1 * 16 + $r * 4 + $c] = $sign * 0.5
}
}
# Unit 2: high-freq checker — alternating +/- in checkerboard pattern
for ($r = 0; $r -lt 4; $r++) {
for ($c = 0; $c -lt 4; $c++) {
$sign = if (($r + $c) % 2 -eq 0) { 1.0 } else { -1.0 }
$W[2 * 16 + $r * 4 + $c] = $sign * 0.5
}
}
# Unit 3: overall brightness — all positive
for ($i = 0; $i -lt 16; $i++) { $W[3 * 16 + $i] = 0.25 }
$layer = New-TLLayer -InSize 16 -OutSize 4 -Activation 'relu' -Seed 3
$layer.W = $W
$layer.B = $B
$model.AddLayer($layer)
return $model
}
else {
throw "Unknown model '$Name'. Run Get-VBAFModelZoo to see available models."
}
}
# ==============================================================================
# LAYER FREEZING
# ==============================================================================
function Set-VBAFLayerFrozen {
<#
.SYNOPSIS
Freeze or unfreeze a specific layer in a TLModel.
Frozen layers skip weight updates during fine-tuning (but still compute gradients).
.PARAMETER Model TLModel instance
.PARAMETER LayerIndex 0-based layer index
.PARAMETER Frozen $true to freeze, $false to unfreeze
.EXAMPLE
$m = Get-VBAFPretrainedModel -Name "ShapeEncoder"
Set-VBAFLayerFrozen -Model $m -LayerIndex 0 -Frozen $true # freeze layer 0
Set-VBAFLayerFrozen -Model $m -LayerIndex 1 -Frozen $false # unfreeze layer 1
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)][int] $LayerIndex,
[Parameter(Mandatory)][bool] $Frozen
)
if ($LayerIndex -lt 0 -or $LayerIndex -ge $Model.Layers.Count) {
throw "LayerIndex $LayerIndex out of range (model has $($Model.Layers.Count) layers)"
}
$lyr = [hashtable]$Model.Layers[$LayerIndex]
$lyr.Frozen = $Frozen
$status = if ($Frozen) { '❄ frozen' } else { '▶ trainable' }
Write-Host (" Layer {0} ({1}→{2}): {3}" -f $LayerIndex, $lyr.InSize, $lyr.OutSize, $status) -ForegroundColor Cyan
}
function Set-VBAFAllLayersFrozen {
<#
.SYNOPSIS Freeze or unfreeze ALL layers in a model at once.
.EXAMPLE
Set-VBAFAllLayersFrozen -Model $m -Frozen $true # freeze everything
Set-VBAFAllLayersFrozen -Model $m -Frozen $false # unfreeze everything
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)][bool] $Frozen
)
for ($li = 0; $li -lt $Model.Layers.Count; $li++) {
$lyr = [hashtable]$Model.Layers[$li]
$lyr.Frozen = $Frozen
}
$status = if ($Frozen) { '❄ ALL frozen' } else { '▶ ALL trainable' }
Write-Host (" {0} ({1} layers)" -f $status, $Model.Layers.Count) -ForegroundColor Cyan
}
function Get-VBAFFrozenLayers {
<#
.SYNOPSIS Return indices of all frozen layers.
.OUTPUTS int[] (comma-protected)
#>
param([Parameter(Mandatory)][TLModel]$Model)
$result = [System.Collections.ArrayList]::new()
for ($li = 0; $li -lt $Model.Layers.Count; $li++) {
$lyr = [hashtable]$Model.Layers[$li]
if ($lyr.Frozen) { $result.Add($li) | Out-Null }
}
return ,$result
}
function Get-VBAFTrainableLayers {
<#
.SYNOPSIS Return indices of all trainable (unfrozen) layers.
.OUTPUTS ArrayList of int (comma-protected)
#>
param([Parameter(Mandatory)][TLModel]$Model)
$result = [System.Collections.ArrayList]::new()
for ($li = 0; $li -lt $Model.Layers.Count; $li++) {
$lyr = [hashtable]$Model.Layers[$li]
if (-not $lyr.Frozen) { $result.Add($li) | Out-Null }
}
return ,$result
}
function Show-VBAFModelStatus {
<#
.SYNOPSIS Print a layer-by-layer summary showing frozen/trainable status.
.EXAMPLE
Show-VBAFModelStatus -Model $m
#>
param([Parameter(Mandatory)][TLModel]$Model)
Write-Host ''
Write-Host (" ── {0} ──" -f $Model.Name) -ForegroundColor Magenta
Write-Host (" {0}" -f $Model.Description) -ForegroundColor DarkGray
Write-Host ''
Write-Host ' Idx Shape Activation Status Params' -ForegroundColor DarkGray
Write-Host ' ─────────────────────────────────────────────────' -ForegroundColor DarkGray
$totalParams = 0
$trainableParams = 0
for ($li = 0; $li -lt $Model.Layers.Count; $li++) {
$lyr = [hashtable]$Model.Layers[$li]
$params = [int]$lyr.InSize * [int]$lyr.OutSize + [int]$lyr.OutSize
$totalParams += $params
$status = if ($lyr.Frozen) { '❄ frozen ' } else { '▶ trainable' }
if (-not $lyr.Frozen) { $trainableParams += $params }
$color = if ($lyr.Frozen) { 'DarkCyan' } else { 'Green' }
Write-Host (" {0,3} {1,4}→{2,-4} {3,-10} {4} {5,6}" -f `
$li, $lyr.InSize, $lyr.OutSize, $lyr.Activation, $status, $params) -ForegroundColor $color
}
Write-Host ' ─────────────────────────────────────────────────' -ForegroundColor DarkGray
Write-Host (" Total params: {0} Trainable: {1} Frozen: {2}" -f `
$totalParams, $trainableParams, ($totalParams - $trainableParams)) -ForegroundColor DarkGray
Write-Host ''
}
# ==============================================================================
# FEATURE EXTRACTION
# ==============================================================================
function Get-VBAFFeatures {
<#
.SYNOPSIS
Run a forward pass and return activations at a specified layer depth.
Use this to treat a frozen pretrained encoder as a feature extractor.
.DESCRIPTION
This is the core of transfer learning:
1. Load pretrained model
2. Freeze all layers
3. Run Get-VBAFFeatures to get rich representations
4. Feed those representations to a new trainable head
.PARAMETER Model TLModel instance
.PARAMETER X Input vector [double[]]
.PARAMETER FromLayer Layer index whose OUTPUT to return (0-based)
-1 = return final output (default)
.OUTPUTS [double[]] activation vector at the requested depth (comma-protected)
.EXAMPLE
$m = Get-VBAFPretrainedModel -Name "EdgeDetector"
$feats = Get-VBAFFeatures -Model $m -X $ds.X[0] -FromLayer 0
# $feats is the 8-dim edge feature vector
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)][double[]]$X,
[int]$FromLayer = -1
)
if ($FromLayer -lt 0) { $FromLayer = $Model.Layers.Count - 1 }
$feats = [double[]]$Model.ForwardTo($X, $FromLayer)
return ,$feats
}
function Get-VBAFBatchFeatures {
<#
.SYNOPSIS
Extract features for an entire dataset. Returns ArrayList of [double[]].
.PARAMETER Model TLModel instance
.PARAMETER Data ArrayList of [double[]] samples
.PARAMETER FromLayer Layer index to extract from (-1 = last layer)
.OUTPUTS ArrayList of [double[]] (comma-protected)
.EXAMPLE
$m = Get-VBAFPretrainedModel -Name "ShapeEncoder"
$feats = Get-VBAFBatchFeatures -Model $m -Data $ds.X -FromLayer 0
# $feats[0] is the 8-dim feature vector for sample 0
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)]$Data,
[int]$FromLayer = -1
)
$results = [System.Collections.ArrayList]::new()
for ($si = 0; $si -lt $Data.Count; $si++) {
$x = [double[]]$Data[$si]
$feats = [double[]](Get-VBAFFeatures -Model $Model -X $x -FromLayer $FromLayer)
$results.Add($feats) | Out-Null
}
return ,$results
}
# ==============================================================================
# FINE-TUNING
# ==============================================================================
function Get-TLCrossEntropyLoss {
<#
.SYNOPSIS Scalar cross-entropy loss for classification.
L = -Σ label[i] * log(pred[i] + ε)
#>
param([double[]]$Pred, [int]$TrueClass)
$eps = 1e-10
$p = [Math]::Max($eps, [double]$Pred[$TrueClass])
return (-[Math]::Log($p))
}
function Get-TLCrossEntropyGrad {
<#
.SYNOPSIS Gradient of cross-entropy w.r.t. softmax output.
dL/dPred[i] = Pred[i] - 1(i == TrueClass)
(Combined softmax + cross-entropy gradient — numerically clean)
.OUTPUTS [double[]] (comma-protected)
#>
param([double[]]$Pred, [int]$TrueClass)
$n = $Pred.Length
$dL = [double[]]::new($n)
for ($i = 0; $i -lt $n; $i++) {
$dL[$i] = [double]$Pred[$i]
}
$dL[$TrueClass] -= 1.0
return ,$dL
}
function Get-TLMSELoss {
<#
.SYNOPSIS MSE loss for reconstruction tasks during fine-tuning.
#>
param([double[]]$Pred, [double[]]$Target)
$sum = 0.0; $n = $Pred.Length
for ($i = 0; $i -lt $n; $i++) {
$d = [double]$Pred[$i] - [double]$Target[$i]; $sum += $d * $d
}
return ($sum / $n)
}
function Get-TLMSEGrad {
<#
.SYNOPSIS Gradient of MSE w.r.t. predictions.
.OUTPUTS [double[]] (comma-protected)
#>
param([double[]]$Pred, [double[]]$Target)
$n = $Pred.Length; $dL = [double[]]::new($n)
for ($i = 0; $i -lt $n; $i++) {
$dL[$i] = 2.0 * ([double]$Pred[$i] - [double]$Target[$i]) / $n
}
return ,$dL
}
function Invoke-VBAFFineTune {
<#
.SYNOPSIS
Fine-tune a TLModel using backprop only through UNFROZEN layers.
Frozen layers still compute gradients (needed to pass gradient backward)
but their weights are NOT updated.
.DESCRIPTION
Typical transfer learning workflow:
1. Get-VBAFPretrainedModel load pretrained weights
2. Set-VBAFAllLayersFrozen $true freeze everything
3. Set-VBAFLayerFrozen $m -LayerIndex N -Frozen $false unfreeze head
4. Invoke-VBAFFineTune train only the head
Loss function:
Classification (Labels provided) → cross-entropy
Reconstruction (Targets provided) → MSE
.PARAMETER Model TLModel to fine-tune
.PARAMETER Data ArrayList of [double[]] input samples
.PARAMETER Labels ArrayList of [int] class labels (classification)
.PARAMETER Targets ArrayList of [double[]] target vectors (reconstruction)
.PARAMETER Epochs Training epochs (default 50)
.PARAMETER LR Learning rate (default 0.01)
.PARAMETER PrintEvery Print every N epochs. 0 = silent. (default 10)
.OUTPUTS Hashtable: FinalLoss, LossHistory, Model, Accuracy
.EXAMPLE
$m = Get-VBAFPretrainedModel -Name "ShapeEncoder"
Set-VBAFAllLayersFrozen -Model $m -Frozen $true
Set-VBAFLayerFrozen -Model $m -LayerIndex 1 -Frozen $false
$r = Invoke-VBAFFineTune -Model $m -Data $ds.X -Labels $ds.Labels -Epochs 50 -LR 0.05
$r.FinalLoss
$r.Accuracy
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)]$Data,
$Labels = $null,
$Targets = $null,
[int] $Epochs = 50,
[double]$LR = 0.01,
[int] $PrintEvery = 10
)
if ($null -eq $Labels -and $null -eq $Targets) {
throw "Provide either -Labels (classification) or -Targets (reconstruction)"
}
$isClassification = ($null -ne $Labels)
$nSamples = $Data.Count
$lossHistory = [System.Collections.ArrayList]::new()
# Count trainable params for display
$trainable = Get-VBAFTrainableLayers -Model $Model
Write-Host (" Fine-tuning [{0}] {1} trainable layer(s), {2} frozen" -f `
$Model.Name, $trainable.Count, ($Model.Layers.Count - $trainable.Count)) -ForegroundColor Magenta
$taskLabel = if ($isClassification) {'Classification'} else {'Reconstruction'}
Write-Host (" Task: {0} Samples: {1} Epochs: {2} LR: {3}" -f `
$taskLabel, $nSamples, $Epochs, $LR) -ForegroundColor DarkGray
Write-Host ''
for ($ep = 1; $ep -le $Epochs; $ep++) {
$epochLoss = 0.0
$epochCorrect = 0
for ($si = 0; $si -lt $nSamples; $si++) {
$x = [double[]]$Data[$si]
$pred = [double[]]$Model.Forward($x)
if ($isClassification) {
$lbl = [int]$Labels[$si]
$epochLoss += Get-TLCrossEntropyLoss -Pred $pred -TrueClass $lbl
$dLoss = [double[]](Get-TLCrossEntropyGrad -Pred $pred -TrueClass $lbl)
# Accuracy: argmax of prediction
$maxIdx = 0; $maxVal = [double]$pred[0]
for ($k = 1; $k -lt $pred.Length; $k++) {
if ([double]$pred[$k] -gt $maxVal) { $maxVal = [double]$pred[$k]; $maxIdx = $k }
}
if ($maxIdx -eq $lbl) { $epochCorrect++ }
} else {
$tgt = [double[]]$Targets[$si]
$epochLoss += Get-TLMSELoss -Pred $pred -Target $tgt
$dLoss = [double[]](Get-TLMSEGrad -Pred $pred -Target $tgt)
}
$Model.Backward($dLoss, $LR)
}
$avgLoss = $epochLoss / $nSamples
$lossHistory.Add($avgLoss) | Out-Null
if ($PrintEvery -gt 0 -and ($ep -eq 1 -or $ep % $PrintEvery -eq 0)) {
$acc = if ($isClassification) { ' Acc: {0:P0}' -f ($epochCorrect / $nSamples) } else { '' }
$color = if ($avgLoss -lt 0.5) { 'Green' } elseif ($avgLoss -lt 1.0) { 'Yellow' } else { 'Red' }
Write-Host (" Epoch {0,4}/{1} Loss: {2:F5}{3}" -f $ep, $Epochs, $avgLoss, $acc) -ForegroundColor $color
}
}
$finalLoss = [double]$lossHistory[$lossHistory.Count - 1]
# Final accuracy pass
$finalCorrect = 0
if ($isClassification) {
for ($si = 0; $si -lt $nSamples; $si++) {
$x = [double[]]$Data[$si]
$pred = [double[]]$Model.Forward($x)
$lbl = [int]$Labels[$si]
$maxIdx = 0; $maxVal = [double]$pred[0]
for ($k = 1; $k -lt $pred.Length; $k++) {
if ([double]$pred[$k] -gt $maxVal) { $maxVal = [double]$pred[$k]; $maxIdx = $k }
}
if ($maxIdx -eq $lbl) { $finalCorrect++ }
}
}
$finalAcc = if ($isClassification) { $finalCorrect / $nSamples } else { 0.0 }
Write-Host ''
Write-Host (" ── Fine-tune complete ── Loss: {0:F5} Acc: {1:P0}" -f $finalLoss, $finalAcc) -ForegroundColor Magenta
Write-Host ''
return @{
FinalLoss = $finalLoss
LossHistory = $lossHistory
Accuracy = $finalAcc
Model = $Model
}
}
# ==============================================================================
# WEIGHT SAVE / LOAD (JSON, compatible with VBAF.ML.CNN.ps1 pattern)
# ==============================================================================
function Save-VBAFTLWeights {
<#
.SYNOPSIS
Save TLModel weights to JSON.
Format compatible with VBAF.ML.CNN.ps1 Save-CNNWeights pattern.
.PARAMETER Model TLModel to save
.PARAMETER Path File path for JSON output
.EXAMPLE
Save-VBAFTLWeights -Model $m -Path "C:\Temp\ShapeEncoder.json"
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)][string]$Path
)
$layerData = [System.Collections.ArrayList]::new()
for ($li = 0; $li -lt $Model.Layers.Count; $li++) {
$lyr = [hashtable]$Model.Layers[$li]
$layerData.Add(@{
Index = $li
InSize = $lyr.InSize
OutSize = $lyr.OutSize
Activation = $lyr.Activation
Frozen = $lyr.Frozen
W = $lyr.W
B = $lyr.B
}) | Out-Null
}
$payload = @{
ModelName = $Model.Name
Description = $Model.Description
NumLayers = $Model.Layers.Count
SavedAt = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
Layers = $layerData
}
$payload | ConvertTo-Json -Depth 10 | Out-File -FilePath $Path -Encoding UTF8
Write-Host (" ✅ Saved [{0}] → {1}" -f $Model.Name, $Path) -ForegroundColor Green
}
function Load-VBAFTLWeights {
<#
.SYNOPSIS
Load TLModel weights from JSON. Returns a TLModel.
.PARAMETER Path Path to JSON file saved by Save-VBAFTLWeights
.OUTPUTS TLModel with weights restored
.EXAMPLE
$m = Load-VBAFTLWeights -Path "C:\Temp\ShapeEncoder.json"
$m.ToString()
#>
param([Parameter(Mandatory)][string]$Path)
if (-not (Test-Path $Path)) { throw "File not found: $Path" }
$raw = Get-Content $Path -Raw | ConvertFrom-Json
$model = [TLModel]::new($raw.ModelName, $raw.Description)
for ($li = 0; $li -lt $raw.Layers.Count; $li++) {
$ld = $raw.Layers[$li]
$layer = New-TLLayer -InSize $ld.InSize -OutSize $ld.OutSize -Activation $ld.Activation -Seed 1
# Restore weights — must use index assignment on hashtable array ✅
$wArr = $ld.W
for ($i = 0; $i -lt $wArr.Count; $i++) { $layer.W[$i] = [double]$wArr[$i] }
$bArr = $ld.B
for ($i = 0; $i -lt $bArr.Count; $i++) { $layer.B[$i] = [double]$bArr[$i] }
$layer.Frozen = [bool]$ld.Frozen
$model.AddLayer($layer)
}
Write-Host (" ✅ Loaded [{0}] ← {1}" -f $model.Name, $Path) -ForegroundColor Green
return $model
}
# ==============================================================================
# EVALUATION HELPER
# ==============================================================================
function Test-VBAFTLClassify {
<#
.SYNOPSIS
Run classification on a dataset and print per-class accuracy.
.PARAMETER Model TLModel (output layer should be softmax)
.PARAMETER Data ArrayList of [double[]]
.PARAMETER Labels ArrayList of [int]
.PARAMETER ClassNames string[] for display
.OUTPUTS Hashtable: Accuracy, CorrectCount, ConfusionMatrix
.EXAMPLE
$r = Test-VBAFTLClassify -Model $m -Data $ds.X -Labels $ds.Labels -ClassNames $ds.ClassNames
$r.Accuracy
#>
param(
[Parameter(Mandatory)][TLModel]$Model,
[Parameter(Mandatory)]$Data,