本文整理汇总了Python中allennlp.nn.util.get_range_vector方法的典型用法代码示例。如果您正苦于以下问题:Python util.get_range_vector方法的具体用法?Python util.get_range_vector怎么用?Python util.get_range_vector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类allennlp.nn.util
的用法示例。
在下文中一共展示了util.get_range_vector方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_head_tags
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def _get_head_tags(self,
head_tag_representation: torch.Tensor,
child_tag_representation: torch.Tensor,
head_indices: torch.Tensor) -> torch.Tensor:
"""
Decodes the head tags given the head and child tag representations
and a tensor of head indices to compute tags for. Note that these are
either gold or predicted heads, depending on whether this function is
being called to compute the loss, or if it's being called during inference.
Parameters
----------
head_tag_representation : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
child_tag_representation : ``torch.Tensor``, required
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
head_indices : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length). The indices of the heads
for every word.
Returns
-------
head_tag_logits : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length, num_head_tags),
representing logits for predicting a distribution over tags
for each arc.
"""
batch_size = head_tag_representation.size(0)
# shape (batch_size,)
range_vector = get_range_vector(batch_size, get_device_of(head_tag_representation)).unsqueeze(1)
# This next statement is quite a complex piece of indexing, which you really
# need to read the docs to understand. See here:
# https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#advanced-indexing
# In effect, we are selecting the indices corresponding to the heads of each word from the
# sequence length dimension for each element in the batch.
# shape (batch_size, sequence_length, tag_representation_dim)
selected_head_tag_representations = head_tag_representation[range_vector, head_indices]
selected_head_tag_representations = selected_head_tag_representations.contiguous()
# shape (batch_size, sequence_length, num_head_tags)
head_tag_logits = self.tag_bilinear(selected_head_tag_representations,
child_tag_representation)
return head_tag_logits
示例2: forward
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def forward(self, inputs , offsets ) :
u"""
Parameters
----------
inputs: ``torch.Tensor``, required
A ``(batch_size, num_timesteps)`` tensor representing the byte-pair encodings
for the current batch.
offsets: ``torch.Tensor``, required
A ``(batch_size, max_sequence_length)`` tensor representing the word offsets
for the current batch.
Returns
-------
``[torch.Tensor]``
An embedding representation of the input sequence
having shape ``(batch_size, sequence_length, embedding_dim)``
"""
# pylint: disable=arguments-differ
batch_size, num_timesteps = inputs.size()
# the transformer "vocab" consists of the actual vocab and the
# positional encodings. Here we want the count of just the former.
vocab_size = self._transformer.vocab_size - self._transformer.n_ctx
# vocab_size, vocab_size + 1, ...
positional_encodings = get_range_vector(num_timesteps, device=get_device_of(inputs)) + vocab_size
# Combine the inputs with positional encodings
batch_tensor = torch.stack([
inputs, # (batch_size, num_timesteps)
positional_encodings.expand(batch_size, num_timesteps)
], dim=-1)
byte_pairs_mask = inputs != 0
# Embeddings is num_output_layers x (batch_size, num_timesteps, embedding_dim)
layer_activations = self._transformer(batch_tensor)
# Output of scalar_mix is (batch_size, num_timesteps, embedding_dim)
mix = self._scalar_mix(layer_activations, byte_pairs_mask)
# These embeddings are one per byte-pair, but we want one per original _word_.
# So we choose the embedding corresponding to the last byte pair for each word,
# which is captured by the ``offsets`` input.
range_vector = get_range_vector(batch_size, device=get_device_of(mix)).unsqueeze(1)
last_byte_pair_embeddings = mix[range_vector, offsets]
return last_byte_pair_embeddings
示例3: _generate_valid_antecedents
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def _generate_valid_antecedents(num_spans_to_keep ,
max_antecedents ,
device ):
u"""
This method generates possible antecedents per span which survived the pruning
stage. This procedure is `generic across the batch`. The reason this is the case is
that each span in a batch can be coreferent with any previous span, but here we
are computing the possible `indices` of these spans. So, regardless of the batch,
the 1st span _cannot_ have any antecedents, because there are none to select from.
Similarly, each element can only predict previous spans, so this returns a matrix
of shape (num_spans_to_keep, max_antecedents), where the (i,j)-th index is equal to
(i - 1) - j if j <= i, or zero otherwise.
Parameters
----------
num_spans_to_keep : ``int``, required.
The number of spans that were kept while pruning.
max_antecedents : ``int``, required.
The maximum number of antecedent spans to consider for every span.
device: ``int``, required.
The CUDA device to use.
Returns
-------
valid_antecedent_indices : ``torch.IntTensor``
The indices of every antecedent to consider with respect to the top k spans.
Has shape ``(num_spans_to_keep, max_antecedents)``.
valid_antecedent_offsets : ``torch.IntTensor``
The distance between the span and each of its antecedents in terms of the number
of considered spans (i.e not the word distance between the spans).
Has shape ``(1, max_antecedents)``.
valid_antecedent_log_mask : ``torch.FloatTensor``
The logged mask representing whether each antecedent span is valid. Required since
different spans have different numbers of valid antecedents. For example, the first
span in the document should have no valid antecedents.
Has shape ``(1, num_spans_to_keep, max_antecedents)``.
"""
# Shape: (num_spans_to_keep, 1)
target_indices = util.get_range_vector(num_spans_to_keep, device).unsqueeze(1)
# Shape: (1, max_antecedents)
valid_antecedent_offsets = (util.get_range_vector(max_antecedents, device) + 1).unsqueeze(0)
# This is a broadcasted subtraction.
# Shape: (num_spans_to_keep, max_antecedents)
raw_antecedent_indices = target_indices - valid_antecedent_offsets
# In our matrix of indices, the upper triangular part will be negative
# because the offsets will be > the target indices. We want to mask these,
# because these are exactly the indices which we don't want to predict, per span.
# We're generating a logspace mask here because we will eventually create a
# distribution over these indices, so we need the 0 elements of the mask to be -inf
# in order to not mess up the normalisation of the distribution.
# Shape: (1, num_spans_to_keep, max_antecedents)
valid_antecedent_log_mask = (raw_antecedent_indices >= 0).float().unsqueeze(0).log()
# Shape: (num_spans_to_keep, max_antecedents)
valid_antecedent_indices = F.relu(raw_antecedent_indices.float()).long()
return valid_antecedent_indices, valid_antecedent_offsets, valid_antecedent_log_mask
示例4: _get_head_tags
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def _get_head_tags(self,
head_tag_representation ,
child_tag_representation ,
head_indices ) :
u"""
Decodes the head tags given the head and child tag representations
and a tensor of head indices to compute tags for. Note that these are
either gold or predicted heads, depending on whether this function is
being called to compute the loss, or if it's being called during inference.
Parameters
----------
head_tag_representation : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
child_tag_representation : ``torch.Tensor``, required
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
head_indices : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length). The indices of the heads
for every word.
Returns
-------
head_tag_logits : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length, num_head_tags),
representing logits for predicting a distribution over tags
for each arc.
"""
batch_size = head_tag_representation.size(0)
# shape (batch_size,)
range_vector = get_range_vector(batch_size, get_device_of(head_tag_representation)).unsqueeze(1)
# This next statement is quite a complex piece of indexing, which you really
# need to read the docs to understand. See here:
# https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#advanced-indexing
# In effect, we are selecting the indices corresponding to the heads of each word from the
# sequence length dimension for each element in the batch.
# shape (batch_size, sequence_length, tag_representation_dim)
selected_head_tag_representations = head_tag_representation[range_vector, head_indices]
selected_head_tag_representations = selected_head_tag_representations.contiguous()
# shape (batch_size, sequence_length, num_head_tags)
head_tag_logits = self.tag_bilinear(selected_head_tag_representations,
child_tag_representation)
return head_tag_logits
示例5: forward
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def forward(self, inputs: torch.Tensor, offsets: torch.Tensor = None) -> torch.Tensor:
"""
Parameters
----------
inputs: ``torch.Tensor``, required
A ``(batch_size, num_timesteps)`` tensor representing the byte-pair encodings
for the current batch.
offsets: ``torch.Tensor``, required
A ``(batch_size, max_sequence_length)`` tensor representing the word offsets
for the current batch.
Returns
-------
``[torch.Tensor]``
An embedding representation of the input sequence
having shape ``(batch_size, sequence_length, embedding_dim)``
"""
# pylint: disable=arguments-differ
batch_size, num_timesteps = inputs.size()
# the transformer embedding consists of the byte pair embeddings,
# the special embeddings and the position embeddings.
# the position embeddings are always at least self._transformer.n_ctx,
# but may be longer.
# the transformer "vocab" consists of the actual vocab and the
# positional encodings. Here we want the count of just the former.
vocab_size = self._transformer.vocab_size - self._transformer.n_ctx
# vocab_size, vocab_size + 1, ...
positional_encodings = get_range_vector(num_timesteps, device=get_device_of(inputs)) + vocab_size
# Combine the inputs with positional encodings
batch_tensor = torch.stack([
inputs, # (batch_size, num_timesteps)
positional_encodings.expand(batch_size, num_timesteps)
], dim=-1)
byte_pairs_mask = inputs != 0
# Embeddings is num_output_layers x (batch_size, num_timesteps, embedding_dim)
layer_activations = self._transformer(batch_tensor)
# Output of scalar_mix is (batch_size, num_timesteps, embedding_dim)
if self._top_layer_only:
mix = layer_activations[-1]
else:
mix = self._scalar_mix(layer_activations, byte_pairs_mask)
# These embeddings are one per byte-pair, but we want one per original _word_.
# So we choose the embedding corresponding to the last byte pair for each word,
# which is captured by the ``offsets`` input.
if offsets is not None:
range_vector = get_range_vector(batch_size, device=get_device_of(mix)).unsqueeze(1)
last_byte_pair_embeddings = mix[range_vector, offsets]
else:
# allow to return all byte pairs by passing no offsets
seq_len = (byte_pairs_mask > 0).long().sum(dim=1).max()
last_byte_pair_embeddings = mix[:, :seq_len]
return last_byte_pair_embeddings
示例6: forward
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def forward(self,
source_sentences: Dict[str, torch.Tensor],
sentences_tags: torch.LongTensor = None) -> Dict[str, torch.Tensor]:
tokens = source_sentences["tokens"]
batch_size = tokens.size(0)
sentences_count = tokens.size(1)
max_sentence_length = tokens.size(2)
tokens = tokens.reshape(batch_size * sentences_count, max_sentence_length)
sentences_embeddings = self._encode({"tokens": tokens})
sentences_embeddings = sentences_embeddings.reshape(batch_size, sentences_count, -1)
sentences_embeddings = self._dropout_layer(sentences_embeddings)
h_sentences = self._sentence_accumulator(sentences_embeddings, mask=None)
h_sentences = self._dropout_layer(h_sentences)
output_dict = dict()
content = self._content_projection_layer(h_sentences).squeeze(2)
output_dict["content"] = content
predictions = content
if self._use_salience:
document_embedding = self._document_linear_layer(torch.mean(h_sentences, dim=1))
document_embedding = torch.tanh(document_embedding)
salience_intermediate = self._salience_linear_layer(document_embedding).unsqueeze(2)
salience = torch.bmm(h_sentences, salience_intermediate).squeeze(2)
predictions = predictions + salience
output_dict["salience"] = salience
if self._use_pos_embedding:
assert sentences_count <= self._pos_embedding_num
position_ids = util.get_range_vector(sentences_count, tokens.device.index)
position_ids = position_ids.unsqueeze(0).expand((batch_size, sentences_count))
positional_embeddings = self._pos_embedding_layer(position_ids)
positional_projection = self._pos_projection_layer(positional_embeddings).squeeze(2)
predictions = predictions + positional_projection
output_dict["pos"] = positional_projection
if self._use_output_bias:
predictions = predictions + self._output_bias
if self._use_novelty:
summary_representation = sentences_embeddings.new_zeros((batch_size, self._h_sentence_dim))
novelty = content.new_zeros((batch_size, sentences_count))
for sentence_num in range(sentences_count):
novelty_intermediate = self._novelty_linear_layer(torch.tanh(summary_representation)).unsqueeze(2)
sentence_num_state = h_sentences[:, sentence_num, :]
novelty[:, sentence_num] = -torch.bmm(sentence_num_state.unsqueeze(1), novelty_intermediate).squeeze(2).squeeze(1)
predictions[:, sentence_num] += novelty[:, sentence_num]
probabilities = torch.sigmoid(predictions[:, sentence_num])
summary_representation += torch.mv(sentence_num_state.transpose(0, 1), probabilities)
output_dict["novelty"] = novelty
output_dict["predicted_tags"] = predictions
if sentences_tags is not None:
loss = torch.nn.BCEWithLogitsLoss()(predictions, sentences_tags.float())
output_dict["loss"] = loss
return output_dict
示例7: forward
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def forward(self,
sequence_tensor: torch.FloatTensor,
span_indices: torch.LongTensor,
span_indices_mask: torch.LongTensor = None) -> torch.FloatTensor:
# both of shape (batch_size, num_spans, 1)
span_starts, span_ends = span_indices.split(1, dim=-1)
# shape (batch_size, num_spans, 1)
# These span widths are off by 1, because the span ends are `inclusive`.
span_widths = span_ends - span_starts
# We need to know the maximum span width so we can
# generate indices to extract the spans from the sequence tensor.
# These indices will then get masked below, such that if the length
# of a given span is smaller than the max, the rest of the values
# are masked.
max_batch_span_width = span_widths.max().item() + 1
# Shape: (1, 1, max_batch_span_width)
max_span_range_indices = util.get_range_vector(max_batch_span_width,
util.get_device_of(sequence_tensor)).view(1, 1, -1)
# Shape: (batch_size, num_spans, max_batch_span_width)
# This is a broadcasted comparison - for each span we are considering,
# we are creating a range vector of size max_span_width, but masking values
# which are greater than the actual length of the span.
#
# We're using <= here (and for the mask below) because the span ends are
# inclusive, so we want to include indices which are equal to span_widths rather
# than using it as a non-inclusive upper bound.
span_mask = (max_span_range_indices <= span_widths).float()
raw_span_indices = span_ends - max_span_range_indices
# We also don't want to include span indices which are less than zero,
# which happens because some spans near the beginning of the sequence
# have an end index < max_batch_span_width, so we add this to the mask here.
span_mask = span_mask * (raw_span_indices >= 0).float()
span_indices = torch.nn.functional.relu(raw_span_indices.float()).long()
# Shape: (batch_size * num_spans * max_batch_span_width)
flat_span_indices = util.flatten_and_batch_shift_indices(span_indices, sequence_tensor.size(1))
# Shape: (batch_size, num_spans, max_batch_span_width, embedding_dim)
span_embeddings = util.batched_index_select(sequence_tensor, span_indices, flat_span_indices)
text_embeddings = span_embeddings * span_mask.unsqueeze(-1)
sum_text_embeddings = text_embeddings.sum(dim=2)
return sum_text_embeddings
示例8: forward
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def forward(self,
sequence_tensor: torch.FloatTensor,
span_indices: torch.LongTensor,
span_indices_mask: torch.LongTensor = None) -> torch.FloatTensor:
# both of shape (batch_size, num_spans, 1)
span_starts, span_ends = span_indices.split(1, dim=-1)
# shape (batch_size, num_spans, 1)
# These span widths are off by 1, because the span ends are `inclusive`.
span_widths = span_ends - span_starts
# We need to know the maximum span width so we can
# generate indices to extract the spans from the sequence tensor.
# These indices will then get masked below, such that if the length
# of a given span is smaller than the max, the rest of the values
# are masked.
max_batch_span_width = span_widths.max().item() + 1
# Shape: (1, 1, max_batch_span_width)
max_span_range_indices = util.get_range_vector(max_batch_span_width,
util.get_device_of(sequence_tensor)).view(1, 1, -1)
# Shape: (batch_size, num_spans, max_batch_span_width)
# This is a broadcasted comparison - for each span we are considering,
# we are creating a range vector of size max_span_width, but masking values
# which are greater than the actual length of the span.
#
# We're using <= here (and for the mask below) because the span ends are
# inclusive, so we want to include indices which are equal to span_widths rather
# than using it as a non-inclusive upper bound.
span_mask = (max_span_range_indices <= span_widths).float()
raw_span_indices = span_ends - max_span_range_indices
# We also don't want to include span indices which are less than zero,
# which happens because some spans near the beginning of the sequence
# have an end index < max_batch_span_width, so we add this to the mask here.
span_mask = span_mask * (raw_span_indices >= 0).float()
span_indices = torch.nn.functional.relu(raw_span_indices.float()).long()
# Shape: (batch_size * num_spans * max_batch_span_width)
flat_span_indices = util.flatten_and_batch_shift_indices(span_indices, sequence_tensor.size(1))
# Shape: (batch_size, num_spans, max_batch_span_width, embedding_dim)
span_embeddings = util.batched_index_select(sequence_tensor, span_indices, flat_span_indices)
text_embeddings = span_embeddings * span_mask.unsqueeze(-1)
sum_text_embeddings = text_embeddings.sum(dim=2)
span_num = span_mask.unsqueeze(-1).sum(dim=2)
mean_text_embeddings = sum_text_embeddings / span_num
return mean_text_embeddings
# sequence_tensor = torch.randn(2, 5, 5)
# span_indices = torch.LongTensor([[[0, 1]], [[1, 3]]])
# extractor = MeanSpanExtractor(5)
# print(extractor(sequence_tensor, span_indices))
# print("====")
# print((sequence_tensor[0][0] + sequence_tensor[0][1]) / 2)
# print((sequence_tensor[1][1] + sequence_tensor[1][2] + sequence_tensor[1][3])/3 )
示例9: _get_head_tags
# 需要导入模块: from allennlp.nn import util [as 别名]
# 或者: from allennlp.nn.util import get_range_vector [as 别名]
def _get_head_tags(self,
head_tag_representation: torch.Tensor,
child_tag_representation: torch.Tensor,
head_indices: torch.Tensor) -> torch.Tensor:
"""
Decodes the head tags given the head and child tag representations
and a tensor of head indices to compute tags for. Note that these are
either gold or predicted heads, depending on whether this function is
being called to compute the loss, or if it's being called during inference.
Parameters
----------
head_tag_representation : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
child_tag_representation : ``torch.Tensor``, required
A tensor of shape (batch_size, sequence_length, tag_representation_dim),
which will be used to generate predictions for the dependency tags
for the given arcs.
head_indices : ``torch.Tensor``, required.
A tensor of shape (batch_size, sequence_length). The indices of the heads
for every word.
Returns
-------
head_tag_logits : ``torch.Tensor``
A tensor of shape (batch_size, sequence_length, num_head_tags),
representing logits for predicting a distribution over tags
for each arc.
"""
batch_size = head_tag_representation.size(0)
# shape (batch_size,)
range_vector = get_range_vector(batch_size, get_device_of(head_tag_representation)).unsqueeze(1)
# This next statement is quite a complex piece of indexing, which you really
# need to read the docs to understand. See here:
# https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#advanced-indexing
# In effect, we are selecting the indices corresponding to the heads of each word from the
# sequence length dimension for each element in the batch.
# shape (batch_size, sequence_length, tag_representation_dim)
selected_head_tag_representations = head_tag_representation[range_vector, head_indices]
selected_head_tag_representations = selected_head_tag_representations.contiguous()
# shape (batch_size, sequence_length, num_head_tags)
head_tag_logits = self.tag_bilinear(selected_head_tag_representations,
child_tag_representation)
return head_tag_logits