Activation Functions in Neural Networks Using R (original) (raw)

Last Updated : 23 Jul, 2025

Activation functions are essential components of neural networks that play a crucial role in determining how a model processes and interprets data. They introduce non-linearity into the network, enabling it to learn and capture complex patterns and relationships within the data. By applying mathematical transformations to the input values, activation functions help the network make more accurate predictions and classifications. In neural networks implemented in R, activation functions can be easily utilized to enhance the model’s capability, making them a fundamental aspect of machine learning and deep learning projects.

What Are Activation Functions?

An activation function is a mathematical formula that decides whether a neuron should be activated or not. It takes the input to a neuron, performs a calculation, and then produces an output. This output is then passed to the next layer of the network. Activation functions add non-linearity to the model, allowing it to learn and perform more complex tasks.

Why Are Activation Functions Important?

Common Activation Functions

Now we will discuss different types of Activation Functions in R Programming Language.

1. Sigmoid (Logistic) Function

The sigmoid function maps any real-valued number into the range of [0, 1]. It’s often used in binary classification problems.

**Formula:

( f(x) = \frac{1}{1 + e^{-x}} )

R `

Sigmoid Activation Function

sigmoid <- function(x) { return(1 / (1 + exp(-x))) }

Example usage

x <- c(-1, 0, 1, 2, 3) cat("Sigmoid: ", sigmoid(x), "\n")

`

**Output:

Sigmoid: 0.2689414 0.5 0.7310586 0.8807971 0.9525741

2. Hyperbolic Tangent (tanh) Function

The tanh function maps input values to the range [-1, 1]. It’s zero-centered, which often leads to faster convergence in training than the sigmoid function.

**Formula:

( f(x) = \tanh(x) )

R `

Hyperbolic Tangent (tanh) Function

tanh <- function(x) { return(2 / (1 + exp(-2 * x)) - 1) }

Example usage

x <- c(-1, 0, 1, 2, 3) cat("tanh: ", tanh(x), "\n")

`

**Output:

tanh: -0.7615942 0 0.7615942 0.9640276 0.9950548

3. Rectified Linear Unit (ReLU)

ReLU is one of the most popular activation functions in deep learning. It maps all negative inputs to zero, while positive inputs remain unchanged.

**Formula:

( f(x) = \max(0, x) )

R `

Rectified Linear Unit (ReLU)

relu <- function(x) { return(pmax(0, x)) }

Example usage

x <- c(-1, 0, 1, 2, 3) cat("ReLU: ", relu(x), "\n")

`

**Output:

ReLU: 0 0 1 2 3

4. Leaky ReLU

A variation of ReLU that allows a small, non-zero gradient when the input is negative.

**Formula:

Leaky ReLU(x)=max(αx,x)

R `

Leaky ReLU

leaky_relu <- function(x, alpha = 0.01) { return(ifelse(x > 0, x, alpha * x)) }

Example usage

x <- c(-1, 0, 1, 2, 3) cat("Leaky ReLU: ", leaky_relu(x), "\n")

`

**Output:

Leaky ReLU: -0.01 0 1 2 3

5. Softmax

Often used in the output layer of a network for multi-class classification problems, the softmax function converts logits (raw model outputs) into probabilities.

**Formula:

( f(x_i) = \frac{e^{x_i}}{\sum_{j} e^{x_j}} )

R `

Softmax Function

softmax <- function(x) { exp_x <- exp(x) return(exp_x / sum(exp_x)) }

Example usage

x <- c(-1, 0, 1, 2, 3) cat("Softmax: ", softmax(x), "\n")

`

**Output:

Softmax: 0.01165623 0.03168492 0.08612854 0.2341217 0.6364086

Choosing the Right Activation Function

Choosing the right activation characteristic depends on different factors such as:

Conclusion

Activation functions are crucial for making neural networks capable of learning complex patterns. By introducing non-linearity into the model, they allow the network to perform a wide range of tasks, from classification to regression. In R, we can easily implement these functions using packages like neuralnet, making it straightforward to build and train neural networks. Understanding and choosing the right activation function for our problem can greatly impact the performance of our model.