AI Procurement Chat Bot

Intro

Utilizing Deep Learning for procurement bot that is able to answer most frequently asked questions regarding corporate internal web-shop.

Artificial Neural Network:
  • Consisted of artificial neurons which has their iunputs and outputs.
  • Inputs can be values of some features, e.g. image brightness or number of pixels (in image recognition app).
  • All conections between input values and neurons are weighted somehow - value of given feature is multiplied by a weight
  • We sum all the results of multiplication from each feature_value * weight and the sum goes through the so-called activation function:
    x1 = input0 * weight
    x2 = input1 * weight
       adding bias prevents from getting nulls
    sum = x1 + x2 + bias
    activ_func(sum)
       we can simplify this with below equation:
    output = F(∑(xw)+bias)
       where F is an activation function.
  • Activation function does the following transformation:
    - if sum > 0 then output = 1
    - if sum < 0 then output = 0
  • The calculated output is a neuron's prediction itself.


  • source: udemy
Deep Learning:
  • The adjective "deep" refers to use of multiple neuron layers, which raw data input comes through, between input and output layer.
  • First outer layer is an input layer, last outer layer is an output layer, layers between are so-called hidden layers.
  • Hidden layers are those ones that accept input data, perform computations and produce output.
  • When 3 or more hidden layers we can call neural network as deep network.
  • Neural network progressively transforms data input starting from low-level (simplified) to high-level (detailed) features.
  • That way in image recognition application, first hidden layer recognizes edges when last hidden layer recognizes a human's face.
Cost Function:
  • We can evaluate performance of neurons using a cost fucnction that measures how far we are with prediction from an expected value.
  • Cost Function measures the error - difference between output (predicted value) and reality (expected value).
  • Natural sequence of measureing error is attempting to correct the prediction (getting closer to expected values) in order to decrease an error.
  • The process of producing predictions and correcting predictions, getting lower and lower error, is what in fact we call learning.
  • Getting lower error requeires us to minimize a cost function. We can achieve this by using the algorithm: gradient descent.
Gradient Descent:
  • Algorithm used for finding the local minimum of a function.
  • Finding the minimum of the cost function is the main task in the deep learning process.

    source: udemy
  • Using gradient descent we can determine the best possible neural network's parameters, like weights in neuron's inputs, for minimizing the cost.
  • Adjusting the optimal parameters across an entire network requires us to apply so-called backpropagation.
  • When data processed once, backpropagation goes back through the neuron layers and recalculates error contribution of each neuron.
Here is what makes the bot working:
  • Script converts human's language into numbers.
  • Each occurance of a word is being tracked down into a list which finally serves to train the ML model.
  • Script takes advantage of tensorflow library that is commonly used for deep learning projects.

Features

App includes following features:

  • Tensorflow
  • TFLearn
  • NLTK

Demo

Application:

  1. At first, we need to learn a bot how to response depending on the question's pattern:
    - if user asks questions similar to a pattern the bot was fed with during training, then bot is able to give a right answer back,
    - bot compares user's question to a patterns it learnt, then it gives the best maching answer,
    - there are ready patterns and responses given with JSON file which is data input for bot training,
    - the category of what given pattern and corresponding response refers to, indicates JSON tag key.
  2. Preparing so-called bag of words to put sentences down into, understandable for computer, digits:
    - bag of words is being prepared for both a bot and an user,
    - for bot, we train a bot to be able to decode human's language,
    - for user, we translate what user says into computer's lanuage.
  3. Bag of words depends on sequence of operations on humans language:
    - sentences splitting into collection of single words called tokens:
             'Hello World' --> ['Hello', 'World']
    - bringing all words into lowercase characters
    - words stemming for faster model training:
             'saying' --> 'say'
    - throwing all processed and simplified words into one collection - a bag
    - words vectorization:
             if word from a bag occurs in pattern sentence then it scores 1
             bag of words: ['all', 'great' 'is']
             pattern: ['great','work']
             vectorized bag of words: [0,1,0]
             as 'great' word from bag occured in the pattern
  4. Script uses TFLearn to build the Deep Neural Network consiste of:
    - first layer of data input,
    - x number of hidden layers in between first and last layer,
    - last layer of data output as an outocme of goind data thorugh neuorns and so-called activation function.

Chatting with bot:

Setup

Python libraries installation required:

pip install tensorflow
pip install tflearn
pip install nltk

Source Code

You can view the source code: HERE