World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
An RBF (Radial Basis Function) network is a type of neural network that uses radial basis functions as activation functions. In PyTorch, you can implement an RBF network by defining a custom neural network class that inherits from the nn.Module
class.
Here is an example implementation of an RBF network in PyTorch:
import torch import torch.nn as nn class RBFNet(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim, num_centers, sigma): super(RBFNet, self).__init__() self.hidden_dim = hidden_dim self.output_dim = output_dim self.num_centers = num_centers self.centers = nn.Parameter(torch.randn(num_centers, input_dim)) self.beta = nn.Parameter(torch.ones(num_centers, 1) / num_centers) self.sigma = sigma self.fc = nn.Linear(num_centers, output_dim) def radial_basis(self, x): C = self.centers.view(self.num_centers, -1) return torch.exp(-torch.sum((x - C) ** 2, dim=1) / (2 * self.sigma ** 2)) def forward(self, x): batch_size = x.size(0) x = x.view(batch_size, -1) H = self.radial_basis(x) out = self.fc(H) return out
In this implementation, we define an RBFNet class that takes in the input dimension, the hidden dimension, the output dimension, the number of centers, and the sigma value as input parameters. In the constructor, we initialize the parameters for the RBF network: the centers, beta, and sigma values. We use the nn.Parameter method to specify that these parameters are learnable.
The radial_basis method calculates the radial basis functions for the input x. It calculates the Euclidean distance between x and the centers of the RBF network and then applies the radial basis function to each distance value.
The forward method applies the RBF network to the input x. It first flattens the input x and then calculates the radial basis functions for the input. Finally, it applies a linear transformation to the radial basis functions to obtain the output of the RBF network.
To use the RBFNet class, you can create an instance of it and use it like any other PyTorch neural network:
input_dim = 10 hidden_dim = 100 output_dim = 2 num_centers = 20 sigma = 1.0 model = RBFNet(input_dim, hidden_dim, output_dim, num_centers, sigma) x = torch.randn(32, input_dim) output = model(x)
In this example, we create an RBFNet instance with the specified parameters and apply it to a batch of input x with 32 examples. The output is a tensor with dimensions (32, 2), which corresponds to the output dimension of the RBF network.