SlideShare a Scribd company logo
1 of 18
Download to read offline
CoreML for NLP
An Automated Tensorflow (Keras) workflow for Android
and CoreML for NLP text parsing
Topics to be covered
• Background to Natural Language Processing
• Word Embeddings
• Recurrent Neural Networks
• Keras/Tensorflow CoreML
• Automated build workflow for Android and iOS
using Python templating
What is Natural Language
Processing?
• NLP is the process of having
machines understand
language
• iOS 11 has built in examples
such as
NSLinguisticTagger for
tokenisation and
lemmatisation
• Intent Classification & Slot
Tagging is a sub-field of NLP.
Used as a technique for Siri,
Alexa, and Google Assistant
Play me a jazz song from Louis
Armstrong from 1967
{
"intent": "play_music",
"slots": [
0: {
"genre": "jazz",
"artist": "Louis Armstrong",
"year": "1967"
}
]
}
Tokenisation:
Lemmatisation:
“The cat sat on the mat” = [“The”, “cat”, “sat”, “on”, “the”, “mat”]
“Running” = “run”, “Swam” = “Swim”
Intent Classification & Slot Tagging:
Pros and cons of on device NLP (and most ML/AI)
Pros: Cons:
Privacy Flexibility
Cost Memory
Availability Compute
Speed Difficulty
Data Vectorisation
• In machine learning, data
representation is very
important.
• 🗑 ! fn(x) ! 🗑
• We like our data to be
represented as vectors.
• Images are vectors (JPEG)
and so are sounds (WAV)
• How do we vectorise words for
NLP?
255 45 199 12
23 129 34 5
49 56 94 254
87 142237175
123243192188
255 45 199 12
23 129 34 5
49 56 94 254
87 142237175
123243192188
255 45 199 12
23 129 34 5
49 56 94 254
87 142237175
123243192188
R
G
B
Colour images can be represented as a Rank 3
Tensor
Imagine this construct = [[[xr],[yr]], [[xg,][yg]],
[[xb],[yb]]]
Word Embeddings
• One-hot encoding can be done but is
sparse and does not capture
semantics.
• Word Embeddings are a solution to
creating a dense vector
representation of words.
• What makes for a good word
embedding?
• What is semantic representation?
• How do we make a word embedding?
• Word2Vec vs FastText vs GloVe
Hello
1
0
0
0
0
World Taylor Swift
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
Nope
0
0
0
0
1
One Hot Encoding
http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/
Good Word2Vec Tutorial
Semantic Representation
Artificial Neural Networks
• Artificial Neural Networks are
the basic building blocks for
our Deep Learning NLP
solution.
• Word Embedding systems like
FastText, GloVe and Word2Vec
are built using ANNs.
• Multiple ANNs = Deep
Learning.
• TLDR; Multiple linear equations
with a non-linearity applied.
Artificial Neuron
Multilayered Perceptron (Dense Networks)
+b
Recurrent Neural Networks
• Deep Learning Models are made up lots
of small artificial neural networks wired up
in a particular way.
• Convolutional Neural Networks are very
popular for image recognition and can
even do sequence modelling.
• Recurrent Neural Networks are currently
the best way to model sequences due to it
having “memory”
• Downsides of Recurrent Neural Networks
is the “vanishing gradient” problem.
• Long-short-term Memory (LSTM) and
Gated Recurrent Units (GRU) help reduce
“vanishing gradient” by having “forget
gates”
http://colah.github.io/posts/2015-08-Understanding-LSTMs/
Useful resource about LSTMs
Automated CoreML Training &
Deployment Architecture
React WebApp
FastText Word
Embedding Server
Tensorflow Training
Server
GCloud Bucket
React WebApp iOS Build Server
Android Build
Server
FastText Word Embeddings
• FastText Word Embeddings were
chosen to chosen as token
vectorisation.
• Pre-trained Gigaword corpus (6B
tokens) capture high-quality
semantic representation.
• Takes up 8GB of RAM.
• Solution is to search for 1000 closest
words based on L2 (euclidean
distance) to original word corpus.
• Server generates SQLite DB with
token and 128-float vectors of tokens
“Hello”
0.87
0.34
0.20
0.09
0.34
0.22
Pre-Trained FastText
Model
300-vector
Token Vectors SQLite DB
Keras on Tensorflow
• Keras is a high-level abstraction developed for Deep Learning that
runs on multiple backends (Tensorflow/Theano/CNTK/MxNet)
• Keras is CoreML compatible.
• Keras can also be converted to run on Tensorflow.
• Android has good Tensorflow support.
model = Sequential([
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
TimeDistributed(Dense(64)),
Activation('relu'),
TimeDistributed(Dense(32)),
Activation('relu'),
TimeDistributed(Dense(num_labels)),
Activation('softmax', name="output")
])
CoreML
• CoreML was built to reflect Numpy which is a common numerical
analysis and matrix manipulation library for Python.
• MLMultiArray is the most important class in CoreML.
• If you can manipulate MLMultiArray, then you can make CoreML
do anything!
• Converting CoreML is as simple as calling
coremltools.converters.keras.convert(model)
• CoreML models are statically compiled and cannot be modified at
runtime.
Extending The Model
model = Sequential([
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True),
TimeDistributed(Dense(64)),
Activation('relu'),
TimeDistributed(Dense(32)),
Activation('relu'),
TimeDistributed(Dense(num_labels)),
Activation('softmax', name="output")
])
This means that the length of
the input sequence is
computed dynamically at
training time.
The output vector is squashed
from 32 to n-number of labels. 

