Using Neural Network for Ranked Recommendations in R

Introduction

Neural networks can be used is varieties of innovative ways for predictions, regression, forecasting, deep learning features, and recommendations. What makes neural networks so versatile is its feature of tuning the number of nodes (neurons) in its input and output layers (of course, the great flexibility offered by the hidden layer(s) is beyond appreciation!!). Basically what such kind of framework offers is: You can have any number of outputs against any number of inputs. So if you have a powerful “machine” to run the training phase of a neural network, you can use it in a number of powerful ways.

Here I discuss an R code to implement a neural network to make ranked recommendations. I use the FCNN4R library of R for this (you can read about it here). The dataset I used is a public one with a lot of pre-processing already done. To save space, I avoid doing in the details of the data, but I show its header to show how it looks.

Data

The data describes how a product can be labeled with tags from a finite list of tags. The product is described by its features, a unique object identifier, and a set of tags which categorize it into multiple groups. Each row is denoted in one hot encoding format. A product can be described using features from a list 37 unique features and can be labeled from a list of 437 unique category tags.

table_head

A sample of the train data used to learn the weights in out neural network model. The data represents what a one-hot coding would look like.

Recommendation Problem

For a given product (described by its features), we want to recommend a list of relevant category tags. So the inputs are the product feature(s) and the outputs are the category tag(s). Below I describe how we can use a neural network to solve this problem.

nn_diagram

A representation of neural network for tag recommendation problem (the number of input and output nodes is reduced for illustration purpose).

Training

So first we design and train a fast neural network!  We build a neural network consisting of 37 nodes in the input layer, 200 nodes in the hidden layer (only 1 hidden layer and it can have any number of nodes, I choose to start with 200! ) and 437 nodes in the output layer. There are 9172 train samples in my data, each with 37 input features and 437 output features. The input and output features are connected to the input and output nodes respectively. We use a resilient backpropagation algorithm to learn the neural network weights.


inp = data.matrix(train[,c(2:38)])

outp = data.matrix(train[,c(39:475)])
library(FCNN4R)
# create a 37-250-437 network
net = mlp_net(c(dim(inp)[2],250,dim(outp)[2]))
# set activation function in all layers; I am using sigmoid
net = mlp_set_activation(net, layer = "a", "sigmoid")
# randomise weights
net = mlp_rnd_weights(net)
# tolerance level
tol = 0.5e-3
# teach using Rprop, assign trained network and plot learning history
netmse = mlp_teach_rprop(net, inp, outp, tol_level = tol,
max_epochs = 500, report_freq = 10)
net = netmse$net

# if the algorithm had converged, prune using Optimal Brain Surgeon and plot
if (mlp_mse(net, inp, outp) <= tol) {
net = mlp_prune_obs(net, inp, outp, tol_level = tol,
max_reteach_epochs = 500, report = TRUE)[[1]]
mlp_plot(net, TRUE)
}

Testing

So, now our neural network is already trained. That means it has “learned” which product features should map to what category tags! Now given a product described in the 37 dimension feature space, we can predict a set of tags for it.

output_NN = round(mlp_eval(net, data.matrix(test[,c(2:37)])), digits = 5)

 

Ranking

For a given product (in feature space), our neural network predicts scores (between 0 and 1) on each output node (each tag). We can use these scores to rank these tags for the input product. By keeping only those tags which obtain a score > 0.0 (or some minimum threshold) and ordering them based on the score values. This gives us a ranked list of tags for a given product.

Performance validation

The performance of this approach can be easily validated if you create a test set containing products and their corresponding category tags. The actual category tags can be compared with the ranked list of predicted tags. The following information retrieval metrics will be useful: Recall@k, Precision@k, Mean Average Precision (MAP).

Conclusion

We described a neural network approach to solve a tag recommendation problem. We leverage the flexibility of neural network model to allow multiple inputs and output nodes, to learn a mapping between product features and its category tags. Although we have only presented the approach for product-feature to category tags mapping, but one can easily think of several modifications of this problem settings.

Let us know if this approach helps in any of your projects or if you need any help implementing, testing and developing products/services using such approach. We, Contata Solutions, have the expertise and resources to provide a full end-to-end solution for your business needs.