How to implement an Attention-RNN model into solving a marketing problem: Multi-Channel Attribution
By: Weixing Travis
Tags: Marketing, Machine Learning, Multi Channel Attribution, Attention Mechanism, Lstm

BACKGROUND

Recently I worked on a Capstone project that requires my team to build a model that could deal with a typical business problem: multi-channel attribution. Its definition is as follows:

Multi-channel attribution refers to the process of determining which marketing channels ultimately lead to a sale and giving each channel the appropriate amount of credit per its role in the sales cycle.

In this specific scenario (due to the secrecy agreement, I will use fake names and only show part of the data), the company F wants a model that can accurately assign weights to the digital channels (like Email, social media ads, etc) that lead a sale on their e-commerce website. The model is gonna be informed by the last 6 months of consumer data in terms of their browsing and demographic behavior from Adobe Analytics Data Feed.

pic2. part of the fake data (not the real data for privacy)

A fraction of the fake data is shown above. Each line is a session with the VISITOR_KEY as cookie id. For example, in the first line, a customer visited the e-commerce website from a Natural Search (maybe google search) result link and ended up not purchasing anything (no conversion). Within the concealed data, it contains other customer information (like demography, segmentation, etc).

PROBLEM INTERPRETATION

The calculation of attribution means a lot for your business. “Having a clearer idea of which marketing channels are driving conversions and sales means you can better allocate your marketing dollars to the most effective channels and better track the interactions on a prospective customer’s journey”.

Let’s take a specific customer C as an example. In the beginning, C might hear company F from somewhere and she searched F on Google and clicked a link to F’s website on 7/11/2018. At that time, she didn’t purchase anything but registered on the website. Then on 7/13/2018, she hit the link in the email sent by F and visited the website again. On 7/20, she browsed the website again from a paid searched result and on 7/31, she went through an advertisement on a certain media, by this time, she finally made a deal on F’s site.

The question is, if F has a certain amount of marketing budget, how would F allocate that money to those digital channels? Which channel is more important? Does customer profile influence her interaction with those channels and even the final conversion rate?

pic3. customer digital journey

MODELING

There are several models in the market aiming at those questions: Linear model (distributes the weights equally), first-interaction (gives all to the first channel), last-interaction (all to the last channel), Markov Chain (a probability-based model) and so forth. But each of these models has flaws and can’t show the whole picture (some of them fail to consider the sequential characteristics of the channel path; some didn’t take the customer profile into consideration, etc).

So, the model I will introduce takes advantage of the features of the Deep neural network to provide an optimal solution to the multi-channel attribution problem and got a state-of-the-art performance.

Basically, the model should be able to devour both the channel data (feature a sequential pattern) and other customer profile data.

process data

After some data wrangling, part of the path data, and customer profile data are listed below (due to secrecy agreement, I won’t display all the data):

pic4. channels data

pic5. Customer profile data

pic6. time-decay data

Then, using the code below, the channels data are vectorized and padded into the qualified input data for RNN model with the same length. The time-lapse data are scaled and will be used in the revised softmax function in Attention layer.

RNN with Attention

pic7. Attention RNN

LSTM to capture the sequential pattern

Let’s take a look at the middle column of the processed data in picture 4. It looks like that the channels for a specific customer journey are not isolated but connected together in a sequence. You can’t owe a successful conversion all to the last channel (like media in the above case) but ignore the effects of the previous ones. In that, it is not hard thinking of RNN (recurrent neural network) model, which is widely used for managing sequential data task like natural language processing.

Unlike feedforward neural networks, RNNs can use their internal state (memory) to process sequences of inputs. This makes them applicable to tasks such as unsegmented, connected handwriting recognition or speech recognition.

Simply put, as shown below, the RNN model introduces the parameter a (hidden state) here to take on a role that could memorize the information of the previous step. But there are some problems concerning the basic RNN model: like when there are more steps, it’s hard to propagate back and influence the weights of earlier layers (vanishing gradient). LSTM then came out as a consequence.

pic8: a basic RNN model

LSTM is short for Long Short-Term Memory model. I won’t talk much here, but maybe I will write another article to explain this model and all the mathematics and codes behind it. Basically, LSTM cell features a combination of an input gate, an output gate and a forget gate. Those three gates would regulate how much to memorize and how much to forget about the previous information so as to control the flow of the information in and out of the cell.

pic9. within an LSTM cell

The path variables as input data are sent into the LSTM layer and got output for each one.

pic10. Model step 1: LSTM layer

The LSTM architecture is able to capture the sequential pattern of the channels data, however, it doesn’t have the ability to give credits to them.

The Attention Mechanism to assign credits

When I was learning machine translation, I began to meet the Attention mechanism, which deals with the problem that the Bleu Score will drop when the sentence is longer by assigning weights to the original words of the sentence on their importance to the translation. For instance, the model is translating a Chinese sentence “他爱旅游” into English “he loves traveling”. When it comes to the word “loves”, the attention vector might be like [0.2,0.79,0.05,0.05] that corresponds to [他,爱,旅,游]. The reason is that 1. “爱” means “love”, so the word “爱” has the highest weights; 2. “他” means “he”, which is the third person singular. This will influence the verb “love” to be “loves”. Thus it has the second highest weight. During the training process, the model will learn those weights.

So, how is the Attention layer like? Below is one step of the attention mechanism. Let’s look at the right image with more details: The a is the output of LSTM. Then the concatenation of a with the hidden state s, <a,s>, as the input is sent to a dense layer and a softmax layer; the outcome of the softmax layer is a vector with probabilities (like [0.2,0.79,0.05,0.05] in the translation example) used to multiply the corresponding matrix <a,s>, then the summation of all will be one output context<t>. In the machine translation context, this step will be repeated to get the output vector context with the length equals to the number of the translated words, then sent these outputs into another LSTM again to get the final translation result. But in this case, all we need is only one output from attention.