Vector order is important!
Extending the model in Python is easy, but
not so straightforward for CoreML
CoreML models are statically compiled
and cannot be modified at runtime.
If you can manipulate MLMultiArray, then you can
make CoreML do anything!
🤔
Jinja Templating
• After the training phase of the
NLP model, we generate a
config.json 

• JSON describes the output
models, and CoreML and
Android model files and the
required parameters
{
"models": [
{
"type": "IntentClassifier",
"name": "UtteranceIntent",
"intents": ["Make", "Weather", "News", "Recommend", "Help"],
"android": {
"filename": "intent_classifier.pb",
"inputOperationName": "lstm_8_input",
"outputOperationName": "activation_18/Softmax"
},
"ios": {
"filename": "CCLabsIntentClassifierModel.mlmodel",
"inputOperationName": "input1",
"outputOperationName": "output1"
},
"inputShape": "300"
},
{
"type": "SlotTagger",
"name": "Weather",
"slots": ["None", "Weather", "Time", "Location"],
"android": {
"filename": "weather_slot_tagger.pb",
"inputOperationName": "lstm_1_input",
"outputOperationName": "output/truediv"
},
"ios" : {
"inputOperationName": "input1",
"outputOperationName": "output1"
},
"inputShape": "300"
},...]
}
Jinja Templating Cont’d
{
"models": [
{
"type": "SlotTagger",
"name": "Weather",
"slots": ["None", "Weather", "Time", "Location"],
"android": {
"filename": "weather_slot_tagger.pb",
"inputOperationName": "lstm_1_input",
"outputOperationName": "output/truediv"
},
"ios" : {
"inputOperationName": "input1",
"outputOperationName": "output1"
},
"inputShape": "300"
},...]
}
// Flatten vector
NSMutableArray *flattenedVector = [[NSMutableArray alloc] init];
for (NSArray<NSNumber*>* element in vector.data) {
[flattenedVector addObjectsFromArray:element];
}
// Convert vector to multiarray
NSNumber* vectorCount = [NSNumber numberWithUnsignedInteger: vector.tokens.count];
NSNumber* utteranceCount = [NSNumber numberWithUnsignedInteger: 1];
NSNumber* valueCount = [NSNumber numberWithUnsignedInteger: {{ input_shape }}];
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithShape:@[vectorCount, utteranceCount, valueCount]
dataType:MLMultiArrayDataTypeFloat32
error: &predictionError];
Assembling Framework
• Once the Jinja templates are
applied, we save the .m/.h files
we run xcodegen
• xcodegen generates Xcode
project files based on a folder
structure. It is required in order
to re-build the project once the
template process is complete.
• Using xcodegen we can inject
the SQLite database and the
CoreML files with token vectors
for complete offline operation
xcodegen
xcodebuild
Demo Time

More Related Content

What's hot

Deep Learning with CNTK
Deep Learning with CNTKDeep Learning with CNTK
Deep Learning with CNTKAshish Jaiman
 
Deep learning with keras
Deep learning with kerasDeep learning with keras
Deep learning with kerasMOHITKUMAR1379
 
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016Tom Peters, Software Engineer, Ufora at MLconf ATL 2016
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016MLconf
 
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16MLconf
 
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016MLconf
 
Vectorization - Georgia Tech - CSE6242 - March 2015
Vectorization - Georgia Tech - CSE6242 - March 2015Vectorization - Georgia Tech - CSE6242 - March 2015
Vectorization - Georgia Tech - CSE6242 - March 2015Josh Patterson
 
