<jupyter_start><jupyter_text>✂️ Snorkel Intro Tutorial: _Data Slicing_In real-world applications, some model outcomes are often more important than others — e.g. vulnerable cyclist detections in an autonomous driving task, or, in our running **spam** application, potentially malicious link redirects to external websites.Traditional machine learning systems optimize for overall quality, which may be too coarse-grained.Models that achieve high overall performance might produce unacceptable failure rates on critical slices of the data — data subsets that might correspond to vulnerable cyclist detection in an autonomous driving task, or in our running spam detection application, external links to potentially malicious websites.In this tutorial, we:1. **Introduce _Slicing Functions (SFs)_** as a programming interface1. **Monitor** application-critical data subsets2. **Improve model performance** on slices First, we'll set up our notebook for reproducibility and proper logging.<jupyter_code>import logging
import os
import numpy as np
import random
import torch

# For reproducibility
os.environ["PYTHONHASHSEED"] = "0"
SEED = 123
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)

# Make sure we're running from the spam/ directory
if os.path.basename(os.getcwd()) == "snorkel-tutorials":
    os.chdir("spam")

# To visualize logs
logger = logging.getLogger()
logger.setLevel(logging.WARNING)<jupyter_output><empty_output><jupyter_text>If you want to display all comment text untruncated, change `DISPLAY_ALL_TEXT` to `True` below.<jupyter_code>import pandas as pd


DISPLAY_ALL_TEXT = False

pd.set_option("display.max_colwidth", 0 if DISPLAY_ALL_TEXT else 50)<jupyter_output><empty_output><jupyter_text>_Note:_ this tutorial differs from the labeling tutorial in that we use ground truth labels in the train split for demo purposes.SFs are intended to be used *after the training set has already been labeled* by LFs (or by hand) in the training data pipeline.<jupyter_code>from utils import load_spam_dataset

df_train, df_test = load_spam_dataset(load_train_labels=True)<jupyter_output><empty_output><jupyter_text>1. Write slicing functionsWe leverage *slicing functions* (SFs), which output binary _masks_ indicating whether an data point is in the slice or not.Each slice represents some noisily-defined subset of the data (corresponding to an SF) that we'd like to programmatically monitor. In the following cells, we use the [`@slicing_function()`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/slicing/snorkel.slicing.slicing_function.htmlsnorkel.slicing.slicing_function) decorator to initialize an SF that identifies short commentsYou'll notice that the `short_comment` SF is a heuristic, like the other programmatic ops we've defined, and may not fully cover the slice of interest.That's okay — in last section, we'll show how a model can handle this in Snorkel.<jupyter_code>import re
from snorkel.slicing import slicing_function


@slicing_function()
def short_comment(x):
    """Ham comments are often short, such as 'cool video!'"""
    return len(x.text.split()) < 5


sfs = [short_comment]<jupyter_output><empty_output><jupyter_text>Visualize slices With a utility function, [`slice_dataframe`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/slicing/snorkel.slicing.slice_dataframe.htmlsnorkel.slicing.slice_dataframe), we can visualize data points belonging to this slice in a `pandas.DataFrame`.<jupyter_code>from snorkel.slicing import slice_dataframe

short_comment_df = slice_dataframe(df_test, short_comment)
short_comment_df[["text", "label"]].head()<jupyter_output><empty_output><jupyter_text>2. Monitor slice performance with [`Scorer.score_slices`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/analysis/snorkel.analysis.Scorer.htmlsnorkel.analysis.Scorer.score_slices) In this section, we'll demonstrate how we might monitor slice performance on the `short_comment` slice — this approach is compatible with _any modeling framework_.  Train a simple classifierFirst, we featurize the data — as you saw in the introductory Spam tutorial, we can extract simple bag-of-words features and store them as numpy arrays.<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer
from utils import df_to_features

vectorizer = CountVectorizer(ngram_range=(1, 1))
X_train, Y_train = df_to_features(vectorizer, df_train, "train")
X_test, Y_test = df_to_features(vectorizer, df_test, "test")<jupyter_output><empty_output><jupyter_text>We define a `LogisticRegression` model from `sklearn`.<jupyter_code>from sklearn.linear_model import LogisticRegression

sklearn_model = LogisticRegression(C=0.001, solver="liblinear")
sklearn_model.fit(X=X_train, y=Y_train)
from snorkel.utils import preds_to_probs

preds_test = sklearn_model.predict(X_test)
probs_test = preds_to_probs(preds_test, 2)
from sklearn.metrics import f1_score

print(f"Test set F1: {100 * f1_score(Y_test, preds_test):.1f}%")<jupyter_output>Test set F1: 92.5%<jupyter_text>Store slice metadata in `S` We apply our list of `sfs` to the data using an SF applier.For our data format, we leverage the [`PandasSFApplier`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/slicing/snorkel.slicing.PandasSFApplier.htmlsnorkel.slicing.PandasSFApplier).The output of the `applier` is an [`np.recarray`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.recarray.html) which stores vectors in named fields indicating whether each of $n$ data points belongs to the corresponding slice.<jupyter_code>from snorkel.slicing import PandasSFApplier

