Skip to content
Closed
Changes from 5 commits
Commits
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
89 changes: 89 additions & 0 deletions neural_network/radial_basis_function_neural_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -

Check failure on line 2 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/radial_basis_function_neural_network.py:2:58: W291 Trailing whitespace
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
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -

Check failure on line 12 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

neural_network/radial_basis_function_neural_network.py:12:57: W291 Trailing whitespace
"""

import numpy as np # For numerical operations

class RBFNN:

Check failure on line 17 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

neural_network/radial_basis_function_neural_network.py:15:1: I001 Import block is un-sorted or un-formatted
def __init__(self, input_size, hidden_size, output_size):

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

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

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

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

"""
Initialize the RBFNN parameters.

Check failure on line 21 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/radial_basis_function_neural_network.py:21:1: W293 Blank line contains whitespace
: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

Check failure on line 29 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/radial_basis_function_neural_network.py:29:1: W293 Blank line contains whitespace
rng = np.random.default_rng() # Create a random number generator
# Initialize centers and spread of the RBF neurons
self.centers = rng.random((hidden_size, input_size)) # Centers for RBF
self.spread = rng.random(hidden_size) # Spread for each RBF

# Initialize weights for the output layer
self.weights = rng.random((hidden_size, output_size)) # Weights for output layer

Check failure on line 36 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:36:89: E501 Line too long (89 > 88)

def rbf(self, x, center, spread):

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 rbf

Please provide return type hint for the function: rbf. 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 type hint for the parameter: x

Please provide type hint for the parameter: center

Please provide type hint for the parameter: spread

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 rbf

Please provide return type hint for the function: rbf. 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 type hint for the parameter: x

Please provide type hint for the parameter: center

Please provide type hint for the parameter: spread

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 rbf

Please provide return type hint for the function: rbf. 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 type hint for the parameter: x

Please provide type hint for the parameter: center

Please provide type hint for the parameter: spread

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 rbf

Please provide return type hint for the function: rbf. 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 type hint for the parameter: x

Please provide type hint for the parameter: center

Please provide type hint for the parameter: spread

""" Radial Basis Function (Gaussian). """
return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2))

def forward(self, 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 forward

Please provide return type hint for the function: forward. 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 type hint 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 forward

Please provide return type hint for the function: forward. 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 type hint 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 forward

Please provide return type hint for the function: forward. 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 type hint 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 forward

Please provide return type hint for the function: forward. 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 type hint for the parameter: x

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

Check failure on line 46 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:46:89: E501 Line too long (99 > 88)

output = np.dot(hidden_outputs, self.weights) # Compute final output
return output

def train(self, x_train, y_train, epochs, learning_rate):

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 train

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

Please provide type hint for the parameter: x_train

Please provide type hint for the parameter: y_train

Please provide type hint for the parameter: epochs

Please provide type hint for the parameter: learning_rate

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 train

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

Please provide type hint for the parameter: x_train

Please provide type hint for the parameter: y_train

Please provide type hint for the parameter: epochs

Please provide type hint for the parameter: learning_rate

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 train

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

Please provide type hint for the parameter: x_train

Please provide type hint for the parameter: y_train

Please provide type hint for the parameter: epochs

Please provide type hint for the parameter: learning_rate

"""
Train the RBFNN model.

Check failure on line 54 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/radial_basis_function_neural_network.py:54:1: W293 Blank line contains whitespace
:param x_train: Input data
:param y_train: Target output
:param epochs: Number of training iterations
:param learning_rate: Learning rate for weight updates
"""
for _ in range(epochs): # Use underscore for unused loop variable
for i in range(len(x_train)):
x_i = x_train[i]
y_i = y_train[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

Check failure on line 71 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/radial_basis_function_neural_network.py:71:1: W293 Blank line contains whitespace
# Calculate the error
error = y_i - output

Check failure on line 74 in neural_network/radial_basis_function_neural_network.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

neural_network/radial_basis_function_neural_network.py:74:1: W293 Blank line contains whitespace
# Update weights
self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error

def predict(self, x_test):

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 return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x_test

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 return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x_test

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 return type hint for the function: predict. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x_test

"""
Predict outputs for given input data.

:param x_test: Input data
:return: Predicted outputs
"""
predictions = []
for x in x_test:
output = self.forward(x) # Forward pass to get prediction
predictions.append(output)
return np.array(predictions)