Python源码示例:torch.BoolTensor()
示例1
def load_reddit():
from dgl.data import RedditDataset
# load reddit data
data = RedditDataset(self_loop=True)
train_mask = data.train_mask
val_mask = data.val_mask
features = th.Tensor(data.features)
labels = th.LongTensor(data.labels)
# Construct graph
g = data.graph
g.ndata['features'] = features
g.ndata['labels'] = labels
g.ndata['train_mask'] = th.BoolTensor(data.train_mask)
g.ndata['val_mask'] = th.BoolTensor(data.val_mask)
g.ndata['test_mask'] = th.BoolTensor(data.test_mask)
return g, data.num_labels
示例2
def forward(self, inputs: torch.Tensor, mask: torch.BoolTensor = None) -> torch.Tensor:
"""
# Parameters
inputs : `torch.Tensor`, required.
A tensor of shape (batch_size, timesteps, input_dim)
mask : `torch.BoolTensor`, optional (default = `None`).
A tensor of shape (batch_size, timesteps).
# Returns
A tensor of shape (batch_size, timesteps, output_dim).
"""
if mask is None:
return self._feedforward(inputs)
else:
outputs = self._feedforward(inputs)
return outputs * mask.unsqueeze(dim=-1)
示例3
def forward(self, inputs: torch.Tensor, mask: torch.BoolTensor):
output = inputs
if self._sinusoidal_positional_encoding:
output = add_positional_features(output)
if self._positional_embedding is not None:
position_ids = torch.arange(inputs.size(1), dtype=torch.long, device=output.device)
position_ids = position_ids.unsqueeze(0).expand(inputs.shape[:-1])
output = output + self._positional_embedding(position_ids)
# For some reason the torch transformer expects the shape (sequence, batch, features), not the more
# familiar (batch, sequence, features), so we have to fix it.
output = output.permute(1, 0, 2)
# For some other reason, the torch transformer takes the mask backwards.
mask = ~mask
output = self._transformer(output, src_key_padding_mask=mask)
output = output.permute(1, 0, 2)
return output
示例4
def forward(self, inputs: torch.Tensor, mask: torch.BoolTensor = None) -> torch.Tensor:
"""
# Parameters
inputs : `torch.Tensor`, required.
A tensor of shape (batch_size, timesteps, input_dim)
mask : `torch.BoolTensor`, optional (default = `None`).
A tensor of shape (batch_size, timesteps).
# Returns
A tensor of shape (batch_size, timesteps, output_dim),
where output_dim = input_dim.
"""
if mask is None:
return inputs
else:
# We should mask out the output instead of the input.
# But here, output = input, so we directly mask out the input.
return inputs * mask.unsqueeze(dim=-1)
示例5
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
示例6
def as_padded_tensor_dict(
self, tokens: IndexedTokenList, padding_lengths: Dict[str, int]
) -> Dict[str, torch.Tensor]:
"""
This method pads a list of tokens given the input padding lengths (which could actually
truncate things, depending on settings) and returns that padded list of input tokens as a
`Dict[str, torch.Tensor]`. This is a dictionary because there should be one key per
argument that the `TokenEmbedder` corresponding to this class expects in its `forward()`
method (where the argument name in the `TokenEmbedder` needs to make the key in this
dictionary).
The base class implements the case when all you want to do is create a padded `LongTensor`
for every list in the `tokens` dictionary. If your `TokenIndexer` needs more complex
logic than that, you need to override this method.
"""
tensor_dict = {}
for key, val in tokens.items():
if val and isinstance(val[0], bool):
tensor = torch.BoolTensor(
pad_sequence_to_length(val, padding_lengths[key], default_value=lambda: False)
)
else:
tensor = torch.LongTensor(pad_sequence_to_length(val, padding_lengths[key]))
tensor_dict[key] = tensor
return tensor_dict
示例7
def get_lengths_from_binary_sequence_mask(mask: torch.BoolTensor) -> torch.LongTensor:
"""
Compute sequence lengths for each batch element in a tensor using a
binary mask.
# Parameters
mask : `torch.BoolTensor`, required.
A 2D binary mask of shape (batch_size, sequence_length) to
calculate the per-batch sequence lengths from.
# Returns
`torch.LongTensor`
A torch.LongTensor of shape (batch_size,) representing the lengths
of the sequences in the batch.
"""
return mask.sum(-1)
示例8
def get_mask_from_sequence_lengths(
sequence_lengths: torch.Tensor, max_length: int
) -> torch.BoolTensor:
"""
Given a variable of shape `(batch_size,)` that represents the sequence lengths of each batch
element, this function returns a `(batch_size, max_length)` mask variable. For example, if
our input was `[2, 2, 3]`, with a `max_length` of 4, we'd return
`[[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]]`.
We require `max_length` here instead of just computing it from the input `sequence_lengths`
because it lets us avoid finding the max, then copying that value from the GPU to the CPU so
that we can use it to construct a new tensor.
"""
# (batch_size, max_length)
ones = sequence_lengths.new_ones(sequence_lengths.size(0), max_length)
range_tensor = ones.cumsum(dim=1)
return sequence_lengths.unsqueeze(1) >= range_tensor
示例9
def masked_max(
vector: torch.Tensor, mask: torch.BoolTensor, dim: int, keepdim: bool = False,
) -> torch.Tensor:
"""
To calculate max along certain dimensions on masked values
# Parameters
vector : `torch.Tensor`
The vector to calculate max, assume unmasked parts are already zeros
mask : `torch.BoolTensor`
The mask of the vector. It must be broadcastable with vector.
dim : `int`
The dimension to calculate max
keepdim : `bool`
Whether to keep dimension
# Returns
`torch.Tensor`
A `torch.Tensor` of including the maximum values.
"""
replaced_vector = vector.masked_fill(~mask, min_value_of_dtype(vector.dtype))
max_value, _ = replaced_vector.max(dim=dim, keepdim=keepdim)
return max_value
示例10
def replace_masked_values(
tensor: torch.Tensor, mask: torch.BoolTensor, replace_with: float
) -> torch.Tensor:
"""
Replaces all masked values in `tensor` with `replace_with`. `mask` must be broadcastable
to the same shape as `tensor`. We require that `tensor.dim() == mask.dim()`, as otherwise we
won't know which dimensions of the mask to unsqueeze.
This just does `tensor.masked_fill()`, except the pytorch method fills in things with a mask
value of 1, where we want the opposite. You can do this in your own code with
`tensor.masked_fill(~mask, replace_with)`.
"""
if tensor.dim() != mask.dim():
raise ConfigurationError(
"tensor.dim() (%d) != mask.dim() (%d)" % (tensor.dim(), mask.dim())
)
return tensor.masked_fill(~mask, replace_with)
示例11
def __call__(
self,
predictions: torch.Tensor,
gold_labels: torch.Tensor,
mask: Optional[torch.BoolTensor] = None,
):
"""
# Parameters
predictions : `torch.Tensor`, required.
A tensor of predictions of shape (batch_size, ...).
gold_labels : `torch.Tensor`, required.
A tensor of the same shape as `predictions`.
mask : `torch.BoolTensor`, optional (default = `None`).
A tensor of the same shape as `predictions`.
"""
predictions, gold_labels, mask = self.detach_tensors(predictions, gold_labels, mask)
self._predictions_labels_covariance(predictions, gold_labels, mask)
self._predictions_variance(predictions, predictions, mask)
self._labels_variance(gold_labels, gold_labels, mask)
示例12
def __call__(
self, # type: ignore
logits: torch.Tensor,
mask: Optional[torch.BoolTensor] = None,
):
"""
# Parameters
logits : `torch.Tensor`, required.
A tensor of unnormalized log probabilities of shape (batch_size, ..., num_classes).
mask : `torch.BoolTensor`, optional (default = `None`).
A masking tensor of shape (batch_size, ...).
"""
logits, mask = self.detach_tensors(logits, mask)
if mask is None:
mask = torch.ones(logits.size()[:-1], device=logits.device).bool()
log_probs = torch.nn.functional.log_softmax(logits, dim=-1)
probabilities = torch.exp(log_probs) * mask.unsqueeze(-1)
weighted_negative_likelihood = -log_probs * probabilities
entropy = weighted_negative_likelihood.sum(-1)
self._entropy += entropy.sum() / mask.sum()
self._count += 1
示例13
def __call__(
self,
predictions: torch.Tensor,
gold_labels: torch.Tensor,
mask: Optional[torch.BoolTensor] = None,
):
"""
# Parameters
predictions : `torch.Tensor`, required.
A tensor of predictions of shape (batch_size, ...).
gold_labels : `torch.Tensor`, required.
A tensor of the same shape as `predictions`.
mask : `torch.BoolTensor`, optional (default = `None`).
A tensor of the same shape as `predictions`.
"""
predictions, gold_labels, mask = self.detach_tensors(predictions, gold_labels, mask)
absolute_errors = torch.abs(predictions - gold_labels)
if mask is not None:
absolute_errors *= mask
self._total_count += torch.sum(mask)
else:
self._total_count += gold_labels.numel()
self._absolute_error += torch.sum(absolute_errors)
示例14
def forward_layers(
self, tensor: torch.Tensor, mask: torch.BoolTensor
) -> torch.Tensor:
"""
Apply transformer layers to input.
:param tensor:
embedded input
:param mask:
mask of input
:return tensor:
return embedding after applying transformer layers
"""
if getattr(self.layers, 'is_model_parallel', False):
# factored out for readability. It is equivalent to the other
# condition
tensor = self._apply_model_parallel(tensor, mask)
else:
for i in range(self.n_layers):
tensor = self.layers[i](tensor, mask)
return tensor
示例15
def forward(self, inputs, seq_lengths):
output = None
for module in self.sequential:
output = module(inputs)
mask = torch.BoolTensor(output.size()).fill_(0)
if output.is_cuda:
mask = mask.cuda()
seq_lengths = self.get_seq_lengths(module, seq_lengths)
for idx, length in enumerate(seq_lengths):
length = length.item()
if (mask[idx].size(2) - length) > 0:
mask[idx].narrow(dim=2, start=length, length=mask[idx].size(2) - length).fill_(1)
output = output.masked_fill(mask, 0)
inputs = output
return output, seq_lengths
示例16
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "Instances":
"""
Args:
item: an index-like object and will be used to index all the fields.
Returns:
If `item` is a string, return the data in the corresponding field.
Otherwise, returns an `Instances` where all fields are indexed by `item`.
"""
if type(item) == int:
if item >= len(self) or item < -len(self):
raise IndexError("Instances index out of range!")
else:
item = slice(item, None, len(self))
ret = Instances(self._image_size)
for k, v in self._fields.items():
ret.set(k, v[item])
return ret
示例17
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "RotatedBoxes":
"""
Returns:
RotatedBoxes: Create a new :class:`RotatedBoxes` by indexing.
The following usage are allowed:
1. `new_boxes = boxes[3]`: return a `RotatedBoxes` which contains only one box.
2. `new_boxes = boxes[2:10]`: return a slice of boxes.
3. `new_boxes = boxes[vector]`, where vector is a torch.ByteTensor
with `length = len(boxes)`. Nonzero elements in the vector will be selected.
Note that the returned RotatedBoxes might share storage with this RotatedBoxes,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return RotatedBoxes(self.tensor[item].view(1, -1))
b = self.tensor[item]
assert b.dim() == 2, "Indexing on RotatedBoxes with {} failed to return a matrix!".format(
item
)
return RotatedBoxes(b)
示例18
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]):
"""
Args:
item: int, slice, or a BoolTensor
Returns:
Boxes: Create a new :class:`Boxes` by indexing.
The following usage are allowed:
1. `new_boxes = boxes[3]`: return a `Boxes` which contains only one box.
2. `new_boxes = boxes[2:10]`: return a slice of boxes.
3. `new_boxes = boxes[vector]`, where vector is a torch.BoolTensor
with `length = len(boxes)`. Nonzero elements in the vector will be selected.
Note that the returned Boxes might share storage with this Boxes,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return Boxes(self.tensor[item].view(1, -1))
b = self.tensor[item]
assert b.dim() == 2, "Indexing on Boxes with {} failed to return a matrix!".format(item)
return Boxes(b)
示例19
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "Keypoints":
"""
Create a new `Keypoints` by indexing on this `Keypoints`.
The following usage are allowed:
1. `new_kpts = kpts[3]`: return a `Keypoints` which contains only one instance.
2. `new_kpts = kpts[2:10]`: return a slice of key points.
3. `new_kpts = kpts[vector]`, where vector is a torch.ByteTensor
with `length = len(kpts)`. Nonzero elements in the vector will be selected.
Note that the returned Keypoints might share storage with this Keypoints,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return Keypoints([self.tensor[item]])
return Keypoints(self.tensor[item])
示例20
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "RotatedBoxes":
"""
Returns:
RotatedBoxes: Create a new :class:`RotatedBoxes` by indexing.
The following usage are allowed:
1. `new_boxes = boxes[3]`: return a `RotatedBoxes` which contains only one box.
2. `new_boxes = boxes[2:10]`: return a slice of boxes.
3. `new_boxes = boxes[vector]`, where vector is a torch.ByteTensor
with `length = len(boxes)`. Nonzero elements in the vector will be selected.
Note that the returned RotatedBoxes might share storage with this RotatedBoxes,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return RotatedBoxes(self.tensor[item].view(1, -1))
b = self.tensor[item]
assert b.dim() == 2, "Indexing on RotatedBoxes with {} failed to return a matrix!".format(
item
)
return RotatedBoxes(b)
示例21
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "Boxes":
"""
Returns:
Boxes: Create a new :class:`Boxes` by indexing.
The following usage are allowed:
1. `new_boxes = boxes[3]`: return a `Boxes` which contains only one box.
2. `new_boxes = boxes[2:10]`: return a slice of boxes.
3. `new_boxes = boxes[vector]`, where vector is a torch.BoolTensor
with `length = len(boxes)`. Nonzero elements in the vector will be selected.
Note that the returned Boxes might share storage with this Boxes,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return Boxes(self.tensor[item].view(1, -1))
b = self.tensor[item]
assert b.dim() == 2, "Indexing on Boxes with {} failed to return a matrix!".format(item)
return Boxes(b)
示例22
def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "Keypoints":
"""
Create a new `Keypoints` by indexing on this `Keypoints`.
The following usage are allowed:
1. `new_kpts = kpts[3]`: return a `Keypoints` which contains only one instance.
2. `new_kpts = kpts[2:10]`: return a slice of key points.
3. `new_kpts = kpts[vector]`, where vector is a torch.ByteTensor
with `length = len(kpts)`. Nonzero elements in the vector will be selected.
Note that the returned Keypoints might share storage with this Keypoints,
subject to Pytorch's indexing semantics.
"""
if isinstance(item, int):
return Keypoints([self.tensor[item]])
return Keypoints(self.tensor[item])
示例23
def forward(self, inputs: torch.Tensor, mask: torch.BoolTensor):
output = inputs
if self._sinusoidal_positional_encoding:
output = add_positional_features(output)
if self._positional_embedding is not None:
position_ids = torch.arange(inputs.size(1), dtype=torch.long, device=output.device)
position_ids = position_ids.unsqueeze(0).expand(inputs.shape[:-1])
output = output + self._positional_embedding(position_ids)
# print()
# print(sum(output[0][4]), sum(output[0][100]))
# For some reason the torch transformer expects the shape (sequence, batch, features), not the more
# familiar (batch, sequence, features), so we have to fix it.
output = output.permute(1, 0, 2)
# For some other reason, the torch transformer takes the mask backwards.
mask = ~mask
output = self._transformer(output, src_key_padding_mask=mask)
output = output.permute(1, 0, 2)
# print(sum(inputs[0][4]), sum(inputs[0][100]))
# print(sum(output[0][4]), sum(output[0][100]))
# print()
return output
示例24
def forward(
self, h: FloatTensor, labels: LongTensor, mask: BoolTensor
) -> FloatTensor:
"""
:param h: hidden matrix (batch_size, seq_len, num_labels)
:param labels: answer labels of each sequence
in mini batch (batch_size, seq_len)
:param mask: mask tensor of each sequence
in mini batch (batch_size, seq_len)
:return: The log-likelihood (batch_size)
"""
log_numerator = self._compute_numerator_log_likelihood(h, labels, mask)
log_denominator = self._compute_denominator_log_likelihood(h, mask)
return log_numerator - log_denominator
示例25
def load_cora_data():
data = citegrh.load_cora()
features = th.FloatTensor(data.features)
labels = th.LongTensor(data.labels)
train_mask = th.BoolTensor(data.train_mask)
test_mask = th.BoolTensor(data.test_mask)
g = DGLGraph(data.graph)
return g, features, labels, train_mask, test_mask
###############################################################################
# When a model is trained, we can use the following method to evaluate
# the performance of the model on the test dataset:
示例26
def load_cora_data():
data = citegrh.load_cora()
features = torch.FloatTensor(data.features)
labels = torch.LongTensor(data.labels)
mask = torch.BoolTensor(data.train_mask)
g = DGLGraph(data.graph)
return g, features, labels, mask
##############################################################################
# The training loop is exactly the same as in the GCN tutorial.
示例27
def __init__(self, encoder_output, encoder_mask, hidden, cell):
"""
LSTMDecoder对应的State,保存encoder的输出以及LSTM解码过程中的一些中间状态
:param torch.FloatTensor encoder_output: bsz x src_seq_len x encode_output_size,encoder的输出
:param torch.BoolTensor encoder_mask: bsz x src_seq_len, 为0的地方是padding
:param torch.FloatTensor hidden: num_layers x bsz x hidden_size, 上个时刻的hidden状态
:param torch.FloatTensor cell: num_layers x bsz x hidden_size, 上个时刻的cell状态
"""
super().__init__(encoder_output, encoder_mask)
self.hidden = hidden
self.cell = cell
self._input_feed = hidden[0] # 默认是上一个时刻的输出
示例28
def get_mask_for_instance(self, instance: List[int]) -> torch.BoolTensor:
start_token_idx = self.vocabulary.get_idx_from_token(
self.vocabulary.start_token
)
end_token_idx = self.vocabulary.get_idx_from_token(self.vocabulary.end_token)
pad_token_idx = self.vocabulary.get_idx_from_token(self.vocabulary.pad_token)
unk_token_idx = self.vocabulary.get_idx_from_token(self.vocabulary.unk_token)
masked_tokens = [start_token_idx, end_token_idx, pad_token_idx, unk_token_idx]
assert len(set(masked_tokens)) == 4
mask = [1 if token in masked_tokens else 0 for token in instance]
mask = torch.BoolTensor(mask)
return mask
示例29
def get_mask_for_batch_instances(
self, instances: List[List[int]]
) -> torch.BoolTensor:
masks = []
for instance in instances:
mask = self.get_mask_for_instance(instance=instance).tolist()
masks.append(mask)
masks = torch.BoolTensor(masks)
return masks
示例30
def test_print_confusion_matrix_works(self, setup_data_basecase):
predicted_probs, labels, metric, dataset_manager, expected = setup_data_basecase
labels_mask = torch.zeros_like(predicted_probs).type(torch.BoolTensor)
try:
metric.print_confusion_metrics(
predicted_probs=predicted_probs, labels=labels, labels_mask=labels_mask
)
except:
pytest.fail("Precision Recall and FMeasure print_confusion_metrics fails")