Classify spotify artists using an Artificial Neural Network

This guide trains a neural network model to classify spotify artists, like taylor swift and beyonce. It is adapted from this colab

Import the Spotify Data

Plot the first 25 elements

To see what it looks like

Build the model

Building the neural network requires configuring the layers of the model, then compiling the model.

Set up the layers

The basic building block of a neural network is the layer. Layers extract representations from the data fed into them. Hopefully, these representations are meaningful for the problem at hand.

Most of deep learning consists of chaining together simple layers. Most layers, such as tf.keras.layers.Dense, have parameters that are learned during training.

The network consists of a sequence of two tf.keras.layers.Dense layers. These are densely connected, or fully connected, neural layers. The first Dense layer has 128 nodes (or neurons). The second (and last) layer returns a logits array with length of 5. Each node contains a score that indicates the current image belongs to one of the 5 classes.

Compile the model

Before the model is ready for training, it needs a few more settings. These are added during the model's compile step:

Train the model

Training the neural network model requires the following steps:

  1. Feed the training data to the model. In this example, the training data is in the train_images and train_labels arrays.
  2. The model learns to associate images and labels.
  3. You ask the model to make predictions about a test set—in this example, the test_images array.
  4. Verify that the predictions match the labels from the test_labels array.

Feed the model

To start training, call the model.fit method—so called because it "fits" the model to the training data:

As the model trains, the loss and accuracy metrics are displayed. This model reaches an accuracy of about 80% on the training data.

Evaluate accuracy

Next, compare how the model performs on the test dataset:

It turns out that the accuracy on the test dataset is a little less than the accuracy on the training dataset. This gap between training accuracy and test accuracy represents overfitting. Overfitting happens when a machine learning model performs worse on new, previously unseen inputs than it does on the training data. An overfitted model "memorizes" the noise and details in the training dataset to a point where it negatively impacts the performance of the model on the new data.

One way to deal with this is by introducing some regularization with a Dropout layer. To do this, you need to go back to re-initialize the Sequential class with one layer being tk.keras.layers.Dropout(0.2), where 0.2 is the amount of units in percentage to remove between layers. The more you take, the stronger the regularization.

Make predictions

With the model trained, you can use it to make predictions about some images. The model's linear outputs, logits. Attach a softmax layer to convert the logits to probabilities, which are easier to interpret.

Here, the model has predicted the label for each artist in the testing set. Let's take a look at the first prediction:

A prediction is an array of 5 numbers. They represent the model's "confidence" that the image corresponds to each of the 5 different articles of clothing. You can see which label has the highest confidence value:

So, the model is most confident that this is artist class_names[1]. Examining the test label shows that this classification is correct:

Confusion Matrix

Use the trained model

Finally, use the trained model to make a prediction about a single image.