1
1
"""
2
2
Radial Basis Function Neural Network (RBFNN)
3
3
4
- A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural
5
- network that uses radial basis functions as activation functions.
6
- RBFNNs are particularly effective for function approximation, regression, and
7
- classification tasks. The architecture typically consists of an input layer,
8
- a hidden layer with radial basis functions, and an output layer.
9
-
10
- In an RBFNN:
11
- - The hidden layer applies a radial basis function (often Gaussian) to the
12
- input data, transforming it into a higher-dimensional space.
13
- - The output layer combines the results from the hidden layer using
14
- weighted sums to produce the final output.
4
+ A Radial Basis Function Neural Network (RBFNN) is a type of artificial
5
+ neural network that uses radial basis functions as activation functions.
6
+ RBFNNs are particularly effective for function approximation, regression,
7
+ and classification tasks.
15
8
16
9
#### Reference
10
+
17
11
- Wikipedia: https://en.wikipedia.org/wiki/Radial_basis_function_network
18
12
"""
19
13
@@ -40,14 +34,14 @@ def __init__(self, num_centers: int, spread: float) -> None:
40
34
spread (float): Spread of the radial basis functions.
41
35
42
36
Examples:
43
- >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3,spread=1.0)
37
+ >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3, spread=1.0)
44
38
>>> rbf_nn.num_centers
45
39
3
46
40
"""
47
41
self .num_centers = num_centers
48
42
self .spread = spread
49
- self .centers : np .ndarray = None
50
- self .weights : np .ndarray = None
43
+ self .centers : np .ndarray = None # To be initialized during training
44
+ self .weights : np .ndarray = None # To be initialized during training
51
45
52
46
def _gaussian_rbf (self , input_vector : np .ndarray , center : np .ndarray ) -> float :
53
47
"""
@@ -66,9 +60,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float:
66
60
>>> rbf_nn._gaussian_rbf(np.array([0, 0]), center)
67
61
0.1353352832366127
68
62
"""
69
- # Calculate the squared distances
70
- distances = np .linalg .norm (input_vector [:, np . newaxis ] - center , axis = 2 ) ** 2
71
- return np . exp ( - distances / ( 2 * self . spread ** 2 ) )
63
+ return np . exp (
64
+ - ( np .linalg .norm (input_vector - center ) ** 2 ) / ( 2 * self . spread ** 2 )
65
+ )
72
66
73
67
def _compute_rbf_outputs (self , input_data : np .ndarray ) -> np .ndarray :
74
68
"""
@@ -88,7 +82,12 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray:
88
82
[0.60653066, 1. ]])
89
83
"""
90
84
assert self .centers is not None , "Centers initialized before computing outputs."
91
- return self ._gaussian_rbf (input_data , self .centers )
85
+
86
+ rbf_outputs = np .zeros ((input_data .shape [0 ], self .num_centers ))
87
+ for i , center in enumerate (self .centers ):
88
+ for j in range (input_data .shape [0 ]):
89
+ rbf_outputs [j , i ] = self ._gaussian_rbf (input_data [j ], center )
90
+ return rbf_outputs
92
91
93
92
def fit (self , input_data : np .ndarray , target_values : np .ndarray ) -> None :
94
93
"""
@@ -115,7 +114,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None:
115
114
)
116
115
117
116
# Initialize centers using random samples from input_data
118
- rng = np .random .default_rng ()
117
+ rng = np .random .default_rng () # Create a random number generator
119
118
random_indices = rng .choice (
120
119
input_data .shape [0 ], self .num_centers , replace = False
121
120
)
0 commit comments