PyTorch Tutorial 12 - Activation Functions

Patrick Loeber · Beginner ·🧬 Deep Learning ·6y ago

Key Takeaways

This video tutorial covers activation functions in neural networks using PyTorch, including the binary step function, sigmoid function, hyperbolic tangent function, ReLU, Leaky ReLU, and softmax function.

Full Transcript

hi everybody welcome to a new PI torch tutorial this time I want to talk about activation functions activation functions are an extremely important feature of neural networks so let's have a look at what activation functions are why they are used what different types of functions there are and how we incorporate them into our PI torch model so activation functions apply a linear transformation to the layer output and basically decide whether a neuron should be activated or not so why do we use them why it's only a linear transformation not good enough so typically we would have a linear layer in our network that applies a linear transformation so here it multiplies the input input with some weights and maybe add sub buyers and then delivers the output and let's suppose we don't have activation functions in between then we would have only linear transformations after each other so our whole network from input to output is essentially just a linear regression model and this linear model is not suited for more complex tasks so the conclusion is that with nonlinear transformations in between our network and learn better and perform more complex tasks so after each layer we typically want to apply this activation functions so here first we have our normal linear layer and then we also apply this activation function and with this our network can learn better and now let's talk about the most popular activation functions so the ones I want to show you is the binary step function the sigmoid function the hyperbolic tangent function the real ulla the leaky riilu and the softmax so let's start with the simple step function so this will just output one if our input is greater than a threshold so here the threshold is zero and zero otherwise so this is not used in practice actually but this search should demonstrate the example of if the neuron should be activated or not and yeah so a more popular choice is the sigmoid function and you should already know this if you've watched my tutorial tutorial about logistic regression so the formula is 1 over 1 plus e to the minus X and this will output a probability between 0 & 1 and this is typically used in the last layer of a binary classification problem so yeah then we have the hyperbolic tangent function or tan H this is basically a scaled seed MoIT function and also a little bit shifted so this will output a value between minus 1 and plus 1 and this is actually a good choice in hidden layers so you should know about the tun h function then we have the real uu function and this is the most popular choice in in most of the networks so the real uu function will output 0 for negative values and it will simply output the input as output for positive values so it is actually a linear function for well use greater than 0 and it is just 0 for negative values so it doesn't look that much different from just a linear transformation but in fact it is nonlinear and it is actually the most popular choice in the networks and it's typically a very good choice for an activation function so the rule of thumb is if you don't know which function you should new use then just use a real uu for hidden layers yeah so this is the real uu very popular choice then we also have the leaky real uu function so this is a slightly modified and slightly improved version of the real ooo so this will still just output the input for X greater than zero but this will multiply our input with a very small value for negative numbers so here I've written a times X for negative numbers and this a is typically very small so it's for example point zero zero one and this is an improved version of the riilu that tries to solve the so-called vanishing gradient problem because with a normal real ooh our values here are zero and this means that also the gradient later in the backpropagation is zero and when the gradient is zero then this means that these weights will never be updated so these neurons won't learn anything and we also say that these neurons are dead and this is why sometimes you want to use the leaky real function so whenever you notice that your weights won't update you're in training then try to use the leaky riilu instead of the normal reroute and yeah then as the last function I want to show you the softmax function and you also should already know this because I have a whole tutorial about the soft match softmax function so this will just this will basically squash the inputs to be outputs between 0 and 1 so that we have a probability as an output and this is typically a good choice in the last layer of a multi-class classification problem so yeah that's the different activation functions I wanted to show you and now let's jump to the code and see how we can use them in pi torch so we have two options and the first one is to create our functions as n n modules so in our network in the init function first we define all the layers we want to have so here for example first we have a linear and then after that we want to have a real ooh activation function so we create our real ooh module here and we can get that from the torch dot and end module so this contains all the different functions I just showed you and then we have the next layer for your example it's a next linear layer and then the next activation function so here we have a sigmoid at the end and then in the forward pass we simply call all these functions after each other so first we have the linear the first linear layer which gets an output and then we use this output at and put it into our riilu and then again we use this output and put it in the next linear layer and so on so this is the first way how we can use it and the second way is to use these functions directly so in the init function we only define our linear layers so linear one and linear two and then in the forward pass we apply this linear layer and then also call this torch dot riilu function here and then the torch dot sigmoid function directly so this is just from the torch API and yeah this is a different way how we can use it both ways we'll achieve the same thing it's just how you prefer your code and yeah so all the functions that I just showed you you can get from the n n module so here we had n n riilu but we can for example also have n n dot sigmoid and we have n n dot softmax and we have an n dot ton H and also and n dot leaky real ooh so all these functions are available here and they are also available in the torch API like this so here we have towards dot reload and we have torch dot sigmoid we also have torch dot softmax and torch dot ton age and but sometimes they are not used in the the functions are not available in the torch API directly but they are available in torch dot and n dot functional so here I imported torch and and functional SF and then I can call here for example F dot riilu so this is the same as torch dot real but here for example is the torch is f dot Li kiri Lewis only available in this API so yeah but that's how we can use the activation functions in pi torch and it's actually very easy and I hope you understood everything and now feel comfortable with activation functions if you like this please subscribe to the channel and see you next time bye

Original Description

New Tutorial series about Deep Learning with PyTorch! ⭐ Check out Tabnine, the FREE AI-powered code completion tool I use to help me code faster: https://www.tabnine.com/?utm_source=youtube.com&utm_campaign=PythonEngineer * In this part we learn about activation functions in neural nets. What are activation functions, why are they needed, and how do we apply them in PyTorch. I go over following activation functions: - Binary Step - Sigmoid - TanH (Hyperbolic Tangent) - ReLU - Leaky ReLU - Softmax 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN Part 12: Activation Functions If you enjoyed this video, please subscribe to the channel! Official website: https://pytorch.org/ Part 01: https://youtu.be/EMXfZB8FVUA Code for this tutorial series: https://github.com/patrickloeber/pytorchTutorial You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python #DeepLearning #Pytorch ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 48 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial teaches how to use activation functions in PyTorch to build neural networks, covering different types of activation functions and how to implement them in PyTorch.

Key Takeaways
  1. Define a neural network with linear layers and activation functions
  2. Apply activation functions to the output of each linear layer
  3. Use the torch.nn module to create activation function modules
  4. Use the torch API to apply activation functions directly
  5. Choose the appropriate activation function for each layer
💡 Activation functions are essential for neural networks to learn complex tasks, and choosing the right activation function for each layer is crucial for good performance.

Related AI Lessons

Want to get started with deep learning
Get started with deep learning by leveraging resources like Andrew Karpathy's playlist and frameworks such as TensorFlow or PyTorch
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Learn to build a deepfake detector from scratch and understand the challenges involved in detecting AI-generated fake media
Medium · Deep Learning
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Learn about high-dimensional invariance and its relation to the flat 2D plane of neural networks, and how to apply these concepts to improve model performance
Medium · Deep Learning
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Learn to implement Neural Style Transfer from scratch and understand its significance in deep learning
Medium · Deep Learning
Up next
Image Classification with ml5.js
The Coding Train
Watch →