-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Issue 12322 #12323
Changes from 5 commits
b6e2f37
74212a6
5fbe96d
f66b55f
bb7ac35
aeb9015
2dadd7f
ca15518
03eed71
90f0a5f
79239b5
4c7d0a2
ae74131
c47d8e3
0831d48
f7a69e3
d00b366
21dcb4d
30dc616
301d98f
d76d6bb
fe4a0c9
42826a1
1140d30
64fc4a7
0225212
42e34a1
8767b57
e91f39f
70f2b0c
ea1fcee
db59731
e7c58c9
352458d
911ed3a
52e01d1
49272c7
371f23a
aace140
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
""" | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: |
||
""" | ||
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. | ||
""" | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
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) | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide descriptive name for the parameter: |
||
""" | ||
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]] | ||
|
||
|
||
|
There was a problem hiding this comment.
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: