Пример #1
0
    def test_two_means_invalid_dims(self):
        two_means = TwoMeansBiasDirection()
        with pytest.raises(ConfigurationError):
            two_means(torch.zeros(2), torch.zeros(2))

        with pytest.raises(ConfigurationError):
            two_means(torch.zeros(2, 2), torch.zeros(2, 3))
Пример #2
0
    def setup_method(self):
        super().setup_method()

        # embedding data from VERB demo
        emb_filename = str(self.FIXTURES_ROOT / "fairness" /
                           "bias_embeddings.json")
        with open(emb_filename) as emb_file:
            emb_data = json.load(emb_file)

        seed_embeddings1 = torch.cat([
            torch.Tensor(emb_data["he"]).reshape(1, -1),
            torch.Tensor(emb_data["him"]).reshape(1, -1),
        ])
        seed_embeddings2 = torch.cat([
            torch.Tensor(emb_data["she"]).reshape(1, -1),
            torch.Tensor(emb_data["her"]).reshape(1, -1),
        ])
        tm = TwoMeansBiasDirection()
        self.bias_direction = tm(seed_embeddings1, seed_embeddings2)

        evaluation_embeddings = []
        expected_bias_mitigated_embeddings = []
        for word in ["engineer", "banker", "nurse", "receptionist"]:
            evaluation_embeddings.append(
                torch.Tensor(emb_data[word]).reshape(1, -1))
            expected_bias_mitigated_embeddings.append(
                torch.Tensor(emb_data["linear_two_means_" + word]).reshape(
                    1, -1))
        self.evaluation_embeddings = torch.cat(evaluation_embeddings).reshape(
            2, 2, -1)
        self.expected_bias_mitigated_embeddings = torch.cat(
            expected_bias_mitigated_embeddings).reshape(2, 2, -1)
Пример #3
0
    def test_two_means_with_grad(self, device: str):
        seed_embeddings1 = torch.eye(2, device=device)
        seed_embeddings2 = 1 - torch.eye(2, device=device)
        seed_embeddings1 = seed_embeddings1.requires_grad_()
        seed_embeddings2 = seed_embeddings2.requires_grad_()
        assert seed_embeddings1.grad is None
        assert seed_embeddings2.grad is None

        two_means = TwoMeansBiasDirection(requires_grad=True)
        test_bias_direction = two_means(seed_embeddings1, seed_embeddings2)
        test_bias_direction.sum().backward()
        assert seed_embeddings1.grad is not None
        assert seed_embeddings2.grad is not None
Пример #4
0
    def test_two_means_without_grad(self, device: str):
        seed_embeddings1 = torch.eye(2, device=device)
        seed_embeddings2 = 1 - torch.eye(2, device=device)
        two_means = TwoMeansBiasDirection()

        expected_bias_direction = torch.tensor(
            [float("nan"), float("nan")], device=device)
        test_bias_direction = two_means(seed_embeddings1, seed_embeddings2)
        assert allclose(expected_bias_direction,
                        test_bias_direction,
                        equal_nan=True)
        assert seed_embeddings1.grad is None
        assert seed_embeddings2.grad is None
Пример #5
0
 def __init__(
     self,
     seed_word_pairs_file: Union[PathLike, str],
     tokenizer: Tokenizer,
     direction_vocab: Optional[Vocabulary] = None,
     namespace: str = "tokens",
     requires_grad: bool = False,
     noise: float = 1e-10,
 ):
     self.ids1, self.ids2 = load_word_pairs(seed_word_pairs_file, tokenizer,
                                            direction_vocab, namespace)
     self.direction = TwoMeansBiasDirection(requires_grad=requires_grad)
     self.noise = noise