Recommending Songs and Playlists: A Guide to Building a Music Recommendation System

Jun 11, 2022 | Data Science

Have you ever wondered how your favorite music streaming platforms suggest the perfect songs for your mood? In this article, we’re taking a dive into the world of music recommendation systems, specifically through the lens of a fascinating project by Lucas De Oliveira, Chandrish Ambati, and Anish Mukherjee. We’ll walk through the steps they took to build an efficient song and playlist recommendation system using some powerful technologies. Buckle up as we explore the melody of machine learning!

Introduction and Motivation

It all started with a dataset. In 2018, Spotify organized an Association for Computing Machinery (ACM) RecSys Challenge, where they released a massive dataset of one million playlists, challenging participants to recommend a list of 500 songs based on user-created playlists. The challenge intrigued music lovers and data scientists alike, prompting an exploration of how to build a scalable recommendation system that would work with over 4 billion user-curated playlists on Spotify.

Workflow

Collecting Lyrics

The first phase of the project involved gathering lyrics for songs in the playlists. The team began with over 2 million unique songs, but collecting lyrics initially proved to be a daunting task. They started with simple Python requests, but soon pivoted to using asyncio for concurrent requests, drastically reducing the time it took to gather the lyrics from 15 minutes to under a minute for 10,000 songs. In the end, they managed to collect lyrics for 138,000 songs.

Preprocessing

The original dataset had 1 million playlists spread across more than 1,000 JSON files, totaling around 33 GB. Using PySpark within Databricks, they combined these separate JSON files into a single SparkSQL DataFrame and joined it with the collected lyrics. This streamlined the data for their analyses, allowing them to read from MongoDB back into Databricks easily.

Training Song Embeddings

The next step was to create embeddings for the songs using Spark MLlib’s Word2Vec model. Here, the playlists’ song lists were fed into the model akin to sentences in natural language processing. The entire training process was efficiently completed in just three lines of PySpark code. The resulting song embeddings were saved back into MongoDB for later use.


# Example of training song embeddings in PySpark
song_data = spark.read.format('mongodb').load('song_embeddings')
song_embeddings = Word2Vec(vectorSize=32, minCount=0, inputCol="song_list", outputCol="result").fit(song_data)

Training Playlist Embeddings

Expanding the task, the team developed a method for recommending playlists rather than just songs. By treating playlists as a “continuous bag of songs,” they averaged the song embeddings to create a playlist embedding, which was then saved in MongoDB.

Training Lyrics Embeddings

Lastly, they also trained lyrical embeddings. Each song’s lyrics were processed to create embeddings based on the average embedding of the individual words using the Word2Vec model.

Notes on Embedding Training Approach

You might be curious why they opted for a language model like Word2Vec instead of alternative methods. The rationale stemmed from wanting to operate within the Spark ecosystem while maintaining the advantages of using a context-aware model. By utilizing Word2Vec, the system effectively captured the similarities of songs based on how they co-occurred across thousands of playlists.

Visualization and Recommendation

With the embeddings trained, the team employed Tensorflow’s Embedding Projector to visualize the embeddings in a three-dimensional space. This interactive tool allowed them to assess and understand the recommendations effectively.

To explore the song embeddings, click here for the full 2 million songs projection, or here for a simpler version with a random sample of 100,000 songs.

Conclusions

The approach of training embeddings yielded surprisingly effective results, managing to group songs together meaningfully. The results not only underscore the feasibility of this method but also show that certain playlists had recognizable clusters—which validates the overall embeddings model.

Troubleshooting Ideas

If you encounter challenges while implementing a similar music recommendation system, consider these tips:

  • Ensure your data is clean and well-structured before preprocessing.
  • Adjust the parameters of your models carefully to optimize for your data’s specific characteristics.
  • If you’re struggling with performance, look into optimizing your Spark settings for better distributed processing.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Further Scope

  • Consider deploying these trained song embeddings for further tasks or applications and download the embeddings here and here.
  • Explore alternative methods of training embeddings, such as recurrent neural networks for deeper insights.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox