|
1 | 1 | """
|
2 |
| - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - |
| 2 | + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - |
3 | 3 | Name - - RBFNN - Radial Basis Function Neural Network
|
4 | 4 | Goal - - Recognize Patterns in Data
|
5 | 5 | Detail: Total 3 layers neural network
|
|
9 | 9 | Author: Your Name
|
10 | 10 |
|
11 | 11 | Date: 2024.10.31
|
12 |
| -- - - - - -- - - - - - - - - - - - - - - - - - - - - - - |
| 12 | +- - - - - -- - - - - - - - - - - - - - - - - - - - - - - |
13 | 13 | """
|
14 | 14 |
|
15 | 15 | import numpy as np # For numerical operations
|
16 | 16 |
|
17 |
| - |
18 | 17 | class RBFNN:
|
19 | 18 | def __init__(self, input_size, hidden_size, output_size):
|
20 | 19 | """
|
21 | 20 | Initialize the RBFNN parameters.
|
22 |
| -
|
| 21 | + |
23 | 22 | :param input_size: Number of input features
|
24 | 23 | :param hidden_size: Number of hidden units in the RBF layer
|
25 | 24 | :param output_size: Number of output classes
|
26 | 25 | """
|
27 | 26 | self.input_size = input_size # Size of input layer
|
28 | 27 | self.hidden_size = hidden_size # Size of hidden layer
|
29 | 28 | self.output_size = output_size # Size of output layer
|
30 |
| - |
| 29 | + |
| 30 | + rng = np.random.default_rng() # Create a random number generator |
31 | 31 | # 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 |
34 | 34 |
|
35 | 35 | # 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 |
39 | 37 |
|
40 | 38 | 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)) |
43 | 41 |
|
44 | 42 | def forward(self, x):
|
45 |
| - """Forward pass through the network.""" |
| 43 | + """ Forward pass through the network. """ |
46 | 44 | hidden_outputs = np.zeros(self.hidden_size) # Outputs of hidden layer
|
47 | 45 | 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 |
51 | 47 |
|
52 | 48 | output = np.dot(hidden_outputs, self.weights) # Compute final output
|
53 | 49 | return output
|
54 | 50 |
|
55 |
| - def train(self, X, y, epochs, learning_rate): |
| 51 | + def train(self, x_train, y_train, epochs, learning_rate): |
56 | 52 | """
|
57 | 53 | 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 |
61 | 57 | :param epochs: Number of training iterations
|
62 | 58 | :param learning_rate: Learning rate for weight updates
|
63 | 59 | """
|
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] |
68 | 64 |
|
69 | 65 | # Forward pass
|
70 | 66 | hidden_outputs = np.zeros(self.hidden_size)
|
71 | 67 | for j in range(self.hidden_size):
|
72 | 68 | hidden_outputs[j] = self.rbf(x_i, self.centers[j], self.spread[j])
|
73 | 69 |
|
74 | 70 | output = np.dot(hidden_outputs, self.weights) # Output layer
|
75 |
| - |
| 71 | + |
76 | 72 | # Calculate the error
|
77 | 73 | error = y_i - output
|
78 |
| - |
| 74 | + |
79 | 75 | # Update weights
|
80 | 76 | self.weights += learning_rate * hidden_outputs.reshape(-1, 1) * error
|
81 | 77 |
|
82 |
| - def predict(self, X): |
| 78 | + def predict(self, x_test): |
83 | 79 | """
|
84 | 80 | Predict outputs for given input data.
|
85 |
| -
|
86 |
| - :param X: Input data |
| 81 | + |
| 82 | + :param x_test: Input data |
87 | 83 | :return: Predicted outputs
|
88 | 84 | """
|
89 | 85 | predictions = []
|
90 |
| - for x in X: |
| 86 | + for x in x_test: |
91 | 87 | output = self.forward(x) # Forward pass to get prediction
|
92 | 88 | predictions.append(output)
|
93 | 89 | return np.array(predictions)
|
0 commit comments