# Machine learning algorithm which gives multiple outputs from single input

I need some help, i am working on a problem where i have the OCR of an image of an invoice and i want to extract certain data from it like invoice number, amount, date etc which is all present within the OCR. I tried with the classification model where i was individually passing each sentence from the OCR to the model and to predict it the invoice number or date or anything else, but this approach takes a lot of time and i don't think this is the right approach.

So, i was thinking whether there is an algorithm where i can have an input string and have outputs mapped from that string like, invoice number, date and amount are present within the string.

E.g: Inp string: the invoice #1234 is due on 12 oct 2018 with amount of 287 Output: Invoice Number: #1234, Date: 12 oct 2018, Amount 287

So, my question is, is there an algorithm which i can train on several invoices and then make predictions?

Keras functional API's are a way your can solve you problem. Using keras functional API, we can build models that resembles more like graphs such as this:

In order to build a model like this, you can use keras as follows:

from keras.models import Model
from keras import layers
from keras import Input

input_layer = Input(shape=(100,), dtype='float32', name="Input")
split_layer = layers.Dense(32, activation='relu', name='split_layer')(input_layer)
first_layer = layers.Dense(32, activation='relu', name='first_layer')(split_layer)
second_layer = layers.Dense(32, activation='relu', name='second_layer')(split_layer)
model = Model(input_layer,[first_layer, second_layer])
model.summary()


In order to compile this model, we can define different loss functions for different layers

model.comile(optimizer=optimizer,
loss={'first_layer':'mse', 'second_layer':'binary_crossentropy'},
metrics=['accuracy'])


Once you are done with building the network, you could simply fit you data as follows:

model.fit(X,
{'first_layer': first_layer_Y,
'second_layer': second_layer_targets},
epochs=10
)

• Thank you for your answer but, how can input a string directly onto the keras, and how to prepare my dataset like the one i mentioned above? I'll have to preprocess my input string through some vectorizer and then encode the target values. How to do all that so my output is mapped from within the input string itself – Bhawesh Chandola Oct 10 '18 at 6:42
• If the data is in images, I cannot suggest you anything other than OCR to extract the text. Once the text is extracted, you can solve your problem by either Building attention models or Using IOB tagging etc. If this is not helpful, kindly rephrase your question here in comment section properly. – thanatoz Oct 10 '18 at 6:55
• I have done the OCR of the image and the text of the image. Now i want to extract certain data from that OCR'ed text like invoice number, amount and date. So, i want to make a model where as input i have the OCR of the image and for output i want to map the invoice number, amount and date from the OCR input itself. – Bhawesh Chandola Oct 10 '18 at 6:59
• Try the attention model and try to converge the model to classify the required data ( invoice number, amount and date ). – thanatoz Oct 10 '18 at 8:15
• OKay, I looked into it and found encoder decoder sequence to sequence with attention model and I think it might be a possible solution. Thanks a lot. – Bhawesh Chandola Oct 10 '18 at 10:36