This tutorial demonstrates how to build and train a simple neural network using PyTorch in Google Colab, where a single-neuron network learns to mimic a mystery function by minimizing mean squared error loss through gradient descent optimization.
Build Your First Neural Network in PyTorch: A 15-Minute Tutorial
Added:hi in this 15-minute tutorial you will build a neural network from scratch and train it to mimic a simple mystery function using only 20 lines of code we will be using google colab a free service from google that enables you to code using your own web browser we will be using the deep learning ai library called pytorch to build and train our neural network if you're new to ai programming this tutorial will help you dive right into the basics of how to develop these amazing neural networks and train them to make real predictions let's get started now let's take a look at this mystery function in more detail this mystery function takes an a and a b as input and then produces a value c as an output now the goal for us is to build a neural network that can learn how to mimic the mystery function for that we're going to use the most simplest neural network available in this case we're using a single neuron and a neuron will take inputs so in this case we'll just provide two inputs to mimic a and b and then it produces a single output in this case now to train a neuron to mimic a function we need some provided examples so what we're going to do is we're going to provide examples of inputs and the output associated with those inputs and then we're going to train the neural network so that it can hopefully learn what the mystery function is so to get started i'm going to open up a chrome browser and head over to colab by typing colab.research.google.com and hit enter now the first thing i like to do when i initially go to collab is to log in so if you click on the sign in button at the upper right hand side you can then enter in your google account information if you don't have one you can create one at this time alright so now i'm logged in i'm going to click new notebook and from here the first thing i like to do is to connect the notebook and then we can title notebook so in this case we can call it maybe first neural network and if you're not familiar with collab the way it works is you're going to click on the cell and then here you can do all your coding so to start we're gonna import the different libraries that we're gonna need to build and train our neural network so we're gonna start by importing pytorch and you can do that by typing import torch we also use the nn library a lot from there so we're going to import and then specifically as well and then we are also going to use the random library to generate some random numbers so to execute the cell you can just click this play button and then to add another cell you can just click on this plus code button up here and then we'll define our mystery function so here i'm just gonna build a function called mystery it takes two inputs and for my function i'm just gonna initially have something quite simple like a plus three times b so this is the value that my mystery function is going to return which is the c now the only difference here is when we're dealing with neural networks neural networks are going to take all their data in the form of tensors so we need to convert this value to a tensor so a very simple way to do that is to just call torch.tensor on this value and it will nicely convert this thing to a tensor for us so to execute this code i can just hit this play button again and now we have our mystery function so next up i'm gonna add another cell and then we're gonna build our neural network now the most simplest way to build a neural network using pi torch is to call nn.sequential so sequential allows me to just list all the different components of the neural network in this case there's only one component so we call that linear and that component will take two inputs and produce one output and that's it that's our neural network now we typically assign that to a variable in a lot of cases most code will call their network model so we're going to use the same nomenclature and then we can just print out the model after we create it and by hitting play we should see our neural network now so we see that it has indeed two inputs and two outputs and the bias is also true uh don't worry about bias it just is there and it helps us uh with additional weights inside the neural network to make it stronger so that's all good um next we're gonna need to train the neural network so to train it um we're gonna have basically three parts the first part is a way to measure how well the neural network is doing and that is called the criterion so let's specify a criterion and in this case since our neural network is producing values we're going to use something called mse loss stands for mean square error loss this will just kind of give us a value for how much the neural network is producing values that are different from what it should be so the more it produces a value that's further away the higher this value is going to be the next thing we're going to want to do is define how the neural network is updated during the training process and that's typically referred to as an optimizer so we're going to select one of the most commonly used optimizers as a good starting point which is called s g d and what we need to do is just provide the neural network parameters which are the ones that are going to be updated during the training process then we also need to give it a learning rate which is a value that basically tells the training process how quickly to learn from the information that it's getting during a training process and then we also usually set up momentum and i recommend just setting that to 0.9 so let's go ahead and execute those so we have these two and then the last bit is just the training loop itself so here we're going to give it a bunch of examples and then update it to try to teach it what the mystery function is doing so just do that i'm just going to create a simple for loop and in this case um let's start with like maybe a thousand examples we can change that later and so first we need to create the example so i'm going to create a random input using the random function so this will give me a value between 0 and 1 for the first input and i'm going to do the same for this second input now for the output that i'm uh giving my neural network i'm going to use obviously our mystery function above so we're going to call it maybe desired output and we're just going to call the mystery function with the two inputs above so here we have a full input and output set so we can now use these uh to train our network so to train the network uh the first thing we're going to want to do is to pass the two inputs into our network so we can just um pass the values directly into model and then we're going to have an output here so let's call that output so this is the neural network that we built takes two inputs and then produces uh an output these inputs need to be tensors so to create a tensor version of them we can just call tensor torch.tensor and pass in a list next we want to compare how this output is compared to the desired output so we can use our criterion for that so we can just call criterion and then just provide the output and the desired output now this difference is usually referred to as loss so the larger the loss the bigger the difference so our goal is to try to reduce the loss the other thing we want to think about here is just to squeeze this down um neural networks when they train we'll be using something called batch training that means this output has an additional dimension for batch we're going to get rid of it otherwise we'll get a couple warnings here don't worry if you don't know what squeeze does we can learn more about that later now we want to track the loss throughout the training process because we want to see as the network is training that the loss is going down right and so we can print it out but let's not print it out all the time so let's print out every 100 times maybe and we can do that by just modding into the index and then printing the uh the value for loss and there's a couple different ways to do that um i can just use this way but you're free to use your own and the value you want is loss and because loss is a tensor you can actually just get the python value by calling item and then the last bit is to update the weights so the way to do that i'm just going to type it in real quick call zero grad and then we call lost off backward and then we call step so these three different function calls here will update the neural network based on what we've learned above so now let's just go ahead and run this and hopefully we will see that the loss is going down and indeed it is as it's training here we can see the loss getting very very low and that means our neural network has learned how to map the inputs to the outputs and so now we can test it so why don't we add a new code cell and give it a couple of examples so why don't we do like a equals one b equals say minus one and then we can just take the result so output is equal to model and then we just remember we need to convert those inputs into tensors so we can just do that by calling torch.tensor and then we can print out the output by calling item and then if we execute that what we're doing is we're passing the input 1 and input -1 into our model and then seeing what the neural network produces and in this case it produced a value very close to -2 and if we now compare that to our mystery function we can do that by just calling mystery with the two inputs we will see that the mystery function is indeed returning -2 so this is pretty amazing and showcases the power of a neural network without knowing anything about the mystery function the neural network was able to determine what the mystery function was by simply looking at the input and output pairs as a next step you can try to modify the mystery function and make it more difficult for the neural network to guess what it is simply modify it and then click run time and run all to start it all over again afterwards i definitely recommend you attempt the next free tutorial which will show you how to build a neural network to predict lemonade sales if you're watching on youtube and found this video helpful please support us by hitting like and subscribing to our channel if you want to learn more head on over to leaky.ai and check out all our free ai tutorials we also offer an amazing introduction to ai programming course where you will learn all the basics you need to start developing your own ai projects the course is fully self-paced and can usually be completed in about four weeks you can find out more about the course by clicking on courses on the top menu of our website finally subscribe to our free email list where you get updates on some of the latest ai trends thanks so much for watching and happy learning [Music]
Up Next

First-Order Logic & Satisfiability Modulo Theories | Formal Methods
@JanOliverRingert
621 views•2024-02-05

Introduction to Secure Multiparty Computation with Yehuda Lindell
@fhe_org
7.7K views•2021-02-04

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science











![[Machine Learning in Urdu/Hindi] 002 - Supervised Learning setup -00-W2L1-HQ](https://i.ytimg.com/vi/fQ7yIMNk5x4/maxresdefault.jpg)



























