RBF Network: Principles, Examples, and Python Implementation

RBF Network : Guide Complet — Principes, Exemples et Implémentation Python

RBF Network: Complete Guide — Principles, Examples, and Python Implementation

Summary

The RBF Network (Radial Basis Function Network, or radial basis function neural network) is an artificial neural network architecture that differs fundamentally from the classic Multilayer Perceptron (MLP). Instead of using sigmoid or ReLU activation functions in its hidden layer, the RBF Network employs Gaussian radial functions centered around reference points in the input space. This approach gives it properties of universal approximation, rapid convergence, and geometric interpretability that few other models can match. In this complete guide, we will explore the mathematical principles, geometric intuition, implementation from scratch in Python, and practical use cases of the RBF Network.

Mathematical Principle

The RBF Layer: Gaussian Functions

The heart of the RBF Network lies in its hidden layer, composed of radial basis function neurons. Each neuron i computes a response based on the distance between the input vector x and a center cₖ of its own:

φₖ(x) = exp( -||x – cₖ||² / (2σₖ²) )

where:

  • x ∈ ℝⁿ is the input vector of dimension n
  • cₖ ∈ ℝⁿ is the center of the i-th RBF neuron — a reference point in the input space
  • σₖ > 0 is the width parameter (or spread) of the i-th neuron, controlling the range of the zone of influence
  • ||x – cₖ|| is the Euclidean distance between the input and the center
  • φₖ(x) is the output of the RBF neuron, always between 0 and 1

This Gaussian function reaches its maximum value (1) when the input coincides exactly with the center cₖ, then decays exponentially as the input moves away from the center. The parameter σₖ determines the speed of this decay: a small σ creates a narrow, selective Gaussian, while a large σ produces a smoother, more generalized response.

The Output Layer

The final output of the network is a linear combination of the RBF layer activations:

y = Σ wₖ · φₖ(x) + b

where:

  • wₖ is the weight associated with the i-th RBF neuron
  • b is the bias term
  • The sum is over all neurons in the hidden layer

Unlike the MLP which stacks nonlinear layers, the RBF Network applies only a single nonlinear transformation (the Gaussians), followed by a single linear computation. This structural simplicity is both its strength and its weakness.

Two-Phase Learning

Training of the RBF Network typically proceeds in two distinct phases, making it much faster to train than a classical backpropagation network:

Phase 1 — Determining the centers (unsupervised): The centers cₖ of the RBF neurons are positioned in the input space using the K-Means algorithm. The idea is to place each center at the heart of a natural cluster of data. The parameter σₖ is then computed from the distances between centers (e.g., the average distance to the nearest center, or the maximum inter-center distance divided by √(2K)).

Phase 2 — Computing the output weights (supervised): Once the centers and widths are fixed, the activations φₖ(x) become known features for each training example. The weights wₖ and bias b are then determined by linear least squares regression (direct or regularized via ridge regression), or more rarely by gradient descent.

This separation is elegant: the first phase captures the geometric structure of the data without knowing the labels, and the second solves a simple problem of linear algebra.

Intuition: Zones of Influence

To truly understand the RBF Network, you need to go beyond the formulas and develop a geometric intuition.

Imagine a two-dimensional space filled with data. Let’s place a few reference points — our RBF centers — scattered across this space. Each center is surrounded by a bell-shaped zone of influence (the Gaussian). When an input point arrives, it “activates” each zone of influence proportionally to its proximity: the closer it is to a center, the more the corresponding neuron activates.

The final output is simply a weighted average of all these activations. Each neuron contributes to the prediction according to two criteria: how close the input is to its center (via φₖ) and what importance the model has assigned to that center (via wₖ).

Instead of detecting linear patterns like an MLP, each RBF neuron detects whether the input is close to its center — it’s like having zones of influence around reference points, and the output is a weighted average of these zones.

The fundamental difference with an MLP (Multilayer Perceptron) is subtle but powerful:

  • An MLP uses hyperplanes to partition the space: each neuron detects whether the input is on one side or the other of a linear boundary. By stacking layers, complex boundaries are constructed.
  • An RBF Network uses spheres (or ellipsoids): each neuron detects whether the input is “in the neighborhood” of its center. The combination of these spherical zones can approximate any continuous function.

You can think of the RBF Network as a system of local interpolation: each region of the space has its own local “expert” (the neuron whose center is closest), and the prediction results from a smooth blend between neighboring experts.

Python Implementation

From-Scratch Version with KMeans

