The artificial intelligence of Natural Language Processing (NLP) is concerned with how computers and people communicate in everyday language. In light of the deployment of NLP models in production systems, we need to streamline the rising use of NLP applications leading to MLOps (Machine Learning Operations) for NLP being helpful. Automating the creation, training, testing, and deployment of NLP models in production systems is the goal of MLOps for NLP.
This article will examine the MLOps process for NLP models using Sentiment Analysis as a use case and some of the most recent trends and developments in this field.
Learning Objectives
This article was published as a part of the Data Science Blogathon.
In this section, we will look at how to implement the key steps involved in MLOps for NLP with the help of Sentimental Analysis.
The first step in building an NLP model is data preparation. In this use case, I use the IMDb movie review dataset of 50,000 movie reviews labeled as positive or negative. We will split the dataset into training and testing sets using an 80:20 ratio.
You can find this dataset on kaggle.
import pandas as pd from sklearn.model_selection import train_test_split
# loading dataset
df = pd.read_csv('imdb_reviews.csv')
# splitting dataset into training and testing sets
train_reviews,test_reviews,train_labels,test_labels=train_test_split(df['review'],
df['sentiment'],
test_size=0.2,
random_state=42)
The next step is pre-processing the data. This involves transforming the raw text data into a format that an NLP model can easily process. In this use case, we will use the TF-IDF (Term Frequency-Inverse Document Frequency) technique to transform the text data into numerical vectors.
from sklearn.feature_extraction.text import TfidfVectorizer
# initializing vectorizer
vectorizer = TfidfVectorizer()
# preprocessing training data
X_train = vectorizer.fit_transform(train_reviews)
# preprocessing testing data
X_test = vectorizer.transform(test_reviews)
# converting labels to numerical values
y_train = train_labels.replace({'positive': 1, 'negative': 0})
y_test = test_labels.replace({'positive': 1, 'negative': 0})
The next step is model development. In this use case, we will use a simple DL model consisting of an Embedding layer, a GlobalMaxPooling1D layer, and a Dense layer with a sigmoid activation function.
from keras.models import Sequential
from keras.layers import Embedding, GlobalMaxPooling1D, Dense
# defining model architecture
model = Sequential()
model.add(Embedding(input_dim=X_train.shape[1], output_dim=32))
model.add(GlobalMaxPooling1D())
model.add(Dense(units=1, activation='sigmoid'))
# compiling model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
The next step is model training and evaluation. We will train the model using the training data and evaluate its performance using the testing data.
Using the fit() method of the Keras model object to train the model. We will also use the ModelCheckpoint callback to save the best model weights based on the validation loss. This ensures that we always have access to the best-performing model during training.
from keras.callbacks import ModelCheckpoint
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# defining callback to save best model weights
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
# training model
history = model.fit(X_train, y_train,
epochs=10,
batch_size=32,
validation_data=(X_test, y_test), callbacks=[checkpoint])
Once the model is trained, we can evaluate its performance using various metrics such as accuracy and precision.
# evaluating model performance
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5).astype(int)
acc = accuracy_score(y_test, y_pred)
prec = precision_score(y_test, y_pred)
rec = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print('Accuracy:', acc)
print('Precision:', prec)
The final step is model deployment. We can deploy a model in a production environment for real-time inference by training and evaluating it. In this use case, I will deploy the model using Flask, a web application framework for Python.
from flask import Flask, request, jsonify
import numpy as np
import tensorflow as tf
from keras.models import load_model
# initializing Flask app
app = Flask(__name__)
# loading trained model
model = load_model('model.h5')
# initializing vectorizer
vectorizer = TfidfVectorizer()
@app.route('/predict', methods=['POST'])
def predict():
# get input text from request
input_text = request.json['text']
# preprocess input text
input_vector = vectorizer.transform([input_text])
# make prediction
prediction = model.predict(input_vector)[0][0]
if prediction > 0.5:
sentiment = 'positive'
else:
sentiment = 'negative'
# returning prediction as JSON response
response = {
'prediction': sentiment
}
return jsonify(response)
We define an endpoint that accepts a JSON payload containing the input text. The input text is pre-processed using the same vectorizer that was used during training, and the model makes a prediction. The predicted sentiment is returned as a JSON response.
Here are some challenges and considerations involved in MLOps for Natural Language Processing:
Some of the latest trends and advancements in MLOps for NLP are:
Creating new methods and strategies on a regular basis will help in a rapidly developing discipline of MLOps for NLP. For continued competitiveness and the creation of high-performing NLP models, staying current with trends and developments is essential.
As shown above, MLOps for NLP is a crucial technique for programmers to create NLP apps. Automating the entire process of creating, training, testing, and deploying NLP models is possible. This enables developers to create NLP models that are more precise, dependable, and trustworthy.
The key takeaways from this article on MLOps for NLP are:
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.