12
12
"""
13
13
14
14
import numpy as np
15
- from typing import Tuple
16
15
17
16
18
17
class SimpleCNN :
19
- def __init__ (self , input_shape : Tuple [int , int , int ], num_classes : int ) -> None :
18
+ def __init__ (self , input_shape : tuple [int , int , int ], num_classes : int ) -> None :
20
19
"""
21
20
Initialize a simple CNN model.
22
21
@@ -26,42 +25,43 @@ def __init__(self, input_shape: Tuple[int, int, int], num_classes: int) -> None:
26
25
"""
27
26
self .input_shape = input_shape
28
27
self .num_classes = num_classes
29
- self .filters = np .random .randn (8 , input_shape [0 ], 3 , 3 ) * 0.1 # 8 filters
30
- self .fc_weights = np .random .randn (8 * 26 * 26 , num_classes ) * 0.1
28
+ rng = np .random .default_rng ()
29
+ self .filters = rng .normal (0 , 0.1 , size = (8 , input_shape [0 ], 3 , 3 )) # 8 filters
30
+ self .fc_weights = rng .normal (0 , 0.1 , size = (8 * 26 * 26 , num_classes ))
31
31
32
- def relu (self , x : np .ndarray ) -> np .ndarray :
33
- """Apply ReLU activation."""
34
- return np .maximum (0 , x )
32
+ def relu (self , feature_map : np .ndarray ) -> np .ndarray :
33
+ """Apply ReLU activation to the feature map ."""
34
+ return np .maximum (0 , feature_map )
35
35
36
- def convolve (self , x : np .ndarray , filters : np .ndarray ) -> np .ndarray :
37
- """Apply convolution operation."""
38
- batch , height , width = x .shape
36
+ def convolve (self , input_tensor : np .ndarray , filters : np .ndarray ) -> np .ndarray :
37
+ """Apply convolution operation to the input tensor ."""
38
+ _ , height , width = input_tensor .shape
39
39
num_filters , _ , fh , fw = filters .shape
40
40
output = np .zeros ((num_filters , height - fh + 1 , width - fw + 1 ))
41
41
42
42
for f in range (num_filters ):
43
43
for i in range (height - fh + 1 ):
44
44
for j in range (width - fw + 1 ):
45
- region = x [:, i :i + fh , j :j + fw ]
45
+ region = input_tensor [:, i :i + fh , j :j + fw ]
46
46
output [f , i , j ] = np .sum (region * filters [f ])
47
47
return output
48
48
49
- def flatten (self , x : np .ndarray ) -> np .ndarray :
50
- """Flatten the feature map."""
51
- return x .reshape (- 1 )
49
+ def flatten (self , feature_map : np .ndarray ) -> np .ndarray :
50
+ """Flatten the feature map into a 1D array ."""
51
+ return feature_map .reshape (- 1 )
52
52
53
- def forward (self , x : np .ndarray ) -> np .ndarray :
53
+ def forward (self , input_tensor : np .ndarray ) -> np .ndarray :
54
54
"""
55
55
Forward pass through the CNN.
56
56
57
57
Args:
58
- x : Input image of shape (channels, height, width)
58
+ input_tensor : Input image of shape (channels, height, width)
59
59
60
60
Returns:
61
61
Output logits of shape (num_classes,)
62
62
"""
63
- conv_out = self .convolve (x , self .filters )
63
+ conv_out = self .convolve (input_tensor , self .filters )
64
64
activated = self .relu (conv_out )
65
65
flattened = self .flatten (activated )
66
66
logits = flattened @ self .fc_weights
67
- return logits
67
+ return logits
0 commit comments