Skip to content

Commit a3bf625

Browse files
authored
Update radial_basis_function_neural_network.py
1 parent 2a8b201 commit a3bf625

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
2+
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
33
Name - - RBFNN - Radial Basis Function Neural Network
44
Goal - - Recognize Patterns in Data
55
Detail: Total 3 layers neural network
@@ -9,85 +9,81 @@
99
Author: Your Name
1010
1111
Date: 2024.10.31
12-
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
12+
- - - - - -- - - - - - - - - - - - - - - - - - - - - - -
1313
"""
1414

1515
import numpy as np # For numerical operations
1616

17-
1817
class RBFNN:
1918
def __init__(self, input_size, hidden_size, output_size):
2019
"""
2120
Initialize the RBFNN parameters.
22-
21+
2322
:param input_size: Number of input features
2423
:param hidden_size: Number of hidden units in the RBF layer
2524
:param output_size: Number of output classes
2625
"""
2726
self.input_size = input_size # Size of input layer
2827
self.hidden_size = hidden_size # Size of hidden layer
2928
self.output_size = output_size # Size of output layer
30-
29+
30+
rng = np.random.default_rng() # Create a random number generator
3131
# Initialize centers and spread of the RBF neurons
32-
self.centers = np.random.rand(hidden_size, input_size) # Centers for RBF
33-
self.spread = np.random.rand(hidden_size) # Spread for each RBF
32+
self.centers = rng.random((hidden_size, input_size)) # Centers for RBF
33+
self.spread = rng.random(hidden_size) # Spread for each RBF
3434

3535
# Initialize weights for the output layer
36-
self.weights = np.random.rand(
37-
hidden_size, output_size
38-
) # Weights for output layer
36+
self.weights = rng.random((hidden_size, output_size)) # Weights for output layer
3937

4038
def rbf(self, x, center, spread):
41-
"""Radial Basis Function (Gaussian)."""
42-
return np.exp(-(np.linalg.norm(x - center) ** 2) / (2 * spread**2))
39+
""" Radial Basis Function (Gaussian). """
40+
return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * spread ** 2))
4341

4442
def forward(self, x):
45-
"""Forward pass through the network."""
43+
""" Forward pass through the network. """
4644
hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer
4745
for i in range(self.hidden_size):
48-
hidden_outputs[i] = self.rbf(
49-
x, self.centers[i], self.spread[i]
50-
) # Compute RBF outputs
46+
hidden_outputs[i] = self.rbf(x, self.centers[i], self.spread[i]) # Compute RBF outputs
5147

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

55-
def train(self, X, y, epochs, learning_rate):
51+
def train(self, x_train, y_train, epochs, learning_rate):
5652
"""
5753
Train the RBFNN model.
58-
59-
:param X: Input data
60-
:param y: Target output
54+
55+
:param x_train: Input data
56+
:param y_train: Target output
6157
:param epochs: Number of training iterations
6258
:param learning_rate: Learning rate for weight updates
6359
"""
64-
for epoch in range(epochs):
65-
for i in range(len(X)):
66-
x_i = X[i]
67-
y_i = y[i]
60+
for _ in range(epochs): # Use underscore for unused loop variable
61+
for i in range(len(x_train)):
62+
x_i = x_train[i]
63+
y_i = y_train[i]
6864

6965
# Forward pass
7066
hidden_outputs = np.zeros(self.hidden_size)
7167
for j in range(self.hidden_size):
7268
hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j])
7369

7470
output = np.dot(hidden_outputs, self.weights) # Output layer
75-
71+
7672
# Calculate the error
7773
error = y_i - output
78-
74+
7975
# Update weights
8076
self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error
8177

82-
def predict(self, X):
78+
def predict(self, x_test):
8379
"""
8480
Predict outputs for given input data.
85-
86-
:param X: Input data
81+
82+
:param x_test: Input data
8783
:return: Predicted outputs
8884
"""
8985
predictions = []
90-
for x in X:
86+
for x in x_test:
9187
output = self.forward(x) # Forward pass to get prediction
9288
predictions.append(output)
9389
return np.array(predictions)

0 commit comments

Comments
 (0)