Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch class balance errors and test L matrix edge cases #1449

Merged
merged 6 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion snorkel/labeling/model/label_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,25 @@ def _set_class_balance(
"""
if class_balance is not None:
self.p = np.array(class_balance)
if len(self.p) != self.cardinality:
raise ValueError(
f"class_balance has {len(self.p)} entries. Does not match LabelModel cardinality {self.cardinality}."
)
elif Y_dev is not None:
class_counts = Counter(Y_dev)
sorted_counts = np.array([v for k, v in sorted(class_counts.items())])
self.p = sorted_counts / sum(sorted_counts)
if len(self.p) != self.cardinality:
raise ValueError(
f"Y_dev has {len(self.p)} class(es). Does not match LabelModel cardinality {self.cardinality}."
)
else:
self.p = (1 / self.cardinality) * np.ones(self.cardinality)

if np.any(self.p == 0):
raise ValueError(
f"Class balance prior is 0 for class(es) {np.where(self.p)[0]}."
)
self.P = torch.diag(torch.from_numpy(self.p)).float()

def _set_constants(self, L: np.ndarray) -> None:
Expand Down Expand Up @@ -742,8 +755,8 @@ def fit(
f"L_train has cardinality {L_shift.max()}, cardinality={self.cardinality} passed in."
)

self._set_class_balance(class_balance, Y_dev)
self._set_constants(L_shift)
self._set_class_balance(class_balance, Y_dev)
self._create_tree()
lf_analysis = LFAnalysis(L_train)
self.coverage = lf_analysis.lf_coverages()
Expand Down
36 changes: 33 additions & 3 deletions test/labeling/model/test_label_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,56 @@ def _set_up_model(self, L: np.ndarray, class_balance: List[float] = [0.5, 0.5]):

def test_L_form(self):
label_model = LabelModel(cardinality=2, verbose=False)
L = np.array([[0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 1, 0]])
L = np.array([[-1, 1, -1], [-1, 1, -1], [1, -1, -1], [-1, 1, -1]])
label_model._set_constants(L)
self.assertEqual(label_model.n, 4)
self.assertEqual(label_model.m, 3)

L = np.array([[0, 1, 2], [0, 1, 2], [1, 0, 2], [0, 1, 0]])
L = np.array([[-1, 0, 1], [-1, 0, 2], [0, -1, 2], [-1, 0, -1]])
with self.assertRaisesRegex(ValueError, "L_train has cardinality"):
label_model.fit(L, n_epochs=1)

L = np.array([[0], [1], [-1]])
L = np.array([[0, 1], [1, 1], [0, 1]])
with self.assertRaisesRegex(ValueError, "L_train should have at least 3"):
label_model.fit(L, n_epochs=1)

def test_mv_default(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what change is this testing? do we actually add mv as a default anywhere here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's checking for degenerate L matrices (aka low overlap and no conflicts) that the label model should internally default to the predictions MV would assign. it doesn't explicitly call majority vote anywhere

# less than 2 LFs have overlaps
label_model = LabelModel(cardinality=2, verbose=False)
L = np.array([[-1, -1, 1], [-1, 1, -1], [0, -1, -1]])
label_model.fit(L, n_epochs=100)
np.testing.assert_array_almost_equal(
label_model.predict(L), np.array([1, 1, 0])
)

# less than 2 LFs have conflicts
L = np.array([[-1, -1, 1], [-1, 1, 1], [1, 1, 1]])
label_model.fit(L, n_epochs=100)
np.testing.assert_array_almost_equal(
label_model.predict(L), np.array([1, 1, 1])
)

def test_class_balance(self):
label_model = LabelModel(cardinality=2, verbose=False)
# Test class balance
Y_dev = np.array([0, 0, 1, 1, 0, 0, 0, 0, 1, 1])
label_model._set_class_balance(class_balance=None, Y_dev=Y_dev)
np.testing.assert_array_almost_equal(label_model.p, np.array([0.6, 0.4]))

class_balance = np.array([0.0, 1.0])
with self.assertRaisesRegex(ValueError, "Class balance prior is 0"):
label_model._set_class_balance(class_balance=class_balance, Y_dev=Y_dev)

class_balance = np.array([0.0])
with self.assertRaisesRegex(ValueError, "class_balance has 1 entries."):
label_model._set_class_balance(class_balance=class_balance, Y_dev=Y_dev)

Y_dev_one_class = np.array([0, 0, 0])
with self.assertRaisesRegex(
ValueError, "Does not match LabelModel cardinality"
):
label_model._set_class_balance(class_balance=None, Y_dev=Y_dev_one_class)

def test_generate_O(self):
L = np.array([[0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 1, 1]])
label_model = self._set_up_model(L)
Expand Down