How to Build Your First Neural Network

In this free 15-minute tutorial, you will build your first neural network from scratch.  No experience is required.  You will be developing your project using Python, PyTorch and Google Colab.  You will build and train a neural network to mimic a mystery function.

Level:  Beginner
Time:  15 minutes
Equipment:  Google Chrome Browser



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 A.I. library called PyTorch to build and train our neural network.  If you are new to A.I. 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!

To Get Started

Head over to Google Colab by opening up a browser typing in

Step 1 – Colab

Here you will log into Google Colab using your Google account.  If you don’t have a Google account, you can create one for free.  Once you are logged in, you will connect your notebook to a backend server in Google’s datacenter.

  1. Log into Colab by clicking on the Sign-In button in the upper right-hand side
  2. After logging in, click on New Notebook
  3. Click the Connect button on the upper right-hand side

Step 2 – Import our Libraries

First step in writing an A.I. project is to import the software we will be using.  In this case, we will use PyTorch, an open-source Deep Learning library managed by Meta’s A.I. team.  We will also need to import the random generation library.  To do that, type in the following code in the first cell and hit the play button:

import torch
from torch import nn
import random

Step 3 – Define our Mystery Function

Next, we will define our mystery function.  Here we need a simple function that takes two inputs and produces a single result.  We will return the result as a PyTorch tensor.  If you don’t know what tensors are all about, you can always try taking our introduction to A.I. programming course.  For now, think of a tensor as a generic multi-dimensional array of a single type.

def mystery(a, b):
  return torch.tensor(a+3*b)

Step 4 – Create our Neural Network

Next, we will create our neural network.  Here we will use the simple nn.Sequential wrapper to specify our neural network components.  Since our network is a single neuron, all we need is a single call to nn.Linear specifying the number of inputs (2) and the number of outputs (1).

model = nn.Sequential(nn.Linear(2,1))

Step 5 – Check Solution

If you get stuck, you can compare your own code with the solution here:


Hands-On A.I. Programming Course

(4 Week Course - Beginner)

This self-paced hands-on course will give you everything you need to start developing A.I. projects with the popular A.I. library PyTorch. The course will guide you through step-by-step including how to curate datasets, tensors, modeling, training and inference.  You will complete multiple real-world coding projects.

Try FREE for 2 Days

Get full access to the entire first module of the course for 2 days.

Learn More

Step 5 – Train our Neural Network

In order to train our network, we need to specify a way to measure how well our network is making predictions.  This is called the criterion.  A common way to measure neural networks that are predicting a value is to use MSELoss().  Next we want to specify how our neural network is updated during the training process.  SGD is a good place to start.

criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

Finally, we want to pass in samples of input/output data to our neural network and update the network so that next time it sees a similar input, the output it produces will be closer to what we specified during the training process.   You will start by passing in the current input into the neural network and get the current output.  You will then compare that output to what the output should have been and based on that difference, you will update the network.

for i in range(1000):
  a = random.random()
  b = random.random()
  desiredOutput = mystery(a,b)

  output = model(torch.tensor([a,b]))
  loss = criterion(output.squeeze(), desiredOutput)

  if (i % 100)==0:
    print (f"Loss: {loss.item()}")


Step 6 – Check our Predictions

Final, we can test our network with example inputs (a,b) and compare the results to that of our mystery function:

 a = 1.0
b = -1.0
output = model(torch.tensor([a,b]))
print (output.item())

Congratulations for getting to the end of the tutorial!  In just 20 lines of code, you built a neural network and trained it to successfully mimic a mystery function! 

Next Steps

If you want to learn more about developing your own A.I. projects, take the 4-week on-demand Introduction to A.I. programming course!

Happy Learning! team

PyTorch Tutorials (Quarterly)

It’s Free, No Spam, Opt-Out Anytime

We won't send spam. Unsubscribe at any time.