Here is a complete implementation of the RBF Network from scratch, using only NumPy and scikit-learn for KMeans (the centers):

import numpy as np
from sklearn.cluster import KMeans
from numpy.linalg import pinv

class RBFNetwork:
    """Réseau de neurones à fonction de base radiale (RBF Network)
    implémenté from scratch avec NumPy."""

    def __init__(self, n_centers=10, sigma=None):
        self.n_centers = n_centers
        self.sigma = sigma       # Si None, calculé automatiquement
        self.centers = None      # Centres c_i appris par K-Means
        self.weights = None      # Poids w_i de la couche de sortie
        self.bias = None         # Biais b

    def _gaussian(self, X, centers, sigma):
        """Calcule la matrice des activations gaussiennes.
        Résultat : shape (n_samples, n_centers)"""
        distances = np.zeros((X.shape[0], len(centers)))
        for i, center in enumerate(centers):
            distances[:, i] = np.linalg.norm(X - center, axis=1)
        return np.exp(-(distances ** 2) / (2 * sigma ** 2))

    def _compute_sigma(self):
        """Calcule sigma automatiquement comme la distance maximale
        entre centres divisée par sqrt(2 * n_centers)."""
        max_dist = 0
        for i in range(len(self.centers)):
            for j in range(i + 1, len(self.centers)):
                d = np.linalg.norm(self.centers[i] - self.centers[j])
                if d > max_dist:
                    max_dist = d
        return max_dist / np.sqrt(2 * self.n_centers)

    def fit(self, X, y):
        """Entraînement en deux phases."""
        # Phase 1 : centres par K-Means
        kmeans = KMeans(n_clusters=self.n_centers, random_state=42, n_init=10)
        kmeans.fit(X)
        self.centers = kmeans.cluster_centers_

        # Calcul automatique de sigma si non spécifié
        if self.sigma is None:
            self.sigma = self._compute_sigma()

        # Calcul de la matrice d'activation RBF (Phi)
        Phi = self._gaussian(X, self.centers, self.sigma)

        # Phase 2 : régression linéaire (moindres carrés avec pseudo-inverse)
        # Ajout d'une colonne de 1 pour le biais
        Phi_bias = np.column_stack([Phi, np.ones(Phi.shape[0])])

        # Résolution par pseudo-inverse de Moore-Penrose
        params = pinv(Phi_bias) @ y
        self.weights = params[:-1]
        self.bias = params[-1]
        return self

    def predict(self, X):
        """Prédiction sur de nouvelles données."""
        Phi = self._gaussian(X, self.centers, self.sigma)
        return Phi @ self.weights + self.bias

Usage Example: Nonlinear Function Approximation

import matplotlib.pyplot as plt

# Génération de données : fonction sinusoïdale bruitée
np.random.seed(42)
X_train = np.linspace(-3, 3, 100).reshape(-1, 1)
y_train = np.sin(X_train.flatten()) + 0.1 * np.random.randn(100)

# Entraînement du RBF Network
rbf = RBFNetwork(n_centers=15, sigma=0.4)
rbf.fit(X_train, y_train)

# Prédiction sur une grille fine
X_test = np.linspace(-4, 4, 300).reshape(-1, 1)
y_pred = rbf.predict(X_test)

# Visualisation
plt.figure(figsize=(10, 6))
plt.scatter(X_train, y_train, alpha=0.5, label="Données d'entraînement", s=15)
plt.plot(X_test, y_pred, 'r-', linewidth=2, label="Prédiction RBF Network")
plt.plot(X_test, np.sin(X_test.flatten()), 'g--', label="sin(x) (vérité)", linewidth=1.5)
plt.scatter(rbf.centers.flatten(), rbf.predict(rbf.centers),
            marker='x', c='purple', s=80, label="Centres RBF")
