Skip to content

Issue 12322 #12323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b6e2f37
add rbfnn algorithm solving issue 12322
JeninaAngelin Oct 30, 2024
74212a6
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
5fbe96d
add rbfnn in directory solving issue 12322
JeninaAngelin Oct 30, 2024
f66b55f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
bb7ac35
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
aeb9015
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
2dadd7f
correcting errors in radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
ca15518
add doctests to radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
03eed71
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
90f0a5f
breaking long lines in radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
79239b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
4c7d0a2
add docstrings to radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
ae74131
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
c47d8e3
adjust sorting libraries radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
0831d48
resorting libraries radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
f7a69e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
d00b366
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
21dcb4d
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
30dc616
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
301d98f
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
d76d6bb
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
fe4a0c9
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
42826a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
1140d30
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
64fc4a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
0225212
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
42e34a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
8767b57
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
e91f39f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
70f2b0c
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
ea1fcee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
db59731
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
e7c58c9
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
352458d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
911ed3a
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
52e01d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
49272c7
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
371f23a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2024
aace140
Update radial_basis_function_neural_network.py
JeninaAngelin Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
124 changes: 124 additions & 0 deletions neural_network/radial_basis_function_neural_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import numpy as np


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.

Reference:
Radial Basis Function Network: https://en.wikipedia.org/wiki/Radial_basis_function_network
"""

def __init__(self, n_centers: int, sigma: float):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

"""
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.
"""
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

def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x

"""
Calculate the Gaussian radial basis function.

Args:
x (np.ndarray): Input vector.
center (np.ndarray): Center of the RBF.

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))

def _compute_rbf(self, x: np.ndarray) -> np.ndarray:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function _compute_rbf

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function _compute_rbf

Please provide descriptive name for the parameter: x

"""
Compute the output of the radial basis functions for input data.

Args:
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.

Check failure on line 56 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/radial_basis_function_neural_network.py:56:89: E501 Line too long (95 > 88)
"""
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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function fit

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide descriptive name for the parameter: y

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function fit

Please provide return type hint for the function: fit. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: x

Please provide descriptive name for the parameter: y

"""
Train the RBFNN on the provided data.

Args:
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.
"""
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)

Check failure on line 79 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

neural_network/radial_basis_function_neural_network.py:79:26: NPY002 Replace legacy `np.random.choice` call with `np.random.Generator`
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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function predict

Please provide descriptive name for the parameter: x

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file neural_network/radial_basis_function_neural_network.py, please provide doctest for the function predict

Please provide descriptive name for the parameter: x

"""
Predict the output for the given input data.

Args:
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)
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)

# Sample Expected Output:
# Predictions:
# [[0.24826229]
# [0.06598867]
# [0.06598867]
# [0.24826229]]



Loading