applier = PandasSFApplier(sfs)
S_test = applier.apply(df_test)<jupyter_output>0%|          | 0/250 [00:00<?, ?it/s]<jupyter_text>Now, we initialize a [`Scorer`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/analysis/snorkel.analysis.Scorer.htmlsnorkel.analysis.Scorer) using the desired `metrics`.<jupyter_code>from snorkel.analysis import Scorer

scorer = Scorer(metrics=["f1"])<jupyter_output><empty_output><jupyter_text>Using the [`score_slices`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/analysis/snorkel.analysis.Scorer.htmlsnorkel.analysis.Scorer.score_slices) method, we can see both `overall` and slice-specific performance.<jupyter_code>scorer.score_slices(
    S=S_test, golds=Y_test, preds=preds_test, probs=probs_test, as_dataframe=True
)<jupyter_output><empty_output><jupyter_text>Despite high overall performance, the `short_comment` slice performs poorly here!  Write additional slicing functions (SFs)Slices are dynamic — as monitoring needs grow or change with new data distributions or application needs, an ML pipeline might require dozens, or even hundreds, of slices.We'll take inspiration from the labeling tutorial to write additional slicing functions.We demonstrate how the same powerful preprocessors and utilities available for labeling functions can be leveraged for slicing functions.<jupyter_code>from snorkel.slicing import SlicingFunction, slicing_function
from snorkel.preprocess import preprocessor


# Keyword-based SFs
def keyword_lookup(x, keywords):
    return any(word in x.text.lower() for word in keywords)


def make_keyword_sf(keywords):
    return SlicingFunction(
        name=f"keyword_{keywords[0]}",
        f=keyword_lookup,
        resources=dict(keywords=keywords),
    )


keyword_please = make_keyword_sf(keywords=["please", "plz"])


# Regex-based SFs
@slicing_function()
def regex_check_out(x):
    return bool(re.search(r"check.*out", x.text, flags=re.I))


@slicing_function()
def short_link(x):
    """Returns whether text matches common pattern for shortened ".ly" links."""
    return bool(re.search(r"\w+\.ly", x.text))


# Leverage preprocessor in SF
from textblob import TextBlob


@preprocessor(memoize=True)
def textblob_sentiment(x):
    scores = TextBlob(x.text)
    x.polarity = scores.sentiment.polarity
    return x


@slicing_function(pre=[textblob_sentiment])
def textblob_polarity(x):
    return x.polarity > 0.9<jupyter_output><empty_output><jupyter_text>Again, we'd like to visualize data points in a particular slice. This time, we'll inspect the `textblob_polarity` slice.Most data points with high-polarity sentiments are strong opinions about the video — hence, they are usually relevant to the video, and the corresponding labels are $0$ (not spam).We might define a slice here for *product and marketing reasons*, it's important to make sure that we don't misclassify very positive comments from good users.<jupyter_code>polarity_df = slice_dataframe(df_test, textblob_polarity)
polarity_df[["text", "label"]].head()<jupyter_output><empty_output><jupyter_text>We can evaluate performance on _all SFs_ using the model-agnostic [`Scorer`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/analysis/snorkel.analysis.Scorer.htmlsnorkel-analysis-scorer).<jupyter_code>extra_sfs = [keyword_please, regex_check_out, short_link, textblob_polarity]

