Detailed instructions:

Importer — Swift script to import from Watson and others

This sub-project contains Swift code (to be executed on Linux or macOS environments) to import a file containing the dataset used to train the NLC model.

The idea is to provide different importers for different file formats in order to import datasets from existing NLU/NLC/Conversation platforms such as IBM Watson, AWS Alexa/Lex, Google Dialogflow, etc.

The first importer implemented is for the IBM Watson Conversation service. Watson uses a JSON “workspace” file containing intents with several sample utterances, entities (slots), also with sample utterances, as well as a tree for complex dialog management of long-running conversation. This project is solely about NLC, and it only import intents and sample utterances from this file.

Usage example:

Watson import ../../SampleIntents/Watson/WatsonConversationCarWorkspace.json dataset.json

Generated dataset.json example:

{

"intents" : [

{

"intent" : "hello",

"utterances" : [

"hello",

"good morning"

]

}

]

}

Sample Datasets

This folder contains sample datasets with intents and sample utterances from different sources:

Watson/WatsonConversationCarWorkspace.json in the Watson subfolder is an export of the standard sample workspace for demoing IBM Watson Conversation service on the IBM Cloud.

in the Watson subfolder is an export of the standard sample workspace for demoing IBM Watson Conversation service on the IBM Cloud. PharmacyDataset.json (no need to import)

Embedder — Word one-hot encoding implemented in Swift using NSLinguisticTagger

This sub-project contains Swift code (to be executed on a macOS or iOS environments) to import a JSON file containing the dataset to be used for training the NLC model.

This importer uses Apple Foundation NSLinguisticTagger APIs to analyze and tokenize the text in the sample utterances, creating a word embedder. In particular, it outputs a one-hot encoding for stem words, a corpus of documents, and a class of entities to train the data and prepare the TensorFlow model, as well as for inferencing the model with Core ML.

Usage example:

Embedder import ../SampleDatasets/PharmacyDataset.json

This command produces the following files in the current folder: bagOfWords.json , lemmatizedDataset.json and intents.json

ModelNotebook — Instructions to create TensorFlow model

This is a Python Jupyter Notebook using the Keras API and TensorFlow as a backend to create a simple, fully connected Deep Network Classifier.

If you’re new to the Keras/TensorFlow/Jupyter world, here are step-by-step instructions to create the ML model using Keras/TensorFlow and export it on Core ML using CoreMLConversionTool

Download and install Anaconda Python:

2. Create the Keras, TensorFlow, Python, and Core ML environment:

conda env create

This environment is created based on the environment.yml file for installing Python 2.7, TensorFlow 1.1, Keras 2.0.4, CoreMLTools 0.6.3, Pandas and other useful Python packages:

name: SwiftNLC

channels:

- !!python/unicode

'defaults'

dependencies:

- python=2.7

- pip==9.0.1

- numpy==1.12.0

- jupyter==1.0

- matplotlib==2.0.0

- scikit-learn==0.18.1

- scipy==0.19.0

- pandas==0.19.2

- pillow==4.0.0

- seaborn==0.7.1

- h5py==2.7.0

- pip:

- tensorflow==1.6.0

- keras==2.1.5

- coremltools==0.8

- nltk==3.2.5

NB NLTK is only needed for the createModelWithNLTKEmbedding Notebook used on initial testing.

The final createModelWithNSLinguisticTaggerEmbedding does not use NLTK, as the word embedding is implemented in Swift on the Embedder module using the NSLinguisticTagger API.

3. Activate the environment (Mac/Linux):

source activate SwiftNLC

4. Check that your prompt changed to:

(SwiftNLC) $

5. Launch Jupyter Notebook:

jupyter notebook

6. Open your browser to:

To create a basic model with Keras/TensorFlow and export it with CoreMLTools, open createModelWithNSLinguisticTaggerEmbedding in your Jupyter browsing session and execute any cells in order to create, save, and export the Keras Model using Core ML exporting tools.

The basic Core ML model will be saved in the current folder.

Keras / TensorFlow model consideration

As discussed above, the Swift Embedder project is generally responsible for massaging the dataset, and responsible in particular for creating the one-hot word encoding for stem words from sample utterances and for creating a corpus of stemmed, encoded documents to be used for training the model.

For this reason, the Python code to create the model is simplified with very little pre-processing, and it quickly starts to create the TensorFlow model used for learning.

This model is a simple, fully connected network that receives as input an array of embedded 0's and 1's for each sample utterance. It uses ‘ReLU’ as an activator for the first and an internal hidden layer, and then at the end, as this is a multi-class classifier, it uses ‘softmax’ to emphasize the winner prediction.

from keras.models import Sequential

from keras.layers import Dense

from keras.optimizers import Adam model = Sequential()

model.add(Dense(50, input_dim=len(train_x[0]), activation='relu'))

model.add(Dense(8, activation='relu'))

model.add(Dense(len(train_y[0]), activation='softmax'))

model.summary()

The training of the model is even simpler, as per definition the intents/utterances dataset used per input is very limited, and there’s no space at all for creating validation and testing sets.

It basically trains the entire dataset with a sufficient number of epochs to obtain maximum possible accuracy.

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(np.array(train_x), np.array(train_y), epochs=400)

Once the Keras/TensorFlow model is trained, this is easily exported to Core ML using the Apple CoreMLTools Python library: