Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Choosing the Right Activation Function for Your Neural Network
Next article icon

Activation Functions in Neural Networks Using R

Last Updated : 10 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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?

  • Without activation functions, the network would only be able to learn linear relationships. Activation functions help introduce non-linearity, which is crucial for handling more complex problems.
  • By adding non-linearity, activation functions allow the network to learn and make sense of complex patterns in the data, improving its ability to make accurate predictions.
  • Activation functions can control the range of output values, which can be useful for different types of problems (e.g., classification vs. regression).

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.

  • The sigmoid function, denoted as σ(x), maps any real-valued variety to the range [0, 1].
  • Sigmoid is frequently utilized in binary class problems as it squashes the output into a opportunity-like value.
  • However, it suffers from the vanishing gradient trouble, that could gradual down education.

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.

  • The tanh function also maps enter values to a selected range, however this time it’s [-1, 1].
  • It’s zero-targeted, which allows with quicker convergence throughout training.

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.

  • ReLU is widely used and computationally green.
  • It replaces poor inputs with zero and leaves tremendous inputs unchanged.
  • ReLU helps mitigate the vanishing gradient hassle and quickens schooling.
  • However, it could suffer from the “dying ReLU” issue (in which some neurons continue to be inactive).

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.

  • Leaky ReLU is a variant of ReLU that addresses the death ReLU hassle.
  • It permits a small, non-zero gradient for bad inputs.

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.

  • Softmax is on the whole used within the output layer for multi-class classification.
  • Given a set of raw model outputs (logits), softmax converts them into probabilities.
  • It guarantees that the sum of probabilities throughout all lessons equals 1.

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:

  • Sigmoid and Tanh: Often used inside the output layer for binary class (sigmoid) or in hidden layers for models wherein the output is between -1 and 1.
  • ReLU: Generally used in hidden layers for deep networks, as it has a tendency to mitigate the vanishing gradient problem and hurries up gaining knowledge of.
  • Leaky ReLU: Used when ReLU causes dead neurons (neurons that handiest output 0). It’s a very good alternative while going through this trouble.
  • Softmax: Ideal for multi-class class troubles, where the output layer needs to symbolize probabilities across more than one instructions.

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.


Next Article
Choosing the Right Activation Function for Your Neural Network

B

badcaptaiz412
Improve
Article Tags :
  • Deep Learning
  • AI-ML-DS
  • AI-ML-DS With R

Similar Reads

  • Softmax Activation Function in Neural Networks
    Softmax is an activation function commonly used in neural networks for multi-classification problems. This article will explore Softmax's mathematical explanation and how it works in neural networks. Table of Content Introduction of SoftMax in Neural Networks How Softmax Works?Softmax and Cross-Entr
    10 min read
  • Tanh Activation in Neural Network
    Tanh (hyperbolic tangent) is a type of activation function that transforms its input into a value between -1 and 1. It is mathematically defined as: [Tex]f(x) = \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} = \frac{\sinh(x)}{\cosh(x)}[/Tex] Where: [Tex]e[/Tex] is Euler's number (approximately 2.718).
    4 min read
  • Epoch in Neural Network using R
    Deep Learning is a subfield of machine learning and artificial intelligence that focuses on training neural networks to perform various tasks, such as image recognition, natural language processing, and reinforcement learning. When training a deep learning model, the concept of an "epoch" is fundame
    8 min read
  • Choosing the Right Activation Function for Your Neural Network
    Activation functions are a critical component in the design and performance of neural networks. Choosing the right activation function can significantly impact the efficiency and accuracy of a neural network. This article will guide you through the process of selecting the appropriate activation fun
    4 min read
  • Swish activation function in Pytorch
    Activation functions are a fundamental component of artificial neural networks. They introduce non-linearity into the model, allowing it to learn complex relationships in the data. One such activation function, the Swish activation function, has gained attention for its unique properties and potenti
    6 min read
  • Activation Functions in Pytorch
    In this article, we will Understand PyTorch Activation Functions. What is an activation function and why to use them?Activation functions are the building blocks of Pytorch. Before coming to types of activation function, let us first understand the working of neurons in the human brain. In the Artif
    5 min read
  • Graph Neural Networks (GNNs) Using R
    A specialized class of neural networks known as Graph Neural Networks (GNNs) has been developed to learn from such graph-structured data effectively. GNNs are designed to capture the dependencies between nodes in a graph through message passing between the nodes, making them powerful tools for tasks
    8 min read
  • Activation Function in TensorFlow
    Activation functions add non-linearity to deep learning models and allow them to learn complex patterns. TensorFlow’s tf.keras.activations module provides a variety of activation functions to use in different scenarios. An activation function is a mathematical transformation applied to the output of
    4 min read
  • Convolutional Neural Networks (CNNs) in R
    Convolutional Neural Networks (CNNs) are a specialized type of neural network designed to process and analyze visual data. They are particularly effective for tasks involving image recognition and classification due to their ability to automatically and adaptively learn spatial hierarchies of featur
    11 min read
  • What are radial basis function neural networks?
    Radial Basis Function (RBF) Neural Networks are a specialized type of Artificial Neural Network (ANN) used primarily for function approximation tasks. Known for their distinct three-layer architecture and universal approximation capabilities, RBF Networks offer faster learning speeds and efficient p
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences