AI in baby steps, experiment 3: Multilayer Perceptron (MLP) in PyTorch
- Ruwan Rajapakse
- Feb 11
- 2 min read
In our first two experiments, we built and ran a Perceptron and a Multilayer Perceptron (MLP). However, we did this in a "primitive" programming environment—Excel/VBA—because we wanted to understand what happens under the hood of a running neural network. Specifically, we aimed to grasp the tedious matrix operations involved in the forward pass and backpropagation algorithms. Most importantly, we wanted to develop an intuition for how a perceptron "knows" certain facts about a given problem space and how it learns these facts by iterating through data.
As we mentioned at the end of our last experiment, there is much more to explore regarding the nuances of gradient descent, backpropagation, and selecting the best activation function for a given problem. We will examine these topics in detail soon.
For today’s experiment, however, we will do what we did last time—build an MLP—but this time, we will leverage the power of PyTorch. PyTorch is widely regarded as one of the most versatile and user-friendly machine learning libraries available today. In this experiment, we will see why that is the case and gain an initial understanding of its core torch.nn module, which allows us to design neural networks ranging from simple to highly complex architectures.
If you haven’t yet set up Python, PyTorch, and a compatible Integrated Development Environment (IDE) like Visual Studio Code (VSC) on your computer, please follow the hyperlinks I’ve provided. Setting up this development environment is one of the smoothest experiences I’ve encountered—it takes just 5 to 10 minutes, assuming good internet and computer speeds. You can also find a quick guide on creating your first Python project in VSC here (a two-second task, literally).
Now that you’re somewhat familiar with fundamental machine learning concepts, let’s jump right in and take a look at the full code snippet to deliver the said MLP. Please refer back to our previous experiment if you have forgotten the business use case. As you can see, we’ve included extensive comments—which might be a bit excessive for an experienced programmer—but hopefully, they will help clarify the overall process and the specific functions being configured and executed.

As you can see from the code and comments, the powerful modules we can leverage from PyTorch and Python enable us not only to design, instantiate and run the network with great ease, but also to very conveniently scale and normalize the data, and graph the outcome. Here are the .py code file and the training data file, which you can download and paste into your Python working folder. Please ensure you scan the files before use, as one can never fully guarantee the safety of files hosted on public servers.
Here is the generated output, a beautiful presentation of the discovered decision boundary, plotted against the provided training data, in the simple 2-d hyperspace that characterizes this particular problem.

Tweak the network's parameters like the number of nodes in the hidden layer, the learning rate and the number of epochs, and rerun the code, to get a sense of how the network's learning abilities change. More on this soon.
Kommentarer