Get startedGet started for free

How a machine learns (optional)

1. How a machine learns (optional)

SPEAKER: To understand machine learning, you must first understand how neural networks learn. This includes exploring this learning process and the terms associated with it. If you are already familiar with the ML theories and terminologies, feel free to skip this lesson. How do machines learn? And how do they assess their learning? Before you dive into building an ML model, let's take a look at how a neural network learns. You may already know about various neural networks, such as Deep Neural Networks, or DNN, Convolutional Neural Networks, or CNN, Recurrent Neural Networks, or RNN, and more recently, Large Language Models, or LLMs. These networks are used to solve different problems. All of these models stem from the most basic, Artificial Neural Network, or ANN. ANNs are also referred to as neural networks or shallow neural networks. Let's focus on ANN to see how a neural network learns. An ANN has three layers-- an input layer, a hidden layer, and an output layer. Each node represents a neuron. The lines between neurons stimulate synopsys, which is how information is transmitted in a human brain. For instance, if you input article titles from multiple resources, the neural network can tell you which media outlet or platform the article belongs to, such as GadgeHub, Daily Insight, and DevStream. How does an ANN learn from examples and then make predictions? Let's examine how it works in depth. Let's assume you have two input neurons or nodes-- one hidden neuron and one output neuron. Above the link between neurons are weights. The weights retain information that a neural network learned through the training process. They are the mysteries that a neural network aims to discover. The first step is to calculate the weighted sum. This is done by multiplying each input value by its corresponding weight and then summing the products. It normally includes a bias component, bi. However, to focus on the core idea, ignore it for now. The second step is to apply an activation function to the weighted sum. What is an activation function? And why do you need it? Let's pause your curiosity for just a moment and get back to that soon. In the third step, the weighted sum is calculated for the output layer, assuming multiple neurons in the hidden layers. The fourth step is to apply an activation function to the weighted sum. This activation function can be different from the one applied to the hidden layers. The result is the predicted y, which consists of the output layer. You use y-hat to represent the predicted result and y as the actual result. Now let's get back to activation functions. What does an activation function do? Well, an activation function is used to prevent linearity or add non-linearity. What does that mean? Think about a neural network. Without activation functions, the predicted result, y-hat, will always be a linear function of the input, x, regardless of the number of layers between input and output. Let's walk through this for clarity. Without the activation function, the value of the hidden layer, h, equals a total of w1 times x1 and w2 times x2. Please note that, to make this illustration easy, we ignored the bias component, b, which you often see in other ML materials. The output, y-hat, therefore equals to w3 times h and eventually equals to a total of constant number, a, times x1 and a constant number, b, times x2. In other words, the output, y, is a linear combination of the input, x. If y is a linear function of x, you don't need all the hidden layers, but only one input and one output. You might already know that linear models do not perform well when handling comprehensive problems. That's why you must use activation functions to convert a linear network to a nonlinear one. What are the widely used activation functions? You can use the Rectified Linear Unit, or ReLU function, which turns an input value to 0 if it's negative or keeps the original value if it's positive. You can use the sigmoid function, which turns the input to a value between 0 and 1-- and hyperbolic tangent, or tanh function, which shifts the sigmoid curve and generates a value between minus 1 and plus 1. Another interesting and important activation function is called softmax. Think about sigmoid. It generates a value from 0 to 1 and is used for binary classification and logistic regression models. An example for this would be deciding whether an email is spam. What if you have multiple categories, such as GadgeHub, Daily Insight, and DevStream? Here you must use softmax, which is the activation function for multi-class classification. It maps each output to a 0, 1 range in a way that the total adds up to 1. Therefore, the output of softmax is a probability distribution. Skipping the math, you can conclude that softmax is used for multi-class classification. Whereas, sigmoid is used for binary class classification and logistic regression models. Also note that you don't need to have the same activation function across different layers. For instance, you can have ReLU for hidden layers and softmax for the output layer. Now that you understand the activation function and get a predicted y, how do you know if the result is correct? You use an assessment called loss function or cost function to measure the difference between the predicted y and the actual y. Loss function is used to calculate errors for a single training instance. Whereas, cost function is used to calculate errors from the entire training set. Therefore, in step 5, you calculate the cost function to minimize the difference. If the difference is significant, the neural network knows that it did a bad job in predicting and must go back to learn more and adjust parameters. Many different cost functions are used in practice. For regression problems, Mean Squared Error, or MSE, is a common one used in linear regression models. MSE equals the average of the sum of squared differences between y-hat and y. For classification problems, cross-entropy is typically used to measure the difference between the predicted and actual probability distributions in logistic regression models. If the difference between the predicted and actual results is significant, you must go back to adjust weights and biases to minimize the cost. This potential sixth step is called backpropagation. The challenge now is how to adjust the weights. The solution is slightly complex but, indeed, the most interesting part of a neural network. The idea is to take cost functions and turn them into a search strategy. That's where gradient descent comes in. Gradient descent refers to the process of walking down the surface formed by the cost function and finding the bottom. It turns out that the problem of finding the bottom can be divided into two different and important questions. The first one is, which direction should you take? The answer involves the derivative. Let's say you start from the top left. You calculate the derivative of the cost function and find its negative. This means the angle of the slope is negative, and you are on the left side of the curve. To get to the bottom, you must go down and right. Then, at one point, you're on the right side of the curve. And you calculate the derivative again. This time, the value is positive. And you must slide again to the left. You calculate the derivative of the cost function every time to decide which direction to take. Repeat this process, according to gradient descent, and you will eventually reach the regional bottom. The second question in finding the bottom is, what size should the steps be? The step size depends on the learning rate, which determines the learning speed of how fast you bounce around to reach the bottom. Step size or learning rate is a hyperparameter that is set before training. If the step size is too small, your training might take too long. If the step size is too large, you might bounce from wall to wall or even bounce out of the curve entirely without converging. When step size is just right, you're set. The seventh and last step is iteration. One complete pass of the training process from step 1 to step 6 is called an epoch. You can set the number of epochs as a hyperparameter in training. Weights or parameters are adjusted until the cost function reaches its optimum. You can tell that the cost function has reached its optimum when the value stops decreasing, even after many iterations. This is how a neural network learns. It iterates the learning by continuously adjusting weights to improve behavior until it reaches the best result. This is similar to a human learning lessons from the past. We have illustrated a simple example with two input neurons, or nodes-- one hidden neuron, and one output neuron. In practice, you might have many neurons in each layer. Regardless of the number of neurons in the input, hidden, and output layer, the fundamental process of how a neural network learns remains the same. Learning about neural networks can be exciting, but also overwhelming with the large number of new terms. Let's take a moment to review them. In a neural network, weights and biases are parameters learned by the machine during training. You have no control of the parameters except to set the initial values. The number of layers, and neurons, activation functions, learning rate, and epochs are hyperparameters, which are decided by a human before training. The hyperparameters determine how a machine learns. For example, the learning rate decides how fast a machine learns. And the number of epochs defines how many times the learning iterates. Normally, data scientists choose the hyperparameters and experiment with them to find the optimum combination. However, if you use a tool like AutoML, it automatically selects the hyperparameters for you and saves you plenty of experiment time. You also learned about cost or loss functions, which are used to measure the difference between the predicted and actual value. They are used to minimize error and improve performance. You use backpropagation to modify the weights and bias if the difference is significant and gradient descent to decide how to tune the weights and bias and when to stop. Mastering these foundational concepts is your first step toward building powerful ML models. You'll constantly apply them when moving from theory to practice.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.