当前位置: 首页>>代码示例>>Python>>正文


Python util.get_lengths_from_binary_sequence_mask方法代码示例

本文整理汇总了Python中allennlp.nn.util.get_lengths_from_binary_sequence_mask方法的典型用法代码示例。如果您正苦于以下问题:Python util.get_lengths_from_binary_sequence_mask方法的具体用法?Python util.get_lengths_from_binary_sequence_mask怎么用?Python util.get_lengths_from_binary_sequence_mask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在allennlp.nn.util的用法示例。


在下文中一共展示了util.get_lengths_from_binary_sequence_mask方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: forward

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def forward(self, tokens: torch.Tensor, mask: torch.BoolTensor = None):
        if mask is not None:
            tokens = tokens * mask.unsqueeze(-1)

        # Our input has shape `(batch_size, num_tokens, embedding_dim)`, so we sum out the `num_tokens`
        # dimension.
        summed = tokens.sum(1)

        if self._averaged:
            if mask is not None:
                lengths = get_lengths_from_binary_sequence_mask(mask)
                length_mask = lengths > 0

                # Set any length 0 to 1, to avoid dividing by zero.
                lengths = torch.max(lengths, lengths.new_ones(1))
            else:
                lengths = tokens.new_full((1,), fill_value=tokens.size(1))
                length_mask = None

            summed = summed / lengths.unsqueeze(-1).float()

            if length_mask is not None:
                summed = summed * (length_mask > 0).unsqueeze(-1)

        return summed 
开发者ID:allenai,项目名称:allennlp,代码行数:27,代码来源:boe_encoder.py

示例2: test_forward_pulls_out_correct_tensor_with_sequence_lengths

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2SeqWrapper(lstm)
        input_tensor = torch.rand([5, 7, 3])
        input_tensor[1, 6:, :] = 0
        input_tensor[2, 4:, :] = 0
        input_tensor[3, 2:, :] = 0
        input_tensor[4, 1:, :] = 0
        mask = torch.ones(5, 7).bool()
        mask[1, 6:] = False
        mask[2, 4:] = False
        mask[3, 2:] = False
        mask[4, 1:] = False

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        packed_sequence = pack_padded_sequence(
            input_tensor, sequence_lengths.data.tolist(), batch_first=True
        )
        lstm_output, _ = lstm(packed_sequence)
        encoder_output = encoder(input_tensor, mask)
        lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True)
        assert_almost_equal(encoder_output.data.numpy(), lstm_tensor.data.numpy()) 
开发者ID:allenai,项目名称:allennlp,代码行数:24,代码来源:pytorch_seq2seq_wrapper_test.py