DeepLearning4J and Spark: Successes and Challenges - François Garillot
DeepLearning4J and Spark: Successes and Challenges - François GarillotDeepLearning4J and Spark: Successes and Challenges - François Garillot
DeepLearning4J and Spark: Successes and Challenges - François GarillotSteve Moore
 

What's hot (7)

Deep Learning with CNTK
Deep Learning with CNTKDeep Learning with CNTK
Deep Learning with CNTK
 
Deep learning with keras
Deep learning with kerasDeep learning with keras
Deep learning with keras
 
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016Tom Peters, Software Engineer, Ufora at MLconf ATL 2016
Tom Peters, Software Engineer, Ufora at MLconf ATL 2016
 
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16
Jake Mannix, Lead Data Engineer, Lucidworks at MLconf SEA - 5/20/16
 
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016
Hussein Mehanna, Engineering Director, ML Core - Facebook at MLconf ATL 2016
 
Vectorization - Georgia Tech - CSE6242 - March 2015
Vectorization - Georgia Tech - CSE6242 - March 2015Vectorization - Georgia Tech - CSE6242 - March 2015
Vectorization - Georgia Tech - CSE6242 - March 2015
 
DeepLearning4J and Spark: Successes and Challenges - François Garillot
DeepLearning4J and Spark: Successes and Challenges - François GarillotDeepLearning4J and Spark: Successes and Challenges - François Garillot
DeepLearning4J and Spark: Successes and Challenges - François Garillot
 

Similar to CoreML NLP workflow for Android & iOS

The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at TwitterAlex Payne
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsMike North
 
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft..."Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...Dataconomy Media
 
Machine Learning Models on Mobile Devices
Machine Learning Models on Mobile DevicesMachine Learning Models on Mobile Devices
Machine Learning Models on Mobile DevicesLars Gregori
 
Deep Dive on Deep Learning (June 2018)
Deep Dive on Deep Learning (June 2018)Deep Dive on Deep Learning (June 2018)
Deep Dive on Deep Learning (June 2018)Julien SIMON
 
Functional (web) development with Clojure
Functional (web) development with ClojureFunctional (web) development with Clojure
Functional (web) development with ClojureHenrik Eneroth
 
Feature Engineering for NLP
Feature Engineering for NLPFeature Engineering for NLP
Feature Engineering for NLPBill Liu
 
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.Jim Czuprynski
 
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...NoSQLmatters
 
Deep Dive into Apache MXNet on AWS
Deep Dive into Apache MXNet on AWSDeep Dive into Apache MXNet on AWS
Deep Dive into Apache MXNet on AWSKristana Kane
 
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNE
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNEGenerating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNE
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNEDataWorks Summit/Hadoop Summit
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsMarko Gorički
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of ClojureDavid Leung
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!Daniel Cousineau
 
Elasticsearch and Spark
Elasticsearch and SparkElasticsearch and Spark
Elasticsearch and SparkAudible, Inc.
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous ApplicationsJohan Edstrom
 

Similar to CoreML NLP workflow for Android & iOS (20)

The Why and How of Scala at Twitter
The Why and How of Scala at TwitterThe Why and How of Scala at Twitter
The Why and How of Scala at Twitter
 
Modern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.jsModern, Scalable, Ambitious apps with Ember.js
Modern, Scalable, Ambitious apps with Ember.js
 
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft..."Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
"Source Code Abstracts Classification Using CNN", Vadim Markovtsev, Lead Soft...
 
Machine Learning Models on Mobile Devices
Machine Learning Models on Mobile DevicesMachine Learning Models on Mobile Devices
Machine Learning Models on Mobile Devices
 
Deep Dive on Deep Learning (June 2018)
Deep Dive on Deep Learning (June 2018)Deep Dive on Deep Learning (June 2018)
Deep Dive on Deep Learning (June 2018)
 
Deep Learning Summit (DLS01-4)
Deep Learning Summit (DLS01-4)Deep Learning Summit (DLS01-4)
Deep Learning Summit (DLS01-4)
 
OpenCms Days 2014 - Using the SOLR collector
OpenCms Days 2014 - Using the SOLR collectorOpenCms Days 2014 - Using the SOLR collector
OpenCms Days 2014 - Using the SOLR collector
 
Functional (web) development with Clojure
Functional (web) development with ClojureFunctional (web) development with Clojure
Functional (web) development with Clojure
 
