Open In Colab View Notebook on GitHub

Movie Review Sentiment Classification with DISTILL-BERT [scikit-learn + torch preprocessing]

Giskard is an open-source framework for testing all ML models, from LLMs to tabular models. Don’t hesitate to give the project a star on GitHub ⭐️ if you find it useful!

In this notebook, you’ll learn how to create comprehensive test suites for your model in a few lines of code, thanks to Giskard’s open-source Python library.

Use-case:

Outline:

  • Detect vulnerabilities automatically with Giskard’s scan

  • Automatically generate & curate a comprehensive test suite to test your model beyond accuracy-related metrics

Install dependencies

Make sure to install the giskard

[ ]:
%pip install giskard --upgrade

Import libraries

[1]:
from pathlib import Path
from urllib.request import urlretrieve

import numpy as np
import pandas as pd
import torch
import transformers as ppb
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

from giskard import Model, Dataset, scan, testing

Define constants

[2]:
# Constants.
TARGET_COLUMN = "label"
TEXT_COLUMN = "text"

PRETRAINED_WEIGHTS_NAME = "distilbert-base-uncased"

RANDOM_STATE = 0

# Paths.
DATA_URL = "ftp://sys.giskard.ai/pub/unit_test_resources/movie_review_sentiment_classification_dataset/train.jsonl"
DATA_PATH = Path.home() / ".giskard" / "movie_review_sentiment_classification_dataset" / "train.jsonl"

Dataset preparation

Load data

[ ]:
def fetch_from_ftp(url: str, file: Path) -> None:
    if not file.parent.exists():
        file.parent.mkdir(parents=True, exist_ok=True)

    if not file.exists():
        print(f"Downloading data from {url}")
        urlretrieve(url, file)

    print(f"Data was loaded!")


def load_data(**kwargs) -> pd.DataFrame:
    """Load data."""
    fetch_from_ftp(DATA_URL, DATA_PATH)

    df = pd.read_json(DATA_PATH, lines=True, **kwargs)
    df = df.drop(columns="label_text")

    return df


reviews_df = load_data(nrows=2000)

Train-Test split

[5]:
train_df, test_df = train_test_split(reviews_df, random_state=RANDOM_STATE)

Wrap dataset with Giskard

To prepare for the vulnerability scan, make sure to wrap your dataset using Giskard’s Dataset class. More details here.

[ ]:
giskard_dataset = Dataset(
    df=test_df,
    # A pandas.DataFrame that contains the raw data (before all the pre-processing steps) and the actual ground truth variable (target).
    target=TARGET_COLUMN,  # Ground truth variable.
    name="Movie reviews dataset"  # Optional.
)

Model building

Define preprocessing steps

[ ]:
embedder = ppb.DistilBertModel.from_pretrained(PRETRAINED_WEIGHTS_NAME)
tokenizer = ppb.DistilBertTokenizer.from_pretrained(PRETRAINED_WEIGHTS_NAME)


def get_max_sequence_length(corpus: pd.Series) -> int:
    """Define a length of the longest tokenized document."""
    max_length = max(len(tokenizer.encode(document, add_special_tokens=True)) for document in corpus)
    return max_length


max_sequence_length = get_max_sequence_length(reviews_df[TEXT_COLUMN])


def tokenize_documents(corpus: pd.Series) -> torch.Tensor:
    """Tokenization step."""
    tokens_matrix = corpus.apply(lambda document: tokenizer.encode(document, add_special_tokens=True)).values
    tokens_matrix = torch.tensor(
        [tokens_row + [0] * (max_sequence_length - len(tokens_row)) for tokens_row in tokens_matrix])
    return tokens_matrix


def get_documents_embeddings(tokens_matrix: torch.Tensor) -> np.ndarray:
    """Calculate sentence embeddings using distill-BERT model."""
    attention_mask = torch.where(tokens_matrix != 0, 1, 0)

    embedder.eval()
    with torch.no_grad():
        tokens_representations = embedder(tokens_matrix, attention_mask=attention_mask)

    # Take just 'cls token' embeddings, which represent whole sentence embedding.
    documents_embeddings = tokens_representations[0][:, 0, :].numpy()
    return documents_embeddings


def preprocess_text(df: pd.DataFrame) -> np.ndarray:
    """Preprocessing function to be also used in 'giskard.Model'."""
    return get_documents_embeddings(tokenize_documents(df[TEXT_COLUMN]))


X_train, Y_train = preprocess_text(train_df), train_df.label
X_test, Y_test = preprocess_text(test_df), test_df.label

Build estimator

