Learn how to build your first PyTorch model, by using the “magical” Linear layer

Towards Data Science

PyTorch Introduction — Building your First Linear Model | by Ivo Bernardo | Dec, 2023 - image  on https://aiquantumintelligence.com
Regression Model — Image generated by AI

In my last blog post, we’ve learned how to work with PyTorch tensors, the most important object in the PyTorch library. Tensors are the backbone of deep learning models so naturally we can use them to fit simpler machine learning models to our datasets.

Although PyTorch is known for its Deep Learning capabilities, we are also able to fit simple linear models using the framework — and this is actually one of the best ways to get familiar with the torch API!

In this blog post, we’re going to continue with the PyTorch introduction series by checking how we can develop a simple linear regression using the torch library. In the process, we’ll learn about torch Optimizers, Weights and other parameters of our learning model, something that will be extremely useful for more complex architectures.

Let’s start!

For this blog post, we’ll use the song popularity dataset where we’ll want to predict the popularity of a certain song based on some song features. Let’s take a peek at the head of the dataset below:

songPopularity = pd.read_csv(‘./data/song_data.csv’)
PyTorch Introduction — Building your First Linear Model | by Ivo Bernardo | Dec, 2023 - image  on https://aiquantumintelligence.com
Song Popularity Feature Columns — Image by Author

Some of the features of this dataset include interesting metrics about each song, for example:

  • a level of song “energy”
  • a label encoding of the key (for example, A, B, C, D, etc.) of the song
  • Song loudness
  • Song tempo.

Our goal is to use these features to predict the song popularity, an index ranging from 0 to 100. In the examples we show above, we are aiming to predict the following song popularity:

Instead of using sklearn, we are going to use PyTorch modules to predict this continuous variable. The good part of learning how to fit linear regressions in pytorch? The knowledge we’re going to gather can be applied…



Source link