Python源码示例:torch.finfo()
示例1
def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor):
"""
Calculate rank cross entropy loss.
:param y_pred: Predicted result.
:param y_true: Label.
:return: Rank cross loss.
"""
logits = y_pred[::(self.num_neg + 1), :]
labels = y_true[::(self.num_neg + 1), :]
for neg_idx in range(self.num_neg):
neg_logits = y_pred[(neg_idx + 1)::(self.num_neg + 1), :]
neg_labels = y_true[(neg_idx + 1)::(self.num_neg + 1), :]
logits = torch.cat((logits, neg_logits), dim=-1)
labels = torch.cat((labels, neg_labels), dim=-1)
return -torch.mean(
torch.sum(
labels * torch.log(F.softmax(logits, dim=-1) + torch.finfo(float).eps),
dim=-1
)
)
示例2
def __init__(self, ignore_index, reduction, normalize_targets):
"""Intializer for the soft target cross-entropy loss loss.
This allows the targets for the cross entropy loss to be multilabel
Config params:
'weight': weight of sample (not yet implemented),
'ignore_index': sample should be ignored for loss (optional),
'reduction': specifies reduction to apply to the output (optional),
"""
super(SoftTargetCrossEntropyLoss, self).__init__()
self._ignore_index = ignore_index
self._reduction = reduction
assert normalize_targets in [None, "count_based"]
self._normalize_targets = normalize_targets
if self._reduction != "mean":
raise NotImplementedError(
'reduction type "{}" not implemented'.format(self._reduction)
)
self._eps = torch.finfo(torch.float32).eps
示例3
def _quadratic_expand(x, y):
"""
Helper function to calculate quadratic expansion |x-y|**2=|x|**2 + |y|**2 - 2xy
Parameters
----------
x : torch.tensor
2D tensor of size m x f
y : torch.tensor
2D tensor of size n x f
Returns
-------
torch.tensor
2D tensor of size m x n
"""
x_norm = (x ** 2).sum(1).view(-1, 1)
y_t = torch.transpose(y, 0, 1)
y_norm = (y ** 2).sum(1).view(1, -1)
dist = x_norm + y_norm - 2.0 * torch.mm(x, y_t)
info = torch.finfo(dist.dtype)
return torch.clamp(dist, 0.0, info.max)
示例4
def safe_cumprod(x: torch.Tensor,
*args,
**kwargs) -> torch.Tensor:
r"""Computes cumprod of x in logspace using cumsum to avoid underflow.
The cumprod function and its gradient can result in numerical
instabilities when its argument has very small and/or zero values.
As long as the argument is all positive, we can instead compute the
cumulative product as `exp(cumsum(log(x)))`. This function can be called
identically to :torch:`cumprod`.
Args:
x: Tensor to take the cumulative product of.
*args: Passed on to cumsum; these are identical to those in cumprod.
**kwargs: Passed on to cumsum; these are identical to those in cumprod.
Returns:
Cumulative product of x.
"""
if not isinstance(x, torch.Tensor):
x = torch.tensor(x)
tiny = torch.finfo(x.dtype).tiny
return torch.exp(torch.cumsum(torch.log(torch.clamp(x, tiny, 1)),
*args, **kwargs))
示例5
def topk(x, ratio, batch, min_score=None, tol=1e-7):
if min_score is not None:
# Make sure that we do not drop all nodes in a graph.
scores_max = scatter_max(x, batch)[0][batch] - tol
scores_min = scores_max.clamp(max=min_score)
perm = torch.nonzero(x > scores_min).view(-1)
else:
num_nodes = scatter_add(batch.new_ones(x.size(0)), batch, dim=0)
batch_size, max_num_nodes = num_nodes.size(0), num_nodes.max().item()
cum_num_nodes = torch.cat(
[num_nodes.new_zeros(1),
num_nodes.cumsum(dim=0)[:-1]], dim=0)
index = torch.arange(batch.size(0), dtype=torch.long, device=x.device)
index = (index - cum_num_nodes[batch]) + (batch * max_num_nodes)
dense_x = x.new_full((batch_size * max_num_nodes, ),
torch.finfo(x.dtype).min)
dense_x[index] = x
dense_x = dense_x.view(batch_size, max_num_nodes)
_, perm = dense_x.sort(dim=-1, descending=True)
perm = perm + cum_num_nodes.view(-1, 1)
perm = perm.view(-1)
k = (ratio * num_nodes.to(torch.float)).ceil().to(torch.long)
mask = [
torch.arange(k[i], dtype=torch.long, device=x.device) +
i * max_num_nodes for i in range(batch_size)
]
mask = torch.cat(mask, dim=0)
perm = perm[mask]
return perm
示例6
def rodrigues(self, r):
"""
Rodrigues' rotation formula that turns axis-angle tensor into rotation
matrix in a batch-ed manner.
Parameter:
----------
r: Axis-angle rotation tensor of shape [N, 1, 3].
Return:
-------
Rotation matrix of shape [N, 3, 3].
"""
theta = torch.norm(r, dim=(1, 2), keepdim=True)
# avoid division by zero
torch.max(theta, theta.new_full((1,), torch.finfo(theta.dtype).tiny), out=theta)
#The .tiny has to be uploaded to GPU, but self.regress_joints is such a big bottleneck it is not felt.
r_hat = r / theta
z_stick = torch.zeros_like(r_hat[:, 0, 0])
m = torch.stack(
(z_stick, -r_hat[:, 0, 2], r_hat[:, 0, 1],
r_hat[:, 0, 2], z_stick, -r_hat[:, 0, 0],
-r_hat[:, 0, 1], r_hat[:, 0, 0], z_stick), dim=1)
m = m.reshape(-1, 3, 3)
dot = torch.bmm(r_hat.transpose(1, 2), r_hat) # Batched outer product.
# torch.matmul or torch.stack([torch.ger(r, r) for r in r_hat.squeeze(1)] works too.
cos = theta.cos()
R = cos * self.eye + (1 - cos) * dot + theta.sin() * m
return R
示例7
def _finfo(x):
return torch.finfo(x.dtype)
示例8
def _reciprocal(x):
result = x.reciprocal().clamp(max=torch.finfo(x.dtype).max)
return result
示例9
def _safediv(x, y):
try:
finfo = torch.finfo(y.dtype)
except TypeError:
finfo = torch.iinfo(y.dtype)
return x * y.reciprocal().clamp(max=finfo.max)
示例10
def _safesub(x, y):
try:
finfo = torch.finfo(y.dtype)
except TypeError:
finfo = torch.iinfo(y.dtype)
return x + (-y).clamp(max=finfo.max)
示例11
def info_value_of_dtype(dtype: torch.dtype):
"""
Returns the `finfo` or `iinfo` object of a given PyTorch data type. Does not allow torch.bool.
"""
if dtype == torch.bool:
raise TypeError("Does not support torch.bool")
elif dtype.is_floating_point:
return torch.finfo(dtype)
else:
return torch.iinfo(dtype)
示例12
def get_loss(self, y_pred, y_true, *args, **kwargs):
if isinstance(self.criterion_, torch.nn.NLLLoss):
eps = torch.finfo(y_pred.dtype).eps
y_pred = torch.log(y_pred + eps)
return super().get_loss(y_pred, y_true, *args, **kwargs)
# pylint: disable=signature-differs
示例13
def compute_class_ap(self, tp, fp, npos):
"""
Args:
tp (Tensor, shape [N*D]): cumulative sum of true positive detections
fp (Tensor, shape [N*D]): cumulative sum of false positive detections
npos (Tensor, int): actual positives (from ground truth)
Return:
ap (Tensor, float): average precision calculation
"""
#Values for precision-recall curve
rc = tp/npos
pr = tp / torch.clamp(tp + fp, min=torch.finfo(torch.float).eps)
rc_values = torch.linspace(0,1,self.num_points) #sampled recall points for n-point precision-recall curve
#The interpotaled P-R curve will take on the max precision value to the right at each recall
ap = 0.
for t in rc_values:
if torch.sum(rc >= t) == 0:
p = 0
else:
p = torch.max(pr[rc >= t])
ap = ap + p/self.num_points
return ap
示例14
def __new__(cls, dtype):
try:
dtype = heat_type_of(dtype)
except (KeyError, IndexError, TypeError):
# If given type is not heat type
pass
if dtype not in _inexact:
raise TypeError("Data type {} not inexact, not supported".format(dtype))
return super(finfo, cls).__new__(cls)._init(dtype)
示例15
def _init(self, dtype):
_torch_finfo = torch.finfo(dtype.torch_type())
for word in ["bits", "eps", "max", "tiny"]:
setattr(self, word, getattr(_torch_finfo, word))
self.min = -self.max
return self
示例16
def clamp_probs(probs):
eps = torch.finfo(probs.dtype).eps
return probs.clamp(min=eps, max=1 - eps)
示例17
def jeccard_sim(pred, target, jec_p=.5):
"""
Computes the Jaccard Similarity of the semantic segmentation
Arguments:
pred: predicted segmentation output by network
target: binary segmentation targets
jec_p: prediction cutoff for foreground vs background
"""
with torch.no_grad():
pred = pred > jec_p
intersection = (pred[target > jec_p]).float().sum()
union = pred.float().sum() + target.float().sum() - intersection
return (intersection / (union + finfo(torch.float).eps)).item()
示例18
def mAP(pred, target, cutoff=.5):
"""
Computes the Average Precision of the semantic segmentation
Arguments:
pred: predicted segmentation output by network
target: binary segmentation targets
cutoff: prediction cutoff for foreground vs background
"""
with torch.no_grad():
pred = pred > cutoff
intersection = (pred[target > cutoff]).float().sum()
return (intersection / (pred.float().sum() +
finfo(torch.float).eps)).item()
示例19
def forward(self, encoded_utterance, token_embeddings, element_embeddings, utterance_mask):
"""
token_embeddings: token hidden states from BERT encoding of the utterance
encoded_utterance: [CLS] token hidden state from BERT encoding of the utterance
element_embeddings: A tensor of shape (batch_size, num_elements, embedding_dim) extracted from schema
utterance_mask: binary mask for token_embeddings, 1 for real tokens 0 for padded tokens
"""
_, num_elements, _ = element_embeddings.size()
query_layer = self.query(element_embeddings)
key_layer = self.key(token_embeddings)
value_layer = self.value(token_embeddings)
query_layer = self.transpose_for_scores(query_layer)
key_layer = self.transpose_for_scores(key_layer)
value_layer = self.transpose_for_scores(value_layer)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size)
if utterance_mask is not None:
negative_scores = (torch.finfo(attention_scores.dtype).max * -0.7) * torch.ones_like(attention_scores)
new_x_shape = (utterance_mask.size()[0],) + (1, 1) + (utterance_mask.size()[1],)
attention_scores = torch.where(
utterance_mask.view(*new_x_shape).to(bool), attention_scores, negative_scores
)
attention_probs = nn.Softmax(dim=-1)(attention_scores)
attention_probs = self.dropout(attention_probs)
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.embedding_dim,)
context_layer = context_layer.view(*new_context_layer_shape)
logits = self.layer(context_layer)
return logits
示例20
def _get_noncategorical_slot_goals(self, encoded_utterance, utterance_mask, noncat_slot_emb, token_embeddings):
"""
Obtain logits for status and slot spans for non-categorical slots.
Slot status values: none, dontcare, active
"""
# Predict the status of all non-categorical slots.
max_num_slots = noncat_slot_emb.size()[1]
status_logits = self.noncat_slot_layer(
encoded_utterance=encoded_utterance,
token_embeddings=token_embeddings,
element_embeddings=noncat_slot_emb,
utterance_mask=utterance_mask,
)
# Predict the distribution for span indices.
max_num_tokens = token_embeddings.size()[1]
repeated_token_embeddings = token_embeddings.unsqueeze(1).repeat(1, max_num_slots, 1, 1)
repeated_slot_embeddings = noncat_slot_emb.unsqueeze(2).repeat(1, 1, max_num_tokens, 1)
# Shape: (batch_size, max_num_slots, max_num_tokens, 2 * embedding_dim).
slot_token_embeddings = torch.cat([repeated_slot_embeddings, repeated_token_embeddings], axis=3)
# Project the combined embeddings to obtain logits, Shape: (batch_size, max_num_slots, max_num_tokens, 2)
span_logits = self.noncat_layer1(slot_token_embeddings)
span_logits = self.noncat_activation(span_logits)
span_logits = self.noncat_layer2(span_logits)
# Mask out invalid logits for padded tokens.
utterance_mask = utterance_mask.to(bool) # Shape: (batch_size, max_num_tokens).
repeated_utterance_mask = utterance_mask.unsqueeze(1).unsqueeze(3).repeat(1, max_num_slots, 1, 2)
negative_logits = (torch.finfo(span_logits.dtype).max * -0.7) * torch.ones(
span_logits.size(), device=self._device, dtype=span_logits.dtype
)
span_logits = torch.where(repeated_utterance_mask, span_logits, negative_logits)
# Shape of both tensors: (batch_size, max_num_slots, max_num_tokens).
span_start_logits, span_end_logits = torch.unbind(span_logits, dim=3)
return status_logits, span_start_logits, span_end_logits
示例21
def _get_negative_logits(self, logits):
# returns tensor with negative logits that will be used to mask out unused values
# for a particular service
negative_logits = (torch.finfo(logits.dtype).max * -0.7) * torch.ones(
logits.size(), device=self._device, dtype=logits.dtype
)
return negative_logits
示例22
def normalize(samples: Tensor) -> Tensor:
"""
Args:
samples: tensor with shape of [n_samples, features_dim]
Returns:
normalized tensor with the same shape
"""
norms = torch.norm(samples, p=2, dim=1).unsqueeze(1)
samples = samples / (norms + torch.finfo(torch.float32).eps)
return samples
示例23
def draw(
self, n: int = 1, out: Optional[Tensor] = None, dtype: torch.dtype = torch.float
) -> Optional[Tensor]:
r"""Draw `n` qMC samples from the standard Normal.
Args:
n: The number of samples to draw.
out: An option output tensor. If provided, draws are put into this
tensor, and the function returns None.
dtype: The desired torch data type (ignored if `out` is provided).
Returns:
A `n x d` tensor of samples if `out=None` and `None` otherwise.
"""
# get base samples
samples = self._sobol_engine.draw(n, dtype=dtype)
if self._inv_transform:
# apply inverse transform (values to close to 0/1 result in inf values)
v = 0.5 + (1 - torch.finfo(samples.dtype).eps) * (samples - 0.5)
samples_tf = torch.erfinv(2 * v - 1) * math.sqrt(2)
else:
# apply Box-Muller transform (note: [1] indexes starting from 1)
even = torch.arange(0, samples.shape[-1], 2)
Rs = (-2 * torch.log(samples[:, even])).sqrt()
thetas = 2 * math.pi * samples[:, 1 + even]
cos = torch.cos(thetas)
sin = torch.sin(thetas)
samples_tf = torch.stack([Rs * cos, Rs * sin], -1).reshape(n, -1)
# make sure we only return the number of dimension requested
samples_tf = samples_tf[:, : self._d]
if out is None:
return samples_tf
else:
out.copy_(samples_tf)