-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeeplearn_numpy.py
More file actions
1523 lines (1335 loc) · 55.7 KB
/
deeplearn_numpy.py
File metadata and controls
1523 lines (1335 loc) · 55.7 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
# deeplearn_numpy.py
# NumPy-only, parametric deep learning mini-framework in a single file.
# - Supports configurable layers via registries and a JSON-like config
# - Core layers: Dense, Conv2D (im2col), MaxPool2D, AvgPool2D, Flatten, Dropout, Activations, BatchNorm (1D/2D)
# - Losses: MSE, CrossEntropy (with stable softmax), BinaryCrossEntropy
# - Optimizers: SGD, Momentum, Adam
# - Model: Sequential builder from config; training loop (fit/evaluate/predict)
# - Regularization: L1/L2
# - Serialization: save/load weights; save/load config
# - Metrics: accuracy (multi-class), binary_accuracy, top_k_accuracy
#
# Usage (example at bottom of file):
# from deeplearn_numpy import Model, build_model_from_config
# model = build_model_from_config(config)
# model.fit(x_train, y_train, epochs=5, batch_size=32)
#
# This file is self-contained and uses only NumPy.
from __future__ import annotations
import numpy as np
from typing import Any, Dict, List, Tuple, Optional, Iterable, Callable
# ------------------------------------------------------------
# Utility: RNG and dtypes
# ------------------------------------------------------------
def _get_rng(seed: Optional[int]) -> np.random.Generator:
return np.random.default_rng(seed) if seed is not None else np.random.default_rng()
DEFAULT_DTYPE = np.float32
# ------------------------------------------------------------
# Parameter wrapper
# ------------------------------------------------------------
class Parameter:
def __init__(self, data: np.ndarray, name: Optional[str] = None):
self.data = data.astype(DEFAULT_DTYPE, copy=False)
self.grad = np.zeros_like(self.data)
self.name = name
def zero_grad(self):
self.grad[...] = 0
# ------------------------------------------------------------
# Initializers (registry + functions)
# ------------------------------------------------------------
INITIALIZER_REGISTRY: Dict[str, Callable[[Tuple[int, ...], Optional[int]], np.ndarray]] = {}
def register_initializer(name: str):
def deco(fn):
INITIALIZER_REGISTRY[name] = fn
return fn
return deco
@register_initializer("zeros")
def init_zeros(shape: Tuple[int, ...], seed: Optional[int] = None):
return np.zeros(shape, dtype=DEFAULT_DTYPE)
@register_initializer("ones")
def init_ones(shape: Tuple[int, ...], seed: Optional[int] = None):
return np.ones(shape, dtype=DEFAULT_DTYPE)
@register_initializer("random_normal")
def init_random_normal(shape: Tuple[int, ...], seed: Optional[int] = None):
rng = _get_rng(seed)
return rng.standard_normal(size=shape).astype(DEFAULT_DTYPE)
@register_initializer("glorot_uniform")
def init_glorot_uniform(shape: Tuple[int, ...], seed: Optional[int] = None):
# fan_in, fan_out heuristic (for Dense/Conv2D weights)
if len(shape) == 2:
fan_in, fan_out = shape[0], shape[1]
elif len(shape) == 4: # (out_ch, in_ch, kH, kW)
fan_in = shape[1] * shape[2] * shape[3]
fan_out = shape[0] * shape[2] * shape[3]
else:
fan_in = int(np.prod(shape[:-1])) if len(shape) > 1 else shape[0]
fan_out = shape[-1]
limit = np.sqrt(6.0 / (fan_in + fan_out))
rng = _get_rng(seed)
return rng.uniform(-limit, limit, size=shape).astype(DEFAULT_DTYPE)
@register_initializer("he_normal")
def init_he_normal(shape: Tuple[int, ...], seed: Optional[int] = None):
# Good for ReLU
if len(shape) == 2:
fan_in = shape[0]
elif len(shape) == 4:
fan_in = shape[1] * shape[2] * shape[3]
else:
fan_in = int(np.prod(shape[:-1])) if len(shape) > 1 else shape[0]
std = np.sqrt(2.0 / fan_in)
rng = _get_rng(seed)
return (rng.standard_normal(size=shape) * std).astype(DEFAULT_DTYPE)
def get_initializer(name: str) -> Callable[[Tuple[int, ...], Optional[int]], np.ndarray]:
if name not in INITIALIZER_REGISTRY:
raise ValueError(f"Unknown initializer: {name}")
return INITIALIZER_REGISTRY[name]
# ------------------------------------------------------------
# Activations as layers (registry-based)
# ------------------------------------------------------------
class Layer:
def __init__(self):
self.built = False
self.training = True # toggled by model
self._params: List[Parameter] = []
self._cache: Dict[str, Any] = {}
self.name: Optional[str] = None
def build(self, input_shape: Tuple[int, ...], **kwargs) -> Tuple[int, ...]:
self.built = True
return input_shape
def forward(self, x: np.ndarray) -> np.ndarray:
raise NotImplementedError
def backward(self, dy: np.ndarray) -> np.ndarray:
raise NotImplementedError
def params(self) -> Iterable[Parameter]:
return self._params
def zero_grads(self):
for p in self._params:
p.zero_grad()
def set_training(self, mode: bool):
self.training = mode
# Optional: for shape inference without allocating params
def infer_output_shape(self, input_shape: Tuple[int, ...]) -> Tuple[int, ...]:
# default: identity
return input_shape
# Activation registry
ACTIVATION_REGISTRY: Dict[str, "Layer"] = {}
LAYER_REGISTRY: Dict[str, Callable[..., Layer]] = {}
def register_layer(name: str):
def deco(cls):
LAYER_REGISTRY[name] = cls
return cls
return deco
def register_activation(name: str):
def deco(cls):
ACTIVATION_REGISTRY[name] = cls
return cls
return deco
# ---- Activation implementations ----
@register_activation("relu")
@register_layer("ReLU")
class ReLU(Layer):
def forward(self, x):
self._cache["mask"] = x > 0
return np.where(self._cache["mask"], x, 0).astype(DEFAULT_DTYPE)
def backward(self, dy):
return dy * self._cache["mask"]
@register_activation("sigmoid")
@register_layer("Sigmoid")
class Sigmoid(Layer):
def forward(self, x):
out = 1.0 / (1.0 + np.exp(-x))
self._cache["out"] = out
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
out = self._cache["out"]
return dy * out * (1.0 - out)
@register_activation("tanh")
@register_layer("Tanh")
class Tanh(Layer):
def forward(self, x):
out = np.tanh(x)
self._cache["out"] = out
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
out = self._cache["out"]
return dy * (1.0 - out ** 2)
@register_activation("leaky_relu")
@register_layer("LeakyReLU")
class LeakyReLU(Layer):
def __init__(self, alpha: float = 0.01):
super().__init__()
self.alpha = alpha
def forward(self, x):
self._cache["x"] = x
return np.where(x > 0, x, self.alpha * x).astype(DEFAULT_DTYPE)
def backward(self, dy):
x = self._cache["x"]
dx = np.ones_like(x)
dx[x < 0] = self.alpha
return dy * dx
@register_activation("softmax")
@register_layer("Softmax")
class Softmax(Layer):
def __init__(self, axis: int = -1):
super().__init__()
self.axis = axis
def forward(self, x):
# stable softmax
x_shift = x - np.max(x, axis=self.axis, keepdims=True)
exps = np.exp(x_shift)
sums = np.sum(exps, axis=self.axis, keepdims=True)
out = exps / sums
self._cache["out"] = out
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
# General softmax backward: J = diag(s) - s s^T; dy @ J
s = self._cache["out"]
# Works for 2D (batch, classes) or higher dims along axis
# Flatten axis to -1 for computation
orig_shape = s.shape
axis = self.axis if self.axis >= 0 else s.ndim + self.axis
s2d = s.reshape(-1, orig_shape[axis])
dy2d = dy.reshape(-1, orig_shape[axis])
# gradient per row: dy - sum(dy*s) * s
dot = np.sum(dy2d * s2d, axis=1, keepdims=True)
dx2d = s2d * (dy2d - dot)
return dx2d.reshape(orig_shape)
# ------------------------------------------------------------
# Core layers
# ------------------------------------------------------------
@register_layer("Dense")
class Dense(Layer):
def __init__(self, units: int, use_bias: bool = True,
kernel_initializer: str = "glorot_uniform",
bias_initializer: str = "zeros",
seed: Optional[int] = None):
super().__init__()
self.units = units
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.seed = seed
def build(self, input_shape: Tuple[int, ...], **kwargs) -> Tuple[int, ...]:
assert len(input_shape) == 2, "Dense expects (batch, features)"
in_dim = input_shape[1]
W = get_initializer(self.kernel_initializer)((in_dim, self.units), seed=self.seed)
self.W = Parameter(W, name="kernel")
self._params.append(self.W)
if self.use_bias:
b = get_initializer(self.bias_initializer)((self.units,), seed=self.seed)
self.b = Parameter(b, name="bias")
self._params.append(self.b)
else:
self.b = None
self.built = True
return (input_shape[0], self.units)
def forward(self, x):
self._cache["x"] = x
out = x @ self.W.data
if self.b is not None:
out = out + self.b.data
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
x = self._cache["x"]
# grads
self.W.grad += x.T @ dy
if self.b is not None:
self.b.grad += np.sum(dy, axis=0)
dx = dy @ self.W.data.T
return dx
# ---- im2col/col2im utilities ----
def get_output_dim(in_size, k, stride, pad, dilation):
# For 'valid' pad: pad=0; for 'same': computed elsewhere
return (in_size + 2*pad - dilation*(k-1) - 1)//stride + 1
def compute_same_padding(in_size, k, stride, dilation):
# Output size ceil(in/stride); padding to make that
out_size = int(np.ceil(in_size / stride))
pad_needed = max(0, (out_size - 1)*stride + dilation*(k - 1) + 1 - in_size)
# split pad into left/right (we'll use symmetric here)
pad_left = pad_needed // 2
pad_right = pad_needed - pad_left
return pad_left, pad_right, out_size
def im2col_indices(x, kH, kW, pad_h, pad_w, stride_h, stride_w, dilation_h=1, dilation_w=1):
# x: (N, C, H, W)
N, C, H, W = x.shape
eff_kH = dilation_h*(kH-1)+1
eff_kW = dilation_w*(kW-1)+1
out_h = (H + pad_h*2 - eff_kH)//stride_h + 1
out_w = (W + pad_w*2 - eff_kW)//stride_w + 1
x_padded = np.pad(x, ((0,0),(0,0),(pad_h,pad_h),(pad_w,pad_w)), mode='constant')
i0 = np.repeat(np.arange(kH), kW)
i0 = np.tile(i0, C)
i1 = stride_h * np.repeat(np.arange(out_h), out_w)
j0 = np.tile(np.arange(kW), kH * C)
j1 = stride_w * np.tile(np.arange(out_w), out_h)
i = i0.reshape(-1,1) * dilation_h + i1.reshape(1,-1)
j = j0.reshape(-1,1) * dilation_w + j1.reshape(1,-1)
k = np.repeat(np.arange(C), kH * kW).reshape(-1,1)
cols = x_padded[:, k, i, j] # (N, C*kH*kW, out_h*out_w)
cols = cols.transpose(1,2,0).reshape(C*kH*kW, N*out_h*out_w)
return cols, out_h, out_w
def col2im_indices(cols, x_shape, kH, kW, pad_h, pad_w, stride_h, stride_w, dilation_h=1, dilation_w=1):
N, C, H, W = x_shape
eff_kH = dilation_h*(kH-1)+1
eff_kW = dilation_w*(kW-1)+1
out_h = (H + pad_h*2 - eff_kH)//stride_h + 1
out_w = (W + pad_w*2 - eff_kW)//stride_w + 1
cols_reshaped = cols.reshape(C*kH*kW, N, out_h*out_w).transpose(1,0,2)
x_padded = np.zeros((N, C, H + 2*pad_h, W + 2*pad_w), dtype=cols.dtype)
i0 = np.repeat(np.arange(kH), kW)
i0 = np.tile(i0, C)
i1 = stride_h * np.repeat(np.arange(out_h), out_w)
j0 = np.tile(np.arange(kW), kH * C)
j1 = stride_w * np.tile(np.arange(out_w), out_h)
i = i0.reshape(-1,1) * dilation_h + i1.reshape(1,-1)
j = j0.reshape(-1,1) * dilation_w + j1.reshape(1,-1)
k = np.repeat(np.arange(C), kH * kW).reshape(-1,1)
for n in range(N):
x_padded[n, k, i, j] += cols_reshaped[n]
return x_padded[:, :, pad_h:pad_h+H, pad_w:pad_w+W]
@register_layer("Conv2D")
class Conv2D(Layer):
def __init__(self, filters: int, kernel_size: int | Tuple[int,int],
stride: int | Tuple[int,int] = 1,
padding: str | int | Tuple[int,int] = "valid",
dilation: int | Tuple[int,int] = 1,
use_bias: bool = True,
kernel_initializer: str = "he_normal",
bias_initializer: str = "zeros",
seed: Optional[int] = None):
super().__init__()
self.filters = filters
if isinstance(kernel_size, int):
self.kH = self.kW = kernel_size
else:
self.kH, self.kW = kernel_size
if isinstance(stride, int):
self.sH = self.sW = stride
else:
self.sH, self.sW = stride
self.padding = padding
if isinstance(dilation, int):
self.dH = self.dW = dilation
else:
self.dH, self.dW = dilation
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.seed = seed
def _compute_padding(self, H, W):
if self.padding == "valid":
return (0,0,0,0)
if self.padding == "same":
ph1, ph2, out_h = compute_same_padding(H, self.kH, self.sH, self.dH)
pw1, pw2, out_w = compute_same_padding(W, self.kW, self.sW, self.dW)
# symmetric padding; using total pads
return (ph1, pw1, ph2, pw2)
if isinstance(self.padding, int):
return (self.padding, self.padding, self.padding, self.padding)
if isinstance(self.padding, tuple):
pH, pW = self.padding
return (pH, pW, pH, pW)
raise ValueError("Unsupported padding")
def build(self, input_shape: Tuple[int, ...], **kwargs) -> Tuple[int, ...]:
# input: (N, C, H, W)
assert len(input_shape) == 4, "Conv2D expects (N, C, H, W)"
N, C, H, W = input_shape
self.in_channels = C
pad_top, pad_left, pad_bottom, pad_right = self._compute_padding(H, W)
eff_kH = self.dH*(self.kH-1)+1
eff_kW = self.dW*(self.kW-1)+1
out_h = (H + pad_top + pad_bottom - eff_kH)//self.sH + 1
out_w = (W + pad_left + pad_right - eff_kW)//self.sW + 1
Wshape = (self.filters, C, self.kH, self.kW)
W = get_initializer(self.kernel_initializer)(Wshape, seed=self.seed)
self.W = Parameter(W, name="kernel")
self._params.append(self.W)
if self.use_bias:
b = get_initializer(self.bias_initializer)((self.filters,), seed=self.seed)
self.b = Parameter(b, name="bias")
self._params.append(self.b)
else:
self.b = None
self.pad = (pad_top, pad_left, pad_bottom, pad_right)
self.built = True
return (N, self.filters, out_h, out_w)
def forward(self, x):
self._cache["x_shape"] = x.shape
pt, pl, pb, pr = self.pad
cols, out_h, out_w = im2col_indices(
x, self.kH, self.kW, pt, pl, self.sH, self.sW, self.dH, self.dW
)
W_col = self.W.data.reshape(self.filters, -1)
out = W_col @ cols # (filters, N*out_h*out_w)
if self.b is not None:
out += self.b.data[:, None]
N = x.shape[0]
out = out.reshape(self.filters, -1, N).transpose(2, 0, 1)
out = out.reshape(N, self.filters, out_h, out_w)
self._cache["cols"] = cols
self._cache["out_shape"] = (N, self.filters, out_h, out_w)
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
cols = self._cache["cols"]
N, F, out_h, out_w = self._cache["out_shape"]
W_col = self.W.data.reshape(self.filters, -1)
dy_reshaped = dy.reshape(N, F, out_h*out_w).transpose(1,2,0).reshape(F, -1)
# gradients
self.W.grad += (dy_reshaped @ cols.T).reshape(self.W.data.shape)
if self.b is not None:
self.b.grad += np.sum(dy_reshaped, axis=1)
dcols = W_col.T @ dy_reshaped
pt, pl, pb, pr = self.pad
dx = col2im_indices(
dcols, self._cache["x_shape"], self.kH, self.kW, pt, pl, self.sH, self.sW, self.dH, self.dW
)
return dx
@register_layer("MaxPool2D")
class MaxPool2D(Layer):
def __init__(self, pool_size: int | Tuple[int,int] = 2, stride: Optional[int | Tuple[int,int]] = None, padding: str | int | Tuple[int,int] = "valid"):
super().__init__()
if isinstance(pool_size, int):
self.kH = self.kW = pool_size
else:
self.kH, self.kW = pool_size
if stride is None:
self.sH = self.kH
self.sW = self.kW
elif isinstance(stride, int):
self.sH = self.sW = stride
else:
self.sH, self.sW = stride
self.padding = padding
def _compute_padding(self, H, W):
if self.padding == "valid":
return (0,0,0,0)
if self.padding == "same":
ph1, ph2, _ = compute_same_padding(H, self.kH, self.sH, 1)
pw1, pw2, _ = compute_same_padding(W, self.kW, self.sW, 1)
return (ph1, pw1, ph2, pw2)
if isinstance(self.padding, int):
return (self.padding, self.padding, self.padding, self.padding)
if isinstance(self.padding, tuple):
pH, pW = self.padding
return (pH, pW, pH, pW)
raise ValueError("Unsupported padding")
def build(self, input_shape, **kwargs):
N, C, H, W = input_shape
pt, pl, pb, pr = self._compute_padding(H, W)
out_h = (H + pt + pb - self.kH)//self.sH + 1
out_w = (W + pl + pr - self.kW)//self.sW + 1
self.pad = (pt, pl, pb, pr)
self.built = True
return (N, C, out_h, out_w)
def forward(self, x):
N, C, H, W = x.shape
pt, pl, pb, pr = self.pad
x_padded = np.pad(x, ((0,0),(0,0),(pt,pb),(pl,pr)), mode='constant', constant_values=-np.inf)
out_h = (H + pt + pb - self.kH)//self.sH + 1
out_w = (W + pl + pr - self.kW)//self.sW + 1
self._cache["x_shape"] = x.shape
self._cache["pad"] = self.pad
out = np.empty((N, C, out_h, out_w), dtype=DEFAULT_DTYPE)
self._cache["max_idx"] = np.zeros_like(out, dtype=np.int32)
for i in range(out_h):
for j in range(out_w):
h0 = i*self.sH
w0 = j*self.sW
window = x_padded[:, :, h0:h0+self.kH, w0:w0+self.kW] # (N, C, kH, kW)
flat = window.reshape(N, C, -1)
idx = np.argmax(flat, axis=2)
self._cache["max_idx"][:, :, i, j] = idx
out[:, :, i, j] = np.take_along_axis(flat, idx[..., None], axis=2).squeeze(-1)
return out
def backward(self, dy):
N, C, H, W = self._cache["x_shape"]
pt, pl, pb, pr = self._cache["pad"]
out_h, out_w = dy.shape[2], dy.shape[3]
dx_padded = np.zeros((N, C, H + pt + pb, W + pl + pr), dtype=DEFAULT_DTYPE)
idx = self._cache["max_idx"]
for i in range(out_h):
for j in range(out_w):
h0 = i*self.sH
w0 = j*self.sW
mask = np.zeros((N, C, self.kH*self.kW), dtype=DEFAULT_DTYPE)
flat_idx = idx[:, :, i, j]
mask.reshape(N*C, -1)[np.arange(N*C), flat_idx.reshape(-1)] = dy[:, :, i, j].reshape(-1)
dx_padded[:, :, h0:h0+self.kH, w0:w0+self.kW] += mask.reshape(N, C, self.kH, self.kW)
return dx_padded[:, :, pt:pt+H, pl:pl+W]
@register_layer("AvgPool2D")
class AvgPool2D(Layer):
def __init__(self, pool_size: int | Tuple[int,int] = 2, stride: Optional[int | Tuple[int,int]] = None, padding: str | int | Tuple[int,int] = "valid"):
super().__init__()
if isinstance(pool_size, int):
self.kH = self.kW = pool_size
else:
self.kH, self.kW = pool_size
if stride is None:
self.sH = self.kH
self.sW = self.kW
elif isinstance(stride, int):
self.sH = self.sW = stride
else:
self.sH, self.sW = stride
self.padding = padding
def _compute_padding(self, H, W):
if self.padding == "valid":
return (0,0,0,0)
if self.padding == "same":
ph1, ph2, _ = compute_same_padding(H, self.kH, self.sH, 1)
pw1, pw2, _ = compute_same_padding(W, self.kW, self.sW, 1)
return (ph1, pw1, ph2, pw2)
if isinstance(self.padding, int):
return (self.padding, self.padding, self.padding, self.padding)
if isinstance(self.padding, tuple):
pH, pW = self.padding
return (pH, pW, pH, pW)
raise ValueError("Unsupported padding")
def build(self, input_shape, **kwargs):
N, C, H, W = input_shape
pt, pl, pb, pr = self._compute_padding(H, W)
out_h = (H + pt + pb - self.kH)//self.sH + 1
out_w = (W + pl + pr - self.kW)//self.sW + 1
self.pad = (pt, pl, pb, pr)
self.built = True
return (N, C, out_h, out_w)
def forward(self, x):
N, C, H, W = x.shape
pt, pl, pb, pr = self.pad
x_padded = np.pad(x, ((0,0),(0,0),(pt,pb),(pl,pr)), mode='constant', constant_values=0.0)
out_h = (H + pt + pb - self.kH)//self.sH + 1
out_w = (W + pl + pr - self.kW)//self.sW + 1
out = np.empty((N, C, out_h, out_w), dtype=DEFAULT_DTYPE)
for i in range(out_h):
for j in range(out_w):
h0 = i*self.sH
w0 = j*self.sW
window = x_padded[:, :, h0:h0+self.kH, w0:w0+self.kW]
out[:, :, i, j] = np.mean(window, axis=(2,3))
self._cache["x_shape"] = x.shape
self._cache["pad"] = self.pad
return out
def backward(self, dy):
N, C, H, W = self._cache["x_shape"]
pt, pl, pb, pr = self._cache["pad"]
out_h, out_w = dy.shape[2], dy.shape[3]
dx_padded = np.zeros((N, C, H + pt + pb, W + pl + pr), dtype=DEFAULT_DTYPE)
scale = 1.0 / (self.kH * self.kW)
for i in range(out_h):
for j in range(out_w):
h0 = i*self.sH
w0 = j*self.sW
dx_padded[:, :, h0:h0+self.kH, w0:w0+self.kW] += dy[:, :, i, j][:, :, None, None] * scale
return dx_padded[:, :, pt:pt+H, pl:pl+W]
@register_layer("Reshape")
class Reshape(Layer):
def __init__(self, target_shape: Tuple[int, ...]):
super().__init__()
self.target_shape = tuple(target_shape)
self.input_shape = None
def build(self, input_shape, **kwargs):
self.input_shape = input_shape[1:]
return (input_shape[0],) + self.target_shape
def forward(self, x):
self._cache["in_shape"] = x.shape
return x.reshape((x.shape[0],) + self.target_shape)
def backward(self, dy):
return dy.reshape(self._cache["in_shape"])
@register_layer("UpSample2D")
class UpSample2D(Layer):
def __init__(self, scale: int = 2):
super().__init__()
self.scale = scale
def build(self, input_shape, **kwargs):
N, C, H, W = input_shape
return (N, C, H * self.scale, W * self.scale)
def forward(self, x):
self._cache["in_shape"] = x.shape
return np.repeat(np.repeat(x, self.scale, axis=2), self.scale, axis=3)
def backward(self, dy):
scale = self.scale
return dy.reshape(
dy.shape[0], dy.shape[1],
dy.shape[2]//scale, scale,
dy.shape[3]//scale, scale
).mean(axis=(3,5))
@register_layer("LayerNorm")
class LayerNorm(Layer):
def __init__(self, eps: float = 1e-5, affine: bool = True):
super().__init__()
self.eps = eps
self.affine = affine
def build(self, input_shape, **kwargs):
self.normalized_shape = input_shape[1:]
if self.affine:
self.gamma = Parameter(np.ones(self.normalized_shape, dtype=DEFAULT_DTYPE), "gamma")
self.beta = Parameter(np.zeros(self.normalized_shape, dtype=DEFAULT_DTYPE), "beta")
self._params += [self.gamma, self.beta]
return input_shape
def forward(self, x):
mean = np.mean(x, axis=1, keepdims=True)
var = np.var(x, axis=1, keepdims=True)
self._cache["x_centered"] = x - mean
self._cache["inv_std"] = 1.0 / np.sqrt(var + self.eps)
x_hat = self._cache["x_centered"] * self._cache["inv_std"]
if self.affine:
return x_hat * self.gamma.data + self.beta.data
return x_hat
def backward(self, dy):
x_centered = self._cache["x_centered"]
inv_std = self._cache["inv_std"]
N = np.prod(self.normalized_shape)
if self.affine:
self.gamma.grad += np.sum(dy * x_centered * inv_std, axis=0)
self.beta.grad += np.sum(dy, axis=0)
d_xhat = dy * self.gamma.data
else:
d_xhat = dy
dx = (1./N) * inv_std * (
N*d_xhat - np.sum(d_xhat, axis=1, keepdims=True)
- x_centered * (inv_std**2) * np.sum(d_xhat * x_centered, axis=1, keepdims=True)
)
return dx
@register_layer("GlobalAvgPool2D")
class GlobalAvgPool2D(Layer):
def build(self, input_shape, **kwargs):
N, C, H, W = input_shape
return (N, C)
def forward(self, x):
self._cache["in_shape"] = x.shape
return np.mean(x, axis=(2,3))
def backward(self, dy):
N, C, H, W = self._cache["in_shape"]
return dy[:, :, None, None] * np.ones((N, C, H, W), dtype=DEFAULT_DTYPE) / (H * W)
@register_layer("ConvTranspose2D")
class ConvTranspose2D(Layer):
def __init__(self, filters: int, kernel_size: int | Tuple[int,int],
stride: int | Tuple[int,int] = 1, padding: str | int | Tuple[int,int] = "valid",
use_bias: bool = True, kernel_initializer: str = "he_normal",
bias_initializer: str = "zeros", seed: Optional[int] = None):
super().__init__()
self.filters = filters
if isinstance(kernel_size, int):
self.kH = self.kW = kernel_size
else:
self.kH, self.kW = kernel_size
if isinstance(stride, int):
self.sH = self.sW = stride
else:
self.sH, self.sW = stride
self.padding = padding
self.use_bias = use_bias
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.seed = seed
def build(self, input_shape, **kwargs):
N, C, H, W = input_shape
Wshape = (C, self.filters, self.kH, self.kW) # note: swapped in/out channels
W = get_initializer(self.kernel_initializer)(Wshape, seed=self.seed)
self.W = Parameter(W, name="kernel")
self._params.append(self.W)
if self.use_bias:
b = get_initializer(self.bias_initializer)((self.filters,), seed=self.seed)
self.b = Parameter(b, name="bias")
self._params.append(self.b)
else:
self.b = None
out_h = (H - 1) * self.sH + self.kH
out_w = (W - 1) * self.sW + self.kW
return (N, self.filters, out_h, out_w)
def forward(self, x):
self._cache["x"] = x
N, C, H, W = x.shape
out_h = (H - 1) * self.sH + self.kH
out_w = (W - 1) * self.sW + self.kW
out = np.zeros((N, self.filters, out_h, out_w), dtype=DEFAULT_DTYPE)
for n in range(N):
for c in range(C):
for f in range(self.filters):
out[n, f] += np.kron(x[n, c], np.ones((self.sH, self.sW))) * self.W.data[c, f]
if self.b is not None:
out += self.b.data[None, :, None, None]
return out
def backward(self, dy):
x = self._cache["x"]
N, C, H, W = x.shape
dW = np.zeros_like(self.W.data)
dx = np.zeros_like(x)
for n in range(N):
for c in range(C):
for f in range(self.filters):
dW[c, f] += np.sum(
np.kron(x[n, c], np.ones((self.sH, self.sW))) * dy[n, f]
)
dx[n, c] += np.convolve(dy[n, f], self.W.data[c, f], mode="valid")
self.W.grad += dW
if self.b is not None:
self.b.grad += np.sum(dy, axis=(0,2,3))
return dx
@register_layer("Flatten")
class Flatten(Layer):
def build(self, input_shape, **kwargs):
self.input_shape = input_shape
N = input_shape[0]
out = int(np.prod(input_shape[1:]))
self.built = True
return (N, out)
def forward(self, x):
self._cache["in_shape"] = x.shape
return x.reshape(x.shape[0], -1)
def backward(self, dy):
return dy.reshape(self._cache["in_shape"])
@register_layer("Dropout")
class Dropout(Layer):
def __init__(self, rate: float = 0.5, seed: Optional[int] = None):
super().__init__()
assert 0.0 <= rate < 1.0
self.rate = rate
self.seed = seed
def forward(self, x):
if self.training and self.rate > 0.0:
rng = _get_rng(self.seed)
mask = rng.random(x.shape) >= self.rate
scale = 1.0 / (1.0 - self.rate)
self._cache["mask"] = mask.astype(DEFAULT_DTYPE) * scale
return x * self._cache["mask"]
self._cache["mask"] = np.ones_like(x, dtype=DEFAULT_DTYPE)
return x
def backward(self, dy):
return dy * self._cache["mask"]
@register_layer("BatchNorm")
class BatchNorm(Layer):
# Supports 2D (N, D) or 4D (N, C, H, W) with axis=1 as channel
def __init__(self, momentum: float = 0.9, eps: float = 1e-5, affine: bool = True):
super().__init__()
self.momentum = momentum
self.eps = eps
self.affine = affine
def build(self, input_shape, **kwargs):
if len(input_shape) == 2:
C = input_shape[1]
param_shape = (C,)
elif len(input_shape) == 4:
C = input_shape[1]
param_shape = (C, 1, 1)
else:
raise ValueError("BatchNorm expects 2D or 4D input")
self.running_mean = np.zeros(param_shape, dtype=DEFAULT_DTYPE)
self.running_var = np.ones(param_shape, dtype=DEFAULT_DTYPE)
if self.affine:
self.gamma = Parameter(np.ones(param_shape, dtype=DEFAULT_DTYPE), name="gamma")
self.beta = Parameter(np.zeros(param_shape, dtype=DEFAULT_DTYPE), name="beta")
self._params += [self.gamma, self.beta]
else:
self.gamma = None
self.beta = None
self.axis = 1 # channel
self.input_dim = len(input_shape)
self.built = True
return input_shape
def forward(self, x):
if self.input_dim == 2:
axes = (0,)
keep = (1,)
else:
axes = (0, 2, 3)
keep = (1, None, None)
if self.training:
mean = np.mean(x, axis=axes, keepdims=True)
var = np.var(x, axis=axes, keepdims=True)
self.running_mean = self.momentum * self.running_mean + (1 - self.momentum) * mean
self.running_var = self.momentum * self.running_var + (1 - self.momentum) * var
self._cache["x_centered"] = x - mean
self._cache["inv_std"] = 1.0 / np.sqrt(var + self.eps)
x_hat = self._cache["x_centered"] * self._cache["inv_std"]
else:
x_hat = (x - self.running_mean) / np.sqrt(self.running_var + self.eps)
if self.affine:
out = x_hat * self.gamma.data + self.beta.data
else:
out = x_hat
self._cache["x_hat"] = x_hat
return out.astype(DEFAULT_DTYPE)
def backward(self, dy):
x_hat = self._cache["x_hat"]
if self.affine:
self.gamma.grad += np.sum(dy * x_hat, axis=(0,2,3), keepdims=True) if x_hat.ndim==4 else np.sum(dy * x_hat, axis=0, keepdims=True)
self.beta.grad += np.sum(dy, axis=(0,2,3), keepdims=True) if dy.ndim==4 else np.sum(dy, axis=0, keepdims=True)
d_xhat = dy * self.gamma.data
else:
d_xhat = dy
inv_std = self._cache["inv_std"]
x_centered = self._cache["x_centered"]
N = np.prod(dy.shape) / dy.shape[1] # elements per channel
dx = (1./N) * inv_std * (N * d_xhat - np.sum(d_xhat, axis=(0,2,3), keepdims=True) if d_xhat.ndim==4 else np.sum(d_xhat, axis=0, keepdims=True) - x_centered * (inv_std**2) * (np.sum(d_xhat * x_centered, axis=(0,2,3), keepdims=True) if d_xhat.ndim==4 else np.sum(d_xhat * x_centered, axis=0, keepdims=True)))
return dx
# ------------------------------------------------------------
# Losses
# ------------------------------------------------------------
LOSS_REGISTRY: Dict[str, "Loss"] = {}
def register_loss(name: str):
def deco(cls):
LOSS_REGISTRY[name] = cls
return cls
return deco
class Loss:
def forward(self, y_pred: np.ndarray, y_true: np.ndarray) -> float:
raise NotImplementedError
def backward(self) -> np.ndarray:
raise NotImplementedError
@register_loss("mse")
class MSELoss(Loss):
def forward(self, y_pred, y_true):
self._diff = (y_pred - y_true).astype(DEFAULT_DTYPE)
return float(np.mean(self._diff ** 2) / 2.0)
def backward(self):
N = self._diff.shape[0]
return self._diff / N
def softmax_stable(logits: np.ndarray, axis: int = -1) -> np.ndarray:
z = logits - np.max(logits, axis=axis, keepdims=True)
exp = np.exp(z)
return exp / np.sum(exp, axis=axis, keepdims=True)
@register_loss("cross_entropy")
class CrossEntropyLoss(Loss):
# Expects logits; internally applies softmax; y_true as integer class ids or one-hot
def __init__(self, axis: int = -1, from_logits: bool = True):
self.axis = axis
self.from_logits = from_logits
def forward(self, y_pred, y_true):
# y_pred: logits or probabilities
if self.from_logits:
probs = softmax_stable(y_pred, axis=self.axis)
else:
probs = y_pred / np.clip(np.sum(y_pred, axis=self.axis, keepdims=True), 1e-12, None)
self._probs = probs.astype(DEFAULT_DTYPE)
# y_true could be ints or one-hot
if y_true.ndim == y_pred.ndim:
# one-hot
eps = 1e-12
loss = -np.sum(y_true * np.log(np.clip(probs, eps, 1.0 - eps)), axis=self.axis)
else:
# class indices
idx = y_true.astype(np.int64)
# gather per-sample prob
range_idx = np.arange(y_pred.shape[0])
p = probs[range_idx, idx]
eps = 1e-12
loss = -np.log(np.clip(p, eps, 1.0 - eps))
return float(np.mean(loss))
def backward(self, y_true=None):
# If y_true not provided, assume last forward y_true was provided and stored as one-hot? We'll accept y_true in model.backward
raise NotImplementedError("CrossEntropyLoss.backward requires fused implementation in Model._compute_loss_and_grad")
@register_loss("binary_cross_entropy")
class BinaryCrossEntropy(Loss):
def forward(self, y_pred, y_true):
# y_pred as logits OR probabilities? We'll assume probabilities in (0,1)
eps = 1e-12
y_pred = np.clip(y_pred, eps, 1 - eps)
self._y_pred = y_pred
self._y_true = y_true
loss = - (y_true*np.log(y_pred) + (1-y_true)*np.log(1-y_pred))
return float(np.mean(loss))
def backward(self):
N = self._y_true.shape[0]
return (self._y_pred - self._y_true) / (N * np.clip(self._y_pred*(1-self._y_pred), 1e-12, None))
# ------------------------------------------------------------
# Regularization
# ------------------------------------------------------------
def l2_regularization(params: Iterable[Parameter], weight_decay: float) -> Tuple[float, Dict[int, np.ndarray]]:
reg = 0.0
grads = {}
for p in params:
reg += 0.5 * weight_decay * float(np.sum(p.data**2))
grads[id(p)] = weight_decay * p.data
return reg, grads
def l1_regularization(params: Iterable[Parameter], weight_decay: float) -> Tuple[float, Dict[int, np.ndarray]]:
reg = 0.0
grads = {}
for p in params:
reg += weight_decay * float(np.sum(np.abs(p.data)))
grads[id(p)] = weight_decay * np.sign(p.data)
return reg, grads
# ------------------------------------------------------------
# Optimizers
# ------------------------------------------------------------
OPTIMIZER_REGISTRY: Dict[str, "Optimizer"] = {}
def register_optimizer(name: str):
def deco(cls):
OPTIMIZER_REGISTRY[name] = cls
return cls
return deco
class Optimizer:
def step(self, params: Iterable[Parameter]):
raise NotImplementedError
def zero_grad(self, params: Iterable[Parameter]):
for p in params:
p.zero_grad()