Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Snorkel team Snorkel SlicingFunction Init

From Leeroopedia
Knowledge Sources
Domains Data_Slicing, Robustness
Last Updated 2026-02-14 20:00 GMT

Overview

Concrete tool for defining slicing functions that identify critical data subsets, provided by the Snorkel library.

Description

The SlicingFunction class extends LabelingFunction and reuses its full infrastructure (preprocessors, resources, callable interface). The only semantic difference is that slicing functions return binary indicators (1 for in-slice, 0 for not-in-slice) rather than multi-class labels.

The companion slicing_function decorator provides a convenient way to define SFs from simple Python functions.

Usage

Import this class when defining data slices for slice-aware training or monitoring. Use the decorator for simple slice predicates and the class form for SFs requiring resources or preprocessors.

Code Reference

Source Location

  • Repository: snorkel
  • File: snorkel/slicing/sf/core.py
  • Lines: L7-73

Signature

class SlicingFunction(LabelingFunction):
    """Base class for slicing functions.
    See snorkel.labeling.lf.LabelingFunction for details.
    """
    pass

class slicing_function:
    def __init__(
        self,
        name: Optional[str] = None,
        resources: Optional[Mapping[str, Any]] = None,
        pre: Optional[List[BasePreprocessor]] = None,
    ) -> None: ...

    def __call__(self, f: Callable[..., int]) -> SlicingFunction: ...

Import

from snorkel.slicing import SlicingFunction, slicing_function

I/O Contract

Inputs

Name Type Required Description
name str Yes Unique SF identifier
f Callable[..., int] Yes Slicing logic; returns 1 (in slice) or 0 (not in slice)
resources Optional[Mapping[str, Any]] No External resources passed as kwargs
pre Optional[List[BasePreprocessor]] No Preprocessors to run before SF

Outputs

Name Type Description
SlicingFunction instance SlicingFunction Callable returning binary slice membership indicator
__call__ result int 1 if data point is in slice, 0 otherwise

Usage Examples

Simple Slicing Functions

from snorkel.slicing import slicing_function

@slicing_function()
def sf_short_text(x):
    """Slice: short messages (fewer than 5 words)."""
    return 1 if len(x.text.split()) < 5 else 0

@slicing_function()
def sf_contains_link(x):
    """Slice: messages containing URLs."""
    return 1 if "http" in x.text else 0

# Test
from types import SimpleNamespace
dp = SimpleNamespace(text="hi")
print(sf_short_text(dp))  # 1

SF with Resources

from snorkel.slicing import SlicingFunction

important_keywords = {"urgent", "critical", "asap"}

def check_keywords(x, keywords):
    words = set(x.text.lower().split())
    return 1 if words & keywords else 0

sf_keywords = SlicingFunction(
    name="sf_important_keywords",
    f=check_keywords,
    resources={"keywords": important_keywords},
)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment