11"""
22Radial Basis Function Neural Network (RBFNN)
33
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.
158
169#### Reference
10+
1711- Wikipedia: https://en.wikipedia.org/wiki/Radial_basis_function_network
1812"""
1913
@@ -40,14 +34,14 @@ def __init__(self, num_centers: int, spread: float) -> None:
4034 spread (float): Spread of the radial basis functions.
4135
4236 Examples:
43- >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3,spread=1.0)
37+ >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3, spread=1.0)
4438 >>> rbf_nn.num_centers
4539 3
4640 """
4741 self .num_centers = num_centers
4842 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
5145
5246 def _gaussian_rbf (self , input_vector : np .ndarray , center : np .ndarray ) -> float :
5347 """
@@ -66,9 +60,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float:
6660 >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center)
6761 0.1353352832366127
6862 """
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+ )
7266
7367 def _compute_rbf_outputs (self , input_data : np .ndarray ) -> np .ndarray :
7468 """
@@ -88,7 +82,12 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray:
8882 [0.60653066, 1. ]])
8983 """
9084 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
9291
9392 def fit (self , input_data : np .ndarray , target_values : np .ndarray ) -> None :
9493 """
@@ -115,7 +114,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None:
115114 )
116115
117116 # 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
119118 random_indices = rng .choice (
120119 input_data .shape [0 ], self .num_centers , replace = False
121120 )
0 commit comments