示例3: decode

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def decode(self, output_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:

        head_tags = output_dict.pop("head_tags").cpu().detach().numpy()
        heads = output_dict.pop("heads").cpu().detach().numpy()
        mask = output_dict.pop("mask")
        lengths = get_lengths_from_binary_sequence_mask(mask)
        head_tag_labels = []
        head_indices = []
        for instance_heads, instance_tags, length in zip(heads, head_tags, lengths):
            instance_heads = list(instance_heads[1:length])
            instance_tags = instance_tags[1:length]
            labels = [self.vocab.get_token_from_index(label, "head_tags")
                      for label in instance_tags]
            head_tag_labels.append(labels)
            head_indices.append(instance_heads)

        output_dict["predicted_dependencies"] = head_tag_labels
        output_dict["predicted_heads"] = head_indices
        return output_dict 
开发者ID:Hyperparticle,项目名称:udify,代码行数:21,代码来源:dependency_decoder.py

示例4: forward

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def forward(self, tokens              , mask               = None):  #pylint: disable=arguments-differ
        if mask is not None:
            tokens = tokens * mask.unsqueeze(-1).float()

        # Our input has shape `(batch_size, num_tokens, embedding_dim)`, so we sum out the `num_tokens`
        # dimension.
        summed = tokens.sum(1)

        if self._averaged:
            if mask is not None:
                lengths = get_lengths_from_binary_sequence_mask(mask)
                length_mask = (lengths > 0)

                # Set any length 0 to 1, to avoid dividing by zero.
                lengths = torch.max(lengths, lengths.new_ones(1))
            else:
                lengths = tokens.new_full((1,), fill_value=tokens.size(1))
                length_mask = None

            summed = summed / lengths.unsqueeze(-1).float()

            if length_mask is not None:
                summed = summed * (length_mask > 0).float().unsqueeze(-1)

        return summed 
开发者ID:plasticityai,项目名称:magnitude,代码行数:27,代码来源:boe_encoder.py

示例5: decode

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def decode(self, output_dict                         )                           :
        u"""
        Does constrained viterbi decoding on class probabilities output in :func:`forward`.  The
        constraint simply specifies that the output tags must be a valid BIO sequence.  We add a
        ``"tags"`` key to the dictionary with the result.
        """
        all_predictions = output_dict[u'class_probabilities']
        sequence_lengths = get_lengths_from_binary_sequence_mask(output_dict[u"mask"]).data.tolist()

        if all_predictions.dim() == 3:
            predictions_list = [all_predictions[i].detach().cpu() for i in range(all_predictions.size(0))]
        else:
            predictions_list = [all_predictions]
        all_tags = []
        transition_matrix = self.get_viterbi_pairwise_potentials()
        for predictions, length in izip(predictions_list, sequence_lengths):
            max_likelihood_sequence, _ = viterbi_decode(predictions[:length], transition_matrix)
            tags = [self.vocab.get_token_from_index(x, namespace=u"labels")
                    for x in max_likelihood_sequence]
            all_tags.append(tags)
        output_dict[u'tags'] = all_tags
        return output_dict 
开发者ID:plasticityai,项目名称:magnitude,代码行数:24,代码来源:semantic_role_labeler.py

示例6: decode

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def decode(self, output_dict                         )                           :

        head_tags = output_dict.pop(u"head_tags").cpu().detach().numpy()
        heads = output_dict.pop(u"heads").cpu().detach().numpy()
        mask = output_dict.pop(u"mask")
        lengths = get_lengths_from_binary_sequence_mask(mask)
        head_tag_labels = []
        head_indices = []
        for instance_heads, instance_tags, length in izip(heads, head_tags, lengths):
            instance_heads = list(instance_heads[1:length])
            instance_tags = instance_tags[1:length]
            labels = [self.vocab.get_token_from_index(label, u"head_tags")
                      for label in instance_tags]
            head_tag_labels.append(labels)
            head_indices.append(instance_heads)

        output_dict[u"predicted_dependencies"] = head_tag_labels
        output_dict[u"predicted_heads"] = head_indices
        return output_dict 
开发者ID:plasticityai,项目名称:magnitude,代码行数:21,代码来源:biaffine_dependency_parser.py

示例7: test_forward_pulls_out_correct_tensor_with_sequence_lengths

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2SeqWrapper(lstm)
        input_tensor = torch.rand([5, 7, 3])
        input_tensor[1, 6:, :] = 0
        input_tensor[2, 4:, :] = 0
        input_tensor[3, 2:, :] = 0
        input_tensor[4, 1:, :] = 0
        mask = torch.ones(5, 7)
        mask[1, 6:] = 0
        mask[2, 4:] = 0
        mask[3, 2:] = 0
        mask[4, 1:] = 0

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        packed_sequence = pack_padded_sequence(input_tensor, sequence_lengths.data.tolist(), batch_first=True)
        lstm_output, _ = lstm(packed_sequence)
        encoder_output = encoder(input_tensor, mask)
        lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True)
        assert_almost_equal(encoder_output.data.numpy(), lstm_tensor.data.numpy()) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:22,代码来源:pytorch_seq2seq_wrapper_test.py

