Esempio n. 1
0
    def test_constrained_viterbi_tags(self):
        constraints = {(0, 0), (0, 1),
                       (1, 1), (1, 2),
                       (2, 2), (2, 3),
                       (3, 3), (3, 4),
                       (4, 4), (4, 0)}

        # Add the transitions to the end tag
        # and from the start tag.
        for i in range(5):
            constraints.add((5, i))
            constraints.add((i, 6))

        crf = ConditionalRandomField(num_tags=5, constraints=constraints)
        crf.transitions = torch.nn.Parameter(self.transitions)
        crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

        mask = torch.LongTensor([
                [1, 1, 1],
                [1, 1, 0]
        ])

        viterbi_path = crf.viterbi_tags(self.logits, mask)

        # Get just the tags from each tuple of (tags, score).
        viterbi_tags = [x for x, y in viterbi_path]

        # Now the tags should respect the constraints
        assert viterbi_tags == [
                [2, 3, 3],
                [2, 3]
        ]
    def test_constrained_viterbi_tags(self):
        constraints = {(0, 0), (0, 1),
                       (1, 1), (1, 2),
                       (2, 2), (2, 3),
                       (3, 3), (3, 4),
                       (4, 4), (4, 0)}

        # Add the transitions to the end tag
        # and from the start tag.
        for i in range(5):
            constraints.add((5, i))
            constraints.add((i, 6))

        crf = ConditionalRandomField(num_tags=5, constraints=constraints)
        crf.transitions = torch.nn.Parameter(self.transitions)
        crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

        mask = torch.LongTensor([
                [1, 1, 1],
                [1, 1, 0]
        ])

        viterbi_path = crf.viterbi_tags(self.logits, mask)

        # Get just the tags from each tuple of (tags, score).
        viterbi_tags = [x for x, y in viterbi_path]

        # Now the tags should respect the constraints
        assert viterbi_tags == [
                [2, 3, 3],
                [2, 3]
        ]
    def test_constrained_viterbi_tags(self):
        constraints = {(0, 0), (0, 1), (1, 1), (1, 2), (2, 2), (2, 3), (3, 3),
                       (3, 4), (4, 4), (4, 0)}

        crf = ConditionalRandomField(num_tags=5, constraints=constraints)
        crf.transitions = torch.nn.Parameter(self.transitions)
        crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

        mask = Variable(torch.LongTensor([[1, 1, 1], [1, 1, 0]]))

        viterbi_tags = crf.viterbi_tags(self.logits, mask)

        # Now the tags should respect the constraints
        assert viterbi_tags == [[2, 3, 3], [2, 3]]
    def test_constrained_viterbi_tags(self):
        constraints = {(0, 0), (0, 1),
                       (1, 1), (1, 2),
                       (2, 2), (2, 3),
                       (3, 3), (3, 4),
                       (4, 4), (4, 0)}

        crf = ConditionalRandomField(num_tags=5, constraints=constraints)
        crf.transitions = torch.nn.Parameter(self.transitions)
        crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

        mask = Variable(torch.LongTensor([
                [1, 1, 1],
                [1, 1, 0]
        ]))

        viterbi_tags = crf.viterbi_tags(self.logits, mask)

        # Now the tags should respect the constraints
        assert viterbi_tags == [
                [2, 3, 3],
                [2, 3]
        ]
Esempio n. 5
0
    def test_constrained_viterbi_tags(self):
        constraints = {(0, 0), (0, 1), (1, 1), (1, 2), (2, 2), (2, 3), (3, 3),
                       (3, 4), (4, 4), (4, 0)}

        # Add the transitions to the end tag
        # and from the start tag.
        for i in range(5):
            constraints.add((5, i))
            constraints.add((i, 6))

        crf = ConditionalRandomField(num_tags=5, constraints=constraints)
        crf.transitions = torch.nn.Parameter(self.transitions)
        crf.start_transitions = torch.nn.Parameter(self.transitions_from_start)
        crf.end_transitions = torch.nn.Parameter(self.transitions_to_end)

        timestep_constrained_crf = TimestepConstrainedConditionalRandomField(
            num_tags=5, constraints=constraints)
        timestep_constrained_crf.transitions = torch.nn.Parameter(
            self.transitions)
        timestep_constrained_crf.start_transitions = torch.nn.Parameter(
            self.transitions_from_start)
        timestep_constrained_crf.end_transitions = torch.nn.Parameter(
            self.transitions_to_end)

        mask = torch.LongTensor([[1, 1, 1], [1, 1, 0]])

        viterbi_path = crf.viterbi_tags(self.logits, mask)
        # Get just the tags from each tuple of (tags, score).
        viterbi_tags = [x for x, y in viterbi_path]
        # Now the tags should respect the constraints
        assert viterbi_tags == [[2, 3, 3], [2, 3]]

        timestep_constrained_viterbi_path = timestep_constrained_crf.viterbi_tags(
            self.logits, mask)
        # Get just the tags from each tuple of (tags, score).
        timestep_constrained_viterbi_tags = [
            x for x, y in timestep_constrained_viterbi_path
        ]
        # Now the tags should respect the constraints
        assert timestep_constrained_viterbi_tags == viterbi_tags

        timestep_constrained_crf_no_constraints = TimestepConstrainedConditionalRandomField(
            num_tags=5)
        timestep_constrained_crf_no_constraints.transitions = torch.nn.Parameter(
            self.transitions)
        timestep_constrained_crf_no_constraints.start_transitions = torch.nn.Parameter(
            self.transitions_from_start)
        timestep_constrained_crf_no_constraints.end_transitions = torch.nn.Parameter(
            self.transitions_to_end)
        constraint_mask = crf._constraint_mask.data
        expanded_constraint_mask = constraint_mask.unsqueeze(0).expand(
            2, -1, -1)
        # Pass in the constraints at viterbi_tags
        timestep_constrained_viterbi_path = timestep_constrained_crf_no_constraints.viterbi_tags(
            self.logits, mask, expanded_constraint_mask)
        # Get just the tags from each tuple of (tags, score).
        timestep_constrained_viterbi_tags = [
            x for x, y in timestep_constrained_viterbi_path
        ]
        # Now the tags should respect the constraints
        assert timestep_constrained_viterbi_tags == viterbi_tags