-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Create radial_basis_function_neural_network #12342
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
Changes from 3 commits
b8a4a9f
923dcae
fa6233a
2a8b201
a3bf625
c2a5cd0
f6028cc
6d8e719
4b5fa39
3d4150a
a65dcc2
b52bc75
8196fd2
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,88 @@ | ||
""" | ||
- - - - - -- - - - - - - - - - - - - - - - - - - - - - - | ||
Name - - RBFNN - Radial Basis Function Neural Network | ||
Goal - - Recognize Patterns in Data | ||
Detail: Total 3 layers neural network | ||
* Input layer | ||
* Hidden layer with RBF activation | ||
* Output layer | ||
Author: Your Name | ||
Github: [email protected] | ||
Date: 2024.10.31 | ||
- - - - - -- - - - - - - - - - - - - - - - - - - - - - - | ||
""" | ||
|
||
import numpy as np # For numerical operations | ||
|
||
class RBFNN: | ||
def __init__(self, input_size, hidden_size, output_size): | ||
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: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint 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 return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint 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 return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
""" | ||
Initialize the RBFNN parameters. | ||
|
||
:param input_size: Number of input features | ||
:param hidden_size: Number of hidden units in the RBF layer | ||
:param output_size: Number of output classes | ||
""" | ||
self.input_size = input_size # Size of input layer | ||
self.hidden_size = hidden_size # Size of hidden layer | ||
self.output_size = output_size # Size of output layer | ||
|
||
# Initialize centers and spread of the RBF neurons | ||
self.centers = np.random.rand(hidden_size, input_size) # Centers for RBF | ||
self.spread = np.random.rand(hidden_size) # Spread for each RBF | ||
|
||
# Initialize weights for the output layer | ||
self.weights = np.random.rand(hidden_size, output_size) # Weights for output layer | ||
Check failure on line 35 in neural_network/radial_basis_function_neural_network.py
|
||
|
||
def rbf(self, x, center, spread): | ||
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 type hint for the parameter: Please provide type hint for the parameter: Please provide type hint 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 type hint for the parameter: Please provide type hint for the parameter: Please provide type hint 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 type hint for the parameter: Please provide type hint for the parameter: Please provide type hint 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 type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
""" Radial Basis Function (Gaussian). """ | ||
return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2)) | ||
|
||
def forward(self, x): | ||
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 type hint 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 type hint 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 type hint 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 type hint for the parameter: |
||
""" Forward pass through the network. """ | ||
hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer | ||
for i in range(self.hidden_size): | ||
hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs | ||
|
||
output = np.dot(hidden_outputs, self.weights) # Compute final output | ||
return output | ||
|
||
def train(self, X, y, epochs, learning_rate): | ||
|
||
""" | ||
Train the RBFNN model. | ||
|
||
:param X: Input data | ||
:param y: Target output | ||
:param epochs: Number of training iterations | ||
:param learning_rate: Learning rate for weight updates | ||
""" | ||
for epoch in range(epochs): | ||
for i in range(len(X)): | ||
x_i = X[i] | ||
y_i = y[i] | ||
|
||
# Forward pass | ||
hidden_outputs = np.zeros(self.hidden_size) | ||
for j in range(self.hidden_size): | ||
hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j]) | ||
|
||
output = np.dot(hidden_outputs, self.weights) # Output layer | ||
|
||
# Calculate the error | ||
error = y_i - output | ||
|
||
# Update weights | ||
self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error | ||
|
||
def predict(self, X): | ||
|
||
""" | ||
Predict outputs for given input data. | ||
|
||
:param X: Input data | ||
:return: Predicted outputs | ||
""" | ||
predictions = [] | ||
for x in X: | ||
output = self.forward(x) # Forward pass to get prediction | ||
predictions.append(output) | ||
return np.array(predictions) |
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:
Please provide type hint for the parameter:
input_size
Please provide type hint for the parameter:
hidden_size
Please provide type hint for the parameter:
output_size