示例8: test_forward_pulls_out_correct_tensor_for_unsorted_batches

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_for_unsorted_batches(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2SeqWrapper(lstm)
        input_tensor = torch.rand([5, 7, 3])
        input_tensor[0, 3:, :] = 0
        input_tensor[1, 4:, :] = 0
        input_tensor[2, 2:, :] = 0
        input_tensor[3, 6:, :] = 0
        mask = torch.ones(5, 7)
        mask[0, 3:] = 0
        mask[1, 4:] = 0
        mask[2, 2:] = 0
        mask[3, 6:] = 0

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        sorted_inputs, sorted_sequence_lengths, restoration_indices, _ = sort_batch_by_length(input_tensor,
                                                                                              sequence_lengths)
        packed_sequence = pack_padded_sequence(sorted_inputs,
                                               sorted_sequence_lengths.data.tolist(),
                                               batch_first=True)
        lstm_output, _ = lstm(packed_sequence)
        encoder_output = encoder(input_tensor, mask)
        lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True)
        assert_almost_equal(encoder_output.data.numpy(),
                            lstm_tensor.index_select(0, restoration_indices).data.numpy()) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:27,代码来源:pytorch_seq2seq_wrapper_test.py

示例9: test_forward_pulls_out_correct_tensor_with_sequence_lengths

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2VecWrapper(lstm)

        input_tensor = torch.rand([5, 7, 3])
        input_tensor[1, 6:, :] = 0
        input_tensor[2, 4:, :] = 0
        input_tensor[3, 2:, :] = 0
        input_tensor[4, 1:, :] = 0
        mask = torch.ones(5, 7)
        mask[1, 6:] = 0
        mask[2, 4:] = 0
        mask[3, 2:] = 0
        mask[4, 1:] = 0

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        packed_sequence = pack_padded_sequence(input_tensor, sequence_lengths.tolist(), batch_first=True)
        _, state = lstm(packed_sequence)
        # Transpose output state, extract the last forward and backward states and
        # reshape to be of dimension (batch_size, 2 * hidden_size).
        reshaped_state = state[0].transpose(0, 1)[:, -2:, :].contiguous()
        explicitly_concatenated_state = torch.cat([reshaped_state[:, 0, :].squeeze(1),
                                                   reshaped_state[:, 1, :].squeeze(1)], -1)
        encoder_output = encoder(input_tensor, mask)
        assert_almost_equal(encoder_output.data.numpy(), explicitly_concatenated_state.data.numpy()) 
开发者ID:plasticityai,项目名称:magnitude,代码行数:27,代码来源:pytorch_seq2vec_wrapper_test.py

