snorkel.labeling.RandomVoter¶
-
class
snorkel.labeling.RandomVoter(cardinality=2, **kwargs)[source]¶ Bases:
snorkel.labeling.model.baselines.BaselineVoterRandom vote label model.
Example
>>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) >>> random_voter = RandomVoter() >>> predictions = random_voter.predict_proba(L)
-
__init__(cardinality=2, **kwargs)[source]¶ Initialize self. See help(type(self)) for accurate signature.
- Return type
None
Methods
__init__([cardinality])Initialize self.
add_module(name, module)Adds a child module to the current module.
apply(fn)Applies
fnrecursively to every submodule (as returned by.children()) as well as self.buffers([recurse])Returns an iterator over module buffers.
children()Returns an iterator over immediate children modules.
cpu()Moves all model parameters and buffers to the CPU.
cuda([device])Moves all model parameters and buffers to the GPU.
double()Casts all floating point parameters and buffers to
doubledatatype.eval()Sets the module in evaluation mode.
extra_repr()Set the extra representation of the module
fit(*args, **kwargs)Train majority class model.
float()Casts all floating point parameters and buffers to float datatype.
forward(*input)Defines the computation performed at every call.
Return the vector of learned LF weights for combining LFs.
half()Casts all floating point parameters and buffers to
halfdatatype.load(source, **kwargs)Load existing label model.
load_state_dict(state_dict[, strict])Copies parameters and buffers from
state_dictinto this module and its descendants.modules()Returns an iterator over all modules in the network.
named_buffers([prefix, recurse])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children()Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules([memo, prefix])Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters([prefix, recurse])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters([recurse])Returns an iterator over module parameters.
predict(L[, return_probs, tie_break_policy])Return predicted labels, with ties broken according to policy.
Assign random votes to the data points.
register_backward_hook(hook)Registers a backward hook on the module.
register_buffer(name, tensor)Adds a persistent buffer to the module.
register_forward_hook(hook)Registers a forward hook on the module.
register_forward_pre_hook(hook)Registers a forward pre-hook on the module.
register_parameter(name, param)Adds a parameter to the module.
save(destination, **kwargs)Save label model.
score(L, Y[, metrics, tie_break_policy])Calculate one or more scores from user-specified and/or user-defined metrics.
share_memory()state_dict([destination, prefix, keep_vars])Returns a dictionary containing a whole state of the module.
to(*args, **kwargs)Moves and/or casts the parameters and buffers.
train([mode])Sets the module in training mode.
type(dst_type)Casts all parameters and buffers to
dst_type.zero_grad()Sets gradients of all model parameters to zero.
Attributes
dump_patches-
fit(*args, **kwargs)[source]¶ Train majority class model.
Set class balance for majority class label model.
- Parameters
balance – A [k] array of class probabilities
- Return type
None
-
get_weights()[source]¶ Return the vector of learned LF weights for combining LFs.
- Returns
[m,1] vector of learned LF weights for combining LFs.
- Return type
np.ndarray
Example
>>> L = np.array([[1, 1, 1], [1, 1, -1], [-1, 0, 0], [0, 0, 0]]) >>> label_model = LabelModel(verbose=False) >>> label_model.fit(L, seed=123) >>> np.around(label_model.get_weights(), 2) # doctest: +SKIP array([0.99, 0.99, 0.99])
-
static
load(source, **kwargs)[source]¶ Load existing label model.
- Parameters
source (
str) – File location from where to load model**kwargs – Arguments for torch.load
- Returns
LabelModel with appropriate loaded parameters
- Return type
Example
Load parameters saved in
saved_label_model>>> label_model.load('./saved_label_model') # doctest: +SKIP
-
predict(L, return_probs=False, tie_break_policy='random')[source]¶ Return predicted labels, with ties broken according to policy.
Policies to break ties include: “abstain”: return an abstain vote (0) “true-random”: randomly choose among the tied options “random”: randomly choose among tied option using deterministic hash
NOTE: if tie_break_policy=”true-random”, repeated runs may have slightly different results due to difference in broken ties
- Parameters
L (
ndarray) – An [n,m] matrix with values in {-1,0,1,…,k-1}return_probs (
Optional[bool]) – Whether to return probs along with predstie_break_policy (
str) – Policy to break ties when converting probabilistic labels to predictions
- Return type
Union[ndarray,Tuple[ndarray,ndarray]]- Returns
np.ndarray – An [n,1] array of integer labels
(np.ndarray, np.ndarray) – An [n,1] array of integer labels and an [n,k] array of probabilistic labels
Example
>>> L = np.array([[0, 0, -1], [1, 1, -1], [0, 0, -1]]) >>> label_model = LabelModel(verbose=False) >>> label_model.fit(L) >>> label_model.predict(L) array([0, 1, 0])
-
predict_proba(L)[source]¶ Assign random votes to the data points.
- Parameters
L (
ndarray) – An [n, m] matrix of labels- Returns
A [n, k] array of probabilistic labels
- Return type
np.ndarray
Example
>>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) >>> random_voter = RandomVoter() >>> predictions = random_voter.predict_proba(L)
-
save(destination, **kwargs)[source]¶ Save label model.
- Parameters
destination (
str) – File location for saving model**kwargs – Arguments for torch.save
Example
>>> label_model.save('./saved_label_model') # doctest: +SKIP
- Return type
None
-
score(L, Y, metrics=['accuracy'], tie_break_policy='random')[source]¶ Calculate one or more scores from user-specified and/or user-defined metrics.
- Parameters
L (
ndarray) – An [n,m] matrix with values in {-1,0,1,…,k-1}Y (
ndarray) – Gold labels associated with datapoints in Lmetrics (
Optional[List[str]]) – A list of metric namestie_break_policy (
str) – Policy to break ties when converting probabilistic labels to predictions
- Returns
A dictionary mapping metric names to metric scores
- Return type
Dict[str, float]
Example
>>> L = np.array([[1, 1, -1], [0, 0, -1], [1, 1, -1]]) >>> label_model = LabelModel(verbose=False) >>> label_model.fit(L) >>> label_model.score(L, Y=np.array([1, 1, 1])) {'accuracy': 0.6666666666666666} >>> label_model.score(L, Y=np.array([1, 1, 1]), metrics=["f1"]) {'f1': 0.8}
-