Feature Engineering for NLP
Feature Engineering for NLPFeature Engineering for NLP
Feature Engineering for NLP
 
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.
What's Your Super-Power? Mine is Machine Learning with Oracle Autonomous DB.
 
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
 
Deep Dive into Apache MXNet on AWS
Deep Dive into Apache MXNet on AWSDeep Dive into Apache MXNet on AWS
Deep Dive into Apache MXNet on AWS
 
MXNet Workshop
MXNet WorkshopMXNet Workshop
MXNet Workshop
 
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNE
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNEGenerating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNE
Generating Recommendations at Amazon Scale with Apache Spark and Amazon DSSTNE
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and Handlebars
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
A Taste of Clojure
A Taste of ClojureA Taste of Clojure
A Taste of Clojure
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
 
Elasticsearch and Spark
Elasticsearch and SparkElasticsearch and Spark
Elasticsearch and Spark
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 

CoreML NLP workflow for Android & iOS

  • 1. CoreML for NLP An Automated Tensorflow (Keras) workflow for Android and CoreML for NLP text parsing
  • 2. Topics to be covered • Background to Natural Language Processing • Word Embeddings • Recurrent Neural Networks • Keras/Tensorflow CoreML • Automated build workflow for Android and iOS using Python templating
  • 3. What is Natural Language Processing? • NLP is the process of having machines understand language • iOS 11 has built in examples such as NSLinguisticTagger for tokenisation and lemmatisation • Intent Classification & Slot Tagging is a sub-field of NLP. Used as a technique for Siri, Alexa, and Google Assistant Play me a jazz song from Louis Armstrong from 1967 { "intent": "play_music", "slots": [ 0: { "genre": "jazz", "artist": "Louis Armstrong", "year": "1967" } ] } Tokenisation: Lemmatisation: “The cat sat on the mat” = [“The”, “cat”, “sat”, “on”, “the”, “mat”] “Running” = “run”, “Swam” = “Swim” Intent Classification & Slot Tagging:
  • 4. Pros and cons of on device NLP (and most ML/AI) Pros: Cons: Privacy Flexibility Cost Memory Availability Compute Speed Difficulty
  • 5. Data Vectorisation • In machine learning, data representation is very important. • 🗑 ! fn(x) ! 🗑 • We like our data to be represented as vectors. • Images are vectors (JPEG) and so are sounds (WAV) • How do we vectorise words for NLP? 255 45 199 12 23 129 34 5 49 56 94 254 87 142237175 123243192188 255 45 199 12 23 129 34 5 49 56 94 254 87 142237175 123243192188 255 45 199 12 23 129 34 5 49 56 94 254 87 142237175 123243192188 R G B Colour images can be represented as a Rank 3 Tensor Imagine this construct = [[[xr],[yr]], [[xg,][yg]], [[xb],[yb]]]
  • 6. Word Embeddings • One-hot encoding can be done but is sparse and does not capture semantics. • Word Embeddings are a solution to creating a dense vector representation of words. • What makes for a good word embedding? • What is semantic representation? • How do we make a word embedding? • Word2Vec vs FastText vs GloVe Hello 1 0 0 0 0 World Taylor Swift 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 Nope 0 0 0 0 1 One Hot Encoding http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/ Good Word2Vec Tutorial Semantic Representation
  • 7. Artificial Neural Networks • Artificial Neural Networks are the basic building blocks for our Deep Learning NLP solution. • Word Embedding systems like FastText, GloVe and Word2Vec are built using ANNs. • Multiple ANNs = Deep Learning. • TLDR; Multiple linear equations with a non-linearity applied. Artificial Neuron Multilayered Perceptron (Dense Networks) +b
  • 8. Recurrent Neural Networks • Deep Learning Models are made up lots of small artificial neural networks wired up in a particular way. • Convolutional Neural Networks are very popular for image recognition and can even do sequence modelling. • Recurrent Neural Networks are currently the best way to model sequences due to it having “memory” • Downsides of Recurrent Neural Networks is the “vanishing gradient” problem. • Long-short-term Memory (LSTM) and Gated Recurrent Units (GRU) help reduce “vanishing gradient” by having “forget gates” http://colah.github.io/posts/2015-08-Understanding-LSTMs/ Useful resource about LSTMs
  • 9. Automated CoreML Training & Deployment Architecture React WebApp FastText Word Embedding Server Tensorflow Training Server GCloud Bucket React WebApp iOS Build Server Android Build Server
  • 10. FastText Word Embeddings • FastText Word Embeddings were chosen to chosen as token vectorisation. • Pre-trained Gigaword corpus (6B tokens) capture high-quality semantic representation. • Takes up 8GB of RAM. • Solution is to search for 1000 closest words based on L2 (euclidean distance) to original word corpus. • Server generates SQLite DB with token and 128-float vectors of tokens “Hello” 0.87 0.34 0.20 0.09 0.34 0.22 Pre-Trained FastText Model 300-vector Token Vectors SQLite DB
  • 11. Keras on Tensorflow • Keras is a high-level abstraction developed for Deep Learning that runs on multiple backends (Tensorflow/Theano/CNTK/MxNet) • Keras is CoreML compatible. • Keras can also be converted to run on Tensorflow. • Android has good Tensorflow support. model = Sequential([ LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), TimeDistributed(Dense(64)), Activation('relu'), TimeDistributed(Dense(32)), Activation('relu'), TimeDistributed(Dense(num_labels)), Activation('softmax', name="output") ])
  • 12. CoreML • CoreML was built to reflect Numpy which is a common numerical analysis and matrix manipulation library for Python. • MLMultiArray is the most important class in CoreML. • If you can manipulate MLMultiArray, then you can make CoreML do anything! • Converting CoreML is as simple as calling coremltools.converters.keras.convert(model) • CoreML models are statically compiled and cannot be modified at runtime.
  • 13. Extending The Model model = Sequential([ LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), LSTM(128, batch_size=1, input_shape=(None, 300), dropout=0.25, return_sequences=True), TimeDistributed(Dense(64)), Activation('relu'), TimeDistributed(Dense(32)), Activation('relu'), TimeDistributed(Dense(num_labels)), Activation('softmax', name="output") ]) This means that the length of the input sequence is computed dynamically at training time. The output vector is squashed from 32 to n-number of labels. Vector order is important! Extending the model in Python is easy, but not so straightforward for CoreML
  • 14. CoreML models are statically compiled and cannot be modified at runtime. If you can manipulate MLMultiArray, then you can make CoreML do anything! 🤔
  • 15. Jinja Templating • After the training phase of the NLP model, we generate a config.json • JSON describes the output models, and CoreML and Android model files and the required parameters { "models": [ { "type": "IntentClassifier", "name": "UtteranceIntent", "intents": ["Make", "Weather", "News", "Recommend", "Help"], "android": { "filename": "intent_classifier.pb", "inputOperationName": "lstm_8_input", "outputOperationName": "activation_18/Softmax" }, "ios": { "filename": "CCLabsIntentClassifierModel.mlmodel", "inputOperationName": "input1", "outputOperationName": "output1" }, "inputShape": "300" }, { "type": "SlotTagger", "name": "Weather", "slots": ["None", "Weather", "Time", "Location"], "android": { "filename": "weather_slot_tagger.pb", "inputOperationName": "lstm_1_input", "outputOperationName": "output/truediv" }, "ios" : { "inputOperationName": "input1", "outputOperationName": "output1" }, "inputShape": "300" },...] }
  • 16. Jinja Templating Cont’d { "models": [ { "type": "SlotTagger", "name": "Weather", "slots": ["None", "Weather", "Time", "Location"], "android": { "filename": "weather_slot_tagger.pb", "inputOperationName": "lstm_1_input", "outputOperationName": "output/truediv" }, "ios" : { "inputOperationName": "input1", "outputOperationName": "output1" }, "inputShape": "300" },...] } // Flatten vector NSMutableArray *flattenedVector = [[NSMutableArray alloc] init]; for (NSArray<NSNumber*>* element in vector.data) { [flattenedVector addObjectsFromArray:element]; } // Convert vector to multiarray NSNumber* vectorCount = [NSNumber numberWithUnsignedInteger: vector.tokens.count]; NSNumber* utteranceCount = [NSNumber numberWithUnsignedInteger: 1]; NSNumber* valueCount = [NSNumber numberWithUnsignedInteger: {{ input_shape }}]; MLMultiArray *multiArray = [[MLMultiArray alloc] initWithShape:@[vectorCount, utteranceCount, valueCount] dataType:MLMultiArrayDataTypeFloat32 error: &predictionError];
  • 17. Assembling Framework • Once the Jinja templates are applied, we save the .m/.h files we run xcodegen • xcodegen generates Xcode project files based on a folder structure. It is required in order to re-build the project once the template process is complete. • Using xcodegen we can inject the SQLite database and the CoreML files with token vectors for complete offline operation xcodegen xcodebuild