示例10: test_forward_pulls_out_correct_tensor_for_unsorted_batches

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_for_unsorted_batches(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2SeqWrapper(lstm)
        input_tensor = torch.rand([5, 7, 3])
        input_tensor[0, 3:, :] = 0
        input_tensor[1, 4:, :] = 0
        input_tensor[2, 2:, :] = 0
        input_tensor[3, 6:, :] = 0
        mask = torch.ones(5, 7).bool()
        mask[0, 3:] = False
        mask[1, 4:] = False
        mask[2, 2:] = False
        mask[3, 6:] = False

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        sorted_inputs, sorted_sequence_lengths, restoration_indices, _ = sort_batch_by_length(
            input_tensor, sequence_lengths
        )
        packed_sequence = pack_padded_sequence(
            sorted_inputs, sorted_sequence_lengths.data.tolist(), batch_first=True
        )
        lstm_output, _ = lstm(packed_sequence)
        encoder_output = encoder(input_tensor, mask)
        lstm_tensor, _ = pad_packed_sequence(lstm_output, batch_first=True)
        assert_almost_equal(
            encoder_output.data.numpy(),
            lstm_tensor.index_select(0, restoration_indices).data.numpy(),
        ) 
开发者ID:allenai,项目名称:allennlp,代码行数:30,代码来源:pytorch_seq2seq_wrapper_test.py

示例11: setup_method

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def setup_method(self):
        super().setup_method()
        self.lstm = LSTM(
            bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True
        )
        self.rnn = RNN(
            bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True
        )
        self.encoder_base = _EncoderBase(stateful=True)

        tensor = torch.rand([5, 7, 3])
        tensor[1, 6:, :] = 0
        tensor[3, 2:, :] = 0
        self.tensor = tensor
        mask = torch.ones(5, 7).bool()
        mask[1, 6:] = False
        mask[2, :] = False  # <= completely masked
        mask[3, 2:] = False
        mask[4, :] = False  # <= completely masked
        self.mask = mask

        self.batch_size = 5
        self.num_valid = 3
        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        _, _, restoration_indices, sorting_indices = sort_batch_by_length(tensor, sequence_lengths)
        self.sorting_indices = sorting_indices
        self.restoration_indices = restoration_indices 
开发者ID:allenai,项目名称:allennlp,代码行数:29,代码来源:encoder_base_test.py

示例12: test_forward_pulls_out_correct_tensor_with_sequence_lengths

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_forward_pulls_out_correct_tensor_with_sequence_lengths(self):
        lstm = LSTM(bidirectional=True, num_layers=3, input_size=3, hidden_size=7, batch_first=True)
        encoder = PytorchSeq2VecWrapper(lstm)

        input_tensor = torch.rand([5, 7, 3])
        input_tensor[1, 6:, :] = 0
        input_tensor[2, 4:, :] = 0
        input_tensor[3, 2:, :] = 0
        input_tensor[4, 1:, :] = 0
        mask = torch.ones(5, 7).bool()
        mask[1, 6:] = False
        mask[2, 4:] = False
        mask[3, 2:] = False
        mask[4, 1:] = False

        sequence_lengths = get_lengths_from_binary_sequence_mask(mask)
        packed_sequence = pack_padded_sequence(
            input_tensor, sequence_lengths.tolist(), batch_first=True
        )
        _, state = lstm(packed_sequence)
        # Transpose output state, extract the last forward and backward states and
        # reshape to be of dimension (batch_size, 2 * hidden_size).
        reshaped_state = state[0].transpose(0, 1)[:, -2:, :].contiguous()
        explicitly_concatenated_state = torch.cat(
            [reshaped_state[:, 0, :].squeeze(1), reshaped_state[:, 1, :].squeeze(1)], -1
        )
        encoder_output = encoder(input_tensor, mask)
        assert_almost_equal(encoder_output.data.numpy(), explicitly_concatenated_state.data.numpy()) 
开发者ID:allenai,项目名称:allennlp,代码行数:30,代码来源:pytorch_seq2vec_wrapper_test.py

示例13: test_get_sequence_lengths_from_binary_mask

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_get_sequence_lengths_from_binary_mask(self):
        binary_mask = torch.tensor(
            [
                [True, True, True, False, False, False],
                [True, True, False, False, False, False],
                [True, True, True, True, True, True],
                [True, False, False, False, False, False],
            ]
        )
        lengths = util.get_lengths_from_binary_sequence_mask(binary_mask)
        numpy.testing.assert_array_equal(lengths.numpy(), numpy.array([3, 2, 6, 1])) 
开发者ID:allenai,项目名称:allennlp,代码行数:13,代码来源:util_test.py

示例14: test_get_sequence_lengths_converts_to_long_tensor_and_avoids_variable_overflow

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def test_get_sequence_lengths_converts_to_long_tensor_and_avoids_variable_overflow(self):
        # Tests the following weird behaviour in Pytorch 0.1.12
        # doesn't happen for our sequence masks:
        #
        # mask = torch.ones([260]).bool()
        # mask.sum() # equals 260.
        # var_mask = t.a.V(mask)
        # var_mask.sum() # equals 4, due to 8 bit precision - the sum overflows.
        binary_mask = torch.ones(2, 260).bool()
        lengths = util.get_lengths_from_binary_sequence_mask(binary_mask)
        numpy.testing.assert_array_equal(lengths.data.numpy(), numpy.array([260, 260])) 
开发者ID:allenai,项目名称:allennlp,代码行数:13,代码来源:util_test.py

示例15: get_length_and_mask

# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_lengths_from_binary_sequence_mask [as 别名]
def get_length_and_mask(seq):
    len_mask = (seq != 0).long()
    len_t = get_lengths_from_binary_sequence_mask(len_mask)
    return len_mask, len_t 
开发者ID:easonnie,项目名称:combine-FEVER-NSMN,代码行数:6,代码来源:torch_util.py


注:本文中的allennlp.nn.util.get_lengths_from_binary_sequence_mask方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。