sfs = [short_comment] + extra_sfs
slice_names = [sf.name for sf in sfs]<jupyter_output><empty_output><jupyter_text>Let's see how the `sklearn` model we learned before performs on these new slices.<jupyter_code>applier = PandasSFApplier(sfs)
S_test = applier.apply(df_test)
scorer.score_slices(
    S=S_test, golds=Y_test, preds=preds_test, probs=probs_test, as_dataframe=True
)<jupyter_output><empty_output><jupyter_text>Looks like some do extremely well on our small test set, while others do decently.At the very least, we may want to monitor these to make sure that as we iterate to improve certain slices like `short_comment`, we don't hurt the performance of others.Next, we'll introduce a model that helps us to do this balancing act automatically.  3. Improve slice performanceIn the following section, we demonstrate a modeling approach that we call _Slice-based Learning,_ which improves performance by adding extra slice-specific representational capacity to whichever model we're using.Intuitively, we'd like to model to learn *representations that are better suited to handle data points in this slice*.In our approach, we model each slice as a separate "expert task" in the style of [multi-task learning](https://github.com/snorkel-team/snorkel-tutorials/blob/master/multitask/multitask_tutorial.ipynb); for further details of how slice-based learning works under the hood, check out the [code](https://github.com/snorkel-team/snorkel/blob/master/snorkel/slicing/utils.py) (with paper coming soon)!In other approaches, one might attempt to increase slice performance with techniques like _oversampling_ (i.e. with PyTorch's [`WeightedRandomSampler`](https://pytorch.org/docs/stable/data.htmltorch.utils.data.WeightedRandomSampler)), effectively shifting the training distribution towards certain populations.This might work with small number of slices, but with hundreds or thousands or production slices at scale, it could quickly become intractable to tune upsampling weights per slice.  Constructing a [`SliceAwareClassifier`](https://snorkel.readthedocs.io/en/v0.9.3/packages/_autosummary/slicing/snorkel.slicing.SliceAwareClassifier.html) To cope with scale, we will attempt to learn and combine many slice-specific representations with an attention mechanism.(Please see our [Section 3 of our technical report](https://arxiv.org/abs/1909.06349) for details on this approach). First we'll initialize a [`SliceAwareClassifier`](https://snorkel.readthedocs.io/en/v0.9.3/packages/_autosummary/slicing/snorkel.slicing.SliceAwareClassifier.html):* `base_architecture`: We define a simple Multi-Layer Perceptron (MLP) in Pytorch to serve as the primary representation architecture. We note that the `BinarySlicingClassifier` is **agnostic to the base architecture** — you might leverage a Transformer model for text, or a ResNet for images.* `head_dim`: identifies the final output feature dimension of the `base_architecture`* `slice_names`: Specify the slices that we plan to train on with this classifier.<jupyter_code>from snorkel.slicing import SliceAwareClassifier
from utils import get_pytorch_mlp

# Define model architecture
bow_dim = X_train.shape[1]
hidden_dim = bow_dim
mlp = get_pytorch_mlp(hidden_dim=hidden_dim, num_layers=2)

# Initialize slice model
slice_model = SliceAwareClassifier(
    base_architecture=mlp,
    head_dim=hidden_dim,
    slice_names=[sf.name for sf in sfs],
    scorer=scorer,
)<jupyter_output><empty_output><jupyter_text>Next, we'll generate the remaining `S` matrixes with the new set of slicing functions.<jupyter_code>applier = PandasSFApplier(sfs)
S_train = applier.apply(df_train)
S_test = applier.apply(df_test)<jupyter_output>0%|          | 0/1586 [00:00<?, ?it/s]<jupyter_text>In order to train using slice information, we'd like to initialize a **slice-aware dataloader**.To do this, we can use [`slice_model.make_slice_dataloader`](https://snorkel.readthedocs.io/en/v0.9.3/packages/_autosummary/slicing/snorkel.slicing.SliceAwareClassifier.htmlsnorkel.slicing.SliceAwareClassifier.predict) to add slice labels to an existing dataloader.Under the hood, this method leverages slice metadata to add slice labels to the appropriate fields such that it will be compatible with our model, a [`SliceAwareClassifier`](https://snorkel.readthedocs.io/en/v0.9.3/packages/_autosummary/slicing/snorkel.slicing.SliceAwareClassifier.htmlsnorkel-slicing-slicingclassifier).<jupyter_code>from utils import create_dict_dataloader

BATCH_SIZE = 64

train_dl = create_dict_dataloader(X_train, Y_train, "train")
train_dl_slice = slice_model.make_slice_dataloader(
    train_dl.dataset, S_train, shuffle=True, batch_size=BATCH_SIZE
)
test_dl = create_dict_dataloader(X_test, Y_test, "train")
test_dl_slice = slice_model.make_slice_dataloader(
    test_dl.dataset, S_test, shuffle=False, batch_size=BATCH_SIZE
)<jupyter_output><empty_output><jupyter_text>Representation learning with slices Using Snorkel's [`Trainer`](https://snorkel.readthedocs.io/en/master/packages/_autosummary/classification/snorkel.classification.Trainer.html), we fit our classifier with the training set dataloader.<jupyter_code>from snorkel.classification import Trainer

# For demonstration purposes, we set n_epochs=2
trainer = Trainer(n_epochs=2, lr=1e-4, progress_bar=True)
trainer.fit(slice_model, [train_dl_slice])<jupyter_output>Epoch 0::   0%|          | 0/25 [00:00<?, ?it/s]<jupyter_text>At inference time, the primary task head (`spam_task`) will make all final predictions.We'd like to evaluate all the slice heads on the original task head — [`score_slices`](https://snorkel.readthedocs.io/en/v0.9.3/packages/_autosummary/slicing/snorkel.slicing.SliceAwareClassifier.htmlsnorkel.slicing.SliceAwareClassifier.score_slices) remaps all slice-related labels, denoted `spam_task_slice:{slice_name}_pred`, to be evaluated on the `spam_task`.<jupyter_code>slice_model.score_slices([test_dl_slice], as_dataframe=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08915324467018645, 'fraction_numerical': 0.0065494887388892606, 'mean_word_length': 6.444776119402985, 'pattern_counts': {'":': 1, '<': 77, '<?xml version=': 0, '>': 74, 'https://': 18, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '95955', 'license': 'Apache-2.0', 'n_tokens_mistral': 4842, 'n_tokens_neox': 4649, 'n_words': 1358, 'path': 'spam/03_spam_data_slicing_tutorial.ipynb', 'repo_name': 'JungeAlexander/snorkel-tutorials', 'size': '14963'}