Skip to main content
Version: 0.93

snorkelflow.lfs.labeling_function

class snorkelflow.lfs.labeling_function(name=None, resources=None, preprocess_configs=None)

Bases: object

Decorator to create a LabelingFunction object from a user-defined function.

Parameters:
  • name (Optional[str], default: None) – Name of the LF

  • resources (Optional[Mapping[str, Any]], default: None) –

    Labeling resources passed in to f via kwargs.

    note
    While this can be a nested dictionary and can include functions, functions should be its direct children if any otherwise they would break when Snorkel Flow upgrades to a newer version of Python.

  • preprocess_configs (Optional[List[Dict[str, Any]]], default: None) – Preprocessors to run on data points before LF execution

Examples

# Simple example
@labeling_function()
def f(x):
return "SPAM" if "drug" in x.body else "UNKNOWN"

# Example with resources
def find_word_index(text):
import numpy
try:
idx = numpy.where(text.split(" ").index("employee"))
except:
idx = numpy.array([])
return idx

@labeling_function(name="my_lf", resources=dict(find_word_index=find_word_index))
def lf(x, find_word_index):
import numpy
idx = find_word_index(x.text)
if numpy.mean(idx) <= 1000:
return "employment"
else:
return "UNKNOWN"

# Bad example ("find_word_index" function is NOT a direct child of "resources")
@labeling_function(name="my_lf", resources=dict(funcs=dict(find_word_index=find_word_index)))
def bad_lf(x, funcs):
import numpy
idx = funcs["find_word_index"](x.text)
if numpy.mean(idx) <= 1000:
return "employment"
else:
return "UNKNOWN"
__init__(name=None, resources=None, preprocess_configs=None)

Methods

__init__([name, resources, preprocess_configs])