Implementation:Snorkel team Snorkel RandomPolicy Init
| Knowledge Sources | |
|---|---|
| Domains | Data_Augmentation, Sampling |
| Last Updated | 2026-02-14 20:00 GMT |
Overview
Concrete tool for configuring augmentation policies that control TF sampling and sequencing, provided by the Snorkel library.
Description
RandomPolicy and MeanFieldPolicy control how transformation functions are sampled during augmentation. Both extend the Policy base class which provides generate() (sample a TF sequence) and generate_for_example() (sample multiple sequences per data point).
- RandomPolicy: Samples uniformly from available TFs
- MeanFieldPolicy: Samples according to user-specified probability distribution
Usage
Import these classes when configuring how your TFs should be combined. Use RandomPolicy for uniform exploration, MeanFieldPolicy for weighted selection.
Code Reference
Source Location
- Repository: snorkel
- File: snorkel/augmentation/policy/sampling.py (RandomPolicy L70-116, MeanFieldPolicy L8-67), snorkel/augmentation/policy/core.py (Policy L4-76)
Signature
class RandomPolicy(MeanFieldPolicy):
def __init__(
self,
n_tfs: int,
sequence_length: int = 1,
n_per_original: int = 1,
keep_original: bool = True,
) -> None:
"""
Args:
n_tfs: Number of available TFs.
sequence_length: TFs to chain per augmentation.
n_per_original: Augmented copies per original.
keep_original: Retain originals in output.
"""
class MeanFieldPolicy(Policy):
def __init__(
self,
n_tfs: int,
sequence_length: int = 1,
p: Optional[Sequence[float]] = None,
n_per_original: int = 1,
keep_original: bool = True,
) -> None:
"""
Args:
n_tfs: Number of available TFs.
sequence_length: TFs to chain per augmentation.
p: Probability distribution over TFs (must sum to 1).
n_per_original: Augmented copies per original.
keep_original: Retain originals in output.
"""
Import
from snorkel.augmentation import RandomPolicy, MeanFieldPolicy
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| n_tfs | int | Yes | Number of transformation functions |
| sequence_length | int | No | TFs chained per augmentation (default 1) |
| n_per_original | int | No | Augmented copies per original (default 1) |
| keep_original | bool | No | Keep originals in output (default True) |
| p | Optional[Sequence[float]] | No | TF probability distribution (MeanFieldPolicy only) |
Outputs
| Name | Type | Description |
|---|---|---|
| Policy instance | Policy | Configured policy for PandasTFApplier |
| generate() | List[int] | Sequence of TF indices to apply |
| generate_for_example() | List[List[int]] | Multiple TF sequences for one data point |
Usage Examples
from snorkel.augmentation import RandomPolicy, MeanFieldPolicy
# Random policy: 2 augmented copies, chain 2 TFs each
policy = RandomPolicy(
n_tfs=5,
sequence_length=2,
n_per_original=2,
keep_original=True,
)
# Mean field policy: weight certain TFs more
policy = MeanFieldPolicy(
n_tfs=3,
p=[0.5, 0.3, 0.2], # First TF applied 50% of the time
n_per_original=1,
)