From b6e2f376dfb757b944b91c72c4f24f1cf279d836 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 07:25:35 +0530 Subject: [PATCH 01/39] add rbfnn algorithm solving issue 12322 --- .../radial_basis_function_neural_network.py | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 neural_network/radial_basis_function_neural_network.py diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py new file mode 100644 index 000000000000..a9b55842aad1 --- /dev/null +++ b/neural_network/radial_basis_function_neural_network.py @@ -0,0 +1,111 @@ +import numpy as np +from typing import List, Optional + +class RadialBasisFunctionNeuralNetwork: + """ + A simple implementation of a Radial Basis Function Neural Network (RBFNN). + + Attributes: + centers (np.ndarray): Centers of the radial basis functions. + weights (np.ndarray): Weights for the output layer. + sigma (float): Spread of the radial basis functions. + """ + + def __init__(self, n_centers: int, sigma: float): + """ + Initialize the RBFNN with the given number of centers and spread. + + Args: + n_centers: Number of centers for the radial basis functions. + sigma: Spread of the radial basis functions. + """ + self.n_centers = n_centers + self.sigma = sigma + self.centers = None # To be initialized during training + self.weights = None # To be initialized during training + + def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: + """ + Calculate the Gaussian radial basis function. + + Args: + x: Input vector. + center: Center of the RBF. + + Returns: + The output of the RBF evaluated at x. + """ + return np.exp(-np.linalg.norm(x - center)**2 / (2 * self.sigma**2)) + + def _compute_rbf(self, X: np.ndarray) -> np.ndarray: + """ + Compute the output of the radial basis functions for input data. + + Args: + X: Input data matrix (num_samples x num_features). + + Returns: + A matrix of shape (num_samples x n_centers) containing the RBF outputs. + """ + rbf_outputs = np.zeros((X.shape[0], self.n_centers)) + for i, center in enumerate(self.centers): + for j in range(X.shape[0]): + rbf_outputs[j, i] = self._gaussian(X[j], center) + return rbf_outputs + + def fit(self, X: np.ndarray, y: np.ndarray): + """ + Train the RBFNN on the provided data. + + Args: + X: Input data matrix (num_samples x num_features). + y: Target values (num_samples x output_dim). + + Raises: + ValueError: If number of samples in X and y do not match. + """ + if X.shape[0] != y.shape[0]: + raise ValueError("Number of samples in X and y must match.") + + # Initialize centers using random samples from X + random_indices = np.random.choice(X.shape[0], self.n_centers, replace=False) + self.centers = X[random_indices] + + # Compute the RBF outputs for the training data + rbf_outputs = self._compute_rbf(X) + + # Calculate weights using the pseudo-inverse + self.weights = np.linalg.pinv(rbf_outputs).dot(y) + + def predict(self, X: np.ndarray) -> np.ndarray: + """ + Predict the output for the given input data. + + Args: + X: Input data matrix (num_samples x num_features). + + Returns: + Predicted values (num_samples x output_dim). + """ + rbf_outputs = self._compute_rbf(X) + return rbf_outputs.dot(self.weights) + +# Example Usage +if __name__ == "__main__": + # Sample dataset + X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input + y = np.array([[0], [1], [1], [0]]) # Target output for XOR + + # Create and train the RBFNN + rbf_nn = RadialBasisFunctionNeuralNetwork(n_centers=2, sigma=0.5) + rbf_nn.fit(X, y) + + # Predict using the trained model + predictions = rbf_nn.predict(X) + print("Predictions:\n", predictions) + +# Predictions: +# [[0.24826229] +# [0.06598867] +# [0.06598867] +# [0.24826229]] \ No newline at end of file From 74212a69c1714980af8cfab4a1a322e591e57463 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 07:36:03 +0530 Subject: [PATCH 02/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index a9b55842aad1..90588c47a168 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -9,6 +9,8 @@ class RadialBasisFunctionNeuralNetwork: centers (np.ndarray): Centers of the radial basis functions. weights (np.ndarray): Weights for the output layer. sigma (float): Spread of the radial basis functions. + Reference: + Radial Basis Function Network: https://en.wikipedia.org/wiki/Radial_basis_function_network """ def __init__(self, n_centers: int, sigma: float): @@ -16,24 +18,29 @@ def __init__(self, n_centers: int, sigma: float): Initialize the RBFNN with the given number of centers and spread. Args: - n_centers: Number of centers for the radial basis functions. - sigma: Spread of the radial basis functions. + n_centers (int): Number of centers for the radial basis functions. + sigma (float): Spread of the radial basis functions. """ self.n_centers = n_centers self.sigma = sigma - self.centers = None # To be initialized during training - self.weights = None # To be initialized during training + self.centers: Optional[np.ndarray] = None # To be initialized during training + self.weights: Optional[np.ndarray] = None # To be initialized during training def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: """ Calculate the Gaussian radial basis function. Args: - x: Input vector. - center: Center of the RBF. + x (np.ndarray): Input vector. + center (np.ndarray): Center of the RBF. Returns: - The output of the RBF evaluated at x. + float: The output of the RBF evaluated at x. + + >>> import numpy as np + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(n_centers=2, sigma=0.5) + >>> rbf_nn._gaussian(np.array([0, 0]), np.array([1, 1])) + 0.1353352832366127 """ return np.exp(-np.linalg.norm(x - center)**2 / (2 * self.sigma**2)) @@ -42,10 +49,10 @@ def _compute_rbf(self, X: np.ndarray) -> np.ndarray: Compute the output of the radial basis functions for input data. Args: - X: Input data matrix (num_samples x num_features). + X (np.ndarray): Input data matrix (num_samples x num_features). Returns: - A matrix of shape (num_samples x n_centers) containing the RBF outputs. + np.ndarray: A matrix of shape (num_samples x n_centers) containing the RBF outputs. """ rbf_outputs = np.zeros((X.shape[0], self.n_centers)) for i, center in enumerate(self.centers): @@ -58,8 +65,8 @@ def fit(self, X: np.ndarray, y: np.ndarray): Train the RBFNN on the provided data. Args: - X: Input data matrix (num_samples x num_features). - y: Target values (num_samples x output_dim). + X (np.ndarray): Input data matrix (num_samples x num_features). + y (np.ndarray): Target values (num_samples x output_dim). Raises: ValueError: If number of samples in X and y do not match. @@ -82,10 +89,10 @@ def predict(self, X: np.ndarray) -> np.ndarray: Predict the output for the given input data. Args: - X: Input data matrix (num_samples x num_features). + X (np.ndarray): Input data matrix (num_samples x num_features). Returns: - Predicted values (num_samples x output_dim). + np.ndarray: Predicted values (num_samples x output_dim). """ rbf_outputs = self._compute_rbf(X) return rbf_outputs.dot(self.weights) @@ -104,8 +111,9 @@ def predict(self, X: np.ndarray) -> np.ndarray: predictions = rbf_nn.predict(X) print("Predictions:\n", predictions) +# Expected Output: # Predictions: # [[0.24826229] # [0.06598867] # [0.06598867] -# [0.24826229]] \ No newline at end of file +# [0.24826229]] From 5fbe96d984efc25fce3582eb6d419fc735f3aa34 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 07:39:27 +0530 Subject: [PATCH 03/39] add rbfnn in directory solving issue 12322 --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..dce505ed4854 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -832,6 +832,7 @@ * [Input Data](neural_network/input_data.py) * [Simple Neural Network](neural_network/simple_neural_network.py) * [Two Hidden Layers Neural Network](neural_network/two_hidden_layers_neural_network.py) + * [Radial Basis Function Neural Network](neural_network/radial_basis_function_neural_network.py) ## Other * [Activity Selection](other/activity_selection.py) From f66b55f829699914c6166122eb1dab03ac01056f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:13:08 +0000 Subject: [PATCH 04/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 90588c47a168..8ef2145b50b2 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,7 @@ import numpy as np from typing import List, Optional + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -36,13 +37,13 @@ def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: Returns: float: The output of the RBF evaluated at x. - + >>> import numpy as np >>> rbf_nn = RadialBasisFunctionNeuralNetwork(n_centers=2, sigma=0.5) >>> rbf_nn._gaussian(np.array([0, 0]), np.array([1, 1])) 0.1353352832366127 """ - return np.exp(-np.linalg.norm(x - center)**2 / (2 * self.sigma**2)) + return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * self.sigma**2)) def _compute_rbf(self, X: np.ndarray) -> np.ndarray: """ @@ -97,6 +98,7 @@ def predict(self, X: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf(X) return rbf_outputs.dot(self.weights) + # Example Usage if __name__ == "__main__": # Sample dataset From bb7ac352da3fed7d6d6279bcb18190ae991dd7bb Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 07:51:01 +0530 Subject: [PATCH 05/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 8ef2145b50b2..51939ec22027 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,4 @@ import numpy as np -from typing import List, Optional class RadialBasisFunctionNeuralNetwork: @@ -10,6 +9,7 @@ class RadialBasisFunctionNeuralNetwork: centers (np.ndarray): Centers of the radial basis functions. weights (np.ndarray): Weights for the output layer. sigma (float): Spread of the radial basis functions. + Reference: Radial Basis Function Network: https://en.wikipedia.org/wiki/Radial_basis_function_network """ @@ -24,8 +24,8 @@ def __init__(self, n_centers: int, sigma: float): """ self.n_centers = n_centers self.sigma = sigma - self.centers: Optional[np.ndarray] = None # To be initialized during training - self.weights: Optional[np.ndarray] = None # To be initialized during training + self.centers: np.ndarray | None = None # To be initialized during training + self.weights: np.ndarray | None = None # To be initialized during training def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: """ @@ -45,57 +45,57 @@ def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: """ return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * self.sigma**2)) - def _compute_rbf(self, X: np.ndarray) -> np.ndarray: + def _compute_rbf(self, x: np.ndarray) -> np.ndarray: """ Compute the output of the radial basis functions for input data. Args: - X (np.ndarray): Input data matrix (num_samples x num_features). + x (np.ndarray): Input data matrix (num_samples x num_features). Returns: np.ndarray: A matrix of shape (num_samples x n_centers) containing the RBF outputs. """ - rbf_outputs = np.zeros((X.shape[0], self.n_centers)) + rbf_outputs = np.zeros((x.shape[0], self.n_centers)) for i, center in enumerate(self.centers): - for j in range(X.shape[0]): - rbf_outputs[j, i] = self._gaussian(X[j], center) + for j in range(x.shape[0]): + rbf_outputs[j, i] = self._gaussian(x[j], center) return rbf_outputs - def fit(self, X: np.ndarray, y: np.ndarray): + def fit(self, x: np.ndarray, y: np.ndarray): """ Train the RBFNN on the provided data. Args: - X (np.ndarray): Input data matrix (num_samples x num_features). + x (np.ndarray): Input data matrix (num_samples x num_features). y (np.ndarray): Target values (num_samples x output_dim). Raises: - ValueError: If number of samples in X and y do not match. + ValueError: If number of samples in x and y do not match. """ - if X.shape[0] != y.shape[0]: - raise ValueError("Number of samples in X and y must match.") + if x.shape[0] != y.shape[0]: + raise ValueError("Number of samples in x and y must match.") - # Initialize centers using random samples from X - random_indices = np.random.choice(X.shape[0], self.n_centers, replace=False) - self.centers = X[random_indices] + # Initialize centers using random samples from x + random_indices = np.random.choice(x.shape[0], self.n_centers, replace=False) + self.centers = x[random_indices] # Compute the RBF outputs for the training data - rbf_outputs = self._compute_rbf(X) + rbf_outputs = self._compute_rbf(x) # Calculate weights using the pseudo-inverse self.weights = np.linalg.pinv(rbf_outputs).dot(y) - def predict(self, X: np.ndarray) -> np.ndarray: + def predict(self, x: np.ndarray) -> np.ndarray: """ Predict the output for the given input data. Args: - X (np.ndarray): Input data matrix (num_samples x num_features). + x (np.ndarray): Input data matrix (num_samples x num_features). Returns: np.ndarray: Predicted values (num_samples x output_dim). """ - rbf_outputs = self._compute_rbf(X) + rbf_outputs = self._compute_rbf(x) return rbf_outputs.dot(self.weights) @@ -113,9 +113,12 @@ def predict(self, X: np.ndarray) -> np.ndarray: predictions = rbf_nn.predict(X) print("Predictions:\n", predictions) -# Expected Output: +# Sample Expected Output: # Predictions: # [[0.24826229] # [0.06598867] # [0.06598867] # [0.24826229]] + + + From aeb9015b2f4fe9ce5adcd33bedaa452f99d2de7a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:21:32 +0000 Subject: [PATCH 06/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 51939ec22027..f13f3ce3f8b3 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -119,6 +119,3 @@ def predict(self, x: np.ndarray) -> np.ndarray: # [0.06598867] # [0.06598867] # [0.24826229]] - - - From 2dadd7fcd626652921458366c98d95e3f606142c Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 07:53:04 +0530 Subject: [PATCH 07/39] correcting errors in radial_basis_function_neural_network.py From ca15518511c6198a9cd8144648a6cc0e4151b371 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:06:21 +0530 Subject: [PATCH 08/39] add doctests to radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 130 ++++++++++-------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index f13f3ce3f8b3..17f7b98ff012 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,121 +1,143 @@ import numpy as np - class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). Attributes: + num_centers (int): Number of centers for the radial basis functions. + spread (float): Spread of the radial basis functions. centers (np.ndarray): Centers of the radial basis functions. weights (np.ndarray): Weights for the output layer. - sigma (float): Spread of the radial basis functions. - - Reference: - Radial Basis Function Network: https://en.wikipedia.org/wiki/Radial_basis_function_network """ - def __init__(self, n_centers: int, sigma: float): + def __init__(self, num_centers: int, spread: float): """ Initialize the RBFNN with the given number of centers and spread. Args: - n_centers (int): Number of centers for the radial basis functions. - sigma (float): Spread of the radial basis functions. + num_centers (int): Number of centers for the radial basis functions. + spread (float): Spread of the radial basis functions. + + Examples: + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3, spread=1.0) + >>> rbf_nn.num_centers + 3 """ - self.n_centers = n_centers - self.sigma = sigma - self.centers: np.ndarray | None = None # To be initialized during training - self.weights: np.ndarray | None = None # To be initialized during training + self.num_centers = num_centers + self.spread = spread + self.centers: np.ndarray = None # To be initialized during training + self.weights: np.ndarray = None # To be initialized during training - def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float: + def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: """ - Calculate the Gaussian radial basis function. + Calculate the Gaussian radial basis function output for a given input vector and center. Args: - x (np.ndarray): Input vector. - center (np.ndarray): Center of the RBF. + input_vector (np.ndarray): The input vector for which to calculate the RBF output. + center (np.ndarray): The center of the radial basis function. Returns: - float: The output of the RBF evaluated at x. + float: The output of the radial basis function evaluated at the input vector. - >>> import numpy as np - >>> rbf_nn = RadialBasisFunctionNeuralNetwork(n_centers=2, sigma=0.5) - >>> rbf_nn._gaussian(np.array([0, 0]), np.array([1, 1])) - 0.1353352832366127 + Examples: + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=0.5) + >>> center = np.array([1, 1]) + >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) + 0.1353352832366127 """ - return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * self.sigma**2)) + return np.exp(-(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2)) - def _compute_rbf(self, x: np.ndarray) -> np.ndarray: + def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ - Compute the output of the radial basis functions for input data. + Compute the outputs of the radial basis functions for the input data. Args: - x (np.ndarray): Input data matrix (num_samples x num_features). + input_data (np.ndarray): Input data matrix (num_samples x num_features). Returns: - np.ndarray: A matrix of shape (num_samples x n_centers) containing the RBF outputs. + np.ndarray: A matrix of shape (num_samples x num_centers) containing the RBF outputs. + + Examples: + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) + >>> rbf_nn.centers = np.array([[0, 0], [1, 1]]) + >>> rbf_nn._compute_rbf_outputs(np.array([[0, 0], [1, 1]])) + array([[1. , 0.60653066], + [0.60653066, 1. ]]) """ - rbf_outputs = np.zeros((x.shape[0], self.n_centers)) + rbf_outputs = np.zeros((input_data.shape[0], self.num_centers)) for i, center in enumerate(self.centers): - for j in range(x.shape[0]): - rbf_outputs[j, i] = self._gaussian(x[j], center) + for j in range(input_data.shape[0]): + rbf_outputs[j, i] = self._gaussian_rbf(input_data[j], center) return rbf_outputs - def fit(self, x: np.ndarray, y: np.ndarray): + def fit(self, input_data: np.ndarray, target_values: np.ndarray): """ - Train the RBFNN on the provided data. + Train the RBFNN using the provided input data and target values. Args: - x (np.ndarray): Input data matrix (num_samples x num_features). - y (np.ndarray): Target values (num_samples x output_dim). + input_data (np.ndarray): Input data matrix (num_samples x num_features). + target_values (np.ndarray): Target values (num_samples x output_dim). Raises: - ValueError: If number of samples in x and y do not match. + ValueError: If the number of samples in input_data and target_values do not match. + + Examples: + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) + >>> X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) + >>> y = np.array([[0], [1], [1], [0]]) + >>> rbf_nn.fit(X, y) + >>> rbf_nn.weights is not None + True """ - if x.shape[0] != y.shape[0]: - raise ValueError("Number of samples in x and y must match.") + if input_data.shape[0] != target_values.shape[0]: + raise ValueError("Number of samples in input_data and target_values must match.") - # Initialize centers using random samples from x - random_indices = np.random.choice(x.shape[0], self.n_centers, replace=False) - self.centers = x[random_indices] + # Initialize centers using random samples from input_data + random_indices = np.random.choice(input_data.shape[0], self.num_centers, replace=False) + self.centers = input_data[random_indices] # Compute the RBF outputs for the training data - rbf_outputs = self._compute_rbf(x) + rbf_outputs = self._compute_rbf_outputs(input_data) # Calculate weights using the pseudo-inverse - self.weights = np.linalg.pinv(rbf_outputs).dot(y) + self.weights = np.linalg.pinv(rbf_outputs).dot(target_values) - def predict(self, x: np.ndarray) -> np.ndarray: + def predict(self, input_data: np.ndarray) -> np.ndarray: """ - Predict the output for the given input data. + Predict the output for the given input data using the trained RBFNN. Args: - x (np.ndarray): Input data matrix (num_samples x num_features). + input_data (np.ndarray): Input data matrix (num_samples x num_features). Returns: np.ndarray: Predicted values (num_samples x output_dim). + + Examples: + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) + >>> rbf_nn.centers = np.array([[0, 0], [1, 1]]) + >>> rbf_nn.weights = np.array([[0.5], [0.5]]) + >>> rbf_nn.predict(np.array([[0, 0], [1, 1]])) + array([[0.5], + [0.5]]) """ - rbf_outputs = self._compute_rbf(x) + rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) # Example Usage if __name__ == "__main__": - # Sample dataset + import doctest + doctest.testmod() + + # Sample dataset for XOR problem X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input y = np.array([[0], [1], [1], [0]]) # Target output for XOR # Create and train the RBFNN - rbf_nn = RadialBasisFunctionNeuralNetwork(n_centers=2, sigma=0.5) + rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=0.5) rbf_nn.fit(X, y) # Predict using the trained model predictions = rbf_nn.predict(X) print("Predictions:\n", predictions) - -# Sample Expected Output: -# Predictions: -# [[0.24826229] -# [0.06598867] -# [0.06598867] -# [0.24826229]] From 03eed7162c115412f14b8e84dc837132ac0a9f95 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:37:02 +0000 Subject: [PATCH 09/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 17f7b98ff012..d833469d9b48 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,6 @@ import numpy as np + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -46,7 +47,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) 0.1353352832366127 """ - return np.exp(-(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2)) + return np.exp( + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) + ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ @@ -91,10 +94,14 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray): True """ if input_data.shape[0] != target_values.shape[0]: - raise ValueError("Number of samples in input_data and target_values must match.") + raise ValueError( + "Number of samples in input_data and target_values must match." + ) # Initialize centers using random samples from input_data - random_indices = np.random.choice(input_data.shape[0], self.num_centers, replace=False) + random_indices = np.random.choice( + input_data.shape[0], self.num_centers, replace=False + ) self.centers = input_data[random_indices] # Compute the RBF outputs for the training data @@ -128,6 +135,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest + doctest.testmod() # Sample dataset for XOR problem From 90f0a5ffe2477e233ae0b6aa04fe8ffc8bb90038 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:12:04 +0530 Subject: [PATCH 10/39] breaking long lines in radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index d833469d9b48..d127de39213a 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,5 @@ import numpy as np - class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -47,9 +46,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) 0.1353352832366127 """ - return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) - ) + return np.exp(-(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2)) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ @@ -94,14 +91,11 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray): True """ if input_data.shape[0] != target_values.shape[0]: - raise ValueError( - "Number of samples in input_data and target_values must match." - ) + raise ValueError("Number of samples in input_data and target_values must match.") # Initialize centers using random samples from input_data - random_indices = np.random.choice( - input_data.shape[0], self.num_centers, replace=False - ) + rng = np.random.default_rng() # Using new random number generator + random_indices = rng.choice(input_data.shape[0], self.num_centers, replace=False) self.centers = input_data[random_indices] # Compute the RBF outputs for the training data @@ -135,7 +129,6 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest - doctest.testmod() # Sample dataset for XOR problem From 79239b54d414f0c0f6d8b7448abdfc67e7e4ba79 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:42:26 +0000 Subject: [PATCH 11/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index d127de39213a..f8fe6df11451 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,6 @@ import numpy as np + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -46,7 +47,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) 0.1353352832366127 """ - return np.exp(-(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2)) + return np.exp( + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) + ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ @@ -91,11 +94,15 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray): True """ if input_data.shape[0] != target_values.shape[0]: - raise ValueError("Number of samples in input_data and target_values must match.") + raise ValueError( + "Number of samples in input_data and target_values must match." + ) # Initialize centers using random samples from input_data rng = np.random.default_rng() # Using new random number generator - random_indices = rng.choice(input_data.shape[0], self.num_centers, replace=False) + random_indices = rng.choice( + input_data.shape[0], self.num_centers, replace=False + ) self.centers = input_data[random_indices] # Compute the RBF outputs for the training data @@ -129,6 +136,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest + doctest.testmod() # Sample dataset for XOR problem From 4c7d0a2ae1fad4992b44617756d35e858894c78f Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:20:02 +0530 Subject: [PATCH 12/39] add docstrings to radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index f8fe6df11451..de533b5d5ca9 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,5 @@ import numpy as np - class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -12,7 +11,7 @@ class RadialBasisFunctionNeuralNetwork: weights (np.ndarray): Weights for the output layer. """ - def __init__(self, num_centers: int, spread: float): + def __init__(self, num_centers: int, spread: float) -> None: """ Initialize the RBFNN with the given number of centers and spread. @@ -48,7 +47,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2) ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: @@ -74,7 +73,7 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs[j, i] = self._gaussian_rbf(input_data[j], center) return rbf_outputs - def fit(self, input_data: np.ndarray, target_values: np.ndarray): + def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: """ Train the RBFNN using the provided input data and target values. @@ -87,8 +86,8 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray): Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) - >>> X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) - >>> y = np.array([[0], [1], [1], [0]]) + >>> X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input + >>> y = np.array([[0], [1], [1], [0]]) # Target output for XOR >>> rbf_nn.fit(X, y) >>> rbf_nn.weights is not None True @@ -99,7 +98,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray): ) # Initialize centers using random samples from input_data - rng = np.random.default_rng() # Using new random number generator + rng = np.random.default_rng() # Create a random number generator random_indices = rng.choice( input_data.shape[0], self.num_centers, replace=False ) @@ -136,7 +135,6 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest - doctest.testmod() # Sample dataset for XOR problem From ae74131ca9b407057ed64bfb96611631b536275d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:50:24 +0000 Subject: [PATCH 13/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index de533b5d5ca9..6d6878439998 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,6 @@ import numpy as np + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -47,7 +48,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2) + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: @@ -135,6 +136,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest + doctest.testmod() # Sample dataset for XOR problem From c47d8e37d4c124100ed7bafefb4ec7570e964340 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:26:20 +0530 Subject: [PATCH 14/39] adjust sorting libraries radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 6d6878439998..0e3ebaeb20dd 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,5 @@ import numpy as np - class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -32,11 +31,11 @@ def __init__(self, num_centers: int, spread: float) -> None: def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: """ - Calculate the Gaussian radial basis function output for a given input vector and center. + Calculate Gaussian radial basis function output for input vector and center. Args: - input_vector (np.ndarray): The input vector for which to calculate the RBF output. - center (np.ndarray): The center of the radial basis function. + input_vector (np.ndarray): Input vector for which to calculate the RBF output. + center (np.ndarray): Center of the radial basis function. Returns: float: The output of the radial basis function evaluated at the input vector. @@ -48,7 +47,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2) ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: @@ -59,7 +58,7 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: input_data (np.ndarray): Input data matrix (num_samples x num_features). Returns: - np.ndarray: A matrix of shape (num_samples x num_centers) containing the RBF outputs. + np.ndarray: A matrix of shape (num_samples x num_centers) with RBF outputs. Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) @@ -83,7 +82,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: target_values (np.ndarray): Target values (num_samples x output_dim). Raises: - ValueError: If the number of samples in input_data and target_values do not match. + ValueError: If number of samples in input_data and target_values not match. Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) @@ -136,7 +135,6 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": import doctest - doctest.testmod() # Sample dataset for XOR problem From 0831d48d22a1450c851a7828c128f565a5d255b1 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:26:57 +0530 Subject: [PATCH 15/39] resorting libraries radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 0e3ebaeb20dd..18542d4ac8b9 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,4 +1,5 @@ import numpy as np +import doctest class RadialBasisFunctionNeuralNetwork: """ @@ -134,7 +135,6 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": - import doctest doctest.testmod() # Sample dataset for XOR problem From f7a69e3f8ab098783b46d184c4059931d5d0f038 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:57:31 +0000 Subject: [PATCH 16/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 18542d4ac8b9..55717f91ce72 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,7 @@ import numpy as np import doctest + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -48,7 +49,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread ** 2) + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: From d00b36673b2903e05d773823a90406eee3fdf6a6 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:28:24 +0530 Subject: [PATCH 17/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 55717f91ce72..69e1af8ee042 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -36,11 +36,11 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: Calculate Gaussian radial basis function output for input vector and center. Args: - input_vector (np.ndarray): Input vector for which to calculate the RBF output. + input_vector (np.ndarray): Input vector to calculate RBF output. center (np.ndarray): Center of the radial basis function. Returns: - float: The output of the radial basis function evaluated at the input vector. + float: The output of the radial basis function evaluated at input vector. Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=0.5) From 21dcb4db07b7f02d11c8715aeaa7d25557ac801e Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:29:37 +0530 Subject: [PATCH 18/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 69e1af8ee042..660e6b5d19ef 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,5 @@ -import numpy as np import doctest - +import numpy as np class RadialBasisFunctionNeuralNetwork: """ From 30dc6165bd2addece3112f7812094a883c418a49 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:00:50 +0000 Subject: [PATCH 19/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 1 + 1 file changed, 1 insertion(+) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 660e6b5d19ef..55650c1554f0 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,6 +1,7 @@ import doctest import numpy as np + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). From 301d98f23b6a3bf270ffc431ac322903aec5c7d0 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:31:23 +0530 Subject: [PATCH 20/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 55650c1554f0..7bd15b0ccdab 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,4 +1,4 @@ -import doctest +from doctest import testmod import numpy as np From d76d6bb1186876913d0e60535dc888f73262d96d Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:34:25 +0530 Subject: [PATCH 21/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 7bd15b0ccdab..a9359c806577 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,5 @@ -from doctest import testmod import numpy as np +from doctest import testmod class RadialBasisFunctionNeuralNetwork: @@ -136,7 +136,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Example Usage if __name__ == "__main__": - doctest.testmod() + testmod() # Running the doctest # Sample dataset for XOR problem X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input From fe4a0c9b58975bbc4a573d51fcfd3ff8db5c3612 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:36:34 +0530 Subject: [PATCH 22/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index a9359c806577..eea8756b7f29 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,4 @@ import numpy as np -from doctest import testmod class RadialBasisFunctionNeuralNetwork: @@ -133,7 +132,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) - +from doctest import testmod # Example Usage if __name__ == "__main__": testmod() # Running the doctest From 42826a1a9c715200f6870af3bce5eb68a1f5f005 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:07:28 +0000 Subject: [PATCH 23/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index eea8756b7f29..711951034d7c 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -132,7 +132,9 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) + from doctest import testmod + # Example Usage if __name__ == "__main__": testmod() # Running the doctest From 1140d308f668ffc4131058418408faebf3334aa3 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:42:23 +0530 Subject: [PATCH 24/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 711951034d7c..b4c16ae944a2 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,3 +1,15 @@ +''' Radial Basis Function Neural Network (RBFNN) + +A Radial Basis Function Neural Network (RBFNN) is a type of artificial +neural network that uses radial basis functions as activation functions. +RBFNNs are particularly effective for function approximation, regression, +and classification tasks. + +#### Reference + +- Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network +''' + import numpy as np @@ -132,12 +144,9 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) - -from doctest import testmod - # Example Usage if __name__ == "__main__": - testmod() # Running the doctest + # doctest.testmod() # Running the doctest # Sample dataset for XOR problem X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input From 64fc4a7ccaf0df1eeaf98f8e9c728c624b057944 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:12:46 +0000 Subject: [PATCH 25/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index b4c16ae944a2..13040c853421 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,14 +1,14 @@ -''' Radial Basis Function Neural Network (RBFNN) +"""Radial Basis Function Neural Network (RBFNN) -A Radial Basis Function Neural Network (RBFNN) is a type of artificial -neural network that uses radial basis functions as activation functions. -RBFNNs are particularly effective for function approximation, regression, +A Radial Basis Function Neural Network (RBFNN) is a type of artificial +neural network that uses radial basis functions as activation functions. +RBFNNs are particularly effective for function approximation, regression, and classification tasks. #### Reference - Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network -''' +""" import numpy as np @@ -144,6 +144,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) + # Example Usage if __name__ == "__main__": # doctest.testmod() # Running the doctest From 02252128d21f4c7f8a7d0278b24ce4ec0ada0639 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:44:54 +0530 Subject: [PATCH 26/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 13040c853421..57936aa38839 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -10,9 +10,6 @@ - Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network """ -import numpy as np - - class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -144,10 +141,11 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) +import numpy as np + # Example Usage if __name__ == "__main__": - # doctest.testmod() # Running the doctest # Sample dataset for XOR problem X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input From 42e34a1d4c37590ae3aeb020b2b8bbcf55bdab12 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:15:16 +0000 Subject: [PATCH 27/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 57936aa38839..242d30802ee7 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -10,6 +10,7 @@ - Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network """ + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). @@ -141,12 +142,12 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) + import numpy as np # Example Usage if __name__ == "__main__": - # Sample dataset for XOR problem X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) # 2D input y = np.array([[0], [1], [1], [0]]) # Target output for XOR From 8767b572ba1fbfc4694d01a24a0566d643db4f96 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:45:59 +0530 Subject: [PATCH 28/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 242d30802ee7..8fc9eb49e8db 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,4 +1,8 @@ -"""Radial Basis Function Neural Network (RBFNN) +import numpy as np + + +""" +Radial Basis Function Neural Network (RBFNN) A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural network that uses radial basis functions as activation functions. @@ -142,10 +146,6 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) - -import numpy as np - - # Example Usage if __name__ == "__main__": # Sample dataset for XOR problem From e91f39ff961f923fc74c3491744dcdc8b67cc17d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:16:29 +0000 Subject: [PATCH 29/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 1 + 1 file changed, 1 insertion(+) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 8fc9eb49e8db..b9f29e5625fa 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -146,6 +146,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: rbf_outputs = self._compute_rbf_outputs(input_data) return rbf_outputs.dot(self.weights) + # Example Usage if __name__ == "__main__": # Sample dataset for XOR problem From 70f2b0c0dfae0625610cacc849caf02484eb4b07 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:52:39 +0530 Subject: [PATCH 30/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index b9f29e5625fa..a36b97279325 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,5 +1,3 @@ -import numpy as np - """ Radial Basis Function Neural Network (RBFNN) @@ -15,6 +13,9 @@ """ +import numpy as np + + class RadialBasisFunctionNeuralNetwork: """ A simple implementation of a Radial Basis Function Neural Network (RBFNN). From ea1fcee4f40b345041d7d82327d97aab7ae73ea5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:23:02 +0000 Subject: [PATCH 31/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index a36b97279325..b5ab88ad17bc 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,4 +1,3 @@ - """ Radial Basis Function Neural Network (RBFNN) @@ -12,7 +11,6 @@ - Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network """ - import numpy as np From db59731ec48f75dcc2dbc1c12437f1775dcbf9e2 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 08:58:49 +0530 Subject: [PATCH 32/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index b5ab88ad17bc..568937074cae 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -8,7 +8,7 @@ #### Reference -- Wikipedia https://en.wikipedia.org/wiki/Radial_basis_function_network +- Wikipedia: https://en.wikipedia.org/wiki/Radial_basis_function_network """ import numpy as np @@ -96,7 +96,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: target_values (np.ndarray): Target values (num_samples x output_dim). Raises: - ValueError: If number of samples in input_data and target_values not match. + ValueError: If number of samples in input_data and target_values do not match. Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) From e7c58c9fea5ca98ec777b6749cab4d51aaf5f2b7 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:14:02 +0530 Subject: [PATCH 33/39] Update radial_basis_function_neural_network.py --- neural_network/radial_basis_function_neural_network.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 568937074cae..a9cac4ce9bce 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -81,6 +81,8 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: array([[1. , 0.60653066], [0.60653066, 1. ]]) """ + assert self.centers is not None, "Centers initialized before computing outputs." + rbf_outputs = np.zeros((input_data.shape[0], self.num_centers)) for i, center in enumerate(self.centers): for j in range(input_data.shape[0]): @@ -96,7 +98,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: target_values (np.ndarray): Target values (num_samples x output_dim). Raises: - ValueError: If number of samples in input_data and target_values do not match. + ValueError: If number of samples in input_data and target_values not match. Examples: >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) @@ -135,7 +137,7 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: np.ndarray: Predicted values (num_samples x output_dim). Examples: - >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2, spread=1.0) + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=2,spread=1.0) >>> rbf_nn.centers = np.array([[0, 0], [1, 1]]) >>> rbf_nn.weights = np.array([[0.5], [0.5]]) >>> rbf_nn.predict(np.array([[0, 0], [1, 1]])) @@ -159,3 +161,4 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Predict using the trained model predictions = rbf_nn.predict(X) print("Predictions:\n", predictions) + From 352458d40fe040f15585a8c497cadd807fbc6e5f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:44:24 +0000 Subject: [PATCH 34/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- neural_network/radial_basis_function_neural_network.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index a9cac4ce9bce..ba79d83f2dc0 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -82,7 +82,7 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: [0.60653066, 1. ]]) """ assert self.centers is not None, "Centers initialized before computing outputs." - + rbf_outputs = np.zeros((input_data.shape[0], self.num_centers)) for i, center in enumerate(self.centers): for j in range(input_data.shape[0]): @@ -161,4 +161,3 @@ def predict(self, input_data: np.ndarray) -> np.ndarray: # Predict using the trained model predictions = rbf_nn.predict(X) print("Predictions:\n", predictions) - From 911ed3a22f4353a817d498bf90ecebb2bc92713a Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:36:46 +0530 Subject: [PATCH 35/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index ba79d83f2dc0..7998c88f9df0 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -40,8 +40,8 @@ def __init__(self, num_centers: int, spread: float) -> None: """ self.num_centers = num_centers self.spread = spread - self.centers: np.ndarray = None # To be initialized during training - self.weights: np.ndarray = None # To be initialized during training + self.centers: np.ndarray = None + self.weights: np.ndarray = None def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: """ @@ -60,9 +60,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) 0.1353352832366127 """ - return np.exp( - -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) - ) + # Calculate the squared distances + distances = np.linalg.norm(input_data[:, np.newaxis] - centers, axis=2)**2 + return np.exp(-distances / (2 * self.spread**2)) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ @@ -82,12 +82,7 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: [0.60653066, 1. ]]) """ assert self.centers is not None, "Centers initialized before computing outputs." - - rbf_outputs = np.zeros((input_data.shape[0], self.num_centers)) - for i, center in enumerate(self.centers): - for j in range(input_data.shape[0]): - rbf_outputs[j, i] = self._gaussian_rbf(input_data[j], center) - return rbf_outputs + return self._gaussian_rbf(input_data, self.centers) def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: """ @@ -109,22 +104,18 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: True """ if input_data.shape[0] != target_values.shape[0]: - raise ValueError( - "Number of samples in input_data and target_values must match." - ) + raise ValueError("Number of samples in input_data and target_values must match.") # Initialize centers using random samples from input_data - rng = np.random.default_rng() # Create a random number generator - random_indices = rng.choice( - input_data.shape[0], self.num_centers, replace=False - ) + rng = np.random.default_rng() + random_indices = rng.choice(input_data.shape[0], self.num_centers, replace=False) self.centers = input_data[random_indices] # Compute the RBF outputs for the training data rbf_outputs = self._compute_rbf_outputs(input_data) # Calculate weights using the pseudo-inverse - self.weights = np.linalg.pinv(rbf_outputs).dot(target_values) + self.weights = np.linalg.pinv(rbf_outputs).dot(target_values) def predict(self, input_data: np.ndarray) -> np.ndarray: """ From 52e01d189880413673f449bf5ec1039b856d686a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:07:08 +0000 Subject: [PATCH 36/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 7998c88f9df0..922e825f4878 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -61,7 +61,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ # Calculate the squared distances - distances = np.linalg.norm(input_data[:, np.newaxis] - centers, axis=2)**2 + distances = np.linalg.norm(input_data[:, np.newaxis] - centers, axis=2) ** 2 return np.exp(-distances / (2 * self.spread**2)) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: @@ -104,18 +104,22 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: True """ if input_data.shape[0] != target_values.shape[0]: - raise ValueError("Number of samples in input_data and target_values must match.") + raise ValueError( + "Number of samples in input_data and target_values must match." + ) # Initialize centers using random samples from input_data rng = np.random.default_rng() - random_indices = rng.choice(input_data.shape[0], self.num_centers, replace=False) + random_indices = rng.choice( + input_data.shape[0], self.num_centers, replace=False + ) self.centers = input_data[random_indices] # Compute the RBF outputs for the training data rbf_outputs = self._compute_rbf_outputs(input_data) # Calculate weights using the pseudo-inverse - self.weights = np.linalg.pinv(rbf_outputs).dot(target_values) + self.weights = np.linalg.pinv(rbf_outputs).dot(target_values) def predict(self, input_data: np.ndarray) -> np.ndarray: """ From 49272c7424660d2ba66e9ecf7244375a26bf7925 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:42:17 +0530 Subject: [PATCH 37/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 922e825f4878..826d048ab279 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,13 +1,19 @@ """ Radial Basis Function Neural Network (RBFNN) -A Radial Basis Function Neural Network (RBFNN) is a type of artificial -neural network that uses radial basis functions as activation functions. -RBFNNs are particularly effective for function approximation, regression, -and classification tasks. +A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural +network that uses radial basis functions as activation functions. +RBFNNs are particularly effective for function approximation, regression, and +classification tasks. The architecture typically consists of an input layer, +a hidden layer with radial basis functions, and an output layer. + +In an RBFNN: +- The hidden layer applies a radial basis function (often Gaussian) to the +input data, transforming it into a higher-dimensional space. +- The output layer combines the results from the hidden layer using +weighted sums to produce the final output. #### Reference - - Wikipedia: https://en.wikipedia.org/wiki/Radial_basis_function_network """ @@ -34,7 +40,7 @@ def __init__(self, num_centers: int, spread: float) -> None: spread (float): Spread of the radial basis functions. Examples: - >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3, spread=1.0) + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3,spread=1.0) >>> rbf_nn.num_centers 3 """ @@ -61,7 +67,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ # Calculate the squared distances - distances = np.linalg.norm(input_data[:, np.newaxis] - centers, axis=2) ** 2 + distances = np.linalg.norm(input_vector[:, np.newaxis] - center, axis=2)** 2 return np.exp(-distances / (2 * self.spread**2)) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: From 371f23a183982fbf438f39d4c76a4c42f0ee6584 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 04:12:39 +0000 Subject: [PATCH 38/39] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../radial_basis_function_neural_network.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 826d048ab279..3bc6f9c13fb5 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,16 +1,16 @@ """ Radial Basis Function Neural Network (RBFNN) -A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural -network that uses radial basis functions as activation functions. -RBFNNs are particularly effective for function approximation, regression, and -classification tasks. The architecture typically consists of an input layer, +A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural +network that uses radial basis functions as activation functions. +RBFNNs are particularly effective for function approximation, regression, and +classification tasks. The architecture typically consists of an input layer, a hidden layer with radial basis functions, and an output layer. In an RBFNN: -- The hidden layer applies a radial basis function (often Gaussian) to the +- The hidden layer applies a radial basis function (often Gaussian) to the input data, transforming it into a higher-dimensional space. -- The output layer combines the results from the hidden layer using +- The output layer combines the results from the hidden layer using weighted sums to produce the final output. #### Reference @@ -67,7 +67,7 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: 0.1353352832366127 """ # Calculate the squared distances - distances = np.linalg.norm(input_vector[:, np.newaxis] - center, axis=2)** 2 + distances = np.linalg.norm(input_vector[:, np.newaxis] - center, axis=2) ** 2 return np.exp(-distances / (2 * self.spread**2)) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: From aace14060ec6b0417e6c4e1373782e119bb0dc60 Mon Sep 17 00:00:00 2001 From: Jenina Angelin Date: Wed, 30 Oct 2024 09:43:48 +0530 Subject: [PATCH 39/39] Update radial_basis_function_neural_network.py --- .../radial_basis_function_neural_network.py | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/neural_network/radial_basis_function_neural_network.py b/neural_network/radial_basis_function_neural_network.py index 3bc6f9c13fb5..ba79d83f2dc0 100644 --- a/neural_network/radial_basis_function_neural_network.py +++ b/neural_network/radial_basis_function_neural_network.py @@ -1,19 +1,13 @@ """ Radial Basis Function Neural Network (RBFNN) -A Radial Basis Function Neural Network (RBFNN) is a type of artificial neural -network that uses radial basis functions as activation functions. -RBFNNs are particularly effective for function approximation, regression, and -classification tasks. The architecture typically consists of an input layer, -a hidden layer with radial basis functions, and an output layer. - -In an RBFNN: -- The hidden layer applies a radial basis function (often Gaussian) to the -input data, transforming it into a higher-dimensional space. -- The output layer combines the results from the hidden layer using -weighted sums to produce the final output. +A Radial Basis Function Neural Network (RBFNN) is a type of artificial +neural network that uses radial basis functions as activation functions. +RBFNNs are particularly effective for function approximation, regression, +and classification tasks. #### Reference + - Wikipedia: https://en.wikipedia.org/wiki/Radial_basis_function_network """ @@ -40,14 +34,14 @@ def __init__(self, num_centers: int, spread: float) -> None: spread (float): Spread of the radial basis functions. Examples: - >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3,spread=1.0) + >>> rbf_nn = RadialBasisFunctionNeuralNetwork(num_centers=3, spread=1.0) >>> rbf_nn.num_centers 3 """ self.num_centers = num_centers self.spread = spread - self.centers: np.ndarray = None - self.weights: np.ndarray = None + self.centers: np.ndarray = None # To be initialized during training + self.weights: np.ndarray = None # To be initialized during training def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: """ @@ -66,9 +60,9 @@ def _gaussian_rbf(self, input_vector: np.ndarray, center: np.ndarray) -> float: >>> rbf_nn._gaussian_rbf(np.array([0, 0]), center) 0.1353352832366127 """ - # Calculate the squared distances - distances = np.linalg.norm(input_vector[:, np.newaxis] - center, axis=2) ** 2 - return np.exp(-distances / (2 * self.spread**2)) + return np.exp( + -(np.linalg.norm(input_vector - center) ** 2) / (2 * self.spread**2) + ) def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: """ @@ -88,7 +82,12 @@ def _compute_rbf_outputs(self, input_data: np.ndarray) -> np.ndarray: [0.60653066, 1. ]]) """ assert self.centers is not None, "Centers initialized before computing outputs." - return self._gaussian_rbf(input_data, self.centers) + + rbf_outputs = np.zeros((input_data.shape[0], self.num_centers)) + for i, center in enumerate(self.centers): + for j in range(input_data.shape[0]): + rbf_outputs[j, i] = self._gaussian_rbf(input_data[j], center) + return rbf_outputs def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: """ @@ -115,7 +114,7 @@ def fit(self, input_data: np.ndarray, target_values: np.ndarray) -> None: ) # Initialize centers using random samples from input_data - rng = np.random.default_rng() + rng = np.random.default_rng() # Create a random number generator random_indices = rng.choice( input_data.shape[0], self.num_centers, replace=False )