Skip to content

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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
101 changes: 101 additions & 0 deletions neural_network/adaptive_resonance_theory_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
Name - - ART1 - Adaptive Resonance Theory 1
Goal - - Cluster Binary Data
Detail: Unsupervised clustering model using a vigilance parameter
to control cluster formation in binary datasets.
* Initialize with features and vigilance threshold
* Train to form clusters based on input patterns
* Predict for assigning new inputs to clusters
Author: Your Name
Github: [email protected]
Date: 2024.10.31
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
"""

import numpy as np


class ART1:
def __init__(self, num_features, vigilance=0.8):

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: num_features

Please provide type hint for the parameter: vigilance

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: num_features

Please provide type hint for the parameter: vigilance

"""
Initialize the ART1 model with the number of features and the vigilance parameter.

Check failure on line 22 in neural_network/adaptive_resonance_theory_1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory_1.py:22:89: E501 Line too long (90 > 88)

Parameters:
num_features (int): Number of features in input binary data.
vigilance (float): Vigilance parameter to control cluster formation (0 < vigilance <= 1).

Check failure on line 26 in neural_network/adaptive_resonance_theory_1.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

neural_network/adaptive_resonance_theory_1.py:26:89: E501 Line too long (97 > 88)
"""
self.num_features = num_features
self.vigilance = vigilance
self.weights = [] # Stores the weights for clusters

def _similarity(self, x, w):

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/adaptive_resonance_theory_1.py, please provide doctest for the function _similarity

Please provide return type hint for the function: _similarity. 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 descriptive name for the parameter: w

Please provide type hint for the parameter: w

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/adaptive_resonance_theory_1.py, please provide doctest for the function _similarity

Please provide return type hint for the function: _similarity. 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 descriptive name for the parameter: w

Please provide type hint for the parameter: w

"""
Calculate similarity between input vector x and weight vector w.

Parameters:
x (np.array): Input binary vector.
w (np.array): Cluster weight vector.

Returns:
float: Similarity value based on the intersection over the input length.
"""
return np.sum(np.minimum(x, w)) / np.sum(x)

def _weight_update(self, x, w):

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/adaptive_resonance_theory_1.py, please provide doctest for the function _weight_update

Please provide return type hint for the function: _weight_update. 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 descriptive name for the parameter: w

Please provide type hint for the parameter: w

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/adaptive_resonance_theory_1.py, please provide doctest for the function _weight_update

Please provide return type hint for the function: _weight_update. 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 descriptive name for the parameter: w

Please provide type hint for the parameter: w

"""
Update weights for a cluster based on input vector.

Parameters:
x (np.array): Input binary vector.
w (np.array): Cluster weight vector.

Returns:
np.array: Updated weight vector.
"""
return np.minimum(x, w)

def train(self, data):

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/adaptive_resonance_theory_1.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: data

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/adaptive_resonance_theory_1.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: data

"""
Train the ART1 model to form clusters based on the vigilance parameter.

Parameters:
data (np.array): Binary dataset with each row as a sample.
"""
for x in data:
assigned = False
for i, w in enumerate(self.weights):
# Check similarity and update weights if similarity exceeds vigilance
similarity = self._similarity(x, w)
if similarity >= self.vigilance:
self.weights[i] = self._weight_update(x, w)
assigned = True
break
if not assigned:
self.weights.append(x.copy())

def predict(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/adaptive_resonance_theory_1.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 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/adaptive_resonance_theory_1.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 descriptive name for the parameter: x

Please provide type hint for the parameter: x

"""
Predict the cluster for a new input vector or classify it as a new cluster.

Parameters:
x (np.array): Input binary vector.

Returns:
int: Cluster index for the input or -1 if classified as a new cluster.
"""
for i, w in enumerate(self.weights):
# Check similarity for prediction
similarity = self._similarity(x, w)
if similarity >= self.vigilance:
return i
return -1

def get_weights(self):

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/adaptive_resonance_theory_1.py, please provide doctest for the function get_weights

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

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

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

"""
Retrieve the weight vectors of the clusters.

Returns:
list: List of weight vectors for each cluster.
"""
return self.weights
94 changes: 94 additions & 0 deletions neural_network/radial_basis_function_neural_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
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):

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.

: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

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

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

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.

: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

# Calculate the error
error = y_i - output

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