plt.legend()
plt.title("RBF Network : Approximation de sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

With scipy.interpolate (Alternative Approach)

For certain use cases, the SciPy library offers an optimized implementation of RBF interpolation:

from scipy.interpolate import RBFInterpolator

# Mêmes données
rbf_scipy = RBFInterpolator(X_train, y_train,
                            kernel='gaussian',
                            smoothing=0.01)

y_pred_scipy = rbf_scipy(X_test)

SciPy’s RBFInterpolator function uses optimized numerical algorithms and can handle larger problems thanks to sparse solvers.

2D Approximation Case

The RBF Network works naturally in higher dimensions:

# Données 2D : approximation d'une surface z = f(x, y)
X_2d = np.random.uniform(-2, 2, (200, 2))
y_2d = np.exp(-(X_2d[:, 0]**2 + X_2d[:, 1]**2)) + 0.05 * np.random.randn(200)

rbf_2d = RBFNetwork(n_centers=20)
rbf_2d.fit(X_2d, y_2d)
print(f"Erreur quadratique moyenne : {np.mean((rbf_2d.predict(X_2d) - y_2d)**2):.6f}")

Key Hyperparameters

The RBF Network has three main hyperparameters that directly influence its performance:

n_centers (Number of RBF Neurons)

The number of centers controls the model’s capacity:

  • Too few centers: the model is underfit, it does not capture the fine variations of the target function.
  • Too many centers: risk of overfitting, the model memorizes the noise in the training data. Additionally, the computational cost of the linear regression grows as O(n_centers² · n_samples).

In practice, n_centers is chosen between 5% and 30% of the number of training examples, depending on the complexity of the function to approximate.

sigma / width (Gaussian Width)

The parameter σ controls the range of each neuron:

  • Small σ: each neuron has a narrow zone of influence. The model becomes very local and may overfit.
  • Large σ: the zones of influence overlap extensively. The model behaves almost like a linear regression.

The common empirical rule is to compute σ from the inter-center distances:

σ = d_max / √(2 · K)

where d_max is the maximum distance between two centers and K the number of centers. One can also use the average distance to the nearest center (k-nearest neighbor distance).

Regularization

When solving the linear system in phase 2, a ridge regularization (Tikhonov) term can be added to avoid overfitting:

w = (ΦᵀΦ + λI)⁻¹ Φᵀy

Instead of using the pseudo-inverse, a regularized system is solved where λ ≥ 0 controls the penalization of large weights. A high λ smooths the solution; a λ of zero reduces to ordinary least squares.

Advantages and Limitations

Advantages

  1. Fast training: The separation into two phases (unsupervised clustering + linear regression) avoids the costly optimization loops of backpropagation. No epochs, no learning rate, no vanishing gradient.
  2. Universal approximation: Like the MLP, the RBF Network can approximate any continuous function on a compact domain to arbitrary precision (Park & Sandberg theorem, 1991).
  3. Geometric interpretability: Each center has a clear spatial meaning. You can visualize where the model places its “experts” in the input space.
  4. No local minima: Phase 2 (linear regression) is a convex problem — you always find the globally optimal solution for the output weights.
  5. Excellent interpolation: Ideal for problems where the training data covers the space well and you seek accurate predictions between known points.

Limitations

  1. Curse of dimensionality: In high dimensions (d > 50), Euclidean distances lose their discriminative power. All points become “equidistant” and the Gaussians flatten.
  2. Poor extrapolation: The RBF Network is an interpolator. For points very far from all centers, all activations φₖ tend to 0 and the prediction converges to the bias b — not very useful.
  3. Sensitive hyperparameter choice: The number of centers and the width σ have a dramatic impact on performance, and optimizing them requires expensive cross-validation.
  4. Significant memory: The model must store all centers and, for each prediction, compute distances to all centers. Prediction complexity is O(n_centers · n_features).
  5. Less powerful than deep learning: For tasks like computer vision or natural language processing, deep networks with backpropagation far surpass RBF Networks.

4 Practical Use Cases

1. Function Approximation and Physical System Modeling

The RBF Network excels at approximating complex nonlinear functions. In engineering, it is used to create surrogate models of expensive numerical simulations: instead of re-running a CFD (computational fluid dynamics) simulation that takes hours, you train an RBF Network on a sample of results and get near-instant predictions.

2. Medical Classification and Assisted Diagnosis

Due to its local nature, the RBF Network lends itself well to classifying medical data: tumor detection, cardiovascular disease diagnosis, EEG signal analysis. The centers learned by KMeans often correspond to clinically interpretable prototypes. Its training speed is an asset when the data is of moderate size.

3. Robot Control and Real-Time Systems

RBF Networks are used in adaptive robot control and real-time control systems. Their ability to approximate unknown nonlinear functions makes it possible to build controllers that compensate for system nonlinearities (friction, mechanical flexibility) without an accurate analytical model. Prediction speed makes them compatible with strict real-time constraints.

4. Time Series Prediction

In finance, meteorology, or energy management, the RBF Network can model nonlinear time series. The prediction problem is reformulated as a function approximation: the inputs are the k last observed values and the output is the future value. RBF neurons capture local regimes of the series (uptrend, consolidation, high volatility) through their centers positioned in the time-lag space.

See Also