[ ]:
classifier = LogisticRegression()
classifier.fit(X_train, Y_train)

# Validate model.
train_score = classifier.score(X_train, Y_train)
print(f"Train accuracy: {train_score: .2f}")

test_score = classifier.score(X_test, Y_test)
print(f"Test accuracy: {test_score: .2f}")

Wrap model with Giskard

To prepare for the vulnerability scan, make sure to wrap your model using Giskard’s Model class. You can choose to either wrap the prediction function (preferred option) or the model object. More details here.

[ ]:
def prediction_function(df: pd.DataFrame) -> np.ndarray:
    x = preprocess_text(df)
    return classifier.predict_proba(x)


giskard_model = Model(
    model=prediction_function,
    # A prediction function that encapsulates all the data pre-processing steps and that could be executed with the dataset used by the scan.
    model_type="classification",  # Either regression, classification or text_generation.
    name="Movie reviews sentiment classifier",  # Optional.
    classification_labels=classifier.classes_.tolist(),
    # Their order MUST be identical to the prediction_function's output order.
    feature_names=[TEXT_COLUMN],  # Default: all columns of your dataset.
    # classification_threshold=0.5  # Default: 0.5.
)

Y_test_pred_wrapped = giskard_model.predict(giskard_dataset).prediction
wrapped_test_score = accuracy_score(Y_test, Y_test_pred_wrapped)
print(f"Wrapped test accuracy: {wrapped_test_score: .2f}")

Detect vulnerabilities in your model

Scan your model for vulnerabilities with Giskard

Giskard’s scan allows you to detect vulnerabilities in your model automatically. These include performance biases, unrobustness, data leakage, stochasticity, underconfidence, ethical issues, and more. For detailed information about the scan feature, please refer to our scan documentation.

[ ]:
results = scan(giskard_model, giskard_dataset)
[11]:
display(results)

Generate comprehensive test suites automatically for your model

Generate test suites from the scan

The objects produced by the scan can be used as fixtures to generate a test suite that integrate all detected vulnerabilities. Test suites allow you to evaluate and validate your model’s performance, ensuring that it behaves as expected on a set of predefined test cases, and to identify any regressions or issues that might arise during development or updates.

[12]:
test_suite = results.generate_test_suite("My first test suite")
test_suite.run()
2024-05-29 13:52:50,519 pid:67935 MainThread giskard.datasets.base INFO     Casting dataframe columns from {'text': 'object'} to {'text': 'object'}
2024-05-29 13:52:50,520 pid:67935 MainThread giskard.utils.logging_utils INFO     Predicted dataset with shape (62, 2) executed in 0:00:00.007627
Executed 'Precision on data slice “`text` contains "movie"”' with arguments {'model': <giskard.models.function.PredictionFunctionModel object at 0x303676c50>, 'dataset': <giskard.datasets.base.Dataset object at 0x303675ba0>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x32b836530>, 'threshold': 0.8047520661157024}:
               Test failed
               Metric: 0.64


2024-05-29 13:52:50,530 pid:67935 MainThread giskard.core.suite INFO     Executed test suite 'My first test suite'
2024-05-29 13:52:50,536 pid:67935 MainThread giskard.core.suite INFO     result: failed
2024-05-29 13:52:50,543 pid:67935 MainThread giskard.core.suite INFO     Precision on data slice “`text` contains "movie"” ({'model': <giskard.models.function.PredictionFunctionModel object at 0x303676c50>, 'dataset': <giskard.datasets.base.Dataset object at 0x303675ba0>, 'slicing_function': <giskard.slicing.slice.QueryBasedSliceFunction object at 0x32b836530>, 'threshold': 0.8047520661157024}): {failed, metric=0.6363636363636364}
[12]:
close Test suite failed.
Test Precision on data slice “`text` contains "movie"”
Measured Metric = 0.63636 close Failed
model Movie reviews sentiment classifier
dataset Movie reviews dataset
slicing_function `text` contains "movie"
threshold 0.8047520661157024

Customize your suite by loading objects from the Giskard catalog

The Giskard open source catalog will enable to load:

  • Tests such as metamorphic, performance, prediction & data drift, statistical tests, etc

  • Slicing functions such as detectors of toxicity, hate, emotion, etc

  • Transformation functions such as generators of typos, paraphrase, style tune, etc

To create custom tests, refer to this page.

For demo purposes, we will load a simple unit test (test_f1) that checks if the test F1 score is above the given threshold. For more examples of tests and functions, refer to the Giskard catalog.

[ ]:
test_suite.add_test(testing.test_f1(model=giskard_model, dataset=giskard_dataset, threshold=0.7)).run()