本文整理匯總了Python中torch.mean方法的典型用法代碼示例。如果您正苦於以下問題:Python torch.mean方法的具體用法?Python torch.mean怎麽用?Python torch.mean使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch
的用法示例。
在下文中一共展示了torch.mean方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def __init__(self, ignore_index=None, reduction='sum', use_weights=False, weight=None):
"""
Parameters
----------
ignore_index : Specifies a target value that is ignored
and does not contribute to the input gradient
reduction : Specifies the reduction to apply to the output:
'mean' | 'sum'. 'mean': elemenwise mean,
'sum': class dim will be summed and batch dim will be averaged.
use_weight : whether to use weights of classes.
weight : Tensor, optional
a manual rescaling weight given to each class.
If given, has to be a Tensor of size "nclasses"
"""
super(_BaseEntropyLoss2d, self).__init__()
self.ignore_index = ignore_index
self.reduction = reduction
self.use_weights = use_weights
if use_weights:
print("w/ class balance")
print(weight)
self.weight = torch.FloatTensor(weight).cuda()
else:
print("w/o class balance")
self.weight = None
示例2: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def forward(self, Q, P):
"""
Parameters
----------
P: ground truth probability distribution [batch_size, n, n]
Q: predicted probability distribution [batch_size, n, n]
Description
-----------
compute the KL divergence of attention maps. Here P and Q denote
the pixel-level attention map with n spatial positions.
"""
kl_loss = P * safe_log(P / Q)
pixel_loss = torch.sum(kl_loss, dim=-1)
total_loss = torch.mean(pixel_loss)
return total_loss
示例3: cmmd
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def cmmd(source, target, s_label, t_label, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
s_label = s_label.cpu()
s_label = s_label.view(32,1)
s_label = torch.zeros(32, 31).scatter_(1, s_label.data, 1)
s_label = Variable(s_label).cuda()
t_label = t_label.cpu()
t_label = t_label.view(32, 1)
t_label = torch.zeros(32, 31).scatter_(1, t_label.data, 1)
t_label = Variable(t_label).cuda()
batch_size = int(source.size()[0])
kernels = guassian_kernel(source, target,
kernel_mul=kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma)
loss = 0
XX = kernels[:batch_size, :batch_size]
YY = kernels[batch_size:, batch_size:]
XY = kernels[:batch_size, batch_size:]
loss += torch.mean(torch.mm(s_label, torch.transpose(s_label, 0, 1)) * XX +
torch.mm(t_label, torch.transpose(t_label, 0, 1)) * YY -
2 * torch.mm(s_label, torch.transpose(t_label, 0, 1)) * XY)
return loss
示例4: kuzushiji_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def kuzushiji_loss(hm, centers, classes, hm_pred, classes_pred, weights=None):
assert hm.shape == hm_pred.shape
hm = hm.to(hm_pred.dtype)
hm_loss = th.nn.functional.binary_cross_entropy_with_logits(
hm_pred, hm, reduction='mean')
classes_ = []
for sample_ind in range(len(hm)):
center = centers[sample_ind]
center_mask = center[:, 0] != -1
per_image_letters = center_mask.sum().item()
if per_image_letters == 0:
continue
classes_per_img = classes[sample_ind][center_mask]
classes_.append(classes_per_img)
classes = th.cat(classes_, 0)
classes_loss = th.nn.functional.cross_entropy(classes_pred, classes,
reduction='mean')
# print("hm: ", hm_loss.item(), " classes: ", classes_loss)
total_loss = hm_loss + 0.1 * classes_loss
return total_loss
示例5: __init__
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def __init__(
self,
classes,
alpha,
p=0.9,
from_normx=False,
weight=None,
size_average=None,
ignore_index=-100,
reduce=None,
reduction='mean'):
super(L2Softmax, self).__init__(
weight, size_average, reduce, reduction)
alpha_low = math.log(p * (classes - 2) / (1 - p))
assert alpha > alpha_low, "For given probability of p={}, alpha should higher than {}.".format(
p, alpha_low)
self.ignore_index = ignore_index
self.alpha = alpha
self.from_normx = from_normx
示例6: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def forward(self, x, target):
similarity_matrix = x @ x.T # need gard here
label_matrix = target.unsqueeze(1) == target.unsqueeze(0)
negative_matrix = label_matrix.logical_not()
positive_matrix = label_matrix.fill_diagonal_(False)
sp = torch.where(positive_matrix, similarity_matrix,
torch.zeros_like(similarity_matrix))
sn = torch.where(negative_matrix, similarity_matrix,
torch.zeros_like(similarity_matrix))
ap = torch.clamp_min(1 + self.m - sp.detach(), min=0.)
an = torch.clamp_min(sn.detach() + self.m, min=0.)
logit_p = -self.gamma * ap * (sp - self.dp)
logit_n = self.gamma * an * (sn - self.dn)
logit_p = torch.where(positive_matrix, logit_p,
torch.zeros_like(logit_p))
logit_n = torch.where(negative_matrix, logit_n,
torch.zeros_like(logit_n))
loss = F.softplus(torch.logsumexp(logit_p, dim=1) +
torch.logsumexp(logit_n, dim=1)).mean()
return loss
示例7: logits_nll_loss
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def logits_nll_loss(input, target, weight=None, reduction='mean'):
"""logits_nll_loss
Different from nll loss, this is for sigmoid based loss.
The difference is this will add along C(class) dim.
"""
assert input.dim() == 2, 'Input shape should be (B, C).'
if input.size(0) != target.size(0):
raise ValueError(
'Expected input batch_size ({}) to match target batch_size ({}).' .format(
input.size(0), target.size(0)))
ret = input.sum(dim=-1)
if weight is not None:
ret = _batch_weight(weight, target) * ret
return reducing(ret, reduction)
示例8: enc_ans_features
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def enc_ans_features(self, x_type_bow, x_types, x_type_bow_len, x_path_bow, x_paths, x_path_bow_len, x_ctx_ents, x_ctx_ent_len, x_ctx_ent_num):
'''
x_types: answer type
x_paths: answer path, i.e., bow of relation
x_ctx_ents: answer context, i.e., bow of entity words, (batch_size, num_cands, num_ctx, L)
'''
# ans_types = torch.mean(self.ent_type_embed(x_types.view(-1, x_types.size(-1))), 1).view(x_types.size(0), x_types.size(1), -1)
ans_type_bow = (self.lstm_enc_type(x_type_bow.view(-1, x_type_bow.size(-1)), x_type_bow_len.view(-1))[1]).view(x_type_bow.size(0), x_type_bow.size(1), -1)
ans_path_bow = (self.lstm_enc_path(x_path_bow.view(-1, x_path_bow.size(-1)), x_path_bow_len.view(-1))[1]).view(x_path_bow.size(0), x_path_bow.size(1), -1)
ans_paths = torch.mean(self.relation_embed(x_paths.view(-1, x_paths.size(-1))), 1).view(x_paths.size(0), x_paths.size(1), -1)
# Avg over ctx
ctx_num_mask = create_mask(x_ctx_ent_num.view(-1), x_ctx_ents.size(2), self.use_cuda).view(x_ctx_ent_num.shape + (-1,))
ans_ctx_ent = (self.lstm_enc_ctx(x_ctx_ents.view(-1, x_ctx_ents.size(-1)), x_ctx_ent_len.view(-1))[1]).view(x_ctx_ents.size(0), x_ctx_ents.size(1), x_ctx_ents.size(2), -1)
ans_ctx_ent = ctx_num_mask.unsqueeze(-1) * ans_ctx_ent
ans_ctx_ent = torch.sum(ans_ctx_ent, dim=2) / torch.clamp(x_ctx_ent_num.float().unsqueeze(-1), min=VERY_SMALL_NUMBER)
if self.ans_enc_dropout:
# ans_types = F.dropout(ans_types, p=self.ans_enc_dropout, training=self.training)
ans_type_bow = F.dropout(ans_type_bow, p=self.ans_enc_dropout, training=self.training)
ans_path_bow = F.dropout(ans_path_bow, p=self.ans_enc_dropout, training=self.training)
ans_paths = F.dropout(ans_paths, p=self.ans_enc_dropout, training=self.training)
ans_ctx_ent = F.dropout(ans_ctx_ent, p=self.ans_enc_dropout, training=self.training)
return ans_type_bow, None, ans_path_bow, ans_paths, ans_ctx_ent
示例9: transcribe_file
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def transcribe_file(args, task, generator, models, sp, tgt_dict):
path = args.input_file
if not os.path.exists(path):
raise FileNotFoundError("Audio file not found: {}".format(path))
waveform, sample_rate = torchaudio.load_wav(path)
waveform = waveform.mean(0, True)
waveform = torchaudio.transforms.Resample(
orig_freq=sample_rate, new_freq=16000
)(waveform)
start = time.time()
transcription = transcribe(
waveform, args, task, generator, models, sp, tgt_dict
)
transcription_time = time.time() - start
return transcription_time, transcription
示例10: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def forward(self, x, y):
means = torch.mean(x, dim=(2, 3))
m = torch.mean(means, dim=-1, keepdim=True)
v = torch.var(means, dim=-1, keepdim=True)
means = (means - m) / (torch.sqrt(v + 1e-5))
h = self.instance_norm(x)
if self.bias:
gamma, alpha, beta = self.embed(y).chunk(3, dim=-1)
h = h + means[..., None, None] * alpha[..., None, None]
out = gamma.view(-1, self.num_features, 1, 1) * h + beta.view(-1, self.num_features, 1, 1)
else:
gamma, alpha = self.embed(y).chunk(2, dim=-1)
h = h + means[..., None, None] * alpha[..., None, None]
out = gamma.view(-1, self.num_features, 1, 1) * h
return out
示例11: initialize
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def initialize(self, input):
with torch.no_grad():
flatten = input.permute(1, 0, 2, 3).contiguous().view(input.shape[1], -1)
mean = (
flatten.mean(1)
.unsqueeze(1)
.unsqueeze(2)
.unsqueeze(3)
.permute(1, 0, 2, 3)
)
std = (
flatten.std(1)
.unsqueeze(1)
.unsqueeze(2)
.unsqueeze(3)
.permute(1, 0, 2, 3)
)
self.loc.data.copy_(-mean)
self.scale.data.copy_(1 / (std + 1e-6))
示例12: forward
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def forward(self):
""" Calculate loss:
$L(sigma) = (||Phi(embed + epsilon) - Phi(embed)||_2^2)
// (regularization^2) - rate * log(sigma)$
:return: a scalar, the target loss.
:rtype: torch.FloatTensor
"""
ratios = torch.sigmoid(self.ratio) # S * 1
x = self.input_embeddings + 0.0
x_tilde = (
x
+ ratios
* torch.randn(self.input_size, self.input_dimension).to(x.device)
* self.scale
) # S * D
s = self.Phi(x) # D or S * D
s_tilde = self.Phi(x_tilde)
loss = (s_tilde - s) ** 2
if self.regular is not None:
loss = torch.mean(loss / self.regular ** 2)
else:
loss = torch.mean(loss) / torch.mean(s ** 2)
return loss - torch.mean(torch.log(ratios)) * self.rate
示例13: merge_aug_bboxes
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def merge_aug_bboxes(aug_bboxes, aug_scores, img_metas, rcnn_test_cfg):
"""Merge augmented detection bboxes and scores.
Args:
aug_bboxes (list[Tensor]): shape (n, 4*#class)
aug_scores (list[Tensor] or None): shape (n, #class)
img_shapes (list[Tensor]): shape (3, ).
rcnn_test_cfg (dict): rcnn test config.
Returns:
tuple: (bboxes, scores)
"""
recovered_bboxes = []
for bboxes, img_info in zip(aug_bboxes, img_metas):
img_shape = img_info[0]['img_shape']
scale_factor = img_info[0]['scale_factor']
flip = img_info[0]['flip']
flip_direction = img_info[0]['flip_direction']
bboxes = bbox_mapping_back(bboxes, img_shape, scale_factor, flip,
flip_direction)
recovered_bboxes.append(bboxes)
bboxes = torch.stack(recovered_bboxes).mean(dim=0)
if aug_scores is None:
return bboxes
else:
scores = torch.stack(aug_scores).mean(dim=0)
return bboxes, scores
示例14: merge_aug_scores
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def merge_aug_scores(aug_scores):
"""Merge augmented bbox scores."""
if isinstance(aug_scores[0], torch.Tensor):
return torch.mean(torch.stack(aug_scores), dim=0)
else:
return np.mean(aug_scores, axis=0)
示例15: merge_aug_masks
# 需要導入模塊: import torch [as 別名]
# 或者: from torch import mean [as 別名]
def merge_aug_masks(aug_masks, img_metas, rcnn_test_cfg, weights=None):
"""Merge augmented mask prediction.
Args:
aug_masks (list[ndarray]): shape (n, #class, h, w)
img_shapes (list[ndarray]): shape (3, ).
rcnn_test_cfg (dict): rcnn test config.
Returns:
tuple: (bboxes, scores)
"""
recovered_masks = []
for mask, img_info in zip(aug_masks, img_metas):
flip = img_info[0]['flip']
flip_direction = img_info[0]['flip_direction']
if flip:
if flip_direction == 'horizontal':
mask = mask[:, :, :, ::-1]
elif flip_direction == 'vertical':
mask = mask[:, :, ::-1, :]
else:
raise ValueError(
f"Invalid flipping direction '{flip_direction}'")
recovered_masks.append(mask)
if weights is None:
merged_masks = np.mean(recovered_masks, axis=0)
else:
merged_masks = np.average(
np.array(recovered_masks), axis=0, weights=np.array(weights))
return merged_masks