RapNet: A Neural Network For Classifying Hip-Hop Artists Based on Song Lyrics

Alex Chan
6 min readDec 22, 2017

by Alex Chan

­

Introduction

Musical artists distinguish themselves in many ways, defining their own styles and genres to appeal to an audience. One of the ways artists accomplish this is through lyrics. Lyrics act as a medium for the artist to convey a message — whether it be a personal experience, a story, a call to action, or anything in between. The goal of this article is to provide an overview on how to develop a classifier to distinguish between lyrics of different artists, specifically in the hip-hop genre.

Background

From personal experience, machine learning classifiers generally do well on genre classification tasks for both music and books. These approaches usually involve a bag of words representation of the text, and different genres have relatively big differences in their common words. I wondered if it were possible for a model to solve the more difficult task of classifying artists of the same genre.

Data Collection

The data analyzed in this project is song lyrics. Lyrics were retrieved using the Genius API to get the URLs to the lyrics of songs by an artist. Then, using the BeautifulSoup library, the data was parsed from the HTML of the page containing the lyrics. All song lyrics from the entire discography of each artist were collected and saved for use in our model.

Preprocessing

The lyric data was cleaned by normalizing the words. This was done by converting all of the words to lowercase and removing non-alphanumeric characters from the text. Once the data was cleaned, it needed to be transformed into input for the model. First, the data was converted into a document-term matrix, where each row represented a sample (a song lyric) and each column represented a feature, with the sample’s corresponding count of that feature. The features consisted of all of the unigrams and bigrams (one and two word phrases) from the lyric data. After that, the document term matrix was converted to a TFIDF matrix. TFIDF, which stands for Term Frequency-Inverse Document Frequency, is a way of assigning a measure of how important a word is in classifying a sample to a class. Once in this representation, the data was ready to be input into the model.

Model Selection

The data was evaluated by comparing the results from 3 models: Random Forest, SVM, and Neural Networks.

The Random Forest, SVM, and Neural Network models perform with 73%, 80%, and 88% accuracies, respectively. The neural network outperforms the other two models at this task. Neural networks are known to perform exceptionally well for various classification tasks. Neural networks work by doing several computations through various “layers” of the model, computing dot products and activation functions. The output is a set of probabilities that indicate the likelihood of a sample belonging to a particular class. The model improves by first evaluating a “loss function” (how much error the model produces) and then uses backpropagation, an algorithm that uses gradient descent to find the global minimum of a convex function, to adjust the weights of the model to make improved predictions. After tuning the hyperparmeters of our model, which include learning rate, hidden layer size, number of hidden layers, and activation functions, the model that performed best was a 4 layer neural network (with hidden layer sizes 256, 128, 128, and 64, respectively) that used a hyperbolic tangent activation function and a log loss function. Using our TFIDF representation of the data as the input, the model performs the series of computations in the network, and outputs its guess as to which artist the song lyrics belong to.

Below is a small scale example of the neural network. The TFIDF representation of a song would be fed into the input layer. For each node in the hidden layer, the value is computed by the hyperbolic tangent function applied to the dot product of the input layer and the weight matrix between the input and hidden layer. The output goes through the same process, except using a softmax function instead of a hyperbolic tangent function so that it produces a probability as its output. In this example, the model predicts that there is a 91% probability that this song is by Tyler the Creator, and so that will be the model’s classification.

Results

The training and test data consists of song lyrics by the artists Kendrick Lamar, Logic, and Tyler the Creator. The model was trained on 80% of the data, and then tested on the remaining 20% of the data. Grid search was used to tune the hyperparameters of the model (layer size, learning rate, batch size). Once the optimal hyperparameters were found, 5 fold cross validation was used to determine the success of our model. Our model achieves an 88.5% accuracy on the test set and a cross validation accuracy of 83.7%.

Analysis

A test set accuracy of 88.5% shows that our model does pretty well at predicting the artists for each song lyric into the three classes. The table below shows the most frequent words sung by each artist with stop words, common words that don’t contribute to the success of the model, removed.

As seen by the table of word frequencies and the general themes of the artist, Tyler the Creator generally talks about more emotional topics like love and depression. Logic is more involved with topics like life and racial equality. Kendrick Lamar talks about a culmination of these topics, as well as “money” and “high”.

Interestingly, the model seemed to struggle particularly with Kendrick Lamar songs, which overlapped significantly with the themes of the other two artists. While the model was able to predict all of Tyler the Creator and Logic songs in the test set correctly, it misclassified several Kendrick Lamar songs. The song “Love” shares several of the central themes in Tyler the Creator’s songs, mainly including ideas about romantic relationships.

“So give me a run for my money Sippin’ bubbly, feelin’ lovely, livin’ lovely Just love me I wanna be with you, ayy”

Another line from “Kush and Corinthians” talks about living a fulfilling life, which shows many similarities to many of the songs in Logic’s 2017 album, “Everybody”.

“Die to it, live your life, live it right, be different, do different things”

While the model has some shortcomings at differentiating songs with similar themes, overall it does very well at classifying song lyrics to their corresponding artists.

Conclusion

These results can hopefully provide a further step towards solving harder classification problems. While genre classification has had generally great success, classification of artists with semantically similar themes proves to be a harder problem to solve. The evaluated model works well on artists with different themes; it was successfully able to distinguish between Logic and Tyler the Creator songs. However, the model fell short at predicting Kendrick Lamar songs, whose themes range across variety of different topics. Further studies include classifying more artists, and determining methods to make models even better at distinguishing between artists with similar themes, such as rhyme scheme and repeated phrases.

--

--

Alex Chan

Computer Science @ UC Berkeley. Digging deep into Machine Learning.