To get my hands on MLflow, I started off by reading the excellent tutorial at MLflow.org to acquaint myself with the steps of packaging and serving a ML model to a REST API endpoint. The linear regression model and steps described in the tutorial works right out of the box flawlessly.Kudos to the documentation!
To gain a little more exposure, I then tried to deploy the deep learning model described in this databricks blog. Unlike the linear regression model, the DL model uses the keras library and the API input is tensor rather than dataframe.
In this blog post, I will share the steps needed and the modification necessary to the original DL code in order to deploy the model.
For reference, my setup is:
Ubuntu 20.04
Python 3.8
Anaconda3
Keras 2.6
The original DL code which uses Keras 2.7 was throwing errors so I had to downgrade to version 2.6 and consequently modified all instances of “keras” to “tf.keras” in the code. The complete modified code is:
import keras
from keras.layers import Dense, Flatten, Dropout
import numpy as np
import mlflow
import mlflow.keras
from mlflow.models.signature import infer_signature
The steps to deploy the model is described in the MLflow tutorial so I won’t repeat it here. Basically, what is needed to serve the model is the run ID of the model.The run ID is generated each time you run the code.You can find the run ID by launching the MLflow UI:
mlflow ui --host 0.0.0.0
goto http://<IP addr>:5000
Alternatively, you can find it in the mlruns subdirectory where your script was executed. With the run ID, you can deploy the model to an API endpoint by:
If everything goes well, the API should return a list of 10 probabilities which are the predictions for each digit.(The DL model predicts handwritten digits from the MNIST dataset.)
This is my quick stab at setting up Kafka on Ubuntu.As a newbie to Kafka, I started off by following the steps from this excellent but slightly outdated blog. I thought I'll freshen up the steps from that blog and also give a brief introduction to the confluent-kafka library for Python.For basic background info on Kafka, I would still recommend checking out that blog.
We'll also create a Python script to consume messages from the "sample" Kafka topic and displaying them to screen.
from confluent_kafka import Consumer, KafkaError
conf = {
'bootstrap.servers': '127.0.0.1:9092',
'group.id': 'my-group',
'auto.offset.reset': 'earliest'
}
consumer = Consumer(conf)
consumer.subscribe(['sample'])
msg_count = 0
while True:
msg = consumer.poll(1.0)
if msg is None:
continue
if msg.error():
if msg.error().code() == KafkaError._PARTITION_EOF:
continue
else:
print(msg.error())
break
else:
msg_count += 1
print(msg_count, msg.key(), msg.value())
If everything goes well, you should see:
[2021-11-02 17:12:44,424] INFO [GroupCoordinator 0]: Dynamic member with unknown member id joins group my-group in Empty state. Created a new member id rdkafka-3df1f0f2-93c2-477b-9af1-bff1260befe5 and request the member to rejoin with this id. (kafka.coordinator.group.GroupCoordinator)
[2021-11-02 17:12:44,429] INFO [GroupCoordinator 0]: Preparing to rebalance group my-group in state PreparingRebalance with old generation 29 (__consumer_offsets-12) (reason: Adding new member rdkafka-3df1f0f2-93c2-477b-9af1-bff1260befe5 with group instance id None) (kafka.coordinator.group.GroupCoordinator)
[2021-11-02 17:12:44,430] INFO [GroupCoordinator 0]: Stabilized group my-group generation 30 (__consumer_offsets-12) with 1 members (kafka.coordinator.group.GroupCoordinator)
[2021-11-02 17:12:44,433] INFO [GroupCoordinator 0]: Assignment received from leader rdkafka-3df1f0f2-93c2-477b-9af1-bff1260befe5 for group my-group for generation 30. The group has 1 members, 0 of which are static. (kafka.coordinator.group.GroupCoordinator)
1 b'dish' b'spaghetti#1'
2 b'dish' b'spaghetti#2'
3 b'dish' b'spaghetti#3'
...
That's it, but of course, you could do a lot more with Kafka such as different ways of encoding and decoding the messages.For more info, check out the examples directory:
While working on a classification task recently, I started out with the well-known Naive Bayes classifier but soon realized that I wasn't getting the classification performance I needed. At the same time, I was keenly aware that many of the feature variables carry a causal relationship between them that the classifier wasn't exploiting.
I needed another classifier with the simplicity and run-time performance of Naive Bayes so I started looking into Tree-augmented Naive Bayes (TAN). My search for a Python library supporting TAN eventually led me to pgmpy.
Pgmpy is a Python Library for learning (Structure and Parameter) and inference (Statistical and Causal) in Bayesian Networks.
Pgmpy is versatile with capabilities beyond what my task needed. It also has some structure learning algorithms but not TAN so I thought to contribute one to the library. In this article, I'll give a tutorial on how to use TAN in pgmpy.
For demonstration purpose, I'll generate sample data from a handcrafted Bayesian Network (BN) model. With the generated data, I'll train a Naive Bayes classifier and a TAN classifier, and compare their prediction performance. Our BN graph is illustrated below. Node C is our class variable while node R, S, T, U and V are the feature variables. All variables are discrete.
Generate Sample Data
First, import all the packages we will be needing:
import pandas as pd import numpy as np
from pgmpy.models.BayesianModel import BayesianModel from pgmpy.factors.discrete import TabularCPD from pgmpy.sampling import BayesianModelSampling from pgmpy.estimators import TreeSearch, BayesianEstimator
from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn.naive_bayes import MultinomialNB
To encode our BN graph, we instantiate BayesianModel with the directed edges of the graph:
# construct naive bayes graph and add interaction between the features model = BayesianModel([('C', 'R'), ('C', 'S'), ('C', 'T'), ('C', 'U'), ('C', 'V'),
('R', 'S'), ('R', 'T'), ('R', 'U'), ('R', 'V')])
Next, we parameterize the graph by defining the conditional probabilities for each node. For more information on parameterization, check out the pgmpy tutorials.
# add conditional probability distribution to edges
We'll be using the data to train Naive Bayes and TAN classifier to see which one performs better. Before that, we split the data into training and test set.
# split data into training and test set
cols_features = df_data.columns.tolist()
cols_features.remove('C')
X = df_data[cols_features].values
y = df_data[['C']].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=21, stratify=y)
Now, we are ready to train our Naive Bayes model using the training data and then measure classification performance with the test set. I'll use the classifier from sklearn but pgmpy also has a Naive Bayes classifier as well shall you choose.
# train naive bayes classifier and predict
model_nb = MultinomialNB().fit(X_train, y_train)
y_pred = model_nb.predict(X_test)
print(classification_report(y_test, y_pred))
In short, Naive Bayes precision and recall are both 72%.
Predicting with TAN
Next up is TAN. First, let's learn the graph structure from the training data. To capture the interaction between the feature variables, TAN casts a tree structure over them. So, you need to pass in the "root_node" parameter. To be fair, we pretend that we don't know what the real graph looks like, so we pick a random node as the root node:
# learn the TAN graph structure from data
est = TreeSearch(df_train, root_node='U')
dag = est.estimate(estimator_type='tan', class_node='C')
Before we can make predictions, we also need to parameterize the model using the training data.
# construct Bayesian network by parameterizing the graph structure
In the previous post, I’ve used logistic regression and tf-idf from Python’s sklearn to predict ethnicity based on a person forename, middle and surname.In this post, I’ll continue to use the same Wikipedia dataset but this time, the task is to predict if a name is forename or surname.The motivation is to use such classifier for input form validation and ETL.
To start with, let’s summarize the number of records available:
Ethnicity
Forenames
Surnames
Total
Nordic
1,691
3,230
4,921
EastAsian
2,331
2,784
5,115
Germanic
1,975
3,329
5,304
Africans
2,969
3,170
6,139
Muslim
3,539
4,557
8,096
Japanese
3,864
4,236
8,100
IndianSubContinent
5,133
3,839
8,972
EastEuropean
2,888
6,627
9,515
Hispanic
3,662
6,254
9,916
Jewish
3,676
6,304
9,980
Italian
4,315
8,781
13,096
French
3,802
9,630
13,432
British
6,933
16,062
22,995
We observe that:
Some datasets like Nordic have much fewer records than others
The dataset for French has 2.5 times more surnames than forenames
Our intuition tells us that a general classifier to predict forename and surname for all ethnicities is probably a bad idea as it ignores the subtle differences in name spelling among ethnicities.The confusion matrix from the previous post on ethnicity prediction speaks to this point.Therefore, I’ll build a separate forename-surname classifier for each ethnicity.
I will use the same logistic regression approach from the previous post which feature engineers bigrams, trigrams and quadgrams from names.To address data imbalance in observation #2, I’ll upsample the data of the minority class using the handy Imblearn package.To address the curse of dimensionality in observation #1, I’ll employ the following approaches to avoid overfitting the training data:
Penalize large coefficients in logistic regression
Reduce the number of dimensions using SVD
Without further ado, here is the classification performance on a 20% holdout data using upsampling and L2 penalization:
The performance is about the same with SVD dimensionality reduction to capture 80 to 90% of variance in the data. Generally, that reduces the number of features by 80%.
The results are sorted by the size of dataset from smallest to largest.The result for British is missing due to my old laptop running out of memory during SVD matrix factorization.
Interestingly, focusing on the F1 score, we see that poor performance is not necessarily correlated with small data.For example, Nordic being the smallest dataset yields rather high F1 score.The ethnicities with F1 score below 70% are Africans, IndianSubContinent, Japanese, EastAsian and Muslim.
Besides insufficient data being the possible cause of poor classification performance, it's conceivable that some of these misclassified names can be tricky even to humans to decide if it's a forename or surname. For example, "Davis" can be either a forename or surname.
In the meantime, a classifier with 60% accuracy can still be useful. For example, for input form validation where forename and surname are entered and the goal is to validate if they are entered into the right field, we can use the classifier to predict whether a swap has taken place accidentally by the user.
A swap occurs if the classifier predicts that the input forename is a surname and the input surname is a forename. For a classifier with 60% accuracy, the chance of erroneously predicting a swap and alerting the user is only 100 x (0.4 x 0.4) = 16%.