Build NN in 5 mins — Basel AlBarzanji

Build a Neural Network in 5 minutes to accelerate late invoice and debt collection using Keras and Python — Part 2

Basel Barzanji
5 min readOct 21, 2017

--

In part 1, we discussed neural networks in general and built a basic deep learning model.

In part 2 we will modify the code we built in part 1 to solve our late invoice problem. I suggest glancing through part 1 this will make part 2 easier to understand.

Let’s start building our Keras deep learning model to accelerate debt and invoice collection.

Assume that we are a startup or a small business with lots of customers. We do not have the time or resources to follow up on each late bill. We have decided to automate email, sms, and call reminders to our customers using 3 different tone levels(friendly, neutral, firm). So we send a friendly email first, and if no payment action is taken we send an friendly sms, then a friendly automated call, then a neutral email and so on until payment action is taken.

Our aim is to understand what tone levels and number of messages motivates our customers to make a payment on a late invoice.

The diagram below illustrates a rough one hidden layer example of how our model will work. We sent two emails, two sms messages and made zero phone calls to the customer. This resulted in an output of one, meaning the customer was motivated to make a payment.

Here we only have 3 neurons in the hidden layer, in other models you might see 100 or more neurons.

Build NN in 5 mins — Basel AlBarzanji

The Keras model will attempt to adjust the weights going from inputs> hidden layer > output until the model is able to accurately predict an output of 1.

The model will iterate many times over our input data to have a final value of connection weights that fit our input data with the lowest error possible.

I did not have access to public data for this specific example. I made up data with some constraints to see if my model can be trained to make a correct prediction. I assumed the following:

  1. Each Email, SMS, Phone Call can have a value from 0–3, indicating 0-nothing sent, 1-friendly, 2-neutral and 3-firm content tones.
  2. No payment occurs if no email is sent i.e. is email value is 0.
  3. Payment occurs only if the same number of emails and sms messages are sent, i.e 2 and 2, 3 and 3.
  4. No payment occurs if only one 1 email and 1 sms is sent, there has to be a phone call.

As you can see, we can introduce more variables, such as days the invoice is late, invoice amount, customer income level, timing of messages etc… For now we will limit our model to 3 inputs.

Below is the training data we will be feeding our deep learning model:

1 indicates one friendly message was sent to the customer. 2 indicates two messages sent, friendly and neutral tones. 3 indicates 3 messages sent, friendly, neutral and firm tones.

Build NN in 5 mins — Basel AlBarzanji

For the outputs, 1 indicates a payment was, 0 indicates no payment was made.

We want to see if our model can predict the following correctly:

Build NN in 5 mins — Basel AlBarzanji

Based on our assumptions, sending the 9 messages above should have a high likelihood of action being taken. The output should be 1.

Our Keras model will be trained using the following parameters:

1 . We have two deep layers in our model, with 30 and 10 neurons each. See if you can get better results with more or less neurons and layers.

2. We will be using Rectified Linear Unit as our activation function. You can definitely use another activation function such as Sigmoid. But ‘relu’ gave me better results here.

3. We will use ‘Adam’ as our model optimizer instead of SGD or Stochastic Gradient Descent. There are plenty of resources to explain the differences between the two. Keras provides other optimizers as well.

4. Setting our output to ‘to_categorical’ in Keras it will look like the table below. We should have 0 in the first output column and 1 in the second output, which indicates the model predicted an outcome of 1 where action was taken to make a payment:

Build NN in 5 mins — Basel AlBarzanji

5. Because we are classifying our output, as 0 or 1, will use ‘categorical_corssentropy’ loss function instead of MSE (mean-squared-error) loss function.

Here is our model:

Once our compiles, we get a really low model error rate. The model correctly predicted an output of 1 in the second column indicating an action will be taken by the customer when 3 emails, sms messages and phone calls are made:

Build NN in 5 mins — Basel AlBarzanji

Next Steps

Try building and running the model on your own. If you run a business and have access to actual data, you now have a blueprint to see how different variables can help you accelerate customer late payments. Introduce more than 3 variables to your training inputs, such as number of days the invoice is late, invoice amount or the timing of the messages. Analyze how different customer categories respond to different variables.

You can clone or download the model here:

https://github.com/baselbb/keras_debt_nn/blob/master/keras_debt_nn.py

--

--

Basel Barzanji

Product Development Consultant. Founded & sold capital markets platform. Interested in Python, Finance and Machine Learning.