This repository contains a PyTorch-based implementation of a Transformer model to predict the popularity of Spotify songs based on audio features.
The model learns to predict the track_popularity score (0β100) using a set of numerical audio features extracted from the spotify_songs.csv dataset. It uses a Transformer encoder architecture β originally developed for NLP tasks β and adapts it for regression.
-
Transformer Encoder with:
- Positional projection from 1D to
d_model(default: 32) - Multi-head self-attention (
nhead= 4) - Two encoder layers (
num_layers= 2)
- Positional projection from 1D to
-
Output layer flattens sequence and predicts popularity as a single scalar value.
The following features are used as input:
danceabilityenergyloudnessspeechinessacousticnessinstrumentalnesslivenessvalencetempo
Target:
track_popularity
This project expects a CSV file named spotify_songs.csv with the columns listed above.
Missing values are dropped during preprocessing.
Note: Update the CSV path in the script if needed.
- Python 3.8+
- PyTorch
- scikit-learn
- pandas
- matplotlib
You can install dependencies using:
pip install torch scikit-learn pandas matplotlib- Features are standardized using
StandardScaler. - Target is kept as raw values (0β100).
- Data is split into training and testing sets.
- Features are reshaped into sequence format for the Transformer.
- The model is trained using Mean Squared Error (MSE) loss.
- Optimizer: Adam
- Number of epochs: 15
- After training, the model is evaluated on the test set.
- Final MSE loss is printed.
- A training loss chart is displayed.
A plot of training loss over epochs helps visualize the learning curve.
You can save the loss plot in your script using
plt.savefig("loss_plot.png").
βββ spotify_songs.csv # Dataset file
βββ transformer_regression.py # Main training script
βββ README.md # This file
To run the script:
python transformer_regression.py
