1
1
import numpy as np
2
- from typing import List , Optional
3
2
4
3
5
4
class RadialBasisFunctionNeuralNetwork :
@@ -10,6 +9,7 @@ class RadialBasisFunctionNeuralNetwork:
10
9
centers (np.ndarray): Centers of the radial basis functions.
11
10
weights (np.ndarray): Weights for the output layer.
12
11
sigma (float): Spread of the radial basis functions.
12
+
13
13
Reference:
14
14
Radial Basis Function Network: https://en.wikipedia.org/wiki/Radial_basis_function_network
15
15
"""
@@ -24,8 +24,8 @@ def __init__(self, n_centers: int, sigma: float):
24
24
"""
25
25
self .n_centers = n_centers
26
26
self .sigma = sigma
27
- self .centers : Optional [ np .ndarray ] = None # To be initialized during training
28
- self .weights : Optional [ np .ndarray ] = None # To be initialized during training
27
+ self .centers : np .ndarray | None = None # To be initialized during training
28
+ self .weights : np .ndarray | None = None # To be initialized during training
29
29
30
30
def _gaussian (self , x : np .ndarray , center : np .ndarray ) -> float :
31
31
"""
@@ -45,57 +45,57 @@ def _gaussian(self, x: np.ndarray, center: np.ndarray) -> float:
45
45
"""
46
46
return np .exp (- (np .linalg .norm (x - center ) ** 2 ) / (2 * self .sigma ** 2 ))
47
47
48
- def _compute_rbf (self , X : np .ndarray ) -> np .ndarray :
48
+ def _compute_rbf (self , x : np .ndarray ) -> np .ndarray :
49
49
"""
50
50
Compute the output of the radial basis functions for input data.
51
51
52
52
Args:
53
- X (np.ndarray): Input data matrix (num_samples x num_features).
53
+ x (np.ndarray): Input data matrix (num_samples x num_features).
54
54
55
55
Returns:
56
56
np.ndarray: A matrix of shape (num_samples x n_centers) containing the RBF outputs.
57
57
"""
58
- rbf_outputs = np .zeros ((X .shape [0 ], self .n_centers ))
58
+ rbf_outputs = np .zeros ((x .shape [0 ], self .n_centers ))
59
59
for i , center in enumerate (self .centers ):
60
- for j in range (X .shape [0 ]):
61
- rbf_outputs [j , i ] = self ._gaussian (X [j ], center )
60
+ for j in range (x .shape [0 ]):
61
+ rbf_outputs [j , i ] = self ._gaussian (x [j ], center )
62
62
return rbf_outputs
63
63
64
- def fit (self , X : np .ndarray , y : np .ndarray ):
64
+ def fit (self , x : np .ndarray , y : np .ndarray ):
65
65
"""
66
66
Train the RBFNN on the provided data.
67
67
68
68
Args:
69
- X (np.ndarray): Input data matrix (num_samples x num_features).
69
+ x (np.ndarray): Input data matrix (num_samples x num_features).
70
70
y (np.ndarray): Target values (num_samples x output_dim).
71
71
72
72
Raises:
73
- ValueError: If number of samples in X and y do not match.
73
+ ValueError: If number of samples in x and y do not match.
74
74
"""
75
- if X .shape [0 ] != y .shape [0 ]:
76
- raise ValueError ("Number of samples in X and y must match." )
75
+ if x .shape [0 ] != y .shape [0 ]:
76
+ raise ValueError ("Number of samples in x and y must match." )
77
77
78
- # Initialize centers using random samples from X
79
- random_indices = np .random .choice (X .shape [0 ], self .n_centers , replace = False )
80
- self .centers = X [random_indices ]
78
+ # Initialize centers using random samples from x
79
+ random_indices = np .random .choice (x .shape [0 ], self .n_centers , replace = False )
80
+ self .centers = x [random_indices ]
81
81
82
82
# Compute the RBF outputs for the training data
83
- rbf_outputs = self ._compute_rbf (X )
83
+ rbf_outputs = self ._compute_rbf (x )
84
84
85
85
# Calculate weights using the pseudo-inverse
86
86
self .weights = np .linalg .pinv (rbf_outputs ).dot (y )
87
87
88
- def predict (self , X : np .ndarray ) -> np .ndarray :
88
+ def predict (self , x : np .ndarray ) -> np .ndarray :
89
89
"""
90
90
Predict the output for the given input data.
91
91
92
92
Args:
93
- X (np.ndarray): Input data matrix (num_samples x num_features).
93
+ x (np.ndarray): Input data matrix (num_samples x num_features).
94
94
95
95
Returns:
96
96
np.ndarray: Predicted values (num_samples x output_dim).
97
97
"""
98
- rbf_outputs = self ._compute_rbf (X )
98
+ rbf_outputs = self ._compute_rbf (x )
99
99
return rbf_outputs .dot (self .weights )
100
100
101
101
@@ -113,9 +113,12 @@ def predict(self, X: np.ndarray) -> np.ndarray:
113
113
predictions = rbf_nn .predict (X )
114
114
print ("Predictions:\n " , predictions )
115
115
116
- # Expected Output:
116
+ # Sample Expected Output:
117
117
# Predictions:
118
118
# [[0.24826229]
119
119
# [0.06598867]
120
120
# [0.06598867]
121
121
# [0.24826229]]
122
+
123
+
124
+
0 commit comments