PyTorch Tutorial 12 - Activation Functions
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
▶
49
50
51
52
53
54
55
56
57
58
59
60
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
PyTorch Tutorial 01 - Installation
Patrick Loeber
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber
More on: ML Maths Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Want to get started with deep learning
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Medium · Deep Learning
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Medium